ESPHome 2025.5.0
Loading...
Searching...
No Matches
dfplayer.h
Go to the documentation of this file.
1#pragma once
2
6
7const size_t DFPLAYER_READ_BUFFER_LENGTH = 25; // two messages + some extra
8
9namespace esphome {
10namespace dfplayer {
11
13 NORMAL = 0,
14 POP = 1,
15 ROCK = 2,
16 JAZZ = 3,
18 BASS = 5,
19};
20
21enum Device {
22 USB = 1,
24};
25
26// See the datasheet here:
27// https://github.com/DFRobot/DFRobotDFPlayerMini/blob/master/doc/FN-M16P%2BEmbedded%2BMP3%2BAudio%2BModule%2BDatasheet.pdf
28class 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);
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};
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
83DFPLAYER_SIMPLE_ACTION(NextAction, next)
84DFPLAYER_SIMPLE_ACTION(PreviousAction, previous)
85
86template<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
96template<typename... Ts> class PlayFileAction : public Action<Ts...>, public Parented<DFPlayer> {
97 public:
98 TEMPLATABLE_VALUE(uint16_t, file)
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
112template<typename... Ts> class PlayFolderAction : public Action<Ts...>, public Parented<DFPlayer> {
113 public:
114 TEMPLATABLE_VALUE(uint16_t, folder)
115 TEMPLATABLE_VALUE(uint16_t, file)
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
130template<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
140template<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
150template<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
160DFPLAYER_SIMPLE_ACTION(SleepAction, sleep)
161DFPLAYER_SIMPLE_ACTION(ResetAction, reset)
162DFPLAYER_SIMPLE_ACTION(StartAction, start)
163DFPLAYER_SIMPLE_ACTION(PauseAction, pause)
164DFPLAYER_SIMPLE_ACTION(StopAction, stop)
165DFPLAYER_SIMPLE_ACTION(RandomAction, random)
166DFPLAYER_SIMPLE_ACTION(VolumeUpAction, volume_up)
167DFPLAYER_SIMPLE_ACTION(VolumeDownAction, volume_down)
168
169template<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
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
void trigger(Ts... x)
Definition automation.h:96
void dump_config() override
Definition dfplayer.cpp:270
CallbackManager< void()> on_finished_playback_callback_
Definition dfplayer.h:72
void send_cmd_(uint8_t cmd, uint16_t argument=0)
Definition dfplayer.cpp:118
void add_on_finished_playback_callback(std::function< void()> callback)
Definition dfplayer.h:54
void send_cmd_(uint8_t cmd, uint16_t high, uint16_t low)
Definition dfplayer.h:60
void set_volume(uint8_t volume)
Definition dfplayer.cpp:59
void play_folder(uint16_t folder, uint16_t file)
Definition dfplayer.cpp:105
void play_file_loop(uint16_t file)
Definition dfplayer.cpp:32
void set_device(Device device)
Definition dfplayer.cpp:54
void play_file(uint16_t file)
Definition dfplayer.cpp:26
void set_eq(EqPreset preset)
Definition dfplayer.cpp:64
char read_buffer_[DFPLAYER_READ_BUFFER_LENGTH]
Definition dfplayer.h:65
void play_folder_loop(uint16_t folder)
Definition dfplayer.cpp:38
void play_mp3(uint16_t file)
Definition dfplayer.cpp:20
TEMPLATABLE_VALUE(uint16_t, file) TEMPLATABLE_VALUE(bool
loop void play(Ts... x) override
Definition dfplayer.h:101
TEMPLATABLE_VALUE(uint16_t, folder) TEMPLATABLE_VALUE(uint16_t
TEMPLATABLE_VALUE(uint16_t, file) void play(Ts... x) override
Definition dfplayer.h:88
TEMPLATABLE_VALUE(Device, device) void play(Ts... x) override
Definition dfplayer.h:132
TEMPLATABLE_VALUE(EqPreset, eq) void play(Ts... x) override
Definition dfplayer.h:152
TEMPLATABLE_VALUE(uint8_t, volume) void play(Ts... x) override
Definition dfplayer.h:142
ClimatePreset preset
Definition climate.h:8
const size_t DFPLAYER_READ_BUFFER_LENGTH
Definition dfplayer.h:7
void loop()
uint16_t reset
Definition ina226.h:5
DFPLAYER_SIMPLE_ACTION(NextAction, next) DFPLAYER_SIMPLE_ACTION(PreviousAction
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5