ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
es9038q2m_katana.cpp
Go to the documentation of this file.
1#include "es9038q2m_katana.h"
2
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
6
7#include <cmath>
8
10
11ESPHOME_LOG_TAG(TAG, "es9038q2m_katana");
12
13namespace {
14
15constexpr uint8_t KATANA_CHIP_ID = 0x30;
16
17constexpr uint8_t KATANA_REG_CHIP_ID = 0x00;
18constexpr uint8_t KATANA_REG_RESET = 0x01;
19constexpr uint8_t KATANA_REG_VOLUME_1 = 0x02;
20constexpr uint8_t KATANA_REG_VOLUME_2 = 0x03;
21constexpr uint8_t KATANA_REG_MUTE = 0x04;
22constexpr uint8_t KATANA_REG_DSP_PROGRAM = 0x05;
23constexpr uint8_t KATANA_REG_DEEMPHASIS = 0x06;
24constexpr uint8_t KATANA_REG_DOP = 0x07;
25constexpr uint8_t KATANA_REG_FORMAT = 0x08;
26constexpr uint8_t KATANA_REG_COMMAND = 0x09;
27constexpr uint8_t KATANA_REG_MUTE_STREAM = 0x0A;
28
29constexpr float KATANA_VOLUME_MIDPOINT = 0.5f;
30constexpr float KATANA_VOLUME_MIDPOINT_STEPS = 50.0f; // -25 dB at 50% volume.
31// Cubic amplitude gain: -20 * log10(volume^3) / 0.5 attenuation steps.
32constexpr float KATANA_VOLUME_CUBIC_SCALE = 120.0f;
33constexpr uint8_t KATANA_VOLUME_FULL_ATTENUATION = 255;
34
35// The control logic ignores writes for this long after a reset.
36constexpr uint32_t RESET_SETTLE_DELAY_MS = 10;
37
38// Indexed by FilterShape
39PROGMEM_STRING_TABLE(FilterShapeStrings, "linear_phase_fast", "linear_phase_slow", "minimum_phase_fast",
40 "minimum_phase_slow", "apodizing", "brick_wall", "hybrid", "brick_wall");
41
42// Indexed by DeemphasisMode
43PROGMEM_STRING_TABLE(DeemphasisStrings, "bypass", "32khz", "44_1khz", "48khz");
44
45} // namespace
46
48 if (!this->check_chip_id(this, KATANA_REG_CHIP_ID, KATANA_CHIP_ID))
49 return;
50 if (!this->setup_check(this->write_byte(KATANA_REG_RESET, 0x01)))
51 return;
52 delay(RESET_SETTLE_DELAY_MS);
53
54 // MUTE_STREAM is the gate the Linux driver closes between streams; ESPHome has no such
55 // notion, so it stays open and mute is only ever driven through MUTE.
56 if (!this->write_byte(KATANA_REG_FORMAT, this->format_) || !this->write_byte(KATANA_REG_COMMAND, 0x00) ||
57 !this->write_byte(KATANA_REG_DSP_PROGRAM, this->filter_shape_) ||
58 !this->write_byte(KATANA_REG_DEEMPHASIS, this->deemphasis_mode_) ||
59 !this->write_byte(KATANA_REG_DOP, this->dop_enabled_ ? 1 : 0) || !this->write_volume_() ||
60 !this->write_byte(KATANA_REG_MUTE, this->is_muted_ ? 1 : 0) || !this->write_byte(KATANA_REG_MUTE_STREAM, 0x00)) {
61 this->mark_failed(LOG_STR("Failed to configure the control logic"));
62 }
63}
64
66 ESP_LOGCONFIG(TAG,
67 "ES9038Q2M Katana:\n"
68 " Filter shape: %s\n"
69 " De-emphasis: %s\n"
70 " Format register: 0x%02X\n"
71 " DoP: %s",
72 LOG_STR_ARG(FilterShapeStrings::get_log_str(this->filter_shape_)),
73 LOG_STR_ARG(DeemphasisStrings::get_log_str(this->deemphasis_mode_, DEEMPHASIS_BYPASS)), this->format_,
74 ONOFF(this->dop_enabled_));
75 LOG_I2C_DEVICE(this);
76 if (this->is_failed())
77 return;
78#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
79 for (uint8_t reg = KATANA_REG_CHIP_ID; reg <= KATANA_REG_MUTE_STREAM; reg++) {
80 uint8_t value = 0;
81 if (this->read_byte(reg, &value)) {
82 ESP_LOGV(TAG, " Register 0x%02X: 0x%02X", reg, value);
83 } else {
84 ESP_LOGV(TAG, " Register 0x%02X: read failed", reg);
85 }
86 }
87#endif
88}
89
90// Both channel registers follow ESPHome's single master volume.
92 return this->write_byte(KATANA_REG_VOLUME_1, this->volume_reg_) &&
93 this->write_byte(KATANA_REG_VOLUME_2, this->volume_reg_);
94}
95
96// Records the outcome of a runtime write; the cached state only changes on success.
97bool ES9038Q2MKatana::check_write_(bool ok) { return this->status_comm_result(ok); }
98
99bool ES9038Q2MKatana::set_volume(float volume) {
100 if (this->is_failed())
101 return false;
102 volume = clamp(volume, 0.0f, 1.0f);
103 const uint8_t previous = this->volume_reg_;
104 float attenuation = KATANA_VOLUME_FULL_ATTENUATION;
105 if (volume >= KATANA_VOLUME_MIDPOINT) {
106 attenuation = KATANA_VOLUME_MIDPOINT_STEPS * (1.0f - volume) / (1.0f - KATANA_VOLUME_MIDPOINT);
107 } else if (volume > 0.0f) {
108 attenuation = KATANA_VOLUME_MIDPOINT_STEPS - KATANA_VOLUME_CUBIC_SCALE * log10f(volume / KATANA_VOLUME_MIDPOINT);
109 }
110 this->volume_reg_ =
111 static_cast<uint8_t>(lroundf(clamp(attenuation, 0.0f, static_cast<float>(KATANA_VOLUME_FULL_ATTENUATION))));
112 if (this->check_write_(this->write_volume_()))
113 return true;
114 this->volume_reg_ = previous;
115 return false;
116}
117
119 if (this->volume_reg_ == KATANA_VOLUME_FULL_ATTENUATION)
120 return 0.0f;
121 if (this->volume_reg_ <= KATANA_VOLUME_MIDPOINT_STEPS)
122 return 1.0f - this->volume_reg_ * (1.0f - KATANA_VOLUME_MIDPOINT) / KATANA_VOLUME_MIDPOINT_STEPS;
123 return KATANA_VOLUME_MIDPOINT *
124 powf(10.0f, (KATANA_VOLUME_MIDPOINT_STEPS - this->volume_reg_) / KATANA_VOLUME_CUBIC_SCALE);
125}
126
127bool ES9038Q2MKatana::set_mute_state_(bool mute_state) {
128 if (this->is_failed())
129 return false;
130 if (!this->check_write_(this->write_byte(KATANA_REG_MUTE, mute_state ? 1 : 0)))
131 return false;
132 this->is_muted_ = mute_state;
133 return true;
134}
135
136} // namespace esphome::es9038q2m_katana
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:288
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
ESPHOME_LOG_TAG(TAG, "es9038q2m_katana")
PROGMEM_STRING_TABLE(CrashReasonStrings, "Unknown", "Undefined instruction", "Prefetch abort", "Data abort", "Hard fault", "MemManage fault", "Bus fault", "Usage fault", "Secure fault", "Abort")
uint32_t value
Definition hal.cpp:59
void HOT delay(uint32_t ms)
Definition hal.cpp:78
static void uint32_t