-
Notifications
You must be signed in to change notification settings - Fork 7
/
DFPlayerMini.cpp
180 lines (159 loc) · 4.42 KB
/
DFPlayerMini.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "Arduino.h"
#include "DFPlayerMini.h"
DFPlayerMini::DFPlayerMini() {
}
DFPlayerMini::~DFPlayerMini() {
if (serial != NULL) {
delete serial;
}
}
void DFPlayerMini::init(int pinBusy, int pinReceive, int pinTransmit, CallbackMethod whileBusyMethod) {
if (serial != NULL) {
delete serial;
}
serial = new SoftwareSerial(pinReceive, pinTransmit);
serial->begin(9600);
this->pinBusy = pinBusy;
pinMode(pinBusy, INPUT_PULLUP);
this->whileBusyMethod = whileBusyMethod;
reset();
}
void DFPlayerMini::reset() {
#ifdef DEBUG
Serial.println(F("DFPlayer:reset()"));
#endif
send(0x0C);
if (!waitResponse(DFPLAYER_RESET_TIMEOUT, DFPLAYER_CODE_READY)) {
while (true) {
Serial.println(F("DFPlayer not responding - REBOOT"));
delay(5000);
}
}
}
void DFPlayerMini::playFile(int fileNumber, int folderNumber) {
#ifdef DFPLAYER_DEBUG
Serial.print(F("Sound "));
Serial.print(fileNumber);
Serial.print(F(" in "));
Serial.println(folderNumber);
#endif
if (folderNumber == 0) {
send(0x03, fileNumber);
} else {
send(0x0F, (folderNumber << 8) | fileNumber);
}
}
bool DFPlayerMini::playFileAndWait(int fileNumber, int folderNumber, int abortTriggerPin, unsigned long timeout) {
playFile(fileNumber, folderNumber);
return wait(abortTriggerPin, timeout);
}
void DFPlayerMini::loopFile(byte fileNumber, int folderNumber) {
playFile(fileNumber, folderNumber);
loop(true);
}
void DFPlayerMini::loop(bool repeat) {
send(0x19, !repeat);
}
bool DFPlayerMini::wait(int abortTriggerPin, unsigned long timeout) {
// Wait for Playing Done code up to timeout
return waitResponse(timeout, DFPLAYER_CODE_DONE, abortTriggerPin);
}
bool DFPlayerMini::isBusy() {
return digitalRead(pinBusy) == LOW;
}
void DFPlayerMini::setVolume(int volume) {
#ifdef DFPLAYER_DEBUG
Serial.print(F("volume="));
Serial.println(volume);
#endif
send(0x06, constrain(volume, DFPLAYER_MIN_VOLUME, DFPLAYER_MAX_VOLUME));
}
void DFPlayerMini::stop() {
send(0x16);
}
bool DFPlayerMini::send(uint8_t cmd, uint16_t argument) {
// Wait until enough delay from last write (otherwise the latest one it will be ignored)
while (((unsigned long) (millis() - lastSent)) < DFPLAYER_MIN_FROM_LAST) {
if (whileBusyMethod != NULL)
whileBusyMethod();
}
uint8_t message[10] =
{ 0x7E, 0xFF, 0x06, cmd, 0x01, (uint8_t) (argument >> 8), (uint8_t) argument, 0x00, 0x00, 0xEF };
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.print(millis());
Serial.print(F(" - Req: "));
#endif
uint16_t sum = 0;
for (int i = 1; i < 7; i++) {
sum -= message[i];
}
message[7] = (uint8_t) (sum >> 8);
message[8] = (uint8_t) sum;
for (byte i = 0; i < 10; i++) {
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.print(message[i], 16);
Serial.print(F(" "));
#endif
serial->write(message[i]);
if (whileBusyMethod != NULL)
whileBusyMethod();
}
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.println();
Serial.print(millis());
Serial.print(F(" - Res: "));
#endif
bool completed = waitResponse(DFPLAYER_RESPONSE_TIMEOUT, DFPLAYER_CODE_OK);
lastSent = millis();
return completed;
}
bool DFPlayerMini::waitResponse(unsigned long timeout, uint8_t code, int abortTriggerPin) {
unsigned long startTime = millis();
// Expected response: byte 0 means "any value"
uint8_t expectedResponse[10] = { 0x7E, 0xFF, 06, code, 0, 0, 0, 0, 0, 0xEF };
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.print(F("Expected resp: "));
for (int i = 0; i < 10; i++) {
Serial.print(expectedResponse[i], 16);
Serial.print(F(" "));
}
Serial.println();
#endif
int idx = 0;
// Repeat until timeout
while ((unsigned long) (millis() - startTime) <= timeout) {
if (whileBusyMethod != NULL)
whileBusyMethod();
// Read a byte (-1 if nothing found)
int b = serial->read();
if (b >= 0) {
// There was a byte read!
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.print(b, 16);
Serial.print(F(" "));
#endif
if (expectedResponse[idx] == 0 || b == expectedResponse[idx]) {
// If byte matches expected message (0 is ignored), then go to next byte
idx++;
} else {
// Else reset index
idx = 0;
}
if (idx == sizeof(expectedResponse)) {
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.print(F("Res in "));
Serial.println((unsigned long) (millis() - startTime));
#endif
// If all bytes read - return true
return true;
}
}
if (abortTriggerPin > 0 && digitalRead(abortTriggerPin) == LOW)
return false;
}
#ifdef DFPLAYER_DEBUG_HEAVY
Serial.print(F("No expected res in "));
Serial.println((unsigned long) (millis() - startTime));
#endif
return false;
}