From 5afdb1e97fc045a7596f158d59038dcb0797603d Mon Sep 17 00:00:00 2001 From: Carlos Cordero Date: Mon, 12 Jun 2023 21:48:01 +0200 Subject: [PATCH] I2S media player allow setting communication format for external DACs (#4918) Co-authored-by: Carlos Cordero --- esphome/components/i2s_audio/media_player/__init__.py | 7 +++++++ .../i2s_audio/media_player/i2s_audio_media_player.cpp | 1 + .../i2s_audio/media_player/i2s_audio_media_player.h | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/esphome/components/i2s_audio/media_player/__init__.py b/esphome/components/i2s_audio/media_player/__init__.py index dfc3fb2be2..600a308e6c 100644 --- a/esphome/components/i2s_audio/media_player/__init__.py +++ b/esphome/components/i2s_audio/media_player/__init__.py @@ -27,6 +27,7 @@ i2s_dac_mode_t = cg.global_ns.enum("i2s_dac_mode_t") CONF_MUTE_PIN = "mute_pin" CONF_AUDIO_ID = "audio_id" CONF_DAC_TYPE = "dac_type" +CONF_I2S_COMM_FMT = "i2s_comm_fmt" INTERNAL_DAC_OPTIONS = { "left": i2s_dac_mode_t.I2S_DAC_CHANNEL_LEFT_EN, @@ -38,6 +39,8 @@ EXTERNAL_DAC_OPTIONS = ["mono", "stereo"] NO_INTERNAL_DAC_VARIANTS = [esp32.const.VARIANT_ESP32S2] +I2C_COMM_FMT_OPTIONS = ["lsb", "msb"] + def validate_esp32_variant(config): if config[CONF_DAC_TYPE] != "internal": @@ -69,6 +72,9 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_MODE, default="mono"): cv.one_of( *EXTERNAL_DAC_OPTIONS, lower=True ), + cv.Optional(CONF_I2S_COMM_FMT, default="msb"): cv.one_of( + *I2C_COMM_FMT_OPTIONS, lower=True + ), } ).extend(cv.COMPONENT_SCHEMA), }, @@ -94,6 +100,7 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_MUTE_PIN]) cg.add(var.set_mute_pin(pin)) cg.add(var.set_external_dac_channels(2 if config[CONF_MODE] == "stereo" else 1)) + cg.add(var.set_i2s_comm_fmt_lsb(config[CONF_I2S_COMM_FMT] == "lsb")) cg.add_library("WiFiClientSecure", None) cg.add_library("HTTPClient", None) diff --git a/esphome/components/i2s_audio/media_player/i2s_audio_media_player.cpp b/esphome/components/i2s_audio/media_player/i2s_audio_media_player.cpp index 6eaa32c23c..9e2e3f136a 100644 --- a/esphome/components/i2s_audio/media_player/i2s_audio_media_player.cpp +++ b/esphome/components/i2s_audio/media_player/i2s_audio_media_player.cpp @@ -148,6 +148,7 @@ void I2SAudioMediaPlayer::start_() { pin_config.data_out_num = this->dout_pin_; i2s_set_pin(this->parent_->get_port(), &pin_config); + this->audio_->setI2SCommFMT_LSB(this->i2s_comm_fmt_lsb_); this->audio_->forceMono(this->external_dac_channels_ == 1); if (this->mute_pin_ != nullptr) { this->mute_pin_->setup(); diff --git a/esphome/components/i2s_audio/media_player/i2s_audio_media_player.h b/esphome/components/i2s_audio/media_player/i2s_audio_media_player.h index dab9a85d7c..092e6de8e8 100644 --- a/esphome/components/i2s_audio/media_player/i2s_audio_media_player.h +++ b/esphome/components/i2s_audio/media_player/i2s_audio_media_player.h @@ -39,6 +39,8 @@ class I2SAudioMediaPlayer : public Component, public media_player::MediaPlayer, #endif void set_external_dac_channels(uint8_t channels) { this->external_dac_channels_ = channels; } + void set_i2s_comm_fmt_lsb(bool lsb) { this->i2s_comm_fmt_lsb_ = lsb; } + media_player::MediaPlayerTraits get_traits() override; bool is_muted() const override { return this->muted_; } @@ -71,6 +73,8 @@ class I2SAudioMediaPlayer : public Component, public media_player::MediaPlayer, #endif uint8_t external_dac_channels_; + bool i2s_comm_fmt_lsb_; + HighFrequencyLoopRequester high_freq_; optional current_url_{};