8static const char *
const TAG =
"dfplayer";
12 ESP_LOGD(TAG,
"Playing next track");
18 ESP_LOGD(TAG,
"Playing previous track");
23 ESP_LOGD(TAG,
"Playing file %d in mp3 folder", file);
29 ESP_LOGD(TAG,
"Playing file %d", file);
35 ESP_LOGD(TAG,
"Playing file %d in loop", file);
41 ESP_LOGD(TAG,
"Playing folder %d in loop", folder);
46 ESP_LOGD(TAG,
"Increasing volume");
51 ESP_LOGD(TAG,
"Decreasing volume");
56 ESP_LOGD(TAG,
"Setting device to %d", device);
61 ESP_LOGD(TAG,
"Setting volume to %d", volume);
66 ESP_LOGD(TAG,
"Setting EQ to %d",
preset);
72 ESP_LOGD(TAG,
"Putting DFPlayer to sleep");
78 ESP_LOGD(TAG,
"Resetting DFPlayer");
84 ESP_LOGD(TAG,
"Starting playback");
90 ESP_LOGD(TAG,
"Pausing playback");
96 ESP_LOGD(TAG,
"Stopping playback");
102 ESP_LOGD(TAG,
"Playing random file");
107 ESP_LOGD(TAG,
"Playing file %d in folder %d", file, folder);
108 if (folder < 100 && file < 256) {
110 this->
send_cmd_(0x0F, (uint8_t) folder, (uint8_t) file);
111 }
else if (folder <= 15 && file <= 3000) {
113 this->
send_cmd_(0x14, (((uint16_t) folder) << 12) | file);
115 ESP_LOGE(TAG,
"Cannot play folder %d file %d.", folder, file);
120 uint8_t buffer[10]{0x7e, 0xff, 0x06, cmd, 0x01, (uint8_t) (argument >> 8), (uint8_t) argument, 0x00, 0x00, 0xef};
122 for (uint8_t i = 1; i < 7; i++)
130 ESP_LOGV(TAG,
"Send Command %#02x arg %#04x", cmd, argument);
139 size_t to_read = std::min(avail,
sizeof(buf));
144 for (
size_t bi = 0; bi < to_read; bi++) {
145 uint8_t
byte = buf[bi];
157 ESP_LOGW(TAG,
"Expected Version 0xFF, got %#02x",
byte);
164 ESP_LOGW(TAG,
"Expected Buffer length 0x06, got %#02x",
byte);
170#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
171 char byte_sequence[100];
172 byte_sequence[0] =
'\0';
173 for (
size_t i = 0; i < this->
read_pos_ + 1; ++i) {
174 snprintf(byte_sequence + strlen(byte_sequence),
sizeof(byte_sequence) - strlen(byte_sequence),
"%02X ",
177 ESP_LOGVV(TAG,
"Received byte sequence: %s", byte_sequence);
180 ESP_LOGW(TAG,
"Expected end byte 0xEF, got %#02x",
byte);
188 ESP_LOGV(TAG,
"Received message cmd: %#02x arg %#04x", cmd, argument);
193 ESP_LOGI(TAG,
"USB loaded");
194 }
else if (argument == 2) {
195 ESP_LOGI(TAG,
"TF Card loaded");
200 ESP_LOGI(TAG,
"USB unloaded");
201 }
else if (argument == 2) {
202 ESP_LOGI(TAG,
"TF Card unloaded");
207 ESP_LOGI(TAG,
"USB available");
208 }
else if (argument == 2) {
209 ESP_LOGI(TAG,
"TF Card available");
210 }
else if (argument == 3) {
211 ESP_LOGI(TAG,
"USB, TF Card available");
215 ESP_LOGV(TAG,
"Nack");
220 ESP_LOGE(TAG,
"Module is busy or uninitialized");
223 ESP_LOGE(TAG,
"Module is in sleep mode");
226 ESP_LOGE(TAG,
"Serial receive error");
229 ESP_LOGE(TAG,
"Checksum incorrect");
232 ESP_LOGE(TAG,
"Specified track is out of current track scope");
236 ESP_LOGE(TAG,
"Specified track is not found");
241 "Insertion error (an inserting operation only can be done when a track is being played)");
244 ESP_LOGE(TAG,
"SD card reading failed (SD card pulled out or damaged)");
247 ESP_LOGE(TAG,
"Entered into sleep mode");
253 ESP_LOGV(TAG,
"Ack ok");
260 ESP_LOGV(TAG,
"Playback finished (USB drive)");
264 ESP_LOGV(TAG,
"Playback finished (SD card)");
269 ESP_LOGE(TAG,
"Received unknown cmd %#02x arg %#04x", cmd, argument);
281 ESP_LOGCONFIG(TAG,
"DFPlayer:");
void dump_config() override
CallbackManager< void()> on_finished_playback_callback_
void send_cmd_(uint8_t cmd, uint16_t argument=0)
void set_volume(uint8_t volume)
void play_folder(uint16_t folder, uint16_t file)
void play_file_loop(uint16_t file)
void set_device(Device device)
void play_file(uint16_t file)
bool ack_reset_is_playing_
void set_eq(EqPreset preset)
char read_buffer_[DFPLAYER_READ_BUFFER_LENGTH]
void play_folder_loop(uint16_t folder)
void play_mp3(uint16_t file)
optional< std::array< uint8_t, N > > read_array()
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...
void write_array(const uint8_t *data, size_t len)
const size_t DFPLAYER_READ_BUFFER_LENGTH
Providing packet encoding functions for exchanging data with a remote host.