ESPHome  2024.12.2
dfplayer.cpp
Go to the documentation of this file.
1 #include "dfplayer.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace dfplayer {
6 
7 static const char *const TAG = "dfplayer";
8 
9 void DFPlayer::next() {
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 }
20 void 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 
26 void 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 
32 void 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 
38 void 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 
59 void 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 
105 void 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 
118 void 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];
123  checksum = -checksum;
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 message
135  while (this->available()) {
136  uint8_t byte;
137  this->read_byte(&byte);
138 
140  this->read_pos_ = 0;
141 
142  switch (this->read_pos_) {
143  case 0: // Start mark
144  if (byte != 0x7E)
145  continue;
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  continue;
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  continue;
159  }
160  break;
161  case 9: // End byte
162  if (byte != 0xEF) {
163  ESP_LOGW(TAG, "Expected end byte 0xEF, got %#02x", byte);
164  this->read_pos_ = 0;
165  continue;
166  }
167  // Parse valid received command
168  uint8_t cmd = this->read_buffer_[3];
169  uint16_t argument = (this->read_buffer_[5] << 8) | this->read_buffer_[6];
170 
171  ESP_LOGV(TAG, "Received message cmd: %#02x arg %#04x", cmd, argument);
172 
173  switch (cmd) {
174  case 0x3A:
175  if (argument == 1) {
176  ESP_LOGI(TAG, "USB loaded");
177  } else if (argument == 2) {
178  ESP_LOGI(TAG, "TF Card loaded");
179  }
180  break;
181  case 0x3B:
182  if (argument == 1) {
183  ESP_LOGI(TAG, "USB unloaded");
184  } else if (argument == 2) {
185  ESP_LOGI(TAG, "TF Card unloaded");
186  }
187  break;
188  case 0x3F:
189  if (argument == 1) {
190  ESP_LOGI(TAG, "USB available");
191  } else if (argument == 2) {
192  ESP_LOGI(TAG, "TF Card available");
193  } else if (argument == 3) {
194  ESP_LOGI(TAG, "USB, TF Card available");
195  }
196  break;
197  case 0x40:
198  ESP_LOGV(TAG, "Nack");
199  this->ack_set_is_playing_ = false;
200  this->ack_reset_is_playing_ = false;
201  switch (argument) {
202  case 0x01:
203  ESP_LOGE(TAG, "Module is busy or uninitialized");
204  break;
205  case 0x02:
206  ESP_LOGE(TAG, "Module is in sleep mode");
207  break;
208  case 0x03:
209  ESP_LOGE(TAG, "Serial receive error");
210  break;
211  case 0x04:
212  ESP_LOGE(TAG, "Checksum incorrect");
213  break;
214  case 0x05:
215  ESP_LOGE(TAG, "Specified track is out of current track scope");
216  this->is_playing_ = false;
217  break;
218  case 0x06:
219  ESP_LOGE(TAG, "Specified track is not found");
220  this->is_playing_ = false;
221  break;
222  case 0x07:
223  ESP_LOGE(TAG, "Insertion error (an inserting operation only can be done when a track is being played)");
224  break;
225  case 0x08:
226  ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
227  break;
228  case 0x09:
229  ESP_LOGE(TAG, "Entered into sleep mode");
230  this->is_playing_ = false;
231  break;
232  }
233  break;
234  case 0x41:
235  ESP_LOGV(TAG, "Ack ok");
236  this->is_playing_ |= this->ack_set_is_playing_;
237  this->is_playing_ &= !this->ack_reset_is_playing_;
238  this->ack_set_is_playing_ = false;
239  this->ack_reset_is_playing_ = false;
240  break;
241  case 0x3D:
242  ESP_LOGV(TAG, "Playback finished");
243  this->is_playing_ = false;
244  this->on_finished_playback_callback_.call();
245  break;
246  default:
247  ESP_LOGV(TAG, "Received unknown cmd %#02x arg %#04x", cmd, argument);
248  }
249  this->sent_cmd_ = 0;
250  this->read_pos_ = 0;
251  continue;
252  }
253  this->read_buffer_[this->read_pos_] = byte;
254  this->read_pos_++;
255  }
256 }
258  ESP_LOGCONFIG(TAG, "DFPlayer:");
259  this->check_uart_settings(9600);
260 }
261 
262 } // namespace dfplayer
263 } // namespace esphome
void send_cmd_(uint8_t cmd, uint16_t argument=0)
Definition: dfplayer.cpp:118
void dump_config() override
Definition: dfplayer.cpp:257
CallbackManager< void()> on_finished_playback_callback_
Definition: dfplayer.h:72
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
void play_file_loop(uint16_t file)
Definition: dfplayer.cpp:32
uint8_t checksum
Definition: bl0906.h:210
void set_device(Device device)
Definition: dfplayer.cpp:54
void play_file(uint16_t file)
Definition: dfplayer.cpp:26
void play_folder_loop(uint16_t folder)
Definition: dfplayer.cpp: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:13
bool read_byte(uint8_t *data)
Definition: uart.h:29
const size_t DFPLAYER_READ_BUFFER_LENGTH
Definition: dfplayer.h:7
void play_folder(uint16_t folder, uint16_t file)
Definition: dfplayer.cpp:105
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_eq(EqPreset preset)
Definition: dfplayer.cpp:64
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
ClimatePreset preset
Definition: climate.h:578
stm32_cmd_t * cmd
Definition: stm32flash.h:96