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