ESPHome  2024.11.2
i2s_audio.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include <driver/i2s.h>
7 #include "esphome/core/helpers.h"
8 
9 namespace esphome {
10 namespace i2s_audio {
11 
12 class I2SAudioComponent;
13 
14 class I2SAudioBase : public Parented<I2SAudioComponent> {
15  public:
16  void set_i2s_mode(i2s_mode_t mode) { this->i2s_mode_ = mode; }
17  void set_channel(i2s_channel_fmt_t channel) { this->channel_ = channel; }
18  void set_sample_rate(uint32_t sample_rate) { this->sample_rate_ = sample_rate; }
19  void set_bits_per_sample(i2s_bits_per_sample_t bits_per_sample) { this->bits_per_sample_ = bits_per_sample; }
20  void set_bits_per_channel(i2s_bits_per_chan_t bits_per_channel) { this->bits_per_channel_ = bits_per_channel; }
21  void set_use_apll(uint32_t use_apll) { this->use_apll_ = use_apll; }
22 
23  protected:
24  i2s_mode_t i2s_mode_{};
25  i2s_channel_fmt_t channel_;
26  uint32_t sample_rate_;
27  i2s_bits_per_sample_t bits_per_sample_;
28  i2s_bits_per_chan_t bits_per_channel_;
29  bool use_apll_;
30 };
31 
32 class I2SAudioIn : public I2SAudioBase {};
33 
34 class I2SAudioOut : public I2SAudioBase {};
35 
36 class I2SAudioComponent : public Component {
37  public:
38  void setup() override;
39 
40  i2s_pin_config_t get_pin_config() const {
41  return {
42  .mck_io_num = this->mclk_pin_,
43  .bck_io_num = this->bclk_pin_,
44  .ws_io_num = this->lrclk_pin_,
45  .data_out_num = I2S_PIN_NO_CHANGE,
46  .data_in_num = I2S_PIN_NO_CHANGE,
47  };
48  }
49 
50  void set_mclk_pin(int pin) { this->mclk_pin_ = pin; }
51  void set_bclk_pin(int pin) { this->bclk_pin_ = pin; }
52  void set_lrclk_pin(int pin) { this->lrclk_pin_ = pin; }
53 
54  void lock() { this->lock_.lock(); }
55  bool try_lock() { return this->lock_.try_lock(); }
56  void unlock() { this->lock_.unlock(); }
57 
58  i2s_port_t get_port() const { return this->port_; }
59 
60  protected:
62 
63  I2SAudioIn *audio_in_{nullptr};
64  I2SAudioOut *audio_out_{nullptr};
65 
66  int mclk_pin_{I2S_PIN_NO_CHANGE};
67  int bclk_pin_{I2S_PIN_NO_CHANGE};
69  i2s_port_t port_{};
70 };
71 
72 } // namespace i2s_audio
73 } // namespace esphome
74 
75 #endif // USE_ESP32
void setup()
i2s_pin_config_t get_pin_config() const
Definition: i2s_audio.h:40
i2s_channel_fmt_t channel_
Definition: i2s_audio.h:25
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
void set_bits_per_sample(i2s_bits_per_sample_t bits_per_sample)
Definition: i2s_audio.h:19
void set_channel(i2s_channel_fmt_t channel)
Definition: i2s_audio.h:17
i2s_bits_per_sample_t bits_per_sample_
Definition: i2s_audio.h:27
void set_bits_per_channel(i2s_bits_per_chan_t bits_per_channel)
Definition: i2s_audio.h:20
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
i2s_bits_per_chan_t bits_per_channel_
Definition: i2s_audio.h:28
void set_sample_rate(uint32_t sample_rate)
Definition: i2s_audio.h:18
void set_i2s_mode(i2s_mode_t mode)
Definition: i2s_audio.h:16
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522
void set_use_apll(uint32_t use_apll)
Definition: i2s_audio.h:21
Mutex implementation, with API based on the unavailable std::mutex.
Definition: helpers.h:545