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