Skip to content

Commit ae84e5c

Browse files
committed
Lyrat Mini Support
1 parent 5f32c7b commit ae84e5c

File tree

3 files changed

+86
-63
lines changed

3 files changed

+86
-63
lines changed

src/AudioBoard.h

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@ namespace audio_driver {
1212
*/
1313
class AudioBoard {
1414
public:
15-
AudioBoard(AudioDriver *driver, DriverPins* pins=&NoPins) {
16-
this->pins = pins;
17-
this->driver = driver;
15+
16+
AudioBoard(AudioDriver *driver, DriverPins* pins) {
17+
this->p_pins = pins;
18+
this->p_driver = driver;
1819
}
1920

20-
AudioBoard(AudioDriver &driver, DriverPins& pins=NoPins) {
21-
this->pins = &pins;
22-
this->driver = &driver;
21+
AudioBoard(AudioDriver &driver, DriverPins& pins) {
22+
this->p_pins = &pins;
23+
this->p_driver = &driver;
2324
}
2425

2526
bool begin(){
2627
AD_LOGD("AudioBoard::begin");
27-
pins->setSPIActiveForSD(codec_cfg.sd_active);
28-
if (!pins->begin()){
28+
if (p_pins==nullptr){
29+
AD_LOGE("pins are null");
30+
return false;
31+
}
32+
p_pins->setSPIActiveForSD(codec_cfg.sd_active);
33+
if (!p_pins->begin()){
2934
AD_LOGE("AudioBoard::pins::begin failed");
3035
return false;
3136
}
32-
if (!driver->begin(codec_cfg, *pins)){
37+
if (!p_driver->begin(codec_cfg, *p_pins)){
3338
AD_LOGE("AudioBoard::driver::begin failed");
3439
return false;
3540
}
@@ -47,45 +52,53 @@ class AudioBoard {
4752
/// Updates the CodecConfig values -> reconfigures the codec only
4853
bool setConfig(CodecConfig cfg) {
4954
this->codec_cfg = cfg;
50-
return driver->setConfig(cfg);
55+
return p_driver->setConfig(cfg);
5156
}
5257

5358
bool end(void) {
54-
pins->end();
59+
p_pins->end();
5560
is_active = false;
56-
return driver->end();
61+
return p_driver->end();
5762
}
58-
bool setMute(bool enable) { return driver->setMute(enable); }
63+
bool setMute(bool enable) { return p_driver->setMute(enable); }
5964
bool setMute(bool enable, int line) {
6065
if (line == power_amp_line) setPAPower(!enable);
61-
return driver->setMute(enable, line);
66+
return p_driver->setMute(enable, line);
6267
}
6368
bool setVolume(int volume) {
6469
AD_LOGD("setVolume: %d", volume);
6570
// when we get the volume we make sure that we report the same value
6671
// w/o rounding issues
6772
this->volume = volume;
68-
return (is_active) ? driver->setVolume(volume) : false;
73+
return (is_active) ? p_driver->setVolume(volume) : false;
6974
}
7075
int getVolume() {
7176
#if DRIVER_REPORT_DRIVER_VOLUME
7277
return driver->getVolume(); }
7378
#else
74-
return volume >= 0 ? volume : driver->getVolume(); }
79+
return volume >= 0 ? volume : p_driver->getVolume(); }
7580
#endif
76-
DriverPins& getPins() { return *pins; }
77-
bool setPAPower(bool enable) { return driver->setPAPower(enable); }
81+
DriverPins& getPins() { return *p_pins; }
82+
DriverPins& pins() { return *p_pins; }
83+
84+
bool setPAPower(bool enable) { return is_active ? p_driver->setPAPower(enable) : false; }
85+
7886
/// set volume for adc: this is only supported on some defined codecs
79-
bool setInputVolume(int volume) {return driver->setInputVolume(volume);}
87+
bool setInputVolume(int volume) {return p_driver->setInputVolume(volume);}
8088

8189
AudioDriver* getDriver(){
82-
return driver;
90+
return p_driver;
91+
}
92+
AudioDriver& driver(){
93+
return *p_driver;
8394
}
8495

96+
operator bool() { return is_active && p_driver != nullptr && p_pins != nullptr;}
97+
8598
protected:
86-
DriverPins* pins;
99+
AudioDriver* p_driver = nullptr;
100+
DriverPins* p_pins = nullptr;
87101
CodecConfig codec_cfg;
88-
AudioDriver* driver = nullptr;
89102
int power_amp_line = ES8388_PA_LINE;
90103
int volume = -1;
91104
bool is_active = false;

src/Driver.h

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ class CodecConfig : public codec_config_t {
190190
*/
191191
class AudioDriver {
192192
public:
193+
193194
/// Starts the processing
194195
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
195-
AD_LOGD("AudioDriver::begin");
196+
AD_LOGI("AudioDriver::begin");
196197
p_pins = &pins;
197198
pins.setSPIActiveForSD(codecCfg.sd_active);
198199
if (!setConfig(codecCfg)){
@@ -206,27 +207,22 @@ class AudioDriver {
206207
}
207208
/// changes the configuration
208209
virtual bool setConfig(CodecConfig codecCfg) {
210+
AD_LOGI("AudioDriver::setConfig");
209211
codec_cfg = codecCfg;
210212
if (!init(codec_cfg)) {
211-
AD_LOGE("AudioDriver::begin::init failed");
213+
AD_LOGE("AudioDriver init failed");
212214
return false;
213-
} else {
214-
AD_LOGD("AudioDriver::begin::init succeeded");
215-
}
215+
}
216216
codec_mode_t codec_mode = codec_cfg.get_mode();
217217
if (!controlState(codec_mode)) {
218-
AD_LOGE("AudioDriver::begin::controlState failed");
218+
AD_LOGE("AudioDriver controlState failed");
219219
return false;
220-
} else {
221-
AD_LOGD("AudioDriver::begin::controlState succeeded");
222-
}
220+
}
223221
bool result = configInterface(codec_mode, codec_cfg.i2s);
224222
if (!result) {
225-
AD_LOGE("AudioDriver::begin::configInterface failed");
223+
AD_LOGE("AudioDriver configInterface failed");
226224
return false;
227-
} else {
228-
AD_LOGD("AudioDriver::begin::configInterface succeeded");
229-
}
225+
}
230226
return result;
231227
}
232228
/// Ends the processing: shut down dac and adc
@@ -250,28 +246,32 @@ class AudioDriver {
250246
/// Determines if setInputVolume() is supported
251247
virtual bool isInputVolumeSupported() { return false; }
252248
/// Provides the pin information
253-
DriverPins &pins() { return *p_pins; }
249+
virtual DriverPins &pins() { return *p_pins; }
254250

255251
/// Sets the PA Power pin to active or inactive
256252
bool setPAPower(bool enable) {
253+
if (p_pins == nullptr) {
254+
AD_LOGI("pins are null");
255+
return false;
256+
}
257257
GpioPin pin = pins().getPinID(PinFunction::PA);
258258
if (pin == -1) {
259+
AD_LOGI("PinFunction::PA not defined");
259260
return false;
260261
}
261262
AD_LOGI("setPAPower pin %d -> %d", pin, enable);
262263
digitalWrite(pin, enable ? HIGH : LOW);
263264
return true;
264265
}
265266

266-
/// Gets the number of I2S Interfaces
267-
virtual int getI2SCount() { return 1; }
267+
operator bool() {return p_pins != nullptr;}
268268

269269
protected:
270270
CodecConfig codec_cfg;
271271
DriverPins *p_pins = nullptr;
272272

273273
/// Determine the TwoWire object from the I2C config or use Wire
274-
TwoWire *getI2C() {
274+
virtual TwoWire *getI2C() {
275275
if (p_pins == nullptr) return &Wire;
276276
auto i2c = pins().getI2CPins(PinFunction::CODEC);
277277
if (!i2c) {
@@ -281,7 +281,7 @@ class AudioDriver {
281281
return result != nullptr ? result : &Wire;
282282
}
283283

284-
int getI2CAddress() {
284+
virtual int getI2CAddress() {
285285
if (p_pins == nullptr) return -1;
286286
auto i2c = pins().getI2CPins(PinFunction::CODEC);
287287
if (i2c) {
@@ -316,8 +316,8 @@ class AudioDriver {
316316
class NoDriverClass : public AudioDriver {
317317
public:
318318
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
319-
codec_cfg = codecCfg;
320-
p_pins = &pins;
319+
//codec_cfg = codecCfg;
320+
//p_pins = &pins;
321321
return true;
322322
}
323323
virtual bool end(void) { return true; }
@@ -390,6 +390,7 @@ class AudioDriverAD1938Class : public AudioDriver {
390390
return true;
391391
}
392392
virtual bool setConfig(CodecConfig codecCfg) {
393+
assert(p_pins != nullptr);
393394
bool result = begin(codecCfg, *p_pins);
394395
return result;
395396
}
@@ -428,7 +429,6 @@ class AudioDriverAD1938Class : public AudioDriver {
428429

429430
protected:
430431
AD1938 ad1938;
431-
DriverPins *p_pins = nullptr;
432432
int volume = 100;
433433
int volumes[8] = {100};
434434
};
@@ -563,6 +563,7 @@ class AudioDriverCS42448Class : public AudioDriver {
563563
cs42448.setMute(false);
564564
}
565565
} else {
566+
assert(p_pins != nullptr);
566567
result = begin(codecCfg, *p_pins);
567568
}
568569
return result;
@@ -594,7 +595,6 @@ class AudioDriverCS42448Class : public AudioDriver {
594595

595596
protected:
596597
CS42448 cs42448;
597-
DriverPins *p_pins = nullptr;
598598
int volume = 100;
599599
CodecConfig cfg;
600600
};
@@ -1414,27 +1414,35 @@ class AudioDriverPCM3168Class : public AudioDriver {
14141414
class AudioDriverLyratMiniClass : public AudioDriver {
14151415
public:
14161416
bool begin(CodecConfig codecCfg, DriverPins &pins) {
1417-
int rc = 0;
1418-
if (codecCfg.output_device != DAC_OUTPUT_NONE)
1419-
rc += !dac.begin(codecCfg, pins);
1420-
if (codecCfg.input_device != ADC_INPUT_NONE)
1421-
rc += !adc.begin(codecCfg, pins);
1422-
return rc == 0;
1417+
AD_LOGI("AudioDriverLyratMiniClass::begin");
1418+
p_pins = &pins;
1419+
codec_cfg = codecCfg;
1420+
bool ok = true;
1421+
if (codecCfg.output_device != DAC_OUTPUT_NONE){
1422+
AD_LOGI("starting DAC");
1423+
ok = dac.begin(codecCfg, pins);
1424+
}
1425+
if (codecCfg.input_device != ADC_INPUT_NONE){
1426+
AD_LOGI("starting ADC");
1427+
ok = ok && adc.begin(codecCfg, pins);
1428+
}
1429+
if (!ok) {
1430+
AD_LOGI("AudioDriverLyratMiniClass::begin failed");
1431+
}
1432+
return ok;
14231433
}
14241434
bool end(void) {
14251435
int rc = 0;
14261436
rc += dac.end();
14271437
rc += adc.end();
1428-
return rc == 0;
1438+
return rc == 2;
14291439
}
14301440
bool setMute(bool enable) { return dac.setMute(enable); }
14311441
bool setVolume(int volume) { return dac.setVolume(volume); }
14321442
int getVolume() { return dac.getVolume(); }
14331443
bool setInputVolume(int volume) { return adc.setVolume(volume); }
14341444
int getInputVolume() { return adc.getVolume(); }
14351445
bool isInputVolumeSupported() { return true; }
1436-
// Separate ADC and DAC I2S
1437-
int getI2SCount() override { return 2; }
14381446

14391447
protected:
14401448
AudioDriverES8311Class dac;

src/DriverPins.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ struct PinsSPI {
136136
}
137137
return true;
138138
}
139-
void end() { p_spi->end(); }
139+
void end() {
140+
AD_LOGD("PinsSPI::end");
141+
p_spi->end();
142+
}
140143
};
141144

142145
/**
@@ -175,6 +178,7 @@ struct PinsI2C {
175178
operator bool() { return pinsAvailable(); }
176179

177180
bool begin() {
181+
AD_LOGD("PinsI2C::begin: %d", port);
178182
if (set_active) {
179183
AD_LOGD("PinsI2C::begin for %d", function);
180184
// if no pins are defined, just call begin
@@ -239,6 +243,10 @@ struct PinsFunction {
239243
*/
240244
class DriverPins {
241245
public:
246+
DriverPins (const DriverPins&) = delete;
247+
DriverPins& operator= (const DriverPins&) = delete;
248+
DriverPins() = default;
249+
242250
bool addI2S(PinsI2S pin) {
243251
if (getI2SPins(pin.function)) return false;
244252
i2s.push_back(pin);
@@ -356,14 +364,11 @@ class DriverPins {
356364
AD_LOGD("DriverPins::begin");
357365

358366
// setup function pins
359-
AD_LOGD("DriverPins::begin::setupPinMode");
360367
setupPinMode();
361368

362369
// setup spi
363-
AD_LOGD("DriverPins::begin::SPI");
364370
bool result = true;
365371
for (auto &tmp : spi) {
366-
AD_LOGD("DriverPins::begin::SPI::begin");
367372
if (tmp.function == PinFunction::SD) {
368373
if (sd_active)
369374
result &= tmp.begin();
@@ -373,9 +378,7 @@ class DriverPins {
373378
}
374379

375380
// setup i2c
376-
AD_LOGD("DriverPins::begin::I2C");
377381
for (auto &tmp : i2c) {
378-
AD_LOGD("DriverPins::begin::I2C port:%d", tmp.port);
379382
result &= tmp.begin();
380383
}
381384
return result;
@@ -384,7 +387,6 @@ class DriverPins {
384387
void end() {
385388
// setup spi
386389
for (auto &tmp : spi) {
387-
AD_LOGD("DriverPins::begin::SPI::end");
388390
tmp.end();
389391
}
390392
// setup i2c
@@ -427,6 +429,7 @@ class DriverPins {
427429
}
428430

429431
void setupPinMode() {
432+
AD_LOGD("DriverPins::setupPinMode");
430433
// setup pins
431434
for (auto &tmp : pins) {
432435
if (tmp.pin != -1) {
@@ -550,18 +553,17 @@ class PinsLyrat42Class : public DriverPins {
550553
*/
551554
class PinsLyratMiniClass : public DriverPins {
552555
public:
556+
553557
PinsLyratMiniClass() {
554558
// sd pins: CLK, MISO, MOSI, CS
555-
//addSPI(PinFunction::SD, 14, 2, 15, 13, SPI);
556-
//addSPI(PinFunction::SD, 18, 19, 23, 5, SPI);
557559
addSPI(ESP32PinsSD);
558560
// add i2c codec pins: scl, sda, port, frequency
559561
addI2C(PinFunction::CODEC, 23, 18);
560562
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
561563
addI2S(PinFunction::CODEC, 0, 5, 25, 26, 35, 0);
562564
addI2S(PinFunction::CODEC_ADC, 0, 32, 33, -1, 36, 1);
563565

564-
addPin(PinFunction::HEADPHONE_DETECT, 19, PinLogic::InputActiveLow);
566+
addPin(PinFunction::HEADPHONE_DETECT, 19, PinLogic::InputActiveHigh);
565567
addPin(PinFunction::PA, 21, PinLogic::Output);
566568
addPin(PinFunction::LED, 22, PinLogic::Output, 1);
567569
addPin(PinFunction::LED, 27, PinLogic::Output, 2);

0 commit comments

Comments
 (0)