7
7
* @copyright GNU Lesser General Public License
8
8
*
9
9
* @author [Angelo]([email protected] )
10
- * @version V1.0
10
+ * @version V1.0.3
11
11
* @date 2016-12-07
12
12
*/
13
13
@@ -31,15 +31,12 @@ uint16_t DFRobotDFPlayerMini::calculateCheckSum(uint8_t *buffer){
31
31
}
32
32
33
33
void DFRobotDFPlayerMini::sendStack (){
34
- if (_sending[Stack_ACK]) {
34
+ if (_sending[Stack_ACK]) { // if the ack mode is on wait until the last transmition
35
35
while (_isSending) {
36
36
delay (0 );
37
37
available ();
38
38
}
39
39
}
40
- else {
41
- delay (10 );
42
- }
43
40
44
41
#ifdef _DEBUG
45
42
Serial.println ();
@@ -53,6 +50,10 @@ void DFRobotDFPlayerMini::sendStack(){
53
50
_serial->write (_sending, DFPLAYER_SEND_LENGTH);
54
51
_timeOutTimer = millis ();
55
52
_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
+ }
56
57
}
57
58
58
59
void DFRobotDFPlayerMini::sendStack (uint8_t command){
@@ -80,36 +81,41 @@ void DFRobotDFPlayerMini::disableACK(){
80
81
_sending[Stack_ACK] = 0x00 ;
81
82
}
82
83
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
+ }
85
89
while (!available ()){
90
+ if (millis () - timer > duration) {
91
+ return false ;
92
+ }
86
93
delay (0 );
87
94
}
88
- return _handleType != TimeOut ;
95
+ return true ;
89
96
}
90
97
91
98
bool DFRobotDFPlayerMini::begin (Stream &stream, bool isACK, bool doReset){
99
+ _serial = &stream;
100
+
92
101
if (isACK) {
93
102
enableACK ();
94
103
}
95
104
else {
96
105
disableACK ();
97
106
}
98
107
99
- _serial = &stream;
100
-
101
108
if (doReset) {
102
- _timeOutDuration += 3000 ;
103
109
reset ();
104
- waitAvailable ();
105
- _timeOutDuration -= 3000 ;
110
+ waitAvailable (2000 );
106
111
delay (200 );
107
- } else {
112
+ }
113
+ else {
108
114
// assume same state as with reset(): online
109
115
_handleType = DFPlayerCardOnline;
110
116
}
111
117
112
- return (readType () == DFPlayerCardOnline) || !isACK;
118
+ return (readType () == DFPlayerCardOnline) || ( readType () == DFPlayerUSBOnline) || !isACK;
113
119
}
114
120
115
121
uint8_t DFRobotDFPlayerMini::readType (){
@@ -142,34 +148,49 @@ uint8_t DFRobotDFPlayerMini::readCommand(){
142
148
}
143
149
144
150
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;
146
158
_handleParameter = arrayToUint16 (_received + Stack_Parameter);
147
159
148
160
switch (_handleCommand) {
149
161
case 0x3D :
150
162
handleMessage (DFPlayerPlayFinished, _handleParameter);
151
163
break ;
152
164
case 0x3F :
153
- if (_handleParameter & 0x02 ) {
165
+ if (_handleParameter & 0x01 ) {
166
+ handleMessage (DFPlayerUSBOnline, _handleParameter);
167
+ }
168
+ else if (_handleParameter & 0x02 ) {
154
169
handleMessage (DFPlayerCardOnline, _handleParameter);
155
170
}
171
+ else if (_handleParameter & 0x03 ) {
172
+ handleMessage (DFPlayerCardUSBOnline, _handleParameter);
173
+ }
156
174
break ;
157
175
case 0x3A :
158
- if (_handleParameter & 0x02 ) {
176
+ if (_handleParameter & 0x01 ) {
177
+ handleMessage (DFPlayerUSBInserted, _handleParameter);
178
+ }
179
+ else if (_handleParameter & 0x02 ) {
159
180
handleMessage (DFPlayerCardInserted, _handleParameter);
160
181
}
161
182
break ;
162
183
case 0x3B :
163
- if (_handleParameter & 0x02 ) {
184
+ if (_handleParameter & 0x01 ) {
185
+ handleMessage (DFPlayerUSBRemoved, _handleParameter);
186
+ }
187
+ else if (_handleParameter & 0x02 ) {
164
188
handleMessage (DFPlayerCardRemoved, _handleParameter);
165
189
}
166
190
break ;
167
191
case 0x40 :
168
192
handleMessage (DFPlayerError, _handleParameter);
169
193
break ;
170
- case 0x41 :
171
- _isSending = false ;
172
- break ;
173
194
case 0x3C :
174
195
case 0x3E :
175
196
case 0x42 :
@@ -185,7 +206,7 @@ void DFRobotDFPlayerMini::parseStack(){
185
206
case 0x4D :
186
207
case 0x4E :
187
208
case 0x4F :
188
- _isAvailable = true ;
209
+ handleMessage (DFPlayerFeedBack, _handleParameter) ;
189
210
break ;
190
211
default :
191
212
handleError (WrongStack);
@@ -215,7 +236,6 @@ bool DFRobotDFPlayerMini::available(){
215
236
Serial.print (F (" " ));
216
237
#endif
217
238
if (_received[Stack_Header] == 0x7E ) {
218
- _isAvailable = false ;
219
239
_receivedIndex ++;
220
240
}
221
241
}
@@ -247,9 +267,6 @@ bool DFRobotDFPlayerMini::available(){
247
267
if (validateStack ()) {
248
268
_receivedIndex = 0 ;
249
269
parseStack ();
250
- if (_isAvailable && !_sending[Stack_ACK]) {
251
- _isSending = false ;
252
- }
253
270
return _isAvailable;
254
271
}
255
272
else {
@@ -387,7 +404,12 @@ void DFRobotDFPlayerMini::disableDAC(){
387
404
int DFRobotDFPlayerMini::readState (){
388
405
sendStack (0x42 );
389
406
if (waitAvailable ()) {
390
- return read ();
407
+ if (readType () == DFPlayerFeedBack) {
408
+ return read ();
409
+ }
410
+ else {
411
+ return -1 ;
412
+ }
391
413
}
392
414
else {
393
415
return -1 ;
@@ -404,10 +426,15 @@ int DFRobotDFPlayerMini::readVolume(){
404
426
}
405
427
}
406
428
407
- uint8_t DFRobotDFPlayerMini::readEQ (){
429
+ int DFRobotDFPlayerMini::readEQ (){
408
430
sendStack (0x44 );
409
431
if (waitAvailable ()) {
410
- return read ();
432
+ if (readType () == DFPlayerFeedBack) {
433
+ return read ();
434
+ }
435
+ else {
436
+ return -1 ;
437
+ }
411
438
}
412
439
else {
413
440
return -1 ;
@@ -430,7 +457,12 @@ int DFRobotDFPlayerMini::readFileCounts(uint8_t device){
430
457
}
431
458
432
459
if (waitAvailable ()) {
433
- return read ();
460
+ if (readType () == DFPlayerFeedBack) {
461
+ return read ();
462
+ }
463
+ else {
464
+ return -1 ;
465
+ }
434
466
}
435
467
else {
436
468
return -1 ;
@@ -452,7 +484,12 @@ int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
452
484
break ;
453
485
}
454
486
if (waitAvailable ()) {
455
- return read ();
487
+ if (readType () == DFPlayerFeedBack) {
488
+ return read ();
489
+ }
490
+ else {
491
+ return -1 ;
492
+ }
456
493
}
457
494
else {
458
495
return -1 ;
@@ -462,7 +499,12 @@ int DFRobotDFPlayerMini::readCurrentFileNumber(uint8_t device){
462
499
int DFRobotDFPlayerMini::readFileCountsInFolder (int folderNumber){
463
500
sendStack (0x4E , folderNumber);
464
501
if (waitAvailable ()) {
465
- return read ();
502
+ if (readType () == DFPlayerFeedBack) {
503
+ return read ();
504
+ }
505
+ else {
506
+ return -1 ;
507
+ }
466
508
}
467
509
else {
468
510
return -1 ;
@@ -472,7 +514,12 @@ int DFRobotDFPlayerMini::readFileCountsInFolder(int folderNumber){
472
514
int DFRobotDFPlayerMini::readFolderCounts (){
473
515
sendStack (0x4F );
474
516
if (waitAvailable ()) {
475
- return read ();
517
+ if (readType () == DFPlayerFeedBack) {
518
+ return read ();
519
+ }
520
+ else {
521
+ return -1 ;
522
+ }
476
523
}
477
524
else {
478
525
return -1 ;
0 commit comments