ESPHome  2024.11.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 // See the datasheet here:
27 // https://github.com/DFRobot/DFRobotDFPlayerMini/blob/master/doc/FN-M16P%2BEmbedded%2BMP3%2BAudio%2BModule%2BDatasheet.pdf
28 class DFPlayer : public uart::UARTDevice, public Component {
29  public:
30  void loop() override;
31 
32  void next();
33  void previous();
34  void play_mp3(uint16_t file);
35  void play_file(uint16_t file);
36  void play_file_loop(uint16_t file);
37  void play_folder(uint16_t folder, uint16_t file);
38  void play_folder_loop(uint16_t folder);
39  void volume_up();
40  void volume_down();
41  void set_device(Device device);
42  void set_volume(uint8_t volume);
43  void set_eq(EqPreset preset);
44  void sleep();
45  void reset();
46  void start();
47  void pause();
48  void stop();
49  void random();
50 
51  bool is_playing() { return is_playing_; }
52  void dump_config() override;
53 
54  void add_on_finished_playback_callback(std::function<void()> callback) {
55  this->on_finished_playback_callback_.add(std::move(callback));
56  }
57 
58  protected:
59  void send_cmd_(uint8_t cmd, uint16_t argument = 0);
60  void send_cmd_(uint8_t cmd, uint16_t high, uint16_t low) {
61  this->send_cmd_(cmd, ((high & 0xFF) << 8) | (low & 0xFF));
62  }
63  uint8_t sent_cmd_{0};
64 
66  size_t read_pos_{0};
67 
68  bool is_playing_{false};
69  bool ack_set_is_playing_{false};
70  bool ack_reset_is_playing_{false};
71 
73 };
74 
75 #define DFPLAYER_SIMPLE_ACTION(ACTION_CLASS, ACTION_METHOD) \
76  template<typename... Ts> \
77  class ACTION_CLASS : /* NOLINT */ \
78  public Action<Ts...>, \
79  public Parented<DFPlayer> { \
80  void play(Ts... x) override { this->parent_->ACTION_METHOD(); } \
81  };
82 
83 DFPLAYER_SIMPLE_ACTION(NextAction, next)
84 DFPLAYER_SIMPLE_ACTION(PreviousAction, previous)
85 
86 template<typename... Ts> class PlayMp3Action : public Action<Ts...>, public Parented<DFPlayer> {
87  public:
88  TEMPLATABLE_VALUE(uint16_t, file)
89 
90  void play(Ts... x) override {
91  auto file = this->file_.value(x...);
92  this->parent_->play_mp3(file);
93  }
94 };
95 
96 template<typename... Ts> class PlayFileAction : public Action<Ts...>, public Parented<DFPlayer> {
97  public:
98  TEMPLATABLE_VALUE(uint16_t, file)
99  TEMPLATABLE_VALUE(bool, loop)
100 
101  void play(Ts... x) override {
102  auto file = this->file_.value(x...);
103  auto loop = this->loop_.value(x...);
104  if (loop) {
105  this->parent_->play_file_loop(file);
106  } else {
107  this->parent_->play_file(file);
108  }
109  }
110 };
111 
112 template<typename... Ts> class PlayFolderAction : public Action<Ts...>, public Parented<DFPlayer> {
113  public:
114  TEMPLATABLE_VALUE(uint16_t, folder)
115  TEMPLATABLE_VALUE(uint16_t, file)
116  TEMPLATABLE_VALUE(bool, loop)
117 
118  void play(Ts... x) override {
119  auto folder = this->folder_.value(x...);
120  auto file = this->file_.value(x...);
121  auto loop = this->loop_.value(x...);
122  if (loop) {
123  this->parent_->play_folder_loop(folder);
124  } else {
125  this->parent_->play_folder(folder, file);
126  }
127  }
128 };
129 
130 template<typename... Ts> class SetDeviceAction : public Action<Ts...>, public Parented<DFPlayer> {
131  public:
133 
134  void play(Ts... x) override {
135  auto device = this->device_.value(x...);
136  this->parent_->set_device(device);
137  }
138 };
139 
140 template<typename... Ts> class SetVolumeAction : public Action<Ts...>, public Parented<DFPlayer> {
141  public:
142  TEMPLATABLE_VALUE(uint8_t, volume)
143 
144  void play(Ts... x) override {
145  auto volume = this->volume_.value(x...);
146  this->parent_->set_volume(volume);
147  }
148 };
149 
150 template<typename... Ts> class SetEqAction : public Action<Ts...>, public Parented<DFPlayer> {
151  public:
153 
154  void play(Ts... x) override {
155  auto eq = this->eq_.value(x...);
156  this->parent_->set_eq(eq);
157  }
158 };
159 
160 DFPLAYER_SIMPLE_ACTION(SleepAction, sleep)
161 DFPLAYER_SIMPLE_ACTION(ResetAction, reset)
162 DFPLAYER_SIMPLE_ACTION(StartAction, start)
165 DFPLAYER_SIMPLE_ACTION(RandomAction, random)
168 
169 template<typename... Ts> class DFPlayerIsPlayingCondition : public Condition<Ts...>, public Parented<DFPlayer> {
170  public:
171  bool check(Ts... x) override { return this->parent_->is_playing(); }
172 };
173 
175  public:
177  parent->add_on_finished_playback_callback([this]() { this->trigger(); });
178  }
179 };
180 
181 } // namespace dfplayer
182 } // namespace esphome
void send_cmd_(uint8_t cmd, uint16_t argument=0)
Definition: dfplayer.cpp:118
void dump_config() override
Definition: dfplayer.cpp:257
MediaPlayerCommandAction< MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP, Ts... > StopAction
Definition: automation.h:21
CallbackManager< void()> on_finished_playback_callback_
Definition: dfplayer.h:72
void play_file_loop(uint16_t file)
Definition: dfplayer.cpp:32
void add_on_finished_playback_callback(std::function< void()> callback)
Definition: dfplayer.h:54
DFPLAYER_SIMPLE_ACTION(NextAction, next) DFPLAYER_SIMPLE_ACTION(PreviousAction
uint16_t x
Definition: tt21100.cpp:17
void set_device(Device device)
Definition: dfplayer.cpp:54
void send_cmd_(uint8_t cmd, uint16_t high, uint16_t low)
Definition: dfplayer.h:60
void play_file(uint16_t file)
Definition: dfplayer.cpp:26
void play_folder_loop(uint16_t folder)
Definition: dfplayer.cpp:38
UARTComponent * parent_
Definition: uart.h:68
TEMPLATABLE_VALUE(Device, device) void play(Ts... x) override
Definition: dfplayer.h:132
Base class for all automation conditions.
Definition: automation.h:74
TEMPLATABLE_VALUE(uint16_t, file) void play(Ts... x) override
Definition: dfplayer.h:88
const size_t DFPLAYER_READ_BUFFER_LENGTH
Definition: dfplayer.h:7
void play_folder(uint16_t folder, uint16_t file)
Definition: dfplayer.cpp:105
MediaPlayerCommandAction< MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PAUSE, Ts... > PauseAction
Definition: automation.h:19
MediaPlayerCommandAction< MediaPlayerCommand::MEDIA_PLAYER_COMMAND_VOLUME_DOWN, Ts... > VolumeDownAction
Definition: automation.h:27
MediaPlayerCommandAction< MediaPlayerCommand::MEDIA_PLAYER_COMMAND_VOLUME_UP, Ts... > VolumeUpAction
Definition: automation.h:25
TEMPLATABLE_VALUE(uint8_t, volume) void play(Ts... x) override
Definition: dfplayer.h:142
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_eq(EqPreset preset)
Definition: dfplayer.cpp:64
TEMPLATABLE_VALUE(EqPreset, eq) void play(Ts... x) override
Definition: dfplayer.h:152
void set_volume(uint8_t volume)
Definition: dfplayer.cpp:59
char read_buffer_[DFPLAYER_READ_BUFFER_LENGTH]
Definition: dfplayer.h:65
void play_mp3(uint16_t file)
Definition: dfplayer.cpp:20
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522
ClimatePreset preset
Definition: climate.h:578
stm32_cmd_t * cmd
Definition: stm32flash.h:96