Skip to content

Commit

Permalink
make i2s channel configurable (a few more changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-sl committed Apr 7, 2024
1 parent d761df3 commit 7a56188
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ i2s:
dma_buf_count: 8 # default: 8
dma_buf_len: 256 # default: 256
use_apll: true # default: false
channel: right # default: left

# according to datasheet when L/R pin is connected to GND,
# the mic should output its signal in the left channel,
# however in my experience it's the opposite: when I connect
# L/R to GND then the signal is in the right channel
channel: right # default: right

# right shift samples.
# for example if mic has 24 bit resolution, and
Expand Down
2 changes: 1 addition & 1 deletion components/i2s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
cv.Optional(CONF_DMA_BUF_LEN, 256): cv.positive_not_null_int,
cv.Optional(CONF_USE_APLL, False): cv.boolean,
cv.Optional(CONF_BITS_SHIFT, 0): cv.int_range(0, 32),
cv.Optional(CONF_CHANNEL, default="left"): cv.enum(CHANNELS),
cv.Optional(CONF_CHANNEL, default="right"): cv.enum(CHANNELS),
}
).extend(cv.COMPONENT_SCHEMA),
cv.has_at_least_one_key(CONF_DIN_PIN, CONF_DOUT_PIN),
Expand Down
7 changes: 5 additions & 2 deletions components/i2s/i2s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ void I2SComponent::dump_config() {
ESP_LOGCONFIG(TAG, " DMA Buf Len: %u", this->dma_buf_len_);
ESP_LOGCONFIG(TAG, " Use APLL: %s", YESNO(this->use_apll_));
ESP_LOGCONFIG(TAG, " Bits Shift: %u", this->bits_shift_);
ESP_LOGCONFIG(TAG, " Channel: %02X", this->channel_);
ESP_LOGCONFIG(TAG, " Channel: %s",
this->channel_ == I2S_CHANNEL_FMT_ONLY_RIGHT ? "right"
: this->channel_ == I2S_CHANNEL_FMT_ONLY_LEFT ? "left"
: "invalid");
}

bool I2SComponent::read(uint8_t *data, size_t len, size_t *bytes_read, TickType_t ticks_to_wait) {
Expand Down Expand Up @@ -129,7 +132,7 @@ void I2SComponent::setup() {

ESP_LOGCONFIG(TAG, "Setting up I2S %u ...", this->port_num_);

i2s_config_t i2s_config = {.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // TODO: make it configurable
i2s_config_t i2s_config = {.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = this->sample_rate_,
.bits_per_sample = i2s_bits_per_sample_t(this->bits_per_sample_),
.channel_format = this->channel_,
Expand Down
2 changes: 1 addition & 1 deletion components/i2s/i2s.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class I2SComponent : public Component {
int dma_buf_len_{256};
bool use_apll_{false};
uint8_t bits_shift_{0};
i2s_channel_fmt_t channel_{0x00}; // left
i2s_channel_fmt_t channel_{I2S_CHANNEL_FMT_ONLY_RIGHT};
};
} // namespace i2s
} // namespace esphome
7 changes: 6 additions & 1 deletion configs/advanced-example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ i2s:
dma_buf_count: 8 # default: 8
dma_buf_len: 256 # default: 256
use_apll: true # default: false
channel: right # default: left

# according to datasheet when L/R pin is connected to GND,
# the mic should output its signal in the left channel,
# however in my experience it's the opposite: when I connect
# L/R to GND then the signal is in the right channel
channel: right # default: right

# right shift samples.
# for example if mic has 24 bit resolution, and
Expand Down
1 change: 1 addition & 0 deletions configs/sensor-community-example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ i2s:
dma_buf_len: 256
use_apll: true
bits_shift: 8
channel: right

sound_level_meter:
update_interval: 150s # to match original sensor.community firmware settings
Expand Down

0 comments on commit 7a56188

Please sign in to comment.