ESPHome  2024.4.1
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  PLAYING,
25  STOPPING,
26  STOPPED,
27  WARNING = 255,
28 };
29 
30 struct TaskEvent {
32  esp_err_t err;
33 };
34 
35 struct DataEvent {
36  bool stop;
37  size_t len;
38  uint8_t data[BUFFER_SIZE];
39 };
40 
41 class I2SAudioSpeaker : public Component, public speaker::Speaker, public I2SAudioOut {
42  public:
43  float get_setup_priority() const override { return esphome::setup_priority::LATE; }
44 
45  void setup() override;
46  void loop() override;
47 
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_external_dac_channels(uint8_t channels) { this->external_dac_channels_ = channels; }
53 
54  void start() override;
55  void stop() 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_();
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  uint8_t dout_pin_{0};
73 
74 #if SOC_I2S_SUPPORTS_DAC
75  i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
76 #endif
78 };
79 
80 } // namespace i2s_audio
81 } // namespace esphome
82 
83 #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:151
float get_setup_priority() const override
void set_external_dac_channels(uint8_t channels)
uint16_t length
Definition: tt21100.cpp:12
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_internal_dac_mode(i2s_dac_mode_t mode)