ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
14namespace esphome {
15namespace rtttl {
16
24
25#ifdef USE_SPEAKER
26static const size_t SAMPLE_BUFFER_SIZE = 2048;
27
29 int8_t left{0};
30 int8_t right{0};
31};
32#endif
33
34class 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_();
65
66 std::string rtttl_{""};
67 size_t position_{0};
68 uint16_t wholenote_;
71 uint32_t last_note_;
73
74 uint32_t output_freq_;
75 float gain_{0.6f};
77
78#ifdef USE_OUTPUT
80#endif
81
82#ifdef USE_SPEAKER
84 int sample_rate_{16000};
89
90#endif
91
93};
94
95template<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
106template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
107 public:
108 void play(Ts... x) override { this->parent_->stop(); }
109};
110
111template<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
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
void trigger(Ts... x)
Definition automation.h:96
Base class for all output components that can output a variable level, like PWM.
bool check(Ts... x) override
Definition rtttl.h:113
PlayAction(Rtttl *rtttl)
Definition rtttl.h:97
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition rtttl.h:98
uint16_t wholenote_
Definition rtttl.h:68
uint32_t last_note_
Definition rtttl.h:71
uint16_t note_duration_
Definition rtttl.h:72
output::FloatOutput * output_
Definition rtttl.h:79
void dump_config() override
Definition rtttl.cpp:29
void set_state_(State state)
Definition rtttl.cpp:387
uint32_t output_freq_
Definition rtttl.h:74
void loop() override
Definition rtttl.cpp:142
uint8_t get_integer_()
Definition rtttl.h:56
void add_on_finished_playback_callback(std::function< void()> callback)
Definition rtttl.h:51
uint16_t default_duration_
Definition rtttl.h:69
void set_gain(float gain)
Definition rtttl.h:43
uint16_t default_octave_
Definition rtttl.h:70
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:40
speaker::Speaker * speaker_
Definition rtttl.h:83
void set_output(output::FloatOutput *output)
Definition rtttl.h:37
CallbackManager< void()> on_finished_playback_callback_
Definition rtttl.h:92
void play(std::string rtttl)
Definition rtttl.cpp:34
std::string rtttl_
Definition rtttl.h:66
void play(Ts... x) override
Definition rtttl.h:108
bool state
Definition fan.h:0
AlsGain501 gain
@ STATE_STOPPING
Definition rtttl.h:22
@ STATE_RUNNING
Definition rtttl.h:21
@ STATE_STOPPED
Definition rtttl.h:18
@ STATE_STARTING
Definition rtttl.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101
uint16_t x
Definition tt21100.cpp:5