ESPHome 2025.5.0
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 ESP_LOGCONFIG(TAG, "Setting up ES8156...");
21
22 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG02_SCLK_MODE, 0x04));
23 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG20_ANALOG_SYS1, 0x2A));
24 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG21_ANALOG_SYS2, 0x3C));
25 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG22_ANALOG_SYS3, 0x00));
26 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG24_ANALOG_LP, 0x07));
27 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG23_ANALOG_SYS4, 0x00));
28
29 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG0A_TIME_CONTROL1, 0x01));
30 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG0B_TIME_CONTROL2, 0x01));
31 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG11_DAC_SDP, 0x00));
32 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG19_EQ_CONTROL1, 0x20));
33
34 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG0D_P2S_CONTROL, 0x14));
35 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG09_MISC_CONTROL2, 0x00));
36 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG18_MISC_CONTROL3, 0x00));
37 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG08_CLOCK_ON_OFF, 0x3F));
38 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG00_RESET, 0x02));
39 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG00_RESET, 0x03));
40 ES8156_ERROR_FAILED(this->write_byte(ES8156_REG25_ANALOG_SYS5, 0x20));
41}
42
44 ESP_LOGCONFIG(TAG, "ES8156 Audio Codec:");
45
46 if (this->is_failed()) {
47 ESP_LOGCONFIG(TAG, " Failed to initialize");
48 return;
49 }
50}
51
52bool ES8156::set_volume(float volume) {
53 volume = clamp(volume, 0.0f, 1.0f);
54 uint8_t reg = remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 255);
55 ESP_LOGV(TAG, "Setting ES8156_REG14_VOLUME_CONTROL to %u (volume: %f)", reg, volume);
56 return this->write_byte(ES8156_REG14_VOLUME_CONTROL, reg);
57}
58
60 uint8_t reg;
61 this->read_byte(ES8156_REG14_VOLUME_CONTROL, &reg);
62 return remap<float, uint8_t>(reg, 0, 255, 0.0f, 1.0f);
63}
64
65bool ES8156::set_mute_state_(bool mute_state) {
66 uint8_t reg13;
67
68 this->is_muted_ = mute_state;
69
70 if (!this->read_byte(ES8156_REG13_DAC_MUTE, &reg13)) {
71 return false;
72 }
73
74 ESP_LOGV(TAG, "Read ES8156_REG13_DAC_MUTE: %u", reg13);
75
76 if (mute_state) {
77 reg13 |= BIT(1) | BIT(2);
78 } else {
79 reg13 &= ~(BIT(1) | BIT(2));
80 }
81
82 ESP_LOGV(TAG, "Setting ES8156_REG13_DAC_MUTE to %u (muted: %s)", reg13, YESNO(mute_state));
83 return this->write_byte(ES8156_REG13_DAC_MUTE, reg13);
84}
85
86} // namespace es8156
87} // namespace esphome
bool is_failed() const
float volume() override
Gets the current volume out from the DAC.
Definition es8156.cpp:59
void dump_config() override
Definition es8156.cpp:43
void setup() override
Definition es8156.cpp:19
bool set_mute_state_(bool mute_state)
Mutes or unmutes the DAC audio out.
Definition es8156.cpp:65
bool set_volume(float volume) override
Writes the volume out to the DAC.
Definition es8156.cpp:52
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:162
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101