ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
rtttl.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef USE_OUTPUT
8#endif // USE_OUTPUT
9
10#ifdef USE_SPEAKER
12#endif // USE_SPEAKER
13
14namespace esphome::rtttl {
15
16inline constexpr uint8_t DEFAULT_NOTE_DENOMINATOR = 4; // Default note-denominator (quarter note)
17inline constexpr uint8_t DEFAULT_OCTAVE =
18 6; // Default octave for a note (see: `MIN_OCTAVE`, `MAX_OCTAVE` in `rtttl.cpp`)
19
20enum class State : uint8_t {
21 STOPPED = 0,
22 INIT,
24 RUNNING,
26};
27
28class Rtttl : public Component {
29 public:
30#ifdef USE_OUTPUT
31 void set_output(output::FloatOutput *output) { this->output_ = output; }
32#endif // USE_OUTPUT
33
34#ifdef USE_SPEAKER
35 void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
36#endif // USE_SPEAKER
37
38 void dump_config() override;
39 void loop() override;
40 void play(std::string rtttl);
41 void stop();
42
43 float get_gain() { return this->gain_; }
44 void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
45
46 bool is_playing() { return this->state_ != State::STOPPED; }
47
48 void add_on_finished_playback_callback(std::function<void()> callback) {
49 this->on_finished_playback_callback_.add(std::move(callback));
50 }
51
52 protected:
53 inline uint16_t get_integer_() {
54 uint16_t ret = 0;
55 while (isdigit(this->rtttl_[this->position_])) {
56 ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
57 }
58 return ret;
59 }
67 void finish_();
69
71 std::string rtttl_{""};
73 size_t position_{0};
79 uint16_t note_duration_{0};
85 uint32_t output_freq_{0};
87 float gain_{0.6f};
90
91#ifdef USE_OUTPUT
94#endif // USE_OUTPUT
95
96#ifdef USE_SPEAKER
100 uint32_t samples_per_wave_{0};
102 uint32_t samples_sent_{0};
104 uint32_t samples_count_{0};
106 uint32_t samples_gap_{0};
107#endif // USE_SPEAKER
108
111};
112
113template<typename... Ts> class PlayAction : public Action<Ts...> {
114 public:
115 PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
116 TEMPLATABLE_VALUE(std::string, value)
117
118 void play(const Ts &...x) override { this->rtttl_->play(this->value_.value(x...)); }
119
120 protected:
122};
123
124template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
125 public:
126 void play(const Ts &...x) override { this->parent_->stop(); }
127};
128
129template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
130 public:
131 bool check(const Ts &...x) override { return this->parent_->is_playing(); }
132};
133
135 public:
136 explicit FinishedPlaybackTrigger(Rtttl *parent) {
137 parent->add_on_finished_playback_callback([this]() { this->trigger(); });
138 }
139};
140
141} // namespace esphome::rtttl
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:304
Helper class to easily give an object a parent of type T.
Definition helpers.h:1618
void trigger(const Ts &...x)
Definition automation.h:325
Base class for all output components that can output a variable level, like PWM.
bool check(const Ts &...x) override
Definition rtttl.h:131
PlayAction(Rtttl *rtttl)
Definition rtttl.h:115
TEMPLATABLE_VALUE(std::string, value) void play(const Ts &...x) override
Definition rtttl.h:116
uint32_t samples_per_wave_
The number of samples for one full cycle of a note's waveform, in Q10 fixed-point format.
Definition rtttl.h:100
uint32_t last_note_start_time_
The time in milliseconds since microcontroller boot when the last note was started.
Definition rtttl.h:83
uint8_t default_note_denominator_
The default duration of a note (e.g. 4 for a quarter note).
Definition rtttl.h:75
uint16_t note_duration_
The duration of the current note in milliseconds.
Definition rtttl.h:79
uint32_t samples_sent_
The number of samples sent.
Definition rtttl.h:102
output::FloatOutput * output_
The output to write the sound to.
Definition rtttl.h:93
uint16_t get_integer_()
Definition rtttl.h:53
void dump_config() override
Definition rtttl.cpp:82
void set_state_(State state)
Definition rtttl.cpp:427
void finish_()
Finalizes the playback of the RTTTL string.
Definition rtttl.cpp:399
float gain_
The gain of the output.
Definition rtttl.h:87
uint8_t default_octave_
The default octave for a note.
Definition rtttl.h:77
uint32_t output_freq_
The frequency of the current note in Hz.
Definition rtttl.h:85
void loop() override
Definition rtttl.cpp:89
void add_on_finished_playback_callback(std::function< void()> callback)
Definition rtttl.h:48
uint32_t samples_gap_
The number of samples for the gap between notes.
Definition rtttl.h:106
size_t position_
The current position in the RTTTL string.
Definition rtttl.h:73
void set_gain(float gain)
Definition rtttl.h:44
uint32_t samples_count_
The total number of samples to send.
Definition rtttl.h:104
uint16_t wholenote_duration_
The duration of a whole note in milliseconds.
Definition rtttl.h:81
State state_
The current state of the RTTTL player.
Definition rtttl.h:89
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:35
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:98
void set_output(output::FloatOutput *output)
Definition rtttl.h:31
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
Definition rtttl.h:110
void play(std::string rtttl)
Definition rtttl.cpp:273
std::string rtttl_
The RTTTL string to play.
Definition rtttl.h:71
void play(const Ts &...x) override
Definition rtttl.h:126
bool state
Definition fan.h:2
AlsGain501 gain
constexpr uint8_t DEFAULT_OCTAVE
Definition rtttl.h:17
constexpr uint8_t DEFAULT_NOTE_DENOMINATOR
Definition rtttl.h:16
uint16_t x
Definition tt21100.cpp:5