18 ESP_LOGE(TAG,
"Write failed");
25 this->
reg(PCM5122_REG_RESET) = PCM5122_RESET_MODULES;
27 this->
reg(PCM5122_REG_RESET) = 0x00;
30 optional<uint8_t> err_detect = this->
read_byte(PCM5122_REG_ERROR_DETECT);
32 if (!err_detect.has_value()) {
33 this->
mark_failed(LOG_STR(
"Failed to read ERROR_DETECT"));
36 uint8_t err_detect_val = err_detect.value();
37 err_detect_val |= PCM5122_ERROR_DETECT_IGNORE_CLKHALT;
38 err_detect_val &= ~PCM5122_ERROR_DETECT_DISABLE_DIV_AUTOSET;
39 this->
reg(PCM5122_REG_ERROR_DETECT) = err_detect_val;
45 alen = PCM5122_AUDIO_FORMAT_ALEN_16BIT;
48 alen = PCM5122_AUDIO_FORMAT_ALEN_24BIT;
52 alen = PCM5122_AUDIO_FORMAT_ALEN_32BIT;
55 this->
reg(PCM5122_REG_AUDIO_FORMAT) = PCM5122_AUDIO_FORMAT_I2S | alen;
66 optional<uint8_t> pll_ref = this->
read_byte(PCM5122_REG_PLL_REF);
68 if (!pll_ref.has_value()) {
69 this->
mark_failed(LOG_STR(
"Failed to read PLL_REF"));
72 uint8_t pll_ref_val = pll_ref.value();
73 pll_ref_val &= ~PCM5122_PLL_REF_MASK;
74 pll_ref_val |= PCM5122_PLL_REF_SOURCE_BCK;
75 this->
reg(PCM5122_REG_PLL_REF) = pll_ref_val;
89 const char *channel_mix_str;
92 channel_mix_str =
"left only";
95 channel_mix_str =
"right only";
98 channel_mix_str =
"swapped";
101 channel_mix_str =
"stereo";
104 ESP_LOGCONFIG(TAG,
"Audio DAC:");
105 LOG_I2C_DEVICE(
this);
107 " Bits per sample: %u\n"
110 " Volume range: %.1f dB to %.1f dB\n"
140 if (!this->
write_byte(PCM5122_REG_PAGE_SELECT, page)) {
149 uint8_t mute_byte = this->
is_muted() ? 0x11 : 0x00;
151 ESP_LOGE(TAG,
"Writing mute failed");
161 const uint8_t dvol_max_volume =
static_cast<uint8_t
>(lroundf(0x30 - this->
volume_max_db_ * 2.0f));
162 const uint8_t dvol_min_volume =
static_cast<uint8_t
>(lroundf(0x30 - this->
volume_min_db_ * 2.0f));
164 const uint8_t volume_byte =
165 dvol_max_volume +
static_cast<uint8_t
>(lroundf((1.0f - this->
volume_) * (dvol_min_volume - dvol_max_volume)));
167 ESP_LOGV(TAG,
"Setting volume to 0x%.2x", volume_byte);
170 !this->
write_byte(PCM5122_REG_DVOL_RIGHT, volume_byte)) {
171 ESP_LOGE(TAG,
"Writing volume failed");
180 ESP_LOGE(TAG,
"Writing analog gain failed");
189 ESP_LOGE(TAG,
"Writing channel mix failed");
217 (this->
standby_ ? PCM5122_POWER_CONTROL_RQST : 0) | (this->
powerdown_ ? PCM5122_POWER_CONTROL_RQPD : 0);
219 ESP_LOGE(TAG,
"Writing power control failed");
void mark_failed()
Mark this component as failed.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
virtual void digital_write(bool value)=0
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
bool set_standby(bool enable)
bool write_channel_mix_()
bool set_volume(float volume) override
bool set_mute_on() override
bool set_mute_off() override
void dump_config() override
bool write_analog_gain_()
bool select_page_(uint8_t page)
bool set_powerdown(bool enable)
bool write_power_control_()
PCM5122AnalogGain analog_gain_
PCM5122BitsPerSample bits_per_sample_
PCM5122ChannelMix channel_mix_
ESPHOME_LOG_TAG(TAG, "pcm5122")
@ PCM5122_ANALOG_GAIN_0DB
@ PCM5122_CHANNEL_MIX_SWAPPED
@ PCM5122_CHANNEL_MIX_LEFT_ONLY
@ PCM5122_CHANNEL_MIX_RIGHT_ONLY
@ PCM5122_BITS_PER_SAMPLE_16
@ PCM5122_BITS_PER_SAMPLE_32
@ PCM5122_BITS_PER_SAMPLE_24
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
void HOT delay(uint32_t ms)