ESPHome  2024.12.2
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 enum State : uint8_t {
23 };
24 
25 #ifdef USE_SPEAKER
26 static const size_t SAMPLE_BUFFER_SIZE = 2048;
27 
28 struct SpeakerSample {
29  int8_t left{0};
30  int8_t right{0};
31 };
32 #endif
33 
34 class Rtttl : public Component {
35  public:
36 #ifdef USE_OUTPUT
37  void set_output(output::FloatOutput *output) { this->output_ = output; }
38 #endif
39 #ifdef USE_SPEAKER
40  void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
41 #endif
42  float get_gain() { return gain_; }
43  void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
44  void play(std::string rtttl);
45  void stop();
46  void dump_config() override;
47 
48  bool is_playing() { return this->state_ != State::STATE_STOPPED; }
49  void loop() override;
50 
51  void add_on_finished_playback_callback(std::function<void()> callback) {
52  this->on_finished_playback_callback_.add(std::move(callback));
53  }
54 
55  protected:
56  inline uint8_t get_integer_() {
57  uint8_t ret = 0;
58  while (isdigit(this->rtttl_[this->position_])) {
59  ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
60  }
61  return ret;
62  }
63  void finish_();
64  void set_state_(State state);
65 
66  std::string rtttl_{""};
67  size_t position_{0};
68  uint16_t wholenote_;
70  uint16_t default_octave_;
71  uint32_t last_note_;
72  uint16_t note_duration_;
73 
74  uint32_t output_freq_;
75  float gain_{0.6f};
77 
78 #ifdef USE_OUTPUT
80 #endif
81 
82 #ifdef USE_SPEAKER
83  speaker::Speaker *speaker_{nullptr};
84  int sample_rate_{16000};
85  int samples_per_wave_{0};
86  int samples_sent_{0};
87  int samples_count_{0};
88  int samples_gap_{0};
89 
90 #endif
91 
93 };
94 
95 template<typename... Ts> class PlayAction : public Action<Ts...> {
96  public:
97  PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
98  TEMPLATABLE_VALUE(std::string, value)
99 
100  void play(Ts... x) override { this->rtttl_->play(this->value_.value(x...)); }
101 
102  protected:
104 };
105 
106 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
107  public:
108  void play(Ts... x) override { this->parent_->stop(); }
109 };
110 
111 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
112  public:
113  bool check(Ts... x) override { return this->parent_->is_playing(); }
114 };
115 
117  public:
118  explicit FinishedPlaybackTrigger(Rtttl *parent) {
119  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
120  }
121 };
122 
123 } // namespace rtttl
124 } // namespace esphome
uint16_t wholenote_
Definition: rtttl.h:68
void loop()
void set_gain(float gain)
Definition: rtttl.h:43
void play(Ts... x) override
Definition: rtttl.h:108
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
float get_gain()
Definition: rtttl.h:42
CallbackManager< void()> on_finished_playback_callback_
Definition: rtttl.h:92
AlsGain501 gain
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:93
uint32_t output_freq_
Definition: rtttl.h:74
bool check(Ts... x) override
Definition: rtttl.h:113
uint8_t get_integer_()
Definition: rtttl.h:56
Base class for all automation conditions.
Definition: automation.h:74
uint16_t note_duration_
Definition: rtttl.h:72
uint16_t default_duration_
Definition: rtttl.h:69
void set_speaker(speaker::Speaker *speaker)
Definition: rtttl.h:40
void set_output(output::FloatOutput *output)
Definition: rtttl.h:37
output::FloatOutput * output_
Definition: rtttl.h:79
PlayAction(Rtttl *rtttl)
Definition: rtttl.h:97
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition: rtttl.h:98
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: rtttl.h:51
bool is_playing()
Definition: rtttl.h:48
uint16_t default_octave_
Definition: rtttl.h:70
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
FinishedPlaybackTrigger(Rtttl *parent)
Definition: rtttl.h:118
Helper class to easily give an object a parent of type T.
Definition: helpers.h:530
uint32_t last_note_
Definition: rtttl.h:71
bool state
Definition: fan.h:34