ESPHome  2022.6.2
rtttl.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace rtttl {
9 
10 class Rtttl : public Component {
11  public:
12  void set_output(output::FloatOutput *output) { output_ = output; }
13  void play(std::string rtttl);
14  void stop() {
15  note_duration_ = 0;
16  output_->set_level(0.0);
17  }
18  void dump_config() override;
19 
20  bool is_playing() { return note_duration_ != 0; }
21  void loop() override;
22 
23  void add_on_finished_playback_callback(std::function<void()> callback) {
24  this->on_finished_playback_callback_.add(std::move(callback));
25  }
26 
27  protected:
28  inline uint8_t get_integer_() {
29  uint8_t ret = 0;
30  while (isdigit(rtttl_[position_])) {
31  ret = (ret * 10) + (rtttl_[position_++] - '0');
32  }
33  return ret;
34  }
35 
36  std::string rtttl_;
37  size_t position_;
38  uint16_t wholenote_;
40  uint16_t default_octave_;
41  uint32_t last_note_;
42  uint16_t note_duration_;
43 
44  uint32_t output_freq_;
46 
48 };
49 
50 template<typename... Ts> class PlayAction : public Action<Ts...> {
51  public:
52  PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
53  TEMPLATABLE_VALUE(std::string, value)
54 
55  void play(Ts... x) override { this->rtttl_->play(this->value_.value(x...)); }
56 
57  protected:
59 };
60 
61 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
62  public:
63  void play(Ts... x) override { this->parent_->stop(); }
64 };
65 
66 template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
67  public:
68  bool check(Ts... x) override { return this->parent_->is_playing(); }
69 };
70 
71 class FinishedPlaybackTrigger : public Trigger<> {
72  public:
73  explicit FinishedPlaybackTrigger(Rtttl *parent) {
74  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
75  }
76 };
77 
78 } // namespace rtttl
79 } // namespace esphome
uint16_t wholenote_
Definition: rtttl.h:38
void play(std::string rtttl)
Definition: rtttl.cpp:20
void play(Ts... x) override
Definition: rtttl.h:63
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
void dump_config() override
Definition: rtttl.cpp:18
void loop() override
Definition: rtttl.cpp:88
CallbackManager< void()> on_finished_playback_callback_
Definition: rtttl.h:47
uint32_t output_freq_
Definition: rtttl.h:44
void set_level(float state)
Set the level of this float output, this is called from the front-end.
bool check(Ts... x) override
Definition: rtttl.h:68
uint8_t get_integer_()
Definition: rtttl.h:28
Base class for all automation conditions.
Definition: automation.h:74
uint16_t note_duration_
Definition: rtttl.h:42
std::string rtttl_
Definition: rtttl.h:36
uint16_t default_duration_
Definition: rtttl.h:39
void set_output(output::FloatOutput *output)
Definition: rtttl.h:12
size_t position_
Definition: rtttl.h:37
output::FloatOutput * output_
Definition: rtttl.h:45
PlayAction(Rtttl *rtttl)
Definition: rtttl.h:52
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition: rtttl.h:53
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: rtttl.h:23
bool is_playing()
Definition: rtttl.h:20
uint16_t default_octave_
Definition: rtttl.h:40
Definition: a4988.cpp:4
FinishedPlaybackTrigger(Rtttl *parent)
Definition: rtttl.h:73
Helper class to easily give an object a parent of type T.
Definition: helpers.h:494
uint32_t last_note_
Definition: rtttl.h:41