Skip to content

Commit 99679c5

Browse files
committed
API to update pins
1 parent 0ad3e04 commit 99679c5

File tree

8 files changed

+88
-36
lines changed

8 files changed

+88
-36
lines changed

examples/audiotools/audiotools-custom-max/audiotools-custom-max.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @brief We define a custom board with the i2c and i2s pins and output a sine
2+
* @brief We define a custom board with the i2c and i2s pins and output_device a sine
33
* with the help of the AudioTools I2SCodecStream
44
* @author phil schatzmann
55
*/

examples/audiotools/audiotools-custom-min/audiotools-custom-min.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @brief We define a custom board w/o any pins and output a sine tone
2+
* @brief We define a custom board w/o any pins and output_device a sine tone
33
* with the help of the AudioTools I2SCodecStream
44
* @author phil schatzmann
55
*/

examples/audiotools/audiotools-standard/audiotools-standard.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @brief We use a predefied board (AudioKitEs8388V1) and output a sine
2+
* @brief We use a predefied board (AudioKitEs8388V1) and output_device a sine
33
* with the help of the AudioTools I2SCodecStream
44
* @author phil schatzmann
55
*/

examples/custom/custom-max/custom-max.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ void setup() {
1818
// add i2c codec pins: scl, sda, port
1919
my_pins.addI2C(CODEC, 32, 22, 0x20);
2020
// example add other pins: PA on gpio 21
21-
my_pins.addPin(PA, 21, PinLogic::Output);
21+
my_pins.addPin(PA, 21, PinLogic::output_device);
2222

2323
// configure codec
2424
CodecConfig cfg;
25-
cfg.adc_input = ADC_INPUT_LINE1;
26-
cfg.dac_output = DAC_OUTPUT_ALL;
25+
cfg.input_device = input_device_LINE1;
26+
cfg.output_device = output_device_ALL;
2727
cfg.i2s.bits = BIT_LENGTH_16BITS;
2828
cfg.i2s.rate = RATE_44K;
2929
// cfg.i2s.fmt = I2S_NORMAL;

examples/custom/custom-min/custom-min.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ void setup() {
1717
Wire.begin();
1818
// configure codec
1919
CodecConfig cfg;
20-
cfg.adc_input = ADC_INPUT_LINE1;
21-
cfg.dac_output = DAC_OUTPUT_ALL;
20+
cfg.input_device = input_device_LINE1;
21+
cfg.output_device = output_device_ALL;
2222
cfg.i2s.bits = BIT_LENGTH_16BITS;
2323
cfg.i2s.rate = RATE_44K;
2424
// cfg.i2s.fmt = I2S_NORMAL;

examples/custom/custom-standard/custom-standard.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ void setup() {
1212

1313
// configure codec
1414
CodecConfig cfg;
15-
cfg.adc_input = ADC_INPUT_LINE1;
16-
cfg.dac_output = DAC_OUTPUT_ALL;
15+
cfg.input_device = input_device_LINE1;
16+
cfg.output_device = output_device_ALL;
1717
cfg.i2s.bits = BIT_LENGTH_16BITS;
1818
cfg.i2s.rate = RATE_44K;
1919
// cfg.i2s.fmt = I2S_NORMAL;

src/DriverPins.h

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ struct PinsI2C {
197197
}
198198
return true;
199199
}
200-
void end() {
200+
void end() {
201201
#ifndef ESP8266
202202
p_wire->end();
203-
#endif
203+
#endif
204204
}
205205
};
206206

@@ -233,32 +233,57 @@ struct PinsFunction {
233233
*/
234234
class DriverPins {
235235
public:
236-
void addI2S(PinsI2S pin) { i2s.push_back(pin); }
237-
void addI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws,
236+
bool addI2S(PinsI2S pin) {
237+
if (getI2SPins(pin.function)) return false;
238+
i2s.push_back(pin);
239+
return true;
240+
}
241+
242+
bool addI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws,
238243
GpioPin data_out, GpioPin data_in = -1, int port = 0) {
239244
PinsI2S pin{function, mclk, bck, ws, data_out, data_in, port};
240-
addI2S(pin);
245+
return addI2S(pin);
241246
}
242-
void addSPI(PinsSPI pin) { spi.push_back(pin); }
243-
void addSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi,
247+
248+
bool setI2S(PinsI2S pin) { return set<PinsI2S>(pin, i2s); }
249+
250+
bool addSPI(PinsSPI pin) {
251+
if (getSPIPins(pin.function)) return false;
252+
spi.push_back(pin);
253+
return true;
254+
}
255+
256+
bool addSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi,
244257
GpioPin cs, SPIClass &spi = SPI) {
245258
PinsSPI pin(function, clk, miso, mosi, cs, spi);
246-
addSPI(pin);
259+
return addSPI(pin);
247260
}
261+
262+
bool setSPI(PinsSPI pin) { return set<PinsSPI>(pin, spi); }
248263

249-
void addI2C(PinsI2C pin) { i2c.push_back(pin); }
250-
void addI2C(PinFunction function, GpioPin scl, GpioPin sda, int port = -1,
264+
bool addI2C(PinsI2C pin) {
265+
if (getI2CPins(pin.function)) return false;
266+
i2c.push_back(pin);
267+
return true;
268+
}
269+
270+
bool addI2C(PinFunction function, GpioPin scl, GpioPin sda, int port = -1,
251271
uint32_t frequency = 100000, TwoWire &wire = Wire) {
252272
PinsI2C pin(function, scl, sda, port, frequency, wire);
253-
addI2C(pin);
273+
return addI2C(pin);
254274
}
275+
276+
bool setI2C(PinsI2C pin) { return set<PinsI2C>(pin, i2c); }
255277

256-
void addPin(PinsFunction pin) { pins.push_back(pin); }
278+
bool addPin(PinsFunction pin) {
279+
pins.push_back(pin);
280+
return true;
281+
}
257282

258-
void addPin(PinFunction function, GpioPin pinNo, PinLogic logic,
283+
bool addPin(PinFunction function, GpioPin pinNo, PinLogic logic,
259284
int index = 0) {
260285
PinsFunction pin(function, pinNo, logic, index);
261-
addPin(pin);
286+
return addPin(pin);
262287
}
263288
/// Get pin information by function
264289
Optional<PinsFunction> getPin(PinFunction function, int pos = 0) {
@@ -283,17 +308,15 @@ class DriverPins {
283308
}
284309

285310
Optional<PinsI2C> getI2CPins(PinFunction function) {
286-
for (PinsI2C &pin : i2c) {
287-
if (pin.function == function) return pin;
288-
}
289-
return {};
311+
PinsI2C *pins = getPtr<PinsI2C>(function, i2c);
312+
if (pins == nullptr) return {};
313+
return *pins;
290314
}
291315

292316
Optional<PinsSPI> getSPIPins(PinFunction function) {
293-
for (PinsSPI &pins : spi) {
294-
if (pins.function == function) return pins;
295-
}
296-
return {};
317+
PinsSPI *pins = getPtr<PinsSPI>(function, spi);
318+
if (pins == nullptr) return {};
319+
return *pins;
297320
}
298321

299322
/// Finds the I2S pins with the help of the port
@@ -306,10 +329,9 @@ class DriverPins {
306329

307330
/// Finds the I2S pins with the help of the function
308331
Optional<PinsI2S> getI2SPins(PinFunction function = PinFunction::CODEC) {
309-
for (PinsI2S &pins : i2s) {
310-
if (pins.function == function) return pins;
311-
}
312-
return {};
332+
PinsI2S *pins = getPtr<PinsI2S>(function, i2s);
333+
if (pins == nullptr) return {};
334+
return *pins;
313335
}
314336

315337
virtual bool begin() {
@@ -370,6 +392,22 @@ class DriverPins {
370392
Vector<PinsFunction> pins{0};
371393
bool sd_active = true;
372394

395+
template <typename T>
396+
T *getPtr(PinFunction function, Vector<T> &vect) {
397+
for (auto &pins : vect) {
398+
if (pins.function == function) return &pins;
399+
}
400+
return nullptr;
401+
}
402+
403+
template <typename T>
404+
bool set(T pin, Vector<T> &vect) {
405+
T *pins = getPtr<T>(pin.function, vect);
406+
if (pins == nullptr) return false;
407+
*pins = pin;
408+
return true;
409+
}
410+
373411
void setupPinMode() {
374412
// setup pins
375413
for (auto &tmp : pins) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"folders": [
3+
{
4+
"path": ".."
5+
}
6+
],
7+
"settings": {
8+
"files.associations": {
9+
"driverconstants.h": "c",
10+
"*.tcc": "cpp",
11+
"stdbool.h": "c"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)