ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
es8156.cpp
Go to the documentation of this file.
1#include "es8156.h"
2#include "es8156_const.h"
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5#include <cinttypes>
6
7namespace esphome {
8namespace es8156 {
9
10static const char *const TAG = "es8156";
11
12// Mark the component as failed; use only in setup
13#define ES8156_ERROR_FAILED(func) \
14 if (!(func)) { \
15 this->mark_failed(); \
16 return; \
17 }
18
20 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG02_SCLK_MODE, 0x04));
21 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG20_ANALOG_SYS1, 0x2A));
22 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG21_ANALOG_SYS2, 0x3C));
23 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG22_ANALOG_SYS3, 0x00));
24 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG24_ANALOG_LP, 0x07));
25 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG23_ANALOG_SYS4, 0x00));
26
27 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG0A_TIME_CONTROL1, 0x01));
28 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG0B_TIME_CONTROL2, 0x01));
29 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG11_DAC_SDP, 0x00));
30 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG19_EQ_CONTROL1, 0x20));
31
32 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG0D_P2S_CONTROL, 0x14));
33 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG09_MISC_CONTROL2, 0x00));
34 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG18_MISC_CONTROL3, 0x00));
35 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG08_CLOCK_ON_OFF, 0x3F));
36 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG00_RESET, 0x02));
37 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG00_RESET, 0x03));
38 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG25_ANALOG_SYS5, 0x20));
39}
40
42 ESP_LOGCONFIG(TAG, "ES8156 Audio Codec:");
43
44 if (this->is_failed()) {
45 ESP_LOGCONFIG(TAG, " Failed to initialize");
46 return;
47 }
48}
49
50bool ES8156::set_volume(float volume) {
51 volume = clamp(volume, 0.0f, 1.0f);
52 uint8_t reg = remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 255);
53 ESP_LOGV(TAG, "Setting ES8156_REG14_VOLUME_CONTROL to %u (volume: %f)", reg, volume);
54 return this->write_byte(ES8156_REG14_VOLUME_CONTROL, reg);
55}
56
58 uint8_t reg;
59 this->read_byte(ES8156_REG14_VOLUME_CONTROL, &reg);
60 return remap<float, uint8_t>(reg, 0, 255, 0.0f, 1.0f);
61}
62
63bool ES8156::set_mute_state_(bool mute_state) {
64 uint8_t reg13;
65
66 this->is_muted_ = mute_state;
67
68 if (!this->read_byte(ES8156_REG13_DAC_MUTE, &reg13)) {
69 return false;
70 }
71
72 ESP_LOGV(TAG, "Read ES8156_REG13_DAC_MUTE: %u", reg13);
73
74 if (mute_state) {
75 reg13 |= BIT(1) | BIT(2);
76 } else {
77 reg13 &= ~(BIT(1) | BIT(2));
78 }
79
80 ESP_LOGV(TAG, "Setting ES8156_REG13_DAC_MUTE to %u (muted: %s)", reg13, YESNO(mute_state));
81 return this->write_byte(ES8156_REG13_DAC_MUTE, reg13);
82}
83
84} // namespace es8156
85} // namespace esphome
bool is_failed() const
float volume() override
Gets the current volume out from the DAC.
Definition es8156.cpp:57
void dump_config() override
Definition es8156.cpp:41
void setup() override
Definition es8156.cpp:19
bool set_mute_state_(bool mute_state)
Mutes or unmutes the DAC audio out.
Definition es8156.cpp:63
bool set_volume(float volume) override
Writes the volume out to the DAC.
Definition es8156.cpp:50
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
Definition helpers.h:144