Skip to content

Commit 822ba7f

Browse files
committed
simplify Wire
1 parent f69dfff commit 822ba7f

File tree

1 file changed

+80
-93
lines changed

1 file changed

+80
-93
lines changed

src/Driver.h

Lines changed: 80 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -322,29 +322,16 @@ class AudioDriver {
322322
DriverPins *p_pins = nullptr;
323323
int i2c_default_address = -1;
324324

325-
#ifdef ARDUINO
326325
/// Determine the TwoWire object from the I2C config or use Wire
327-
virtual TwoWire *getI2C() {
328-
if (p_pins == nullptr) return &Wire;
326+
virtual i2c_bus_handle_t getI2C() {
327+
if (p_pins == nullptr) return DEFAULT_WIRE;
329328
auto i2c = pins().getI2CPins(PinFunction::CODEC);
330329
if (!i2c) {
331-
return &Wire;
332-
}
333-
TwoWire *result = (TwoWire *)i2c.value().p_wire;
334-
return result != nullptr ? result : &Wire;
335-
}
336-
#else
337-
/// Determine the TwoWire object from the I2C config or use Wire
338-
virtual i2c_bus_handle_t *getI2C() {
339-
if (p_pins == nullptr) return nullptr;
340-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
341-
if (!i2c) {
342-
return nullptr;
330+
return DEFAULT_WIRE;
343331
}
344332
i2c_bus_handle_t *result = (i2c_bus_handle_t *)i2c.value().p_wire;
345333
return result;
346334
}
347-
#endif
348335

349336
virtual bool init(codec_config_t codec_cfg) { return false; }
350337
virtual bool deinit() { return false; }
@@ -420,83 +407,6 @@ class AudioDriverAC101Class : public AudioDriver {
420407
}
421408
};
422409

423-
#ifdef ARDUINO
424-
// currently only supported in Arduino because we do not have a platform
425-
// independent SPI API
426-
/**
427-
* @brief Driver API for AD1938 TDS DAC/ADC
428-
* @author Phil Schatzmann
429-
* @copyright GPLv3
430-
*/
431-
class AudioDriverAD1938Class : public AudioDriver {
432-
public:
433-
bool begin(CodecConfig codecCfg, DriverPins &pins) override {
434-
int clatch = pins.getPinID(PinFunction::LATCH);
435-
if (clatch < 0) return false;
436-
int reset = pins.getPinID(PinFunction::RESET);
437-
if (reset < 0) return false;
438-
auto spi_opt = pins.getSPIPins(PinFunction::CODEC);
439-
SPIClass *p_spi = nullptr;
440-
if (spi_opt) {
441-
p_spi = (SPIClass *)spi_opt.value().p_spi;
442-
} else {
443-
p_spi = &SPI;
444-
p_spi->begin();
445-
}
446-
// setup pins
447-
pins.begin();
448-
// setup ad1938
449-
ad1938.begin(codecCfg, clatch, reset, *p_spi);
450-
ad1938.enable();
451-
ad1938.setMute(false);
452-
return true;
453-
}
454-
virtual bool setConfig(CodecConfig codecCfg) {
455-
assert(p_pins != nullptr);
456-
bool result = begin(codecCfg, *p_pins);
457-
return result;
458-
}
459-
bool end(void) override { return ad1938.end(); }
460-
bool setMute(bool mute) override { return ad1938.setMute(mute); }
461-
// mutes an individual DAC: valid range (0:3)
462-
bool setMute(bool mute, int line) {
463-
if (line > 3) return false;
464-
return ad1938.setVolumeDAC(
465-
line, mute ? 0.0 : (static_cast<float>(volumes[line]) / 100.0f));
466-
}
467-
468-
/// Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
469-
bool setVolume(int volume) override {
470-
this->volume = volume;
471-
for (int j = 0; j < 8; j++) {
472-
volumes[j] = volume;
473-
}
474-
return ad1938.setVolume(static_cast<float>(volume) / 100.0f);
475-
}
476-
/// Defines the Volume per DAC (in %) if volume is 0, mute is enabled,range is
477-
/// 0-100.
478-
bool setVolume(int volume, int line) {
479-
if (line > 7) return false;
480-
volumes[line] = volume;
481-
return ad1938.setVolumeDAC(static_cast<float>(volume) / 100.0f, line);
482-
}
483-
484-
int getVolume() override { return volume; }
485-
bool setInputVolume(int volume) override { return false; }
486-
bool isVolumeSupported() override { return true; }
487-
bool isInputVolumeSupported() override { return false; }
488-
489-
DriverPins &pins() { return *p_pins; }
490-
AD1938 &driver() { return ad1938; }
491-
492-
protected:
493-
AD1938 ad1938;
494-
int volume = 100;
495-
int volumes[8] = {100};
496-
};
497-
498-
#endif
499-
500410
/**
501411
* @brief Driver API for the CS43l22 codec chip on 0x94 (0x4A<<1)
502412
* @author Phil Schatzmann
@@ -1529,6 +1439,83 @@ class AudioDriverLyratMiniClass : public AudioDriver {
15291439
AudioDriverES7243Class adc;
15301440
};
15311441

1442+
#ifdef ARDUINO
1443+
// currently only supported in Arduino because we do not have a platform
1444+
// independent SPI API
1445+
/**
1446+
* @brief Driver API for AD1938 TDS DAC/ADC
1447+
* @author Phil Schatzmann
1448+
* @copyright GPLv3
1449+
*/
1450+
class AudioDriverAD1938Class : public AudioDriver {
1451+
public:
1452+
bool begin(CodecConfig codecCfg, DriverPins &pins) override {
1453+
int clatch = pins.getPinID(PinFunction::LATCH);
1454+
if (clatch < 0) return false;
1455+
int reset = pins.getPinID(PinFunction::RESET);
1456+
if (reset < 0) return false;
1457+
auto spi_opt = pins.getSPIPins(PinFunction::CODEC);
1458+
SPIClass *p_spi = nullptr;
1459+
if (spi_opt) {
1460+
p_spi = (SPIClass *)spi_opt.value().p_spi;
1461+
} else {
1462+
p_spi = &SPI;
1463+
p_spi->begin();
1464+
}
1465+
// setup pins
1466+
pins.begin();
1467+
// setup ad1938
1468+
ad1938.begin(codecCfg, clatch, reset, *p_spi);
1469+
ad1938.enable();
1470+
ad1938.setMute(false);
1471+
return true;
1472+
}
1473+
virtual bool setConfig(CodecConfig codecCfg) {
1474+
assert(p_pins != nullptr);
1475+
bool result = begin(codecCfg, *p_pins);
1476+
return result;
1477+
}
1478+
bool end(void) override { return ad1938.end(); }
1479+
bool setMute(bool mute) override { return ad1938.setMute(mute); }
1480+
// mutes an individual DAC: valid range (0:3)
1481+
bool setMute(bool mute, int line) {
1482+
if (line > 3) return false;
1483+
return ad1938.setVolumeDAC(
1484+
line, mute ? 0.0 : (static_cast<float>(volumes[line]) / 100.0f));
1485+
}
1486+
1487+
/// Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
1488+
bool setVolume(int volume) override {
1489+
this->volume = volume;
1490+
for (int j = 0; j < 8; j++) {
1491+
volumes[j] = volume;
1492+
}
1493+
return ad1938.setVolume(static_cast<float>(volume) / 100.0f);
1494+
}
1495+
/// Defines the Volume per DAC (in %) if volume is 0, mute is enabled,range is
1496+
/// 0-100.
1497+
bool setVolume(int volume, int line) {
1498+
if (line > 7) return false;
1499+
volumes[line] = volume;
1500+
return ad1938.setVolumeDAC(static_cast<float>(volume) / 100.0f, line);
1501+
}
1502+
1503+
int getVolume() override { return volume; }
1504+
bool setInputVolume(int volume) override { return false; }
1505+
bool isVolumeSupported() override { return true; }
1506+
bool isInputVolumeSupported() override { return false; }
1507+
1508+
DriverPins &pins() { return *p_pins; }
1509+
AD1938 &driver() { return ad1938; }
1510+
1511+
protected:
1512+
AD1938 ad1938;
1513+
int volume = 100;
1514+
int volumes[8] = {100};
1515+
};
1516+
1517+
#endif
1518+
15321519
// -- Drivers
15331520
/// @ingroup audio_driver
15341521
static AudioDriverAC101Class AudioDriverAC101;

0 commit comments

Comments
 (0)