ESPHome  2022.5.1
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::play_folder(uint16_t folder, uint16_t file) {
10  if (folder < 100 && file < 256) {
11  this->ack_set_is_playing_ = true;
12  this->send_cmd_(0x0F, (uint8_t) folder, (uint8_t) file);
13  } else if (folder <= 10 && file <= 1000) {
14  this->ack_set_is_playing_ = true;
15  this->send_cmd_(0x14, (((uint16_t) folder) << 12) | file);
16  } else {
17  ESP_LOGE(TAG, "Cannot play folder %d file %d.", folder, file);
18  }
19 }
20 
21 void DFPlayer::send_cmd_(uint8_t cmd, uint16_t argument) {
22  uint8_t buffer[10]{0x7e, 0xff, 0x06, cmd, 0x01, (uint8_t)(argument >> 8), (uint8_t) argument, 0x00, 0x00, 0xef};
23  uint16_t checksum = 0;
24  for (uint8_t i = 1; i < 7; i++)
25  checksum += buffer[i];
26  checksum = -checksum;
27  buffer[7] = checksum >> 8;
28  buffer[8] = (uint8_t) checksum;
29 
30  this->sent_cmd_ = cmd;
31 
32  ESP_LOGD(TAG, "Send Command %#02x arg %#04x", cmd, argument);
33  this->write_array(buffer, 10);
34 }
35 
37  // Read message
38  while (this->available()) {
39  uint8_t byte;
40  this->read_byte(&byte);
41 
43  this->read_pos_ = 0;
44 
45  switch (this->read_pos_) {
46  case 0: // Start mark
47  if (byte != 0x7E)
48  continue;
49  break;
50  case 1: // Version
51  if (byte != 0xFF) {
52  ESP_LOGW(TAG, "Expected Version 0xFF, got %#02x", byte);
53  this->read_pos_ = 0;
54  continue;
55  }
56  break;
57  case 2: // Buffer length
58  if (byte != 0x06) {
59  ESP_LOGW(TAG, "Expected Buffer length 0x06, got %#02x", byte);
60  this->read_pos_ = 0;
61  continue;
62  }
63  break;
64  case 9: // End byte
65  if (byte != 0xEF) {
66  ESP_LOGW(TAG, "Expected end byte 0xEF, got %#02x", byte);
67  this->read_pos_ = 0;
68  continue;
69  }
70  // Parse valid received command
71  uint8_t cmd = this->read_buffer_[3];
72  uint16_t argument = (this->read_buffer_[5] << 8) | this->read_buffer_[6];
73 
74  ESP_LOGV(TAG, "Received message cmd: %#02x arg %#04x", cmd, argument);
75 
76  switch (cmd) {
77  case 0x3A:
78  if (argument == 1) {
79  ESP_LOGI(TAG, "USB loaded");
80  } else if (argument == 2)
81  ESP_LOGI(TAG, "TF Card loaded");
82  break;
83  case 0x3B:
84  if (argument == 1) {
85  ESP_LOGI(TAG, "USB unloaded");
86  } else if (argument == 2)
87  ESP_LOGI(TAG, "TF Card unloaded");
88  break;
89  case 0x3F:
90  if (argument == 1) {
91  ESP_LOGI(TAG, "USB available");
92  } else if (argument == 2) {
93  ESP_LOGI(TAG, "TF Card available");
94  } else if (argument == 3) {
95  ESP_LOGI(TAG, "USB, TF Card available");
96  }
97  break;
98  case 0x40:
99  ESP_LOGV(TAG, "Nack");
100  this->ack_set_is_playing_ = false;
101  this->ack_reset_is_playing_ = false;
102  case 0x41:
103  ESP_LOGV(TAG, "Ack ok");
104  this->is_playing_ |= this->ack_set_is_playing_;
105  this->is_playing_ &= !this->ack_reset_is_playing_;
106  this->ack_set_is_playing_ = false;
107  this->ack_reset_is_playing_ = false;
108  break;
109  case 0x3D: // Playback finished
110  this->is_playing_ = false;
111  this->on_finished_playback_callback_.call();
112  break;
113  default:
114  ESP_LOGD(TAG, "Command %#02x arg %#04x", cmd, argument);
115  }
116  this->sent_cmd_ = 0;
117  this->read_pos_ = 0;
118  continue;
119  }
120  this->read_buffer_[this->read_pos_] = byte;
121  this->read_pos_++;
122  }
123 }
125  ESP_LOGCONFIG(TAG, "DFPlayer:");
126  this->check_uart_settings(9600);
127 }
128 
129 } // namespace dfplayer
130 } // namespace esphome
void loop() override
Definition: dfplayer.cpp:36
void send_cmd_(uint8_t cmd, uint16_t argument=0)
Definition: dfplayer.cpp:21
void dump_config() override
Definition: dfplayer.cpp:124
CallbackManager< void()> on_finished_playback_callback_
Definition: dfplayer.h:102
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
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:12
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:9
uint8_t checksum
Definition: bl0939.h:35
Definition: a4988.cpp:4
char read_buffer_[DFPLAYER_READ_BUFFER_LENGTH]
Definition: dfplayer.h:95
stm32_cmd_t * cmd
Definition: stm32flash.h:96