Skip to content

Commit

Permalink
Cleanup logging and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 1, 2024
1 parent 8e89a4e commit 5f32c7b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
24 changes: 15 additions & 9 deletions src/AudioBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ class AudioBoard {
}

bool begin(){
AD_LOGD("AudioBoard::begin");
pins->setSPIActiveForSD(codec_cfg.sd_active);
AD_LOGD("AudioBoard::pins::begin");
bool result_pins = pins->begin();
AD_LOGD("AudioBoard::pins::begin::returned:%s", result_pins ? "true" : "false");
AD_LOGD("AudioBoard::driver::begin");
bool result_driver = driver->begin(codec_cfg, *pins);
AD_LOGD("AudioBoard::driver::begin::returned:%s", result_driver ? "true" : "false");
if (!pins->begin()){
AD_LOGE("AudioBoard::pins::begin failed");
return false;
}
if (!driver->begin(codec_cfg, *pins)){
AD_LOGE("AudioBoard::driver::begin failed");
return false;
}
setVolume(DRIVER_DEFAULT_VOLUME);
AD_LOGD("AudioBoard::volume::set");
return result_pins && result_driver;
is_active = true;
return true;
}

/// Starts the processing
Expand All @@ -49,6 +52,7 @@ class AudioBoard {

bool end(void) {
pins->end();
is_active = false;
return driver->end();
}
bool setMute(bool enable) { return driver->setMute(enable); }
Expand All @@ -57,10 +61,11 @@ class AudioBoard {
return driver->setMute(enable, line);
}
bool setVolume(int volume) {
AD_LOGD("setVolume: %d", volume);
// when we get the volume we make sure that we report the same value
// w/o rounding issues
this->volume = volume;
return driver->setVolume(volume);
return (is_active) ? driver->setVolume(volume) : false;
}
int getVolume() {
#if DRIVER_REPORT_DRIVER_VOLUME
Expand All @@ -83,6 +88,7 @@ class AudioBoard {
AudioDriver* driver = nullptr;
int power_amp_line = ES8388_PA_LINE;
int volume = -1;
bool is_active = false;
};

// -- Boards
Expand Down
21 changes: 10 additions & 11 deletions src/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,17 @@ class AudioDriver {
public:
/// Starts the processing
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
AD_LOGD("AudioDriver::begin:pins");
AD_LOGD("AudioDriver::begin");
p_pins = &pins;
AD_LOGD("AudioDriver::begin:setSPI");
pins.setSPIActiveForSD(codecCfg.sd_active);
AD_LOGD("AudioDriver::begin:setConfig");
int result = setConfig(codecCfg);
AD_LOGD("AudioDriver::begin:setPAPower");
if (!setConfig(codecCfg)){
AD_LOGE("setConfig has failed");
return false;
}
setPAPower(true);
AD_LOGD("AudioDriver::begin:completed");
// setup default volume
setVolume(DRIVER_DEFAULT_VOLUME);
return result;
return true;
}
/// changes the configuration
virtual bool setConfig(CodecConfig codecCfg) {
Expand Down Expand Up @@ -278,7 +277,8 @@ class AudioDriver {
if (!i2c) {
return &Wire;
}
return i2c.value().p_wire;
TwoWire* result = i2c.value().p_wire;
return result != nullptr ? result : &Wire;
}

int getI2CAddress() {
Expand Down Expand Up @@ -760,15 +760,14 @@ class AudioDriverES8311Class : public AudioDriver {
bool init(codec_config_t codec_cfg) {
int mclk_src = pins().getPinID(PinFunction::MCLK_SOURCE);
if (mclk_src == -1) {
AD_LOGI(
"Pin for PinFunction::MCLK_SOURCE not defined: we assume "
"FROM_MCLK_PIN");
mclk_src = 0; // = FROM_MCLK_PIN;
}
AD_LOGI("MCLK_SOURCE: %d", mclk_src);

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

assert(getI2C()!=nullptr);
return es8311_codec_init(&codec_cfg, getI2C(), mclk_src, i2c_address) ==
RESULT_OK;
}
Expand Down
17 changes: 7 additions & 10 deletions src/DriverPins.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ struct PinsSPI {

/**
* @brief Default SPI pins for ESP32 Lyrat, AudioDriver etc
* CLK, MISO, MOSI, CS
*/

static PinsSPI ESP32PinsSD{PinFunction::SD, 14, 2, 15, 13, SPI};

/**
Expand All @@ -168,7 +170,7 @@ struct PinsI2C {
GpioPin scl = -1;
GpioPin sda = -1;
bool set_active = true;
TwoWire *p_wire;
TwoWire *p_wire = &Wire;
bool pinsAvailable() { return scl != -1 && sda != -1 && frequency != 0; }
operator bool() { return pinsAvailable(); }

Expand Down Expand Up @@ -549,26 +551,21 @@ class PinsLyrat42Class : public DriverPins {
class PinsLyratMiniClass : public DriverPins {
public:
PinsLyratMiniClass() {
// sd pins
// sd pins: CLK, MISO, MOSI, CS
//addSPI(PinFunction::SD, 14, 2, 15, 13, SPI);
//addSPI(PinFunction::SD, 18, 19, 23, 5, SPI);
addSPI(ESP32PinsSD);
// add i2c codec pins: scl, sda, port, frequency
addI2C(PinFunction::CODEC, 23, 18);
// add i2s pins: mclk, bck, ws,data_out, data_in ,(port)
addI2S(PinFunction::CODEC, 0, 5, 25, 26, 35, 0);
addI2S(PinFunction::CODEC_ADC, 0, 32, 33, -1, 36, 1);

// add other pins
addPin(PinFunction::KEY, 5, PinLogic::InputActiveLow, 1);
addPin(PinFunction::KEY, 4, PinLogic::InputActiveLow, 2);
addPin(PinFunction::KEY, 2, PinLogic::InputActiveLow, 3);
addPin(PinFunction::KEY, 3, PinLogic::InputActiveLow, 4);
addPin(PinFunction::KEY, 1, PinLogic::InputActiveLow, 5);
// addPin(PinFunction::KEY, 0, 6};
addPin(PinFunction::HEADPHONE_DETECT, 19, PinLogic::InputActiveLow);
addPin(PinFunction::PA, 21, PinLogic::Output);
addPin(PinFunction::LED, 22, PinLogic::Output, 1);
addPin(PinFunction::LED, 27, PinLogic::Output, 2);
addPin(PinFunction::MCLK_SOURCE, 0, PinLogic::Output);
addPin(PinFunction::MCLK_SOURCE, 0, PinLogic::Inactive);
}
};

Expand Down

0 comments on commit 5f32c7b

Please sign in to comment.