Allow microphone channel to be specified in config (#4871)

This commit is contained in:
Jesse Hills 2023-05-23 07:02:16 +12:00 committed by Keith Burzinski
parent 70aa5d0f6c
commit 148eb03d13
No known key found for this signature in database
GPG Key ID: 802564C5F0EEFFBE
3 changed files with 14 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import esphome.config_validation as cv
import esphome.codegen as cg
from esphome import pins
from esphome.const import CONF_ID, CONF_NUMBER
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_NUMBER
from esphome.components import microphone, esp32
from esphome.components.adc import ESP32_VARIANT_ADC1_PIN_TO_CHANNEL, validate_adc_pin
@ -25,6 +25,12 @@ I2SAudioMicrophone = i2s_audio_ns.class_(
"I2SAudioMicrophone", I2SAudioIn, microphone.Microphone, cg.Component
)
i2s_channel_fmt_t = cg.global_ns.enum("i2s_channel_fmt_t")
CHANNELS = {
"left": i2s_channel_fmt_t.I2S_CHANNEL_FMT_ONLY_LEFT,
"right": i2s_channel_fmt_t.I2S_CHANNEL_FMT_ONLY_RIGHT,
}
INTERNAL_ADC_VARIANTS = [esp32.const.VARIANT_ESP32]
PDM_VARIANTS = [esp32.const.VARIANT_ESP32, esp32.const.VARIANT_ESP32S3]
@ -47,6 +53,7 @@ BASE_SCHEMA = microphone.MICROPHONE_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(I2SAudioMicrophone),
cv.GenerateID(CONF_I2S_AUDIO_ID): cv.use_id(I2SAudioComponent),
cv.Optional(CONF_CHANNEL, default="right"): cv.enum(CHANNELS),
}
).extend(cv.COMPONENT_SCHEMA)
@ -86,4 +93,6 @@ async def to_code(config):
cg.add(var.set_din_pin(config[CONF_I2S_DIN_PIN]))
cg.add(var.set_pdm(config[CONF_PDM]))
cg.add(var.set_channel(CHANNELS[config[CONF_CHANNEL]]))
await microphone.register_microphone(var, config)

View File

@ -49,7 +49,7 @@ void I2SAudioMicrophone::start_() {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 16000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.channel_format = this->channel_,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,

View File

@ -28,6 +28,8 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub
}
#endif
void set_channel(i2s_channel_fmt_t channel) { this->channel_ = channel; }
protected:
void start_();
void stop_();
@ -40,6 +42,7 @@ class I2SAudioMicrophone : public I2SAudioIn, public microphone::Microphone, pub
#endif
bool pdm_{false};
std::vector<uint8_t> buffer_;
i2s_channel_fmt_t channel_;
HighFrequencyLoopRequester high_freq_;
};