ESPHome 2026.11.0b6
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"
5
6#include <cmath>
7
9
10ESPHOME_LOG_TAG(TAG, "es9038q2m_katana");
11
12namespace {
13
14constexpr uint8_t KATANA_CHIP_ID = 0x30;
15
16constexpr uint8_t KATANA_REG_CHIP_ID = 0x00;
17constexpr uint8_t KATANA_REG_RESET = 0x01;
18constexpr uint8_t KATANA_REG_VOLUME_1 = 0x02;
19constexpr uint8_t KATANA_REG_VOLUME_2 = 0x03;
20constexpr uint8_t KATANA_REG_MUTE = 0x04;
21constexpr uint8_t KATANA_REG_DSP_PROGRAM = 0x05;
22constexpr uint8_t KATANA_REG_DEEMPHASIS = 0x06;
23constexpr uint8_t KATANA_REG_DOP = 0x07;
24constexpr uint8_t KATANA_REG_FORMAT = 0x08;
25constexpr uint8_t KATANA_REG_COMMAND = 0x09;
26constexpr uint8_t KATANA_REG_MUTE_STREAM = 0x0A;
27
28constexpr float KATANA_VOLUME_MIDPOINT = 0.5f;
29constexpr float KATANA_VOLUME_MIDPOINT_STEPS = 50.0f; // -25 dB at 50% volume.
30// Cubic amplitude gain: -20 * log10(volume^3) / 0.5 attenuation steps.
31constexpr float KATANA_VOLUME_CUBIC_SCALE = 120.0f;
32constexpr uint8_t KATANA_VOLUME_FULL_ATTENUATION = 255;
33
34// The control logic ignores writes for this long after a reset.
35constexpr uint32_t RESET_SETTLE_DELAY_MS = 10;
36
37const LogString *filter_shape_to_str(FilterShape shape) {
38 switch (shape) {
40 return LOG_STR("linear_phase_fast");
42 return LOG_STR("linear_phase_slow");
44 return LOG_STR("minimum_phase_fast");
46 return LOG_STR("minimum_phase_slow");
48 return LOG_STR("apodizing");
50 return LOG_STR("hybrid");
51 default:
52 return LOG_STR("brick_wall");
53 }
54}
55
56const LogString *deemphasis_to_str(DeemphasisMode mode) {
57 switch (mode) {
59 return LOG_STR("32khz");
61 return LOG_STR("44_1khz");
63 return LOG_STR("48khz");
64 default:
65 return LOG_STR("bypass");
66 }
67}
68
69} // namespace
70
72 if (!this->check_chip_id(this, KATANA_REG_CHIP_ID, KATANA_CHIP_ID))
73 return;
74 if (!this->write_byte(KATANA_REG_RESET, 0x01)) {
75 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
76 return;
77 }
78 delay(RESET_SETTLE_DELAY_MS);
79
80 // MUTE_STREAM is the gate the Linux driver closes between streams; ESPHome has no such
81 // notion, so it stays open and mute is only ever driven through MUTE.
82 if (!this->write_byte(KATANA_REG_FORMAT, this->format_) || !this->write_byte(KATANA_REG_COMMAND, 0x00) ||
83 !this->write_byte(KATANA_REG_DSP_PROGRAM, this->filter_shape_) ||
84 !this->write_byte(KATANA_REG_DEEMPHASIS, this->deemphasis_mode_) ||
85 !this->write_byte(KATANA_REG_DOP, this->dop_enabled_ ? 1 : 0) || !this->write_volume_() ||
86 !this->write_byte(KATANA_REG_MUTE, this->is_muted_ ? 1 : 0) || !this->write_byte(KATANA_REG_MUTE_STREAM, 0x00)) {
87 this->mark_failed(LOG_STR("Failed to configure the control logic"));
88 }
89}
90
92 ESP_LOGCONFIG(TAG,
93 "ES9038Q2M Katana:\n"
94 " Filter shape: %s\n"
95 " De-emphasis: %s\n"
96 " Format register: 0x%02X\n"
97 " DoP: %s",
98 LOG_STR_ARG(filter_shape_to_str(this->filter_shape_)),
99 LOG_STR_ARG(deemphasis_to_str(this->deemphasis_mode_)), this->format_, ONOFF(this->dop_enabled_));
100 LOG_I2C_DEVICE(this);
101 if (this->is_failed())
102 return;
103#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
104 for (uint8_t reg = KATANA_REG_CHIP_ID; reg <= KATANA_REG_MUTE_STREAM; reg++) {
105 uint8_t value = 0;
106 if (this->read_byte(reg, &value)) {
107 ESP_LOGV(TAG, " Register 0x%02X: 0x%02X", reg, value);
108 } else {
109 ESP_LOGV(TAG, " Register 0x%02X: read failed", reg);
110 }
111 }
112#endif
113}
114
115// Both channel registers follow ESPHome's single master volume.
117 return this->write_byte(KATANA_REG_VOLUME_1, this->volume_reg_) &&
118 this->write_byte(KATANA_REG_VOLUME_2, this->volume_reg_);
119}
120
121// Records the outcome of a runtime write; the cached state only changes on success.
122bool ES9038Q2MKatana::check_write_(bool ok) { return this->status_comm_result(ok); }
123
124bool ES9038Q2MKatana::set_volume(float volume) {
125 if (this->is_failed())
126 return false;
127 volume = clamp(volume, 0.0f, 1.0f);
128 const uint8_t previous = this->volume_reg_;
129 float attenuation = KATANA_VOLUME_FULL_ATTENUATION;
130 if (volume >= KATANA_VOLUME_MIDPOINT) {
131 attenuation = KATANA_VOLUME_MIDPOINT_STEPS * (1.0f - volume) / (1.0f - KATANA_VOLUME_MIDPOINT);
132 } else if (volume > 0.0f) {
133 attenuation = KATANA_VOLUME_MIDPOINT_STEPS - KATANA_VOLUME_CUBIC_SCALE * log10f(volume / KATANA_VOLUME_MIDPOINT);
134 }
135 this->volume_reg_ =
136 static_cast<uint8_t>(lroundf(clamp(attenuation, 0.0f, static_cast<float>(KATANA_VOLUME_FULL_ATTENUATION))));
137 if (this->check_write_(this->write_volume_()))
138 return true;
139 this->volume_reg_ = previous;
140 return false;
141}
142
144 if (this->volume_reg_ == KATANA_VOLUME_FULL_ATTENUATION)
145 return 0.0f;
146 if (this->volume_reg_ <= KATANA_VOLUME_MIDPOINT_STEPS)
147 return 1.0f - this->volume_reg_ * (1.0f - KATANA_VOLUME_MIDPOINT) / KATANA_VOLUME_MIDPOINT_STEPS;
148 return KATANA_VOLUME_MIDPOINT *
149 powf(10.0f, (KATANA_VOLUME_MIDPOINT_STEPS - this->volume_reg_) / KATANA_VOLUME_CUBIC_SCALE);
150}
151
152bool ES9038Q2MKatana::set_mute_state_(bool mute_state) {
153 if (this->is_failed())
154 return false;
155 if (!this->check_write_(this->write_byte(KATANA_REG_MUTE, mute_state ? 1 : 0)))
156 return false;
157 this->is_muted_ = mute_state;
158 return true;
159}
160
161} // namespace esphome::es9038q2m_katana
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
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")
uint32_t value
Definition hal.cpp:41
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t