2 #include <DFPlayerMini.h> 3 #include <MutilaDebug.h> 15 pinMode(_busyPin, INPUT_PULLUP);
22 void DFPlayerMini::resetSendBuf()
25 memset(&_sendBuf, 0,
sizeof(uint8_t) * BufferLength);
34 _sendBuf[PacketOffsetCmd] = (uint8_t)cmd;
35 copyBigend(_sendBuf+PacketOffsetArg, arg);
43 void dumpBuf(uint8_t* buf, uint8_t ptr,
bool ln=
true)
45 for (uint8_t i=0; i<DFPlayerMini::BufferLength; i++) {
58 for (uint8_t i=1; i<=tries; i++) {
59 _DB(F(
"DFPlayerMini::query try "));
70 _DB(F(
"DFPlayerMini::query FAILED all "));
83 uint8_t buf[BufferLength];
84 memset(buf, 0, BufferLength*
sizeof(uint8_t));
89 uint32_t startSend = Millis();
91 uint32_t startRecv = Millis();
96 if (MillisSince(startRecv) > ResponseTimeoutMs) {
102 while (_serial.available() > 0 && ptr < BufferLength) {
103 int c = _serial.read();
109 if (ptr == 0 && c != 0x7E) {
111 }
else if (ptr == 1 && c != 0xFF) {
117 buf[ptr++] = (uint8_t)c;
123 if (ptr == BufferLength) {
128 uint32_t durationRecv = MillisSince(startRecv);
131 dumpBuf(buf, ptr,
false);
133 _DB(MillisSince(startRecv, startSend));
142 response.messageType = buf[PacketOffsetCmd];
143 uint8_t* aptr = (uint8_t*)(&(response.arg));
144 aptr[0] = buf[PacketOffsetArg+1];
145 aptr[1] = buf[PacketOffsetArg];
148 uint16_t cksum = calculateChecksum(buf);
150 if (buf[0] != 0x7E || buf[1] != 0xFF || buf[9] != 0xEF) {
151 _DBLN(F(
"ERR: head/term"));
153 }
else if (*((uint8_t*)&cksum) != buf[PacketOffsetCkSum+1] || *(1+(uint8_t*)&cksum) != buf[PacketOffsetCkSum]) {
155 _DBLN(F(
"ERR: cksum"));
167 return !digitalRead(_busyPin);
174 void DFPlayerMini::copyBigend(uint8_t *dst, uint16_t value)
176 uint8_t* dp = (uint8_t*)dst;
177 uint8_t* vp = (uint8_t*)&value;
182 void DFPlayerMini::fillChecksum()
184 uint16_t checksum = calculateChecksum(_sendBuf);
185 copyBigend(_sendBuf+PacketOffsetCkSum, checksum);
188 uint16_t DFPlayerMini::calculateChecksum(uint8_t *thebuf) {
190 for (uint8_t i=1; i<PacketOffsetCkSum; i++) {
196 void DFPlayerMini::serialCmd()
200 while (MillisSince(_lastCmdSent) < MinimumTimeMs) {
204 for (uint8_t i=0; i<BufferLength; i++) {
206 _DB(_sendBuf[i],HEX);
209 _lastCmdSent = Millis();
210 for (uint8_t i=0; i<BufferLength; i++) {
211 _serial.write(_sendBuf[i]);
DFPlayerMini(Stream &serial, const uint8_t busyPin=0)
Indicates broken serial comms.
DFPResponse query(Cmd cmd, uint8_t tries=3)
Messaged received and is valid.
void sendCmd(Cmd cmd, uint16_t arg=0)
Get some meaningless status code.
Response was not validated (bad cksum, header etc)
Message timed out (DFPlayerMini::ResponseTimeoutMs ms passed)
void begin(bool bootWait=true)