ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
dfplayer.cpp
Go to the documentation of this file.
1#include "dfplayer.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "dfplayer");
8
10 this->ack_set_is_playing_ = true;
11 ESP_LOGD(TAG, "Playing next track");
12 this->send_cmd_(0x01);
13}
14
16 this->ack_set_is_playing_ = true;
17 ESP_LOGD(TAG, "Playing previous track");
18 this->send_cmd_(0x02);
19}
20void DFPlayer::play_mp3(uint16_t file) {
21 this->ack_set_is_playing_ = true;
22 ESP_LOGD(TAG, "Playing file %d in mp3 folder", file);
23 this->send_cmd_(0x12, file);
24}
25
26void DFPlayer::play_file(uint16_t file) {
27 this->ack_set_is_playing_ = true;
28 ESP_LOGD(TAG, "Playing file %d", file);
29 this->send_cmd_(0x03, file);
30}
31
32void DFPlayer::play_file_loop(uint16_t file) {
33 this->ack_set_is_playing_ = true;
34 ESP_LOGD(TAG, "Playing file %d in loop", file);
35 this->send_cmd_(0x08, file);
36}
37
38void DFPlayer::play_folder_loop(uint16_t folder) {
39 this->ack_set_is_playing_ = true;
40 ESP_LOGD(TAG, "Playing folder %d in loop", folder);
41 this->send_cmd_(0x17, folder);
42}
43
45 ESP_LOGD(TAG, "Increasing volume");
46 this->send_cmd_(0x04);
47}
48
50 ESP_LOGD(TAG, "Decreasing volume");
51 this->send_cmd_(0x05);
52}
53
55 ESP_LOGD(TAG, "Setting device to %d", device);
56 this->send_cmd_(0x09, device);
57}
58
59void DFPlayer::set_volume(uint8_t volume) {
60 ESP_LOGD(TAG, "Setting volume to %d", volume);
61 this->send_cmd_(0x06, volume);
62}
63
65 ESP_LOGD(TAG, "Setting EQ to %d", preset);
66 this->send_cmd_(0x07, preset);
67}
68
70 this->ack_reset_is_playing_ = true;
71 ESP_LOGD(TAG, "Putting DFPlayer to sleep");
72 this->send_cmd_(0x0A);
73}
74
76 this->ack_reset_is_playing_ = true;
77 ESP_LOGD(TAG, "Resetting DFPlayer");
78 this->send_cmd_(0x0C);
79}
80
82 this->ack_set_is_playing_ = true;
83 ESP_LOGD(TAG, "Starting playback");
84 this->send_cmd_(0x0D);
85}
86
88 this->ack_reset_is_playing_ = true;
89 ESP_LOGD(TAG, "Pausing playback");
90 this->send_cmd_(0x0E);
91}
92
94 this->ack_reset_is_playing_ = true;
95 ESP_LOGD(TAG, "Stopping playback");
96 this->send_cmd_(0x16);
97}
98
100 this->ack_set_is_playing_ = true;
101 ESP_LOGD(TAG, "Playing random file");
102 this->send_cmd_(0x18);
103}
104
106 uint16_t arg = enable ? 0x00 : 0x01;
107 ESP_LOGD(TAG, "Setting current track repeat to %s",
108 enable ? LOG_STR_LITERAL("enabled") : LOG_STR_LITERAL("disabled"));
109 this->send_cmd_(0x19, arg);
110}
111
112void DFPlayer::play_folder(uint16_t folder, uint16_t file) {
113 ESP_LOGD(TAG, "Playing file %d in folder %d", file, folder);
114 if (folder < 100 && file < 256) {
115 this->ack_set_is_playing_ = true;
116 this->send_cmd_(0x0F, (uint8_t) folder, (uint8_t) file);
117 } else if (folder <= 15 && file <= 3000) {
118 this->ack_set_is_playing_ = true;
119 this->send_cmd_(0x14, (((uint16_t) folder) << 12) | file);
120 } else {
121 ESP_LOGE(TAG, "Cannot play folder %d file %d.", folder, file);
122 }
123}
124
125void DFPlayer::send_cmd_(uint8_t cmd, uint16_t argument) {
126 uint8_t buffer[10]{0x7e, 0xff, 0x06, cmd, 0x01, (uint8_t) (argument >> 8), (uint8_t) argument, 0x00, 0x00, 0xef};
127 const uint16_t checksum = static_cast<uint16_t>(-sum16_bytes(buffer + 1, 6));
128 buffer[7] = checksum >> 8;
129 buffer[8] = (uint8_t) checksum;
130
131 this->sent_cmd_ = cmd;
132
133 ESP_LOGV(TAG, "Send Command %#02x arg %#04x", cmd, argument);
134 this->write_array(buffer, 10);
135}
136
138 this->read_available([this](uint8_t byte) {
140 this->read_pos_ = 0;
141
142 switch (this->read_pos_) {
143 case 0: // Start mark
144 if (byte != 0x7E)
145 return;
146 break;
147 case 1: // Version
148 if (byte != 0xFF) {
149 ESP_LOGW(TAG, "Expected Version 0xFF, got %#02x", byte);
150 this->read_pos_ = 0;
151 return;
152 }
153 break;
154 case 2: // Buffer length
155 if (byte != 0x06) {
156 ESP_LOGW(TAG, "Expected Buffer length 0x06, got %#02x", byte);
157 this->read_pos_ = 0;
158 return;
159 }
160 break;
161 case 9: // End byte
162#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
163 char byte_sequence[100];
164 byte_sequence[0] = '\0';
165 for (size_t i = 0; i < this->read_pos_ + 1; ++i) {
166 snprintf(byte_sequence + strlen(byte_sequence), sizeof(byte_sequence) - strlen(byte_sequence), "%02X ",
167 this->read_buffer_[i]);
168 }
169 ESP_LOGVV(TAG, "Received byte sequence: %s", byte_sequence);
170#endif
171 if (byte != 0xEF) {
172 ESP_LOGW(TAG, "Expected end byte 0xEF, got %#02x", byte);
173 this->read_pos_ = 0;
174 return;
175 }
176 // Parse valid received command
177 uint8_t cmd = this->read_buffer_[3];
178 uint16_t argument = (this->read_buffer_[5] << 8) | this->read_buffer_[6];
179
180 ESP_LOGV(TAG, "Received message cmd: %#02x arg %#04x", cmd, argument);
181
182 switch (cmd) {
183 case 0x3A:
184 if (argument == 1) {
185 ESP_LOGI(TAG, "USB loaded");
186 } else if (argument == 2) {
187 ESP_LOGI(TAG, "TF Card loaded");
188 }
189 break;
190 case 0x3B:
191 if (argument == 1) {
192 ESP_LOGI(TAG, "USB unloaded");
193 } else if (argument == 2) {
194 ESP_LOGI(TAG, "TF Card unloaded");
195 }
196 break;
197 case 0x3F:
198 if (argument == 1) {
199 ESP_LOGI(TAG, "USB available");
200 } else if (argument == 2) {
201 ESP_LOGI(TAG, "TF Card available");
202 } else if (argument == 3) {
203 ESP_LOGI(TAG, "USB, TF Card available");
204 }
205 break;
206 case 0x40:
207 ESP_LOGV(TAG, "Nack");
208 this->ack_set_is_playing_ = false;
209 this->ack_reset_is_playing_ = false;
210 switch (argument) {
211 case 0x01:
212 ESP_LOGE(TAG, "Module is busy or uninitialized");
213 break;
214 case 0x02:
215 ESP_LOGE(TAG, "Module is in sleep mode");
216 break;
217 case 0x03:
218 ESP_LOGE(TAG, "Serial receive error");
219 break;
220 case 0x04:
221 ESP_LOGE(TAG, "Checksum incorrect");
222 break;
223 case 0x05:
224 ESP_LOGE(TAG, "Specified track is out of current track scope");
225 this->is_playing_ = false;
226 break;
227 case 0x06:
228 ESP_LOGE(TAG, "Specified track is not found");
229 this->is_playing_ = false;
230 break;
231 case 0x07:
232 ESP_LOGE(TAG, "Insertion error (an inserting operation only can be done when a track is being played)");
233 break;
234 case 0x08:
235 ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
236 break;
237 case 0x09:
238 ESP_LOGE(TAG, "Entered into sleep mode");
239 this->is_playing_ = false;
240 break;
241 default:
242 break;
243 }
244 break;
245 case 0x41:
246 ESP_LOGV(TAG, "Ack ok");
247 this->is_playing_ |= this->ack_set_is_playing_;
248 this->is_playing_ &= !this->ack_reset_is_playing_;
249 this->ack_set_is_playing_ = false;
250 this->ack_reset_is_playing_ = false;
251 break;
252 case 0x3C:
253 ESP_LOGV(TAG, "Playback finished (USB drive)");
254 this->is_playing_ = false;
256 break;
257 case 0x3D:
258 ESP_LOGV(TAG, "Playback finished (SD card)");
259 this->is_playing_ = false;
261 break;
262 default:
263 ESP_LOGE(TAG, "Received unknown cmd %#02x arg %#04x", cmd, argument);
264 }
265 this->sent_cmd_ = 0;
266 this->read_pos_ = 0;
267 return;
268 }
269 this->read_buffer_[this->read_pos_] = byte;
270 this->read_pos_++;
271 });
272}
273void DFPlayer::dump_config() { ESP_LOGCONFIG(TAG, "DFPlayer:"); }
274
275} // namespace esphome::dfplayer
uint8_t checksum
Definition bl0906.h:3
void dump_config() override
Definition dfplayer.cpp:273
CallbackManager< void()> on_finished_playback_callback_
Definition dfplayer.h:77
void send_cmd_(uint8_t cmd, uint16_t argument=0)
Definition dfplayer.cpp:125
void set_volume(uint8_t volume)
Definition dfplayer.cpp:59
void play_folder(uint16_t folder, uint16_t file)
Definition dfplayer.cpp:112
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_current_track_repeat(bool enable)
Definition dfplayer.cpp:105
void set_eq(EqPreset preset)
Definition dfplayer.cpp:64
char read_buffer_[DFPLAYER_READ_BUFFER_LENGTH]
Definition dfplayer.h:70
void play_folder_loop(uint16_t folder)
Definition dfplayer.cpp:38
void play_mp3(uint16_t file)
Definition dfplayer.cpp:20
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
ClimatePreset preset
Definition climate.h:8
const size_t DFPLAYER_READ_BUFFER_LENGTH
Definition dfplayer.h:7
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "dfplayer")
constexpr uint16_t sum16_bytes(const uint8_t *data, size_t len, uint16_t sum=0)
Add up the bytes of data with size len, truncated to 16 bits.
Definition helpers.h:944