ESPHome 2026.5.0b1
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
7static const char *const 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
105void DFPlayer::play_folder(uint16_t folder, uint16_t file) {
106 ESP_LOGD(TAG, "Playing file %d in folder %d", file, folder);
107 if (folder < 100 && file < 256) {
108 this->ack_set_is_playing_ = true;
109 this->send_cmd_(0x0F, (uint8_t) folder, (uint8_t) file);
110 } else if (folder <= 15 && file <= 3000) {
111 this->ack_set_is_playing_ = true;
112 this->send_cmd_(0x14, (((uint16_t) folder) << 12) | file);
113 } else {
114 ESP_LOGE(TAG, "Cannot play folder %d file %d.", folder, file);
115 }
116}
117
118void DFPlayer::send_cmd_(uint8_t cmd, uint16_t argument) {
119 uint8_t buffer[10]{0x7e, 0xff, 0x06, cmd, 0x01, (uint8_t) (argument >> 8), (uint8_t) argument, 0x00, 0x00, 0xef};
120 uint16_t checksum = 0;
121 for (uint8_t i = 1; i < 7; i++)
122 checksum += buffer[i];
124 buffer[7] = checksum >> 8;
125 buffer[8] = (uint8_t) checksum;
126
127 this->sent_cmd_ = cmd;
128
129 ESP_LOGV(TAG, "Send Command %#02x arg %#04x", cmd, argument);
130 this->write_array(buffer, 10);
131}
132
134 // Read all available bytes in batches to reduce UART call overhead.
135 size_t avail = this->available();
136 uint8_t buf[64];
137 while (avail > 0) {
138 size_t to_read = std::min(avail, sizeof(buf));
139 if (!this->read_array(buf, to_read)) {
140 break;
141 }
142 avail -= to_read;
143 for (size_t bi = 0; bi < to_read; bi++) {
144 uint8_t byte = buf[bi];
145
147 this->read_pos_ = 0;
148
149 switch (this->read_pos_) {
150 case 0: // Start mark
151 if (byte != 0x7E)
152 continue;
153 break;
154 case 1: // Version
155 if (byte != 0xFF) {
156 ESP_LOGW(TAG, "Expected Version 0xFF, got %#02x", byte);
157 this->read_pos_ = 0;
158 continue;
159 }
160 break;
161 case 2: // Buffer length
162 if (byte != 0x06) {
163 ESP_LOGW(TAG, "Expected Buffer length 0x06, got %#02x", byte);
164 this->read_pos_ = 0;
165 continue;
166 }
167 break;
168 case 9: // End byte
169#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
170 char byte_sequence[100];
171 byte_sequence[0] = '\0';
172 for (size_t i = 0; i < this->read_pos_ + 1; ++i) {
173 snprintf(byte_sequence + strlen(byte_sequence), sizeof(byte_sequence) - strlen(byte_sequence), "%02X ",
174 this->read_buffer_[i]);
175 }
176 ESP_LOGVV(TAG, "Received byte sequence: %s", byte_sequence);
177#endif
178 if (byte != 0xEF) {
179 ESP_LOGW(TAG, "Expected end byte 0xEF, got %#02x", byte);
180 this->read_pos_ = 0;
181 continue;
182 }
183 // Parse valid received command
184 uint8_t cmd = this->read_buffer_[3];
185 uint16_t argument = (this->read_buffer_[5] << 8) | this->read_buffer_[6];
186
187 ESP_LOGV(TAG, "Received message cmd: %#02x arg %#04x", cmd, argument);
188
189 switch (cmd) {
190 case 0x3A:
191 if (argument == 1) {
192 ESP_LOGI(TAG, "USB loaded");
193 } else if (argument == 2) {
194 ESP_LOGI(TAG, "TF Card loaded");
195 }
196 break;
197 case 0x3B:
198 if (argument == 1) {
199 ESP_LOGI(TAG, "USB unloaded");
200 } else if (argument == 2) {
201 ESP_LOGI(TAG, "TF Card unloaded");
202 }
203 break;
204 case 0x3F:
205 if (argument == 1) {
206 ESP_LOGI(TAG, "USB available");
207 } else if (argument == 2) {
208 ESP_LOGI(TAG, "TF Card available");
209 } else if (argument == 3) {
210 ESP_LOGI(TAG, "USB, TF Card available");
211 }
212 break;
213 case 0x40:
214 ESP_LOGV(TAG, "Nack");
215 this->ack_set_is_playing_ = false;
216 this->ack_reset_is_playing_ = false;
217 switch (argument) {
218 case 0x01:
219 ESP_LOGE(TAG, "Module is busy or uninitialized");
220 break;
221 case 0x02:
222 ESP_LOGE(TAG, "Module is in sleep mode");
223 break;
224 case 0x03:
225 ESP_LOGE(TAG, "Serial receive error");
226 break;
227 case 0x04:
228 ESP_LOGE(TAG, "Checksum incorrect");
229 break;
230 case 0x05:
231 ESP_LOGE(TAG, "Specified track is out of current track scope");
232 this->is_playing_ = false;
233 break;
234 case 0x06:
235 ESP_LOGE(TAG, "Specified track is not found");
236 this->is_playing_ = false;
237 break;
238 case 0x07:
239 ESP_LOGE(TAG,
240 "Insertion error (an inserting operation only can be done when a track is being played)");
241 break;
242 case 0x08:
243 ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
244 break;
245 case 0x09:
246 ESP_LOGE(TAG, "Entered into sleep mode");
247 this->is_playing_ = false;
248 break;
249 }
250 break;
251 case 0x41:
252 ESP_LOGV(TAG, "Ack ok");
253 this->is_playing_ |= this->ack_set_is_playing_;
254 this->is_playing_ &= !this->ack_reset_is_playing_;
255 this->ack_set_is_playing_ = false;
256 this->ack_reset_is_playing_ = false;
257 break;
258 case 0x3C:
259 ESP_LOGV(TAG, "Playback finished (USB drive)");
260 this->is_playing_ = false;
262 break;
263 case 0x3D:
264 ESP_LOGV(TAG, "Playback finished (SD card)");
265 this->is_playing_ = false;
267 break;
268 default:
269 ESP_LOGE(TAG, "Received unknown cmd %#02x arg %#04x", cmd, argument);
270 }
271 this->sent_cmd_ = 0;
272 this->read_pos_ = 0;
273 continue;
274 }
275 this->read_buffer_[this->read_pos_] = byte;
276 this->read_pos_++;
277 }
278 }
279}
281 ESP_LOGCONFIG(TAG, "DFPlayer:");
282 this->check_uart_settings(9600);
283}
284
285} // namespace esphome::dfplayer
uint8_t checksum
Definition bl0906.h:3
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 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
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
ClimatePreset preset
Definition climate.h:8
const size_t DFPLAYER_READ_BUFFER_LENGTH
Definition dfplayer.h:7