ESPHome  2022.5.1
dfplayer.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 const size_t DFPLAYER_READ_BUFFER_LENGTH = 25; // two messages + some extra
8 
9 namespace esphome {
10 namespace dfplayer {
11 
12 enum EqPreset {
13  NORMAL = 0,
14  POP = 1,
15  ROCK = 2,
16  JAZZ = 3,
17  CLASSIC = 4,
18  BASS = 5,
19 };
20 
21 enum Device {
22  USB = 1,
23  TF_CARD = 2,
24 };
25 
26 class DFPlayer : public uart::UARTDevice, public Component {
27  public:
28  void loop() override;
29 
30  void next() {
31  this->ack_set_is_playing_ = true;
32  this->send_cmd_(0x01);
33  }
34  void previous() {
35  this->ack_set_is_playing_ = true;
36  this->send_cmd_(0x02);
37  }
38  void play_file(uint16_t file) {
39  this->ack_set_is_playing_ = true;
40  this->send_cmd_(0x03, file);
41  }
42  void play_file_loop(uint16_t file) {
43  this->ack_set_is_playing_ = true;
44  this->send_cmd_(0x08, file);
45  }
46  void play_folder(uint16_t folder, uint16_t file);
47  void play_folder_loop(uint16_t folder) {
48  this->ack_set_is_playing_ = true;
49  this->send_cmd_(0x17, folder);
50  }
51  void volume_up() { this->send_cmd_(0x04); }
52  void volume_down() { this->send_cmd_(0x05); }
53  void set_device(Device device) { this->send_cmd_(0x09, device); }
54  void set_volume(uint8_t volume) { this->send_cmd_(0x06, volume); }
55  void set_eq(EqPreset preset) { this->send_cmd_(0x07, preset); }
56  void sleep() {
57  this->ack_reset_is_playing_ = true;
58  this->send_cmd_(0x0A);
59  }
60  void reset() {
61  this->ack_reset_is_playing_ = true;
62  this->send_cmd_(0x0C);
63  }
64  void start() {
65  this->ack_set_is_playing_ = true;
66  this->send_cmd_(0x0D);
67  }
68  void pause() {
69  this->ack_reset_is_playing_ = true;
70  this->send_cmd_(0x0E);
71  }
72  void stop() {
73  this->ack_reset_is_playing_ = true;
74  this->send_cmd_(0x16);
75  }
76  void random() {
77  this->ack_set_is_playing_ = true;
78  this->send_cmd_(0x18);
79  }
80 
81  bool is_playing() { return is_playing_; }
82  void dump_config() override;
83 
84  void add_on_finished_playback_callback(std::function<void()> callback) {
85  this->on_finished_playback_callback_.add(std::move(callback));
86  }
87 
88  protected:
89  void send_cmd_(uint8_t cmd, uint16_t argument = 0);
90  void send_cmd_(uint8_t cmd, uint16_t high, uint16_t low) {
91  this->send_cmd_(cmd, ((high & 0xFF) << 8) | (low & 0xFF));
92  }
93  uint8_t sent_cmd_{0};
94 
96  size_t read_pos_{0};
97 
98  bool is_playing_{false};
99  bool ack_set_is_playing_{false};
101 
103 };
104 
105 #define DFPLAYER_SIMPLE_ACTION(ACTION_CLASS, ACTION_METHOD) \
106  template<typename... Ts> \
107  class ACTION_CLASS : /* NOLINT */ \
108  public Action<Ts...>, \
109  public Parented<DFPlayer> { \
110  void play(Ts... x) override { this->parent_->ACTION_METHOD(); } \
111  };
112 
113 DFPLAYER_SIMPLE_ACTION(NextAction, next)
114 DFPLAYER_SIMPLE_ACTION(PreviousAction, previous)
115 
116 template<typename... Ts> class PlayFileAction : public Action<Ts...>, public Parented<DFPlayer> {
117  public:
118  TEMPLATABLE_VALUE(uint16_t, file)
119  TEMPLATABLE_VALUE(bool, loop)
120 
121  void play(Ts... x) override {
122  auto file = this->file_.value(x...);
123  auto loop = this->loop_.value(x...);
124  if (loop) {
125  this->parent_->play_file_loop(file);
126  } else {
127  this->parent_->play_file(file);
128  }
129  }
130 };
131 
132 template<typename... Ts> class PlayFolderAction : public Action<Ts...>, public Parented<DFPlayer> {
133  public:
134  TEMPLATABLE_VALUE(uint16_t, folder)
135  TEMPLATABLE_VALUE(uint16_t, file)
136  TEMPLATABLE_VALUE(bool, loop)
137 
138  void play(Ts... x) override {
139  auto folder = this->folder_.value(x...);
140  auto file = this->file_.value(x...);
141  auto loop = this->loop_.value(x...);
142  if (loop) {
143  this->parent_->play_folder_loop(folder);
144  } else {
145  this->parent_->play_folder(folder, file);
146  }
147  }
148 };
149 
150 template<typename... Ts> class SetDeviceAction : public Action<Ts...>, public Parented<DFPlayer> {
151  public:
153 
154  void play(Ts... x) override {
155  auto device = this->device_.value(x...);
156  this->parent_->set_device(device);
157  }
158 };
159 
160 template<typename... Ts> class SetVolumeAction : public Action<Ts...>, public Parented<DFPlayer> {
161  public:
162  TEMPLATABLE_VALUE(uint8_t, volume)
163 
164  void play(Ts... x) override {
165  auto volume = this->volume_.value(x...);
166  this->parent_->set_volume(volume);
167  }
168 };
169 
170 template<typename... Ts> class SetEqAction : public Action<Ts...>, public Parented<DFPlayer> {
171  public:
173 
174  void play(Ts... x) override {
175  auto eq = this->eq_.value(x...);
176  this->parent_->set_eq(eq);
177  }
178 };
179 
180 DFPLAYER_SIMPLE_ACTION(SleepAction, sleep)
181 DFPLAYER_SIMPLE_ACTION(ResetAction, reset)
182 DFPLAYER_SIMPLE_ACTION(StartAction, start)
183 DFPLAYER_SIMPLE_ACTION(PauseAction, pause)
184 DFPLAYER_SIMPLE_ACTION(StopAction, stop)
185 DFPLAYER_SIMPLE_ACTION(RandomAction, random)
186 DFPLAYER_SIMPLE_ACTION(VolumeUpAction, volume_up)
187 DFPLAYER_SIMPLE_ACTION(VolumeDownAction, volume_down)
188 
189 template<typename... Ts> class DFPlayerIsPlayingCondition : public Condition<Ts...>, public Parented<DFPlayer> {
190  public:
191  bool check(Ts... x) override { return this->parent_->is_playing(); }
192 };
193 
195  public:
197  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
198  }
199 };
200 
201 } // namespace dfplayer
202 } // namespace esphome
void loop() override
Definition: dfplayer.cpp:36
void send_cmd_(uint8_t cmd, uint16_t argument=0)
Definition: dfplayer.cpp:21
void dump_config() override
Definition: dfplayer.cpp:124
CallbackManager< void()> on_finished_playback_callback_
Definition: dfplayer.h:102
void play_file_loop(uint16_t file)
Definition: dfplayer.h:42
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: dfplayer.h:84
DFPLAYER_SIMPLE_ACTION(NextAction, next) DFPLAYER_SIMPLE_ACTION(PreviousAction
void set_device(Device device)
Definition: dfplayer.h:53
void send_cmd_(uint8_t cmd, uint16_t high, uint16_t low)
Definition: dfplayer.h:90
void play_file(uint16_t file)
Definition: dfplayer.h:38
void play_folder_loop(uint16_t folder)
Definition: dfplayer.h:47
UARTComponent * parent_
Definition: uart.h:68
TEMPLATABLE_VALUE(Device, device) void play(Ts... x) override
Definition: dfplayer.h:152
Base class for all automation conditions.
Definition: automation.h:74
const size_t DFPLAYER_READ_BUFFER_LENGTH
Definition: dfplayer.h:7
void play_folder(uint16_t folder, uint16_t file)
Definition: dfplayer.cpp:9
TEMPLATABLE_VALUE(uint8_t, volume) void play(Ts... x) override
Definition: dfplayer.h:162
Definition: a4988.cpp:4
void set_eq(EqPreset preset)
Definition: dfplayer.h:55
TEMPLATABLE_VALUE(EqPreset, eq) void play(Ts... x) override
Definition: dfplayer.h:172
void set_volume(uint8_t volume)
Definition: dfplayer.h:54
char read_buffer_[DFPLAYER_READ_BUFFER_LENGTH]
Definition: dfplayer.h:95
Helper class to easily give an object a parent of type T.
Definition: helpers.h:494
ClimatePreset preset
Definition: climate.h:546
stm32_cmd_t * cmd
Definition: stm32flash.h:96