ESPHome  2024.2.1
rtttl.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #ifdef USE_OUTPUT
8 #endif
9 
10 #ifdef USE_SPEAKER
12 #endif
13 
14 namespace esphome {
15 namespace rtttl {
16 
17 #ifdef USE_SPEAKER
18 static const size_t SAMPLE_BUFFER_SIZE = 256;
19 
20 struct SpeakerSample {
21  int16_t left{0};
22  int16_t right{0};
23 };
24 #endif
25 
26 class Rtttl : public Component {
27  public:
28 #ifdef USE_OUTPUT
29  void set_output(output::FloatOutput *output) { this->output_ = output; }
30 #endif
31 #ifdef USE_SPEAKER
32  void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
33 #endif
34  void play(std::string rtttl);
35  void stop();
36  void dump_config() override;
37 
38  bool is_playing() { return this->note_duration_ != 0; }
39  void loop() override;
40 
41  void add_on_finished_playback_callback(std::function<void()> callback) {
42  this->on_finished_playback_callback_.add(std::move(callback));
43  }
44 
45  protected:
46  inline uint8_t get_integer_() {
47  uint8_t ret = 0;
48  while (isdigit(this->rtttl_[this->position_])) {
49  ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
50  }
51  return ret;
52  }
53 
54  std::string rtttl_{""};
55  size_t position_{0};
56  uint16_t wholenote_;
58  uint16_t default_octave_;
59  uint32_t last_note_;
60  uint16_t note_duration_;
61 
62  uint32_t output_freq_;
63 
64 #ifdef USE_OUTPUT
66 #endif
67 
68  void play_output_();
69 
70 #ifdef USE_SPEAKER
72  void play_speaker_();
73  int sample_rate_{16000};
74  int samples_per_wave_{0};
75  int samples_sent_{0};
76  int samples_count_{0};
77  int samples_gap_{0};
78 #endif
79 
81 };
82 
83 template<typename... Ts> class PlayAction : public Action<Ts...> {
84  public:
85  PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
86  TEMPLATABLE_VALUE(std::string, value)
87 
88  void play(Ts... x) override { this->rtttl_->play(this->value_.value(x...)); }
89 
90  protected:
92 };
93 
94 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
95  public:
96  void play(Ts... x) override { this->parent_->stop(); }
97 };
98 
99 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
100  public:
101  bool check(Ts... x) override { return this->parent_->is_playing(); }
102 };
103 
105  public:
106  explicit FinishedPlaybackTrigger(Rtttl *parent) {
107  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
108  }
109 };
110 
111 } // namespace rtttl
112 } // namespace esphome
uint16_t wholenote_
Definition: rtttl.h:56
void loop()
void play(Ts... x) override
Definition: rtttl.h:96
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
uint16_t x
Definition: tt21100.cpp:17
CallbackManager< void()> on_finished_playback_callback_
Definition: rtttl.h:80
speaker::Speaker * speaker_
Definition: rtttl.h:71
uint32_t output_freq_
Definition: rtttl.h:62
bool check(Ts... x) override
Definition: rtttl.h:101
uint8_t get_integer_()
Definition: rtttl.h:46
Base class for all automation conditions.
Definition: automation.h:74
uint16_t note_duration_
Definition: rtttl.h:60
uint16_t default_duration_
Definition: rtttl.h:57
void set_speaker(speaker::Speaker *speaker)
Definition: rtttl.h:32
void set_output(output::FloatOutput *output)
Definition: rtttl.h:29
output::FloatOutput * output_
Definition: rtttl.h:65
PlayAction(Rtttl *rtttl)
Definition: rtttl.h:85
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition: rtttl.h:86
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: rtttl.h:41
bool is_playing()
Definition: rtttl.h:38
uint16_t default_octave_
Definition: rtttl.h:58
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
FinishedPlaybackTrigger(Rtttl *parent)
Definition: rtttl.h:106
Helper class to easily give an object a parent of type T.
Definition: helpers.h:515
uint32_t last_note_
Definition: rtttl.h:59