ESPHome  2024.9.0
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 
53  void start() override;
54  void stop() override;
55  void finish() override;
56 
57  size_t play(const uint8_t *data, size_t length) override;
58 
59  bool has_buffered_data() const override;
60 
61  protected:
62  void start_();
63  void stop_(bool wait_on_empty);
64  void watch_();
65 
66  static void player_task(void *params);
67 
68  TaskHandle_t player_task_handle_{nullptr};
69  QueueHandle_t buffer_queue_;
70  QueueHandle_t event_queue_;
71 
72  uint32_t timeout_{0};
73  uint8_t dout_pin_{0};
74  bool task_created_{false};
75 
76 #if SOC_I2S_SUPPORTS_DAC
77  i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
78 #endif
79 };
80 
81 } // namespace i2s_audio
82 } // namespace esphome
83 
84 #endif // USE_ESP32
void setup()
void loop()
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:181
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)