diff --git a/src/AudioBoard.h b/src/AudioBoard.h index 0500645..414c382 100644 --- a/src/AudioBoard.h +++ b/src/AudioBoard.h @@ -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 @@ -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); } @@ -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 @@ -83,6 +88,7 @@ class AudioBoard { AudioDriver* driver = nullptr; int power_amp_line = ES8388_PA_LINE; int volume = -1; + bool is_active = false; }; // -- Boards diff --git a/src/Driver.h b/src/Driver.h index 1bc79ef..a8bb044 100644 --- a/src/Driver.h +++ b/src/Driver.h @@ -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) { @@ -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() { @@ -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; } diff --git a/src/DriverPins.h b/src/DriverPins.h index a6af50d..ddc9e1e 100644 --- a/src/DriverPins.h +++ b/src/DriverPins.h @@ -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}; /** @@ -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(); } @@ -549,7 +551,9 @@ 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); @@ -557,18 +561,11 @@ class PinsLyratMiniClass : public DriverPins { 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); } };