arduino-audio-driver
Loading...
Searching...
No Matches
DriverDeviceInfo.h
Go to the documentation of this file.
1
2#pragma once
3#ifdef __zephyr__
5#else
7#include "Platforms/GPIO.h"
9
10namespace audio_driver {
11
21 public:
24 DriverDeviceInfo() = default;
25
26 bool addI2S(InfoI2S pin) {
27 if (getI2SPins(pin.function)) return false;
28 i2s.push_back(pin);
29 return true;
30 }
31
32 bool addI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws,
33 GpioPin data_out, GpioPin data_in = -1, int port = 0) {
34 InfoI2S pin{function, mclk, bck, ws, data_out, data_in, port};
35 return addI2S(pin);
36 }
37
39 bool setI2S(InfoI2S pin) { return set<InfoI2S>(pin, i2s); }
40
41 bool addSPI(InfoSPI pin) {
42 if (getSPIPins(pin.function)) return false;
43 spi.push_back(pin);
44 return true;
45 }
46
47 bool addSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi,
48 GpioPin cs, SPIClass& spi) {
49 InfoSPI pin(function, clk, miso, mosi, cs, spi);
50 return addSPI(pin);
51 }
52
54 bool setSPI(InfoSPI pin) { return set<InfoSPI>(pin, spi); }
55
56 bool addI2C(InfoI2C pin) {
57 if (getI2CPins(pin.function)) return false;
58 i2c.push_back(pin);
59 return true;
60 }
61
62#ifdef ARDUINO
64 bool addI2C(PinFunction function, TwoWire& wire, bool setActive = false) {
65 PinsI2C pin(function, -1, -1, -1, -1, &wire, setActive);
66 return addI2C(pin);
67 }
68
69 bool addI2C(PinFunction function, GpioPin scl, GpioPin sda, int port,
70 uint32_t frequency, TwoWire& wire, bool active = true) {
71 PinsI2C pin(function, scl, sda, port, frequency, &wire, active);
72 return addI2C(pin);
73 }
74#endif
75
76 bool addI2C(PinFunction function, GpioPin scl, GpioPin sda, int port = -1,
77 uint32_t frequency = 100000, i2c_bus_handle_t wire = DEFAULT_WIRE,
78 bool active = true) {
79 InfoI2C pin(function, scl, sda, port, frequency, wire, active);
80 return addI2C(pin);
81 }
82
84 bool setI2C(InfoI2C pin) { return set<InfoI2C>(pin, i2c); }
85
86 bool addPin(InfoGPIO pin) {
87 pins.push_back(pin);
88 return true;
89 }
90
91 bool addPin(PinFunction function, GpioPin pinNo, PinLogic logic,
92 int index = 0) {
93 InfoGPIO pin(function, pinNo, logic, index);
94 return addPin(pin);
95 }
96
98 bool setPin(InfoGPIO updatedPin) {
99 for (InfoGPIO& pin : pins) {
100 if (pin.function == updatedPin.function &&
101 pin.index == updatedPin.index) {
102 pin = updatedPin;
103 return true;
104 }
105 }
106 return false;
107 }
108
110 bool setPinActive(int gpioPin, bool active) {
111 for (InfoGPIO& pin : pins) {
112 if (pin.pin == gpioPin) {
113 pin.active = active;
114 return true;
115 }
116 }
117 return false;
118 }
119
121 bool setPinActive(PinFunction func, int idx, bool active) {
122 auto pin = getPin(func, idx);
123 if (pin) {
124 auto value = pin.value();
125 value.active = active;
126 // update pin
127 return setPin(value);
128 }
129 return false;
130 }
131
134 int pos = 0) {
135 for (InfoGPIO& pin : pins) {
136 if (pin.function == function && pin.index == pos) return pin;
137 }
138 return {};
139 }
140
143 for (InfoGPIO& pin : pins) {
144 if (pin.pin == pinId) return pin;
145 }
146 return {};
147 }
148
149 GpioPin getPinID(PinFunction function, int pos = 0) {
150 auto pin = getPin(function, pos);
151 if (pin) return pin.value().pin;
152 return -1;
153 }
154
155 PinLogic getPinLogic(PinFunction function, int pos = 0) {
156 auto pin = getPin(function, pos);
157 if (pin) return pin.value().pin_logic;
158 return PinLogic::Inactive;
159 }
160
163 PinFunction function) override {
164 InfoI2C* pins = getPtr<InfoI2C>(function, i2c);
165 if (pins == nullptr) return {};
166 return *pins;
167 }
168
171 InfoSPI* pins = getPtr<InfoSPI>(function, spi);
172 if (pins == nullptr) return {};
173 return *pins;
174 }
175
178 for (InfoI2S& pins : i2s) {
179 if (pins.port == port) return pins;
180 }
181 return {};
182 }
183
186 PinFunction function = PinFunction::CODEC) {
187 InfoI2S* pins = getPtr<InfoI2S>(function, i2s);
188 if (pins == nullptr) return {};
189 return *pins;
190 }
191
193 virtual bool begin() {
194 AD_LOGD("DriverDeviceInfo::begin");
195
196 // setup function pins
197 setupPinMode();
198
199 // setup spi
200 bool result = true;
201 for (auto& tmp : spi) {
202 if (tmp.function == PinFunction::SD) {
203 if (sd_active)
204 result &= tmp.begin();
205 else
206 result &= tmp.begin();
207 }
208 }
209
210 // setup i2c
211 for (auto& tmp : i2c) {
212 result &= tmp.begin();
213 }
214 return result;
215 }
216
217 void end() {
218 // close spi
219 for (auto& tmp : spi) {
220 // close SD only when sd_active
221 if (tmp.function == PinFunction::SD) {
222 if (sd_active) tmp.end();
223 } else {
224 tmp.end();
225 }
226 }
227 // close i2c
228 for (auto& tmp : i2c) {
229 AD_LOGD("DriverDeviceInfo::begin::I2C::end");
230 tmp.end();
231 }
232 }
233
235 void setSPIActiveForSD(bool active) {
236 AD_LOGD("DriverDeviceInfo::setSPIActiveForSD: %d", active);
237 sd_active = active; }
238
240 bool isSPIActiveForSD() { return sd_active; }
241
243 void setSDMMCActive(bool active) {
244 AD_LOGD("DriverDeviceInfo::setSDMMCActive: %d", active);
245 sdmmc_active = active;
246 sd_active = false;
247 }
248
250 bool isSDMMCActive() { return sdmmc_active; }
251
253 bool hasPins() { return !pins.empty(); }
254
256 virtual bool isKeyPressed(uint8_t key) {
257 auto pin_opt = getPin(PinFunction::KEY, key);
258 if (!pin_opt) return false;
259 auto pin = pin_opt.value();
260 bool value = gpio.digitalRead(pin.pin);
261 return pin.pin_logic == PinLogic::InputActiveLow ? !value : value;
262 }
263
264 API_GPIO& getGPIO() { return gpio; }
265
266 protected:
271 GPIOExt gpio; // standard Arduino GPIO
272 bool sd_active = false;
273 bool sdmmc_active = false;
274
275 template <typename T>
277 for (auto& pins : vect) {
278 if (pins.function == function) return &pins;
279 }
280 return nullptr;
281 }
282
283 template <typename T>
285 T* pins = getPtr<T>(pin.function, vect);
286 if (pins == nullptr) return false;
287 *pins = pin;
288 return true;
289 }
290
292 gpio.begin(*this);
293 AD_LOGD("DriverDeviceInfo::setupPinMode");
294 // setup pins
295 for (auto& tmp : pins) {
296 if (tmp.pin != -1) {
297 if (!hasConflict(tmp.pin)) {
298 AD_LOGD("pinMode for %d", tmp.pin);
299 switch (tmp.pin_logic) {
301 gpio.pinMode(tmp.pin, INPUT);
302 break;
304 gpio.pinMode(tmp.pin, INPUT_PULLUP);
305 break;
306 case PinLogic::Input:
307 gpio.pinMode(tmp.pin, INPUT);
308 break;
309 case PinLogic::Output:
311 gpio.pinMode(tmp.pin, OUTPUT);
312 break;
313 default:
314 // do nothing
315 break;
316 }
317 } else {
318 AD_LOGW("Pin '%d' not set up because of conflict", tmp.pin);
319 tmp.active = false;
320 }
321 } else {
322 AD_LOGD("Pin is -1");
323 }
324 }
325 }
326
327 bool hasConflict(int pin) {
328 return hasSPIConflict(pin) || hasI2CConflict(pin);
329 }
330
331 bool hasSPIConflict(int pin) {
332 if (sd_active) {
333 auto sd = getSPIPins(PinFunction::SD);
334 if (!sd) return false;
335 InfoSPI spi = sd.value();
336 return spi.cs == pin || spi.clk == pin || spi.miso == pin ||
337 spi.mosi == pin;
338 }
339 return false;
340 }
341
342 bool hasI2CConflict(int pin) {
343 for (auto& i2c_entry : i2c) {
344 if (i2c_entry.scl == pin || i2c_entry.sda == pin) {
345 return true;
346 }
347 }
348 return false;
349 }
350};
351
353
360 bool isKeyPressed(uint8_t key) override {
361 bool result = false;
362#if defined(ESP32) && defined(ARDUINO)
363#if SOC_TOUCH_SENSOR_SUPPORTED
364 auto pin_opt = getPin(PinFunction::KEY, key);
365 if (!pin_opt) return false;
366 auto pin = pin_opt.value();
367 int value = touchRead(pin.pin);
368 result = value <= touch_limit;
369 if (result) {
370 // retry to confirm reading
371 value = touchRead(pin.pin);
372 result = value <= touch_limit;
373 }
374#endif
375#endif
376 return result;
377 }
378 void setTouchLimit(int limit) { touch_limit = limit; }
379
380 protected:
382};
383
384
385// -- Pins
392} // namespace audio_driver
393
394#endif
#define INPUT
Definition DriverCommon.h:41
#define OUTPUT
Definition DriverCommon.h:44
#define INPUT_PULLUP
Definition DriverCommon.h:47
#define DEFAULT_WIRE
Definition DriverCommon.h:32
#define TOUCH_LIMIT
Definition DriverCommon.h:59
Simple GPIO abstraction for digital input/output.
#define AD_LOGD(...)
Definition Logger.h:12
#define AD_LOGW(...)
Definition Logger.h:14
Definition API_GPIO.h:8
All pins for i2s, spi, i2c and other pins.
Definition DriverDeviceInfo.h:20
audio_driver_local::Optional< InfoI2S > getI2SPins(PinFunction function=PinFunction::CODEC)
Finds the I2S pin info with the help of the function.
Definition DriverDeviceInfo.h:185
GpioPin getPinID(PinFunction function, int pos=0)
Definition DriverDeviceInfo.h:149
DriverDeviceInfo & operator=(const DriverDeviceInfo &)=delete
API_GPIO & getGPIO()
Definition DriverDeviceInfo.h:264
PinLogic getPinLogic(PinFunction function, int pos=0)
Definition DriverDeviceInfo.h:155
bool sdmmc_active
Definition DriverDeviceInfo.h:273
bool setI2C(InfoI2C pin)
Updates the I2C pin information using the function as key.
Definition DriverDeviceInfo.h:84
bool isSDMMCActive()
Check if SDMMC has been activated.
Definition DriverDeviceInfo.h:250
GPIOExt gpio
Definition DriverDeviceInfo.h:271
bool setPinActive(PinFunction func, int idx, bool active)
Updates an existing pin active flag for the indicated gpio.
Definition DriverDeviceInfo.h:121
bool addI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws, GpioPin data_out, GpioPin data_in=-1, int port=0)
Definition DriverDeviceInfo.h:32
audio_driver_local::Optional< InfoSPI > getSPIPins(PinFunction function)
Finds the SPI pin info with the help of the function.
Definition DriverDeviceInfo.h:170
virtual bool begin()
Sets up all pins e.g. by calling pinMode()
Definition DriverDeviceInfo.h:193
DriverDeviceInfo(const DriverDeviceInfo &)=delete
audio_driver_local::Optional< InfoI2C > getI2CPins(PinFunction function) override
Finds the I2C pin info with the help of the function.
Definition DriverDeviceInfo.h:162
bool addI2C(InfoI2C pin)
Definition DriverDeviceInfo.h:56
bool addPin(PinFunction function, GpioPin pinNo, PinLogic logic, int index=0)
Definition DriverDeviceInfo.h:91
T * getPtr(PinFunction function, audio_driver_local::Vector< T > &vect)
Definition DriverDeviceInfo.h:276
bool addI2S(InfoI2S pin)
Definition DriverDeviceInfo.h:26
audio_driver_local::Vector< InfoGPIO > pins
Definition DriverDeviceInfo.h:270
bool isSPIActiveForSD()
Check if SPI for SD should be started automatically.
Definition DriverDeviceInfo.h:240
bool hasConflict(int pin)
Definition DriverDeviceInfo.h:327
audio_driver_local::Optional< InfoGPIO > getPin(GpioPin pinId)
Get pin information by pin ID.
Definition DriverDeviceInfo.h:142
bool setSPI(InfoSPI pin)
Updates the SPI pin information using the function as key.
Definition DriverDeviceInfo.h:54
bool setPin(InfoGPIO updatedPin)
Updates an existing pin information using the function and index as key.
Definition DriverDeviceInfo.h:98
bool addSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi, GpioPin cs, SPIClass &spi)
Definition DriverDeviceInfo.h:47
bool setPinActive(int gpioPin, bool active)
Updates an existing pin active flag for the indicated gpio.
Definition DriverDeviceInfo.h:110
void setSDMMCActive(bool active)
Defines if SPI for SD should be started (by default true)
Definition DriverDeviceInfo.h:243
void end()
Definition DriverDeviceInfo.h:217
bool addPin(InfoGPIO pin)
Definition DriverDeviceInfo.h:86
void setupPinMode()
Definition DriverDeviceInfo.h:291
bool sd_active
Definition DriverDeviceInfo.h:272
void setSPIActiveForSD(bool active)
Defines if SPI for SD should be started (by default true)
Definition DriverDeviceInfo.h:235
bool addI2C(PinFunction function, GpioPin scl, GpioPin sda, int port=-1, uint32_t frequency=100000, i2c_bus_handle_t wire=DEFAULT_WIRE, bool active=true)
Definition DriverDeviceInfo.h:76
bool set(T pin, audio_driver_local::Vector< T > &vect)
Definition DriverDeviceInfo.h:284
bool hasPins()
Returns true if some function pins have been defined.
Definition DriverDeviceInfo.h:253
audio_driver_local::Vector< InfoSPI > spi
Definition DriverDeviceInfo.h:268
audio_driver_local::Optional< InfoGPIO > getPin(PinFunction function, int pos=0)
Get pin information by function.
Definition DriverDeviceInfo.h:133
audio_driver_local::Vector< InfoI2S > i2s
Definition DriverDeviceInfo.h:267
bool hasI2CConflict(int pin)
Definition DriverDeviceInfo.h:342
bool hasSPIConflict(int pin)
Definition DriverDeviceInfo.h:331
audio_driver_local::Vector< InfoI2C > i2c
Definition DriverDeviceInfo.h:269
bool addSPI(InfoSPI pin)
Definition DriverDeviceInfo.h:41
audio_driver_local::Optional< InfoI2S > getI2SPins(int port)
Finds the I2S pin info with the help of the port.
Definition DriverDeviceInfo.h:177
virtual bool isKeyPressed(uint8_t key)
returns true if pressed
Definition DriverDeviceInfo.h:256
bool setI2S(InfoI2S pin)
Updates the I2S pin information using the function as key.
Definition DriverDeviceInfo.h:39
Support for Touch.
Definition DriverDeviceInfo.h:359
int touch_limit
Definition DriverDeviceInfo.h:381
Definition GPIOExt.h:19
bool digitalRead(GpioPin pin) override
Definition GPIOExt.h:50
void pinMode(GpioPin pin, int mode) override
Definition GPIOExt.h:35
bool begin(IDriverDeviceInfo &pins) override
Definition GPIOExt.h:26
Definition IDriverDeviceInfo.h:171
A simple optonal implementation.
Definition Optional.h:10
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:15
static DriverDeviceInfo NoPins
Pins need to be set up in the sketch.
Definition DriverDeviceInfo.h:390
Namespace for audio driver components.
Definition AudioBoard.h:5
int16_t GpioPin
Definition DriverCommon.h:153
PinLogic
Definition DriverCommon.h:328
PinFunction
Definition DriverCommon.h:344
InfoI2C PinsI2C
Definition IDriverDeviceInfo.h:168
void * i2c_bus_handle_t
Definition DriverCommon.h:155
Pins for LED, Buttons, AMP etc.
Definition IDriverDeviceInfo.h:24
int index
Definition IDriverDeviceInfo.h:35
PinFunction function
Definition IDriverDeviceInfo.h:33
I2C pins.
Definition IDriverDeviceInfo.h:127
PinFunction function
Definition IDriverDeviceInfo.h:151
I2S pins.
Definition IDriverDeviceInfo.h:47
PinFunction function
Definition IDriverDeviceInfo.h:59
SPI pins: In Arduino we initialize the SPI, on other platform we just provide the pin information.
Definition IDriverDeviceInfo.h:76
PinFunction function
Definition IDriverDeviceInfo.h:94
Definition API_SPI.h:13
GpioPin cs
Definition API_SPI.h:23
Definition API_I2C.h:36