Skip to content

Commit 5f32c7b

Browse files
committed
Cleanup logging and error handling
1 parent 8e89a4e commit 5f32c7b

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

src/AudioBoard.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ class AudioBoard {
2323
}
2424

2525
bool begin(){
26+
AD_LOGD("AudioBoard::begin");
2627
pins->setSPIActiveForSD(codec_cfg.sd_active);
27-
AD_LOGD("AudioBoard::pins::begin");
28-
bool result_pins = pins->begin();
29-
AD_LOGD("AudioBoard::pins::begin::returned:%s", result_pins ? "true" : "false");
30-
AD_LOGD("AudioBoard::driver::begin");
31-
bool result_driver = driver->begin(codec_cfg, *pins);
32-
AD_LOGD("AudioBoard::driver::begin::returned:%s", result_driver ? "true" : "false");
28+
if (!pins->begin()){
29+
AD_LOGE("AudioBoard::pins::begin failed");
30+
return false;
31+
}
32+
if (!driver->begin(codec_cfg, *pins)){
33+
AD_LOGE("AudioBoard::driver::begin failed");
34+
return false;
35+
}
3336
setVolume(DRIVER_DEFAULT_VOLUME);
34-
AD_LOGD("AudioBoard::volume::set");
35-
return result_pins && result_driver;
37+
is_active = true;
38+
return true;
3639
}
3740

3841
/// Starts the processing
@@ -49,6 +52,7 @@ class AudioBoard {
4952

5053
bool end(void) {
5154
pins->end();
55+
is_active = false;
5256
return driver->end();
5357
}
5458
bool setMute(bool enable) { return driver->setMute(enable); }
@@ -57,10 +61,11 @@ class AudioBoard {
5761
return driver->setMute(enable, line);
5862
}
5963
bool setVolume(int volume) {
64+
AD_LOGD("setVolume: %d", volume);
6065
// when we get the volume we make sure that we report the same value
6166
// w/o rounding issues
6267
this->volume = volume;
63-
return driver->setVolume(volume);
68+
return (is_active) ? driver->setVolume(volume) : false;
6469
}
6570
int getVolume() {
6671
#if DRIVER_REPORT_DRIVER_VOLUME
@@ -83,6 +88,7 @@ class AudioBoard {
8388
AudioDriver* driver = nullptr;
8489
int power_amp_line = ES8388_PA_LINE;
8590
int volume = -1;
91+
bool is_active = false;
8692
};
8793

8894
// -- Boards

src/Driver.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,17 @@ class AudioDriver {
192192
public:
193193
/// Starts the processing
194194
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
195-
AD_LOGD("AudioDriver::begin:pins");
195+
AD_LOGD("AudioDriver::begin");
196196
p_pins = &pins;
197-
AD_LOGD("AudioDriver::begin:setSPI");
198197
pins.setSPIActiveForSD(codecCfg.sd_active);
199-
AD_LOGD("AudioDriver::begin:setConfig");
200-
int result = setConfig(codecCfg);
201-
AD_LOGD("AudioDriver::begin:setPAPower");
198+
if (!setConfig(codecCfg)){
199+
AD_LOGE("setConfig has failed");
200+
return false;
201+
}
202202
setPAPower(true);
203-
AD_LOGD("AudioDriver::begin:completed");
204203
// setup default volume
205204
setVolume(DRIVER_DEFAULT_VOLUME);
206-
return result;
205+
return true;
207206
}
208207
/// changes the configuration
209208
virtual bool setConfig(CodecConfig codecCfg) {
@@ -278,7 +277,8 @@ class AudioDriver {
278277
if (!i2c) {
279278
return &Wire;
280279
}
281-
return i2c.value().p_wire;
280+
TwoWire* result = i2c.value().p_wire;
281+
return result != nullptr ? result : &Wire;
282282
}
283283

284284
int getI2CAddress() {
@@ -760,15 +760,14 @@ class AudioDriverES8311Class : public AudioDriver {
760760
bool init(codec_config_t codec_cfg) {
761761
int mclk_src = pins().getPinID(PinFunction::MCLK_SOURCE);
762762
if (mclk_src == -1) {
763-
AD_LOGI(
764-
"Pin for PinFunction::MCLK_SOURCE not defined: we assume "
765-
"FROM_MCLK_PIN");
766763
mclk_src = 0; // = FROM_MCLK_PIN;
767764
}
765+
AD_LOGI("MCLK_SOURCE: %d", mclk_src);
768766

769767
// determine address from data
770768
if (i2c_address <= 0) i2c_address = getI2CAddress();
771769

770+
assert(getI2C()!=nullptr);
772771
return es8311_codec_init(&codec_cfg, getI2C(), mclk_src, i2c_address) ==
773772
RESULT_OK;
774773
}

src/DriverPins.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ struct PinsSPI {
141141

142142
/**
143143
* @brief Default SPI pins for ESP32 Lyrat, AudioDriver etc
144+
* CLK, MISO, MOSI, CS
144145
*/
146+
145147
static PinsSPI ESP32PinsSD{PinFunction::SD, 14, 2, 15, 13, SPI};
146148

147149
/**
@@ -168,7 +170,7 @@ struct PinsI2C {
168170
GpioPin scl = -1;
169171
GpioPin sda = -1;
170172
bool set_active = true;
171-
TwoWire *p_wire;
173+
TwoWire *p_wire = &Wire;
172174
bool pinsAvailable() { return scl != -1 && sda != -1 && frequency != 0; }
173175
operator bool() { return pinsAvailable(); }
174176

@@ -549,26 +551,21 @@ class PinsLyrat42Class : public DriverPins {
549551
class PinsLyratMiniClass : public DriverPins {
550552
public:
551553
PinsLyratMiniClass() {
552-
// sd pins
554+
// sd pins: CLK, MISO, MOSI, CS
555+
//addSPI(PinFunction::SD, 14, 2, 15, 13, SPI);
556+
//addSPI(PinFunction::SD, 18, 19, 23, 5, SPI);
553557
addSPI(ESP32PinsSD);
554558
// add i2c codec pins: scl, sda, port, frequency
555559
addI2C(PinFunction::CODEC, 23, 18);
556560
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
557561
addI2S(PinFunction::CODEC, 0, 5, 25, 26, 35, 0);
558562
addI2S(PinFunction::CODEC_ADC, 0, 32, 33, -1, 36, 1);
559563

560-
// add other pins
561-
addPin(PinFunction::KEY, 5, PinLogic::InputActiveLow, 1);
562-
addPin(PinFunction::KEY, 4, PinLogic::InputActiveLow, 2);
563-
addPin(PinFunction::KEY, 2, PinLogic::InputActiveLow, 3);
564-
addPin(PinFunction::KEY, 3, PinLogic::InputActiveLow, 4);
565-
addPin(PinFunction::KEY, 1, PinLogic::InputActiveLow, 5);
566-
// addPin(PinFunction::KEY, 0, 6};
567564
addPin(PinFunction::HEADPHONE_DETECT, 19, PinLogic::InputActiveLow);
568565
addPin(PinFunction::PA, 21, PinLogic::Output);
569566
addPin(PinFunction::LED, 22, PinLogic::Output, 1);
570567
addPin(PinFunction::LED, 27, PinLogic::Output, 2);
571-
addPin(PinFunction::MCLK_SOURCE, 0, PinLogic::Output);
568+
addPin(PinFunction::MCLK_SOURCE, 0, PinLogic::Inactive);
572569
}
573570
};
574571

0 commit comments

Comments
 (0)