ESPHome  2024.9.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  void set_gain(float gain) {
43  if (gain < 0.1f)
44  gain = 0.1f;
45  if (gain > 1.0f)
46  gain = 1.0f;
47  this->gain_ = gain;
48  }
49  void play(std::string rtttl);
50  void stop();
51  void dump_config() override;
52 
53  bool is_playing() { return this->state_ != State::STATE_STOPPED; }
54  void loop() override;
55 
56  void add_on_finished_playback_callback(std::function<void()> callback) {
57  this->on_finished_playback_callback_.add(std::move(callback));
58  }
59 
60  protected:
61  inline uint8_t get_integer_() {
62  uint8_t ret = 0;
63  while (isdigit(this->rtttl_[this->position_])) {
64  ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
65  }
66  return ret;
67  }
68  void finish_();
69  void set_state_(State state);
70 
71  std::string rtttl_{""};
72  size_t position_{0};
73  uint16_t wholenote_;
75  uint16_t default_octave_;
76  uint32_t last_note_;
77  uint16_t note_duration_;
78 
79  uint32_t output_freq_;
80  float gain_{0.6f};
82 
83 #ifdef USE_OUTPUT
85 #endif
86 
87 #ifdef USE_SPEAKER
88  speaker::Speaker *speaker_{nullptr};
89  int sample_rate_{16000};
90  int samples_per_wave_{0};
91  int samples_sent_{0};
92  int samples_count_{0};
93  int samples_gap_{0};
94 
95 #endif
96 
98 };
99 
100 template<typename... Ts> class PlayAction : public Action<Ts...> {
101  public:
102  PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
103  TEMPLATABLE_VALUE(std::string, value)
104 
105  void play(Ts... x) override { this->rtttl_->play(this->value_.value(x...)); }
106 
107  protected:
109 };
110 
111 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
112  public:
113  void play(Ts... x) override { this->parent_->stop(); }
114 };
115 
116 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
117  public:
118  bool check(Ts... x) override { return this->parent_->is_playing(); }
119 };
120 
122  public:
123  explicit FinishedPlaybackTrigger(Rtttl *parent) {
124  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
125  }
126 };
127 
128 } // namespace rtttl
129 } // namespace esphome
uint16_t wholenote_
Definition: rtttl.h:73
void loop()
void set_gain(float gain)
Definition: rtttl.h:42
void play(Ts... x) override
Definition: rtttl.h:113
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:97
AlsGain501 gain
uint32_t output_freq_
Definition: rtttl.h:79
bool check(Ts... x) override
Definition: rtttl.h:118
uint8_t get_integer_()
Definition: rtttl.h:61
Base class for all automation conditions.
Definition: automation.h:74
uint16_t note_duration_
Definition: rtttl.h:77
uint16_t default_duration_
Definition: rtttl.h:74
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:84
PlayAction(Rtttl *rtttl)
Definition: rtttl.h:102
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition: rtttl.h:103
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: rtttl.h:56
bool is_playing()
Definition: rtttl.h:53
uint16_t default_octave_
Definition: rtttl.h:75
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
FinishedPlaybackTrigger(Rtttl *parent)
Definition: rtttl.h:123
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521
uint32_t last_note_
Definition: rtttl.h:76
bool state
Definition: fan.h:34