Skip to content

Commit 8b4ba4f

Browse files
committed
update to 1.0.3
* Fix the bug that read cannot get the right result. * Add USB support * Add more example.
1 parent c295afa commit 8b4ba4f

File tree

9 files changed

+626
-38
lines changed

9 files changed

+626
-38
lines changed

DFRobotDFPlayerMini.cpp

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @copyright GNU Lesser General Public License
88
*
99
* @author [Angelo]([email protected])
10-
* @version V1.0
10+
* @version V1.0.3
1111
* @date 2016-12-07
1212
*/
1313

@@ -31,15 +31,12 @@ uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){
3131
}
3232

3333
void DFRobotDFPlayerMini::sendStack(){
34-
if (_sending[Stack_ACK]) {
34+
if (_sending[Stack_ACK]) { //if the ack mode is on wait until the last transmition
3535
while (_isSending) {
3636
delay(0);
3737
available();
3838
}
3939
}
40-
else{
41-
delay(10);
42-
}
4340

4441
#ifdef _DEBUG
4542
Serial.println();
@@ -53,6 +50,10 @@ void DFRobotDFPlayerMini::sendStack(){
5350
_serial->write(_sending, DFPLAYER_SEND_LENGTH);
5451
_timeOutTimer = millis();
5552
_isSending = _sending[Stack_ACK];
53+
54+
if (!_sending[Stack_ACK]) { //if the ack mode is off wait 10 ms after one transmition.
55+
delay(10);
56+
}
5657
}
5758

5859
void DFRobotDFPlayerMini::sendStack(uint8_t command){
@@ -80,36 +81,41 @@ void DFRobotDFPlayerMini::disableACK(){
8081
_sending[Stack_ACK] = 0x00;
8182
}
8283

83-
bool DFRobotDFPlayerMini::waitAvailable(){
84-
_isSending = true;
84+
bool DFRobotDFPlayerMini::waitAvailable(unsigned long duration){
85+
unsigned long timer = millis();
86+
if (!duration) {
87+
duration = _timeOutDuration;
88+
}
8589
while (!available()){
90+
if (millis() - timer > duration) {
91+
return false;
92+
}
8693
delay(0);
8794
}
88-
return _handleType != TimeOut;
95+
return true;
8996
}
9097

9198
bool DFRobotDFPlayerMini::begin(Stream &stream, bool isACK, bool doReset){
99+
_serial = &stream;
100+
92101
if (isACK) {
93102
enableACK();
94103
}
95104
else{
96105
disableACK();
97106
}
98107

99-
_serial = &stream;
100-
101108
if (doReset) {
102-
_timeOutDuration += 3000;
103109
reset();
104-
waitAvailable();
105-
_timeOutDuration -= 3000;
110+
waitAvailable(2000);
106111
delay(200);
107-
} else {
112+
}
113+
else {
108114
// assume same state as with reset(): online
109115
_handleType = DFPlayerCardOnline;
110116
}
111117

112-
return (readType() == DFPlayerCardOnline) || !isACK;
118+
return (readType() == DFPlayerCardOnline) || (readType() == DFPlayerUSBOnline) || !isACK;
113119
}
114120

115121
uint8_t DFRobotDFPlayerMini::readType(){
@@ -142,34 +148,49 @@ uint8_t DFRobotDFPlayerMini::readCommand(){
142148
}
143149

144150
void DFRobotDFPlayerMini::parseStack(){
145-
_handleCommand = *(_received + Stack_Command);
151+
uint8_t handleCommand = *(_received + Stack_Command);
152+
if (handleCommand == 0x41) { //handle the 0x41 ack feedback as a spcecial case, in case the pollusion of _handleCommand, _handleParameter, and _handleType.
153+
_isSending = false;
154+
return;
155+
}
156+
157+
_handleCommand = handleCommand;
146158
_handleParameter = arrayToUint16(_received + Stack_Parameter);
147159

148160
switch (_handleCommand) {
149161
case 0x3D:
150162
handleMessage(DFPlayerPlayFinished, _handleParameter);
151163
break;
152164
case 0x3F:
153-
if (_handleParameter & 0x02) {
165+
if (_handleParameter & 0x01) {
166+
handleMessage(DFPlayerUSBOnline, _handleParameter);
167+
}
168+
else if (_handleParameter & 0x02) {
154169
handleMessage(DFPlayerCardOnline, _handleParameter);
155170
}
171+
else if (_handleParameter & 0x03) {
172+
handleMessage(DFPlayerCardUSBOnline, _handleParameter);
173+
}
156174
break;
157175
case 0x3A:
158-
if (_handleParameter & 0x02) {
176+
if (_handleParameter & 0x01) {
177+
handleMessage(DFPlayerUSBInserted, _handleParameter);
178+
}
179+
else if (_handleParameter & 0x02) {
159180
handleMessage(DFPlayerCardInserted, _handleParameter);
160181
}
161182
break;
162183
case 0x3B:
163-
if (_handleParameter & 0x02) {
184+
if (_handleParameter & 0x01) {
185+
handleMessage(DFPlayerUSBRemoved, _handleParameter);
186+
}
187+
else if (_handleParameter & 0x02) {
164188
handleMessage(DFPlayerCardRemoved, _handleParameter);
165189
}
166190
break;
167191
case 0x40:
168192
handleMessage(DFPlayerError, _handleParameter);
169193
break;
170-
case 0x41:
171-
_isSending = false;
172-
break;
173194
case 0x3C:
174195
case 0x3E:
175196
case 0x42:
@@ -185,7 +206,7 @@ void DFRobotDFPlayerMini::parseStack(){
185206
case 0x4D:
186207
case 0x4E:
187208
case 0x4F:
188-
_isAvailable = true;
209+
handleMessage(DFPlayerFeedBack, _handleParameter);
189210
break;
190211
default:
191212
handleError(WrongStack);
@@ -215,7 +236,6 @@ bool DFRobotDFPlayerMini::available(){
215236
Serial.print(F(" "));
216237
#endif
217238
if (_received[Stack_Header] == 0x7E) {
218-
_isAvailable = false;
219239
_receivedIndex ++;
220240
}
221241
}
@@ -247,9 +267,6 @@ bool DFRobotDFPlayerMini::available(){
247267
if (validateStack()) {
248268
_receivedIndex = 0;
249269
parseStack();
250-
if (_isAvailable && !_sending[Stack_ACK]) {
251-
_isSending = false;
252-
}
253270
return _isAvailable;
254271
}
255272
else{
@@ -387,7 +404,12 @@ void DFRobotDFPlayerMini::disableDAC(){
387404
int DFRobotDFPlayerMini::readState(){
388405
sendStack(0x42);
389406
if (waitAvailable()) {
390-
return read();
407+
if (readType() == DFPlayerFeedBack) {
408+
return read();
409+
}
410+
else{
411+
return -1;
412+
}
391413
}
392414
else{
393415
return -1;
@@ -404,10 +426,15 @@ int DFRobotDFPlayerMini::readVolume(){
404426
}
405427
}
406428

407-
uint8_t DFRobotDFPlayerMini::readEQ(){
429+
int DFRobotDFPlayerMini::readEQ(){
408430
sendStack(0x44);
409431
if (waitAvailable()) {
410-
return read();
432+
if (readType() == DFPlayerFeedBack) {
433+
return read();
434+
}
435+
else{
436+
return -1;
437+
}
411438
}
412439
else{
413440
return -1;
@@ -430,7 +457,12 @@ int DFRobotDFPlayerMini::readFileCounts(uint8_t device){
430457
}
431458

432459
if (waitAvailable()) {
433-
return read();
460+
if (readType() == DFPlayerFeedBack) {
461+
return read();
462+
}
463+
else{
464+
return -1;
465+
}
434466
}
435467
else{
436468
return -1;
@@ -452,7 +484,12 @@ int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
452484
break;
453485
}
454486
if (waitAvailable()) {
455-
return read();
487+
if (readType() == DFPlayerFeedBack) {
488+
return read();
489+
}
490+
else{
491+
return -1;
492+
}
456493
}
457494
else{
458495
return -1;
@@ -462,7 +499,12 @@ int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
462499
int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
463500
sendStack(0x4E, folderNumber);
464501
if (waitAvailable()) {
465-
return read();
502+
if (readType() == DFPlayerFeedBack) {
503+
return read();
504+
}
505+
else{
506+
return -1;
507+
}
466508
}
467509
else{
468510
return -1;
@@ -472,7 +514,12 @@ int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
472514
int DFRobotDFPlayerMini::readFolderCounts(){
473515
sendStack(0x4F);
474516
if (waitAvailable()) {
475-
return read();
517+
if (readType() == DFPlayerFeedBack) {
518+
return read();
519+
}
520+
else{
521+
return -1;
522+
}
476523
}
477524
else{
478525
return -1;

DFRobotDFPlayerMini.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @copyright GNU Lesser General Public License
88
*
99
* @author [Angelo]([email protected])
10-
* @version V1.0
10+
* @version V1.0.3
1111
* @date 2016-12-07
1212
*/
1313

@@ -42,6 +42,11 @@
4242
#define DFPlayerCardOnline 4
4343
#define DFPlayerPlayFinished 5
4444
#define DFPlayerError 6
45+
#define DFPlayerUSBInserted 7
46+
#define DFPlayerUSBRemoved 8
47+
#define DFPlayerUSBOnline 9
48+
#define DFPlayerCardUSBOnline 10
49+
#define DFPlayerFeedBack 11
4550

4651
#define Busy 1
4752
#define Sleeping 2
@@ -107,7 +112,7 @@ class DFRobotDFPlayerMini {
107112

108113
bool begin(Stream& stream, bool isACK = true, bool doReset = true);
109114

110-
bool waitAvailable();
115+
bool waitAvailable(unsigned long duration = 0);
111116

112117
bool available();
113118

@@ -177,7 +182,7 @@ class DFRobotDFPlayerMini {
177182

178183
int readVolume();
179184

180-
uint8_t readEQ();
185+
int readEQ();
181186

182187
int readFileCounts(uint8_t device);
183188

Binary file not shown.

0 commit comments

Comments
 (0)