ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
i2s_audio.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
8#include <esp_idf_version.h>
9#include <driver/i2s_std.h>
10
11namespace esphome {
12namespace i2s_audio {
13
15
16class I2SAudioBase : public Parented<I2SAudioComponent> {
17 public:
18 void set_i2s_role(i2s_role_t role) { this->i2s_role_ = role; }
19 void set_slot_mode(i2s_slot_mode_t slot_mode) { this->slot_mode_ = slot_mode; }
20 void set_std_slot_mask(i2s_std_slot_mask_t std_slot_mask) { this->std_slot_mask_ = std_slot_mask; }
21 void set_slot_bit_width(i2s_slot_bit_width_t slot_bit_width) { this->slot_bit_width_ = slot_bit_width; }
22 void set_sample_rate(uint32_t sample_rate) { this->sample_rate_ = sample_rate; }
23 void set_use_apll(uint32_t use_apll) { this->use_apll_ = use_apll; }
24 void set_mclk_multiple(i2s_mclk_multiple_t mclk_multiple) { this->mclk_multiple_ = mclk_multiple; }
25
26 protected:
27 i2s_role_t i2s_role_{};
28 i2s_slot_mode_t slot_mode_;
29 i2s_std_slot_mask_t std_slot_mask_;
30 i2s_slot_bit_width_t slot_bit_width_;
33 i2s_mclk_multiple_t mclk_multiple_;
34};
35
36class I2SAudioIn : public I2SAudioBase {};
37
38class I2SAudioOut : public I2SAudioBase {};
39
41 public:
42 i2s_std_gpio_config_t get_pin_config() const {
43 return {.mclk = (gpio_num_t) this->mclk_pin_,
44 .bclk = (gpio_num_t) this->bclk_pin_,
45 .ws = (gpio_num_t) this->lrclk_pin_,
46 .dout = I2S_GPIO_UNUSED, // add local ports
47 .din = I2S_GPIO_UNUSED,
48 .invert_flags = {
49 .mclk_inv = false,
50 .bclk_inv = false,
51 .ws_inv = false,
52 }};
53 }
54
55 void set_mclk_pin(int pin) { this->mclk_pin_ = pin; }
56 void set_bclk_pin(int pin) { this->bclk_pin_ = pin; }
57 void set_lrclk_pin(int pin) { this->lrclk_pin_ = pin; }
58 void set_port(int port) { this->port_ = port; }
59#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
60 int get_port() const { return this->port_; }
61#else
62 i2s_port_t get_port() const { return static_cast<i2s_port_t>(this->port_); }
63#endif
64
65 void lock() { this->lock_.lock(); }
66 bool try_lock() { return this->lock_.try_lock(); }
67 void unlock() { this->lock_.unlock(); }
68
69 protected:
71
74 int mclk_pin_{I2S_GPIO_UNUSED};
75 int bclk_pin_{I2S_GPIO_UNUSED};
77 int port_{};
78};
79
80} // namespace i2s_audio
81} // namespace esphome
82
83#endif // USE_ESP32
Mutex implementation, with API based on the unavailable std::mutex.
Definition helpers.h:2036
void unlock()
Definition helpers.h:2047
bool try_lock()
Definition helpers.h:2046
Helper class to easily give an object a parent of type T.
Definition helpers.h:2013
i2s_std_slot_mask_t std_slot_mask_
Definition i2s_audio.h:29
i2s_slot_bit_width_t slot_bit_width_
Definition i2s_audio.h:30
void set_sample_rate(uint32_t sample_rate)
Definition i2s_audio.h:22
void set_mclk_multiple(i2s_mclk_multiple_t mclk_multiple)
Definition i2s_audio.h:24
void set_i2s_role(i2s_role_t role)
Definition i2s_audio.h:18
void set_use_apll(uint32_t use_apll)
Definition i2s_audio.h:23
void set_std_slot_mask(i2s_std_slot_mask_t std_slot_mask)
Definition i2s_audio.h:20
i2s_mclk_multiple_t mclk_multiple_
Definition i2s_audio.h:33
void set_slot_bit_width(i2s_slot_bit_width_t slot_bit_width)
Definition i2s_audio.h:21
void set_slot_mode(i2s_slot_mode_t slot_mode)
Definition i2s_audio.h:19
i2s_std_gpio_config_t get_pin_config() const
Definition i2s_audio.h:42
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t