ESPHome  2024.10.2
i2s_audio_speaker.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include "../i2s_audio.h"
6 
7 #include <driver/i2s.h>
8 #include <freertos/FreeRTOS.h>
9 #include <freertos/queue.h>
10 
12 #include "esphome/core/component.h"
13 #include "esphome/core/gpio.h"
14 #include "esphome/core/helpers.h"
15 
16 namespace esphome {
17 namespace i2s_audio {
18 
19 static const size_t BUFFER_SIZE = 1024;
20 
21 enum class TaskEventType : uint8_t {
22  STARTING = 0,
23  STARTED,
24  STOPPING,
25  STOPPED,
26  WARNING = 255,
27 };
28 
29 struct TaskEvent {
31  esp_err_t err;
32 };
33 
34 struct DataEvent {
35  bool stop;
36  size_t len;
37  uint8_t data[BUFFER_SIZE];
38 };
39 
40 class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Component {
41  public:
42  float get_setup_priority() const override { return esphome::setup_priority::LATE; }
43 
44  void setup() override;
45  void loop() override;
46 
47  void set_timeout(uint32_t ms) { this->timeout_ = ms; }
48  void set_dout_pin(uint8_t pin) { this->dout_pin_ = pin; }
49 #if SOC_I2S_SUPPORTS_DAC
50  void set_internal_dac_mode(i2s_dac_mode_t mode) { this->internal_dac_mode_ = mode; }
51 #endif
52  void set_i2s_comm_fmt(i2s_comm_format_t mode) { this->i2s_comm_fmt_ = mode; }
53 
54  void start() override;
55  void stop() override;
56  void finish() override;
57 
58  size_t play(const uint8_t *data, size_t length) override;
59 
60  bool has_buffered_data() const override;
61 
62  protected:
63  void start_();
64  void stop_(bool wait_on_empty);
65  void watch_();
66 
67  static void player_task(void *params);
68 
69  TaskHandle_t player_task_handle_{nullptr};
70  QueueHandle_t buffer_queue_;
71  QueueHandle_t event_queue_;
72 
73  uint32_t timeout_{0};
74  uint8_t dout_pin_{0};
75  bool task_created_{false};
76 
77 #if SOC_I2S_SUPPORTS_DAC
78  i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
79 #endif
80  i2s_comm_format_t i2s_comm_fmt_;
81 };
82 
83 } // namespace i2s_audio
84 } // namespace esphome
85 
86 #endif // USE_ESP32
void setup()
void loop()
void set_i2s_comm_fmt(i2s_comm_format_t mode)
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:28
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
float get_setup_priority() const override
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_internal_dac_mode(i2s_dac_mode_t mode)