13#define ES8388_ERROR_FAILED(func) \
15 this->mark_failed(LOG_STR("Failed to initialize")); \
20#define ES8388_ERROR_CHECK(func) \
27 {ES8388_MASTERMODE, 0x00},
29 {ES8388_CONTROL2, 0x50},
31 {ES8388_CHIPPOWER, 0x00},
34 {ES8388_CONTROL1, 0x12},
36 {ES8388_DACCONTROL1, 0x18},
39 {ES8388_DACCONTROL2, 0x02},
41 {ES8388_DACCONTROL16, 0x00},
43 {ES8388_DACCONTROL17, 0x90},
45 {ES8388_DACCONTROL20, 0x90},
47 {ES8388_DACCONTROL21, 0x80},
49 {ES8388_DACCONTROL23, 0x00},
51 {ES8388_ADCPOWER, 0xFF},
53 {ES8388_ADCCONTROL3, 0x02},
55 {ES8388_ADCCONTROL4, 0x0d},
57 {ES8388_ADCCONTROL5, 0x02},
59 {ES8388_ADCCONTROL8, 0x00},
60 {ES8388_ADCCONTROL9, 0x00},
64 {ES8388_ADCCONTROL10, 0xe2},
66 {ES8388_ADCCONTROL11, 0xa0},
68 {ES8388_ADCCONTROL12, 0x12},
70 {ES8388_ADCCONTROL13, 0x06},
72 {ES8388_ADCCONTROL14, 0xc3},
74 {ES8388_DACCONTROL21, 0x80},
76 {ES8388_CHIPPOWER, 0xF0},
79static constexpr i2c::I2CRegValue ES8388_UNMUTE[]
PROGMEM = {
81 {ES8388_ADCCONTROL7, 0x60},
83 {ES8388_DACCONTROL3, 0x20},
85 {ES8388_ADCPOWER, 0x09},
96 ES8388_ERROR_FAILED(this->
write_byte(ES8388_CHIPPOWER, 0x00));
106#ifdef USE_ES8388_SELECT
107 if (this->dac_output_select_ !=
nullptr) {
110 ES8388_ERROR_FAILED(this->set_dac_output(line));
111 this->dac_output_select_->publish_state(
static_cast<size_t>(line));
113 auto dac_power = this->get_dac_power();
114 if (dac_power.has_value()) {
115 if (this->dac_output_select_->has_index(dac_power.value())) {
116 this->dac_output_select_->publish_state(dac_power.value());
118 ESP_LOGW(TAG,
"Unknown DAC output power value: %d", dac_power.value());
123 if (this->adc_input_mic_select_ !=
nullptr) {
126 ES8388_ERROR_FAILED(this->set_adc_input_mic(line));
127 this->adc_input_mic_select_->publish_state(
static_cast<size_t>(line));
129 auto mic_input = this->get_mic_input();
130 if (mic_input.has_value()) {
131 if (this->adc_input_mic_select_->has_index(mic_input.value())) {
132 this->adc_input_mic_select_->publish_state(mic_input.value());
134 ESP_LOGW(TAG,
"Unknown ADC input mic value: %d", mic_input.value());
142void ES8388::dump_config() {
143 ESP_LOGCONFIG(TAG,
"ES8388 Audio Codec:");
144 LOG_I2C_DEVICE(
this);
145 ESP_LOGCONFIG(TAG,
" Mic gain: %u dB", (this->
mic_gain_ & 0x0F) * 3);
146#ifdef USE_ES8388_SELECT
147 LOG_SELECT(
" ",
"DAC Output", this->dac_output_select_);
148 LOG_SELECT(
" ",
"ADC Input Mic", this->adc_input_mic_select_);
152bool ES8388::set_volume(
float volume) {
153 volume = clamp(volume, 0.0f, 1.0f);
155 ESP_LOGD(TAG,
"Setting ES8388_DACCONTROL4 / ES8388_DACCONTROL5 to 0x%02X (volume: %f)",
value, volume);
162float ES8388::volume() {
172 return this->
update_register(ES8388_DACCONTROL3, ES8388_DACCONTROL3_DAC_MUTE,
173 mute_state ? ES8388_DACCONTROL3_DAC_MUTE : 0);
178 uint8_t reg_out1 = 0;
179 uint8_t reg_out2 = 0;
180 uint8_t dac_power = 0;
186 dac_power = ES8388_DAC_OUTPUT_LOUT1_ROUT1;
190 dac_power = ES8388_DAC_OUTPUT_LOUT2_ROUT2;
195 dac_power = ES8388_DAC_OUTPUT_BOTH;
198 ESP_LOGE(TAG,
"Unknown DAC output line: %d", line);
203 "DAC output config:\n"
204 " DACPOWER: 0x%02X\n"
205 " DACCONTROL24/25: 0x%02X\n"
206 " DACCONTROL26/27: 0x%02X",
207 dac_power, reg_out1, reg_out2);
209 ES8388_ERROR_CHECK(this->
write_byte(ES8388_DACCONTROL24, reg_out1));
210 ES8388_ERROR_CHECK(this->
write_byte(ES8388_DACCONTROL25, reg_out1));
211 ES8388_ERROR_CHECK(this->
write_byte(ES8388_DACCONTROL26, reg_out2));
212 ES8388_ERROR_CHECK(this->
write_byte(ES8388_DACCONTROL27, reg_out2));
214 return this->
write_byte(ES8388_DACPOWER, dac_power);
217optional<DacOutputLine> ES8388::get_dac_power() {
219 if (!this->
read_byte(ES8388_DACPOWER, &dac_power)) {
224 case ES8388_DAC_OUTPUT_LOUT1_ROUT1:
226 case ES8388_DAC_OUTPUT_LOUT2_ROUT2:
228 case ES8388_DAC_OUTPUT_BOTH:
237 uint8_t mic_input = 0;
241 mic_input = ES8388_ADC_INPUT_LINPUT1_RINPUT1;
244 mic_input = ES8388_ADC_INPUT_LINPUT2_RINPUT2;
247 mic_input = ES8388_ADC_INPUT_DIFFERENCE;
250 ESP_LOGE(TAG,
"Unknown ADC input mic line: %d", line);
254 ESP_LOGV(TAG,
"Setting ES8388_ADCCONTROL2 to 0x%02X", mic_input);
255 ES8388_ERROR_CHECK(this->
write_byte(ES8388_ADCCONTROL2, mic_input));
260optional<AdcInputMicLine> ES8388::get_mic_input() {
262 if (!this->
read_byte(ES8388_ADCCONTROL2, &mic_input)) {
267 case ES8388_ADC_INPUT_LINPUT1_RINPUT1:
269 case ES8388_ADC_INPUT_LINPUT2_RINPUT2:
271 case ES8388_ADC_INPUT_DIFFERENCE: