arduino-audio-driver
Loading...
Searching...
No Matches
GPIO_Zephyr.h
Go to the documentation of this file.
1#pragma once
2#include <stdint.h>
3#include "API_GPIO.h"
4#include <zephyr/drivers/gpio.h>
5#include "IDriverDeviceInfo.h"
6
7
13namespace audio_driver {
14
20class GPIOZephyr : public API_GPIO {
21 public:
22 GPIOZephyr() = default;
23
24 bool begin(IDriverDeviceInfo& pins) { return true; }
25
26 void end() {}
27
28 void pinMode(GpioPin pin, int mode) {
29 if (pin == GPIO_UNDEFINED || !gpio_is_ready_dt(&pin)) {
30 AD_LOGE("GPIO pin not ready");
31 return;
32 }
33
34 gpio_flags_t flags;
35 switch (mode) {
36 case OUTPUT:
37 flags = GPIO_OUTPUT;
38 break;
39 case INPUT:
40 flags = GPIO_INPUT;
41 break;
42 case INPUT_PULLUP:
43 flags = GPIO_INPUT | GPIO_PULL_UP;
44 break;
45 // case INPUT_PULLDOWN:
46 // flags = GPIO_INPUT | GPIO_PULL_DOWN;
47 // break;
48 default:
49 flags = GPIO_OUTPUT;
50 break;
51 }
52
53 int rc = gpio_pin_configure_dt(&pin, flags);
54 if (rc != 0) {
55 AD_LOGE("Failed to configure GPIO pin: %d", rc);
56 }
57 }
58
59 bool digitalWrite(GpioPin pin, bool value) {
60 if (pin == GPIO_UNDEFINED || !gpio_is_ready_dt(&pin)) {
61 AD_LOGE("GPIO pin not ready");
62 return false;
63 }
64
65 int rc = gpio_pin_set_dt(&pin, value ? 1 : 0);
66 if (rc != 0) {
67 AD_LOGE("Failed to write GPIO pin: %d", rc);
68 return false;
69 }
70 return true;
71 }
72
73 bool digitalRead(GpioPin pin) {
74 if (pin == GPIO_UNDEFINED || !gpio_is_ready_dt(&pin)) {
75 AD_LOGE("GPIO pin not ready");
76 return false;
77 }
78
79 int rc = gpio_pin_get_dt(&pin);
80 if (rc < 0) {
81 AD_LOGE("Failed to read GPIO pin: %d", rc);
82 return false;
83 }
84 return rc != 0;
85 }
86
88 int analogRead(ADCPin pin) {
89 int rc = adc_channel_setup_dt(&pin);
90 if (rc != 0) {
91 AD_LOGE("ADC channel setup failed: %d", rc);
92 return -1;
93 }
94 int16_t sample = 0;
95 struct adc_sequence sequence = {
96 .buffer = &sample,
97 .buffer_size = sizeof(sample),
98 };
99 adc_sequence_init_dt(&pin, &sequence);
100 rc = adc_read_dt(&pin, &sequence);
101 if (rc != 0) {
102 AD_LOGE("ADC read failed: %d", rc);
103 return -1;
104 }
105 return sample;
106 }
107};
108
110
111} // namespace audio_driver
#define INPUT
Definition DriverCommon.h:41
#define GPIO_UNDEFINED
Definition DriverCommon.h:131
#define OUTPUT
Definition DriverCommon.h:44
#define INPUT_PULLUP
Definition DriverCommon.h:47
#define AD_LOGE(...)
Definition Logger.h:15
Definition API_GPIO.h:8
Definition GPIO_Zephyr.h:20
bool begin(IDriverDeviceInfo &pins)
Definition GPIO_Zephyr.h:24
bool digitalWrite(GpioPin pin, bool value)
Definition GPIO_Zephyr.h:59
int analogRead(ADCPin pin)
Reads the raw ADC sample described by an adc_dt_spec device-tree node.
Definition GPIO_Zephyr.h:88
void pinMode(GpioPin pin, int mode)
Definition GPIO_Zephyr.h:28
void end()
Definition GPIO_Zephyr.h:26
bool digitalRead(GpioPin pin)
Definition GPIO_Zephyr.h:73
Definition IDriverDeviceInfo.h:171
Namespace for audio driver components.
Definition AudioBoard.h:5
int16_t GpioPin
Definition DriverCommon.h:153
int16_t ADCPin
Definition DriverCommon.h:154