-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDFR0534.cpp
1232 lines (1142 loc) · 33 KB
/
DFR0534.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Class: DFR0534
*
* Description:
* Class for controlling a DFR0534 audio module (https://wiki.dfrobot.com/Voice_Module_SKU__DFR0534)
* by SoftwareSerial or HardwareSerial
*
* License: 2-Clause BSD License
* Copyright (c) 2024 codingABI
* For details see: LICENSE.txt
*
* Notes for DFR0534 audio module:
* - Consumes about 20mA when idle (Vcc = 5V)
* - Creates a short "click" noise, when Vcc is powered on
* - Should be used with a 1k resistor on TX when your MCU runs on 5V,
* because the DFR0534 uses 3.3V logic (and 5V on TX causes clicks/noise)
* - Can be controlled by a RX/TX serial connection (9600 baud) or one-wire protocol
* - Can play WAV and MP3 audiofiles
* - Can "insert" audiofiles while another audiofile is running.
* In this case to original audiofile is paused and will be
* resumed after the "inserted" audiofile
* - Can play files in a playlist like mode called "combined"
* for files stored in a directory /ZH
* - Can select the file to play by a file number* or file name**
* *File number is independent from file name. The first WAV or MP3 copied to
* the DFR0534 gets file number 1 and so on. To play a file by number
* use playFileByNumber()
* **File name is a little bit like a 8+3 file path and
* can be used with playFileByName(), but have special rules (see playFileByName() for details)
* - Can send automatically the file runtime every second (when enabled)
* - Has a NS8002 amplifier, JQ8400 Audio chip, W25Q64JVSIQ flash memory
* - Has a Sleep mode 0x1B and this mode only works with one-wire protocol (https://github.com/arduino12/mp3_player_module_wire)
* and does not work for me without additional electric modifications (e.g. disconnecting speakers)
* => Switching off DFR0534 with a FET is a better solution
*
* Home: https://github.com/codingABI/DFR0534
*
* @author codingABI https://github.com/codingABI/
* @copyright 2-Clause BSD License
* @file DFR0534.cpp
* @version 1.0.4
*/
#include "DFR0534.h"
/**@brief
* Get module status
*
* @retval DFR0534::STOPPED Audio module is idle
* @retval DFR0534::PLAYING Audio module is playing a file
* @retval DFR0534::PAUSED Audio module is paused
* @retval DFR0534::STATUSUNKNOWN Error (for example request timeout)
*/
byte DFR0534::getStatus()
{
#define COMMAND 0x01
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEFAILED STATUSUNKNOWN
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) return RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);;
sendDataByte(0x00);;
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff, result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 1) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
result = data;
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Set equalizer to NORMAL, POP, ROCK, JAZZ or CLASSIC
*
* @param[in] mode EQ mode: DFR0534::NORMAL, DFR0534::POP, DFR0534::ROCK, DFR0534::JAZZ or DFR0534::CLASSIC
*/
void DFR0534::setEqualizer(byte mode)
{
if (m_ptrStream == NULL) return; // Should not happen
if (mode >= EQUNKNOWN) return;
sendStartingCode();
sendDataByte(0x1A);
sendDataByte(0x01);
sendDataByte(mode);
sendCheckSum();
}
/**@brief
* Play audio file by number
*
* File number order is "file copy order":
* First audio file copied to the drive gets number 1, second audio file copied gets number 2... )
*
* @param[in] track File number
*/
void DFR0534::playFileByNumber(word track)
{
if (m_ptrStream == NULL) return; // Should not happen
if (track <=0) return;
sendStartingCode();
sendDataByte(0x07);
sendDataByte(0x02);
sendDataByte((track >> 8) & 0xff);
sendDataByte(track & 0xff);
sendCheckSum();
}
/**@brief
* Set volume
*
* Volumen levels 0-30 are allowed. Audio module starts always with level 20.
*
* @param[in] volume Volume level
*/
void DFR0534::setVolume(byte volume)
{
if (m_ptrStream == NULL) return; // Should not happen
if (volume > 30) volume = 30;
sendStartingCode();
sendDataByte(0x13);
sendDataByte(0x01);
sendDataByte(volume);
sendCheckSum();
}
/**@brief
* Play the current selected file
*/
void DFR0534::play()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x02);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Pause the current file
*/
void DFR0534::pause()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x03);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Stop the current file
*/
void DFR0534::stop()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x04);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Play previous file (in "file copy order")
*/
void DFR0534::playPrevious()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x05);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Play next file (in "file copy order")
*/
void DFR0534::playNext()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x06);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Play audio file by file name/path
*
* The file name/path is the full path of the audio file to be played
* in format which looks like a special unix 8+3 format:
* - Without a dot between file name and file extension
* - All characters in upper case
* - Maximal 8 characters for file name
* - Every file and folder whose name length is shorter then 8 chars
* must be filled up to the 8 chars length by space chars
* - Must end with WAV or MP3
* - Only WAV and MP3 files are supported
* - Wildcards * (=multiple arbitrary characters) and ? (=one single arbitrary character)
* are allowed and can be used to reduce the filling space chars
*
* Valid examples:
* - "/01 WAV" for a file '/01.wav'
* - "/99-AFR~1MP3" for a file '/99-Africa.mp3'
* - "/SUN*MP3" for first file matching '/sun*.mp3', for example '/sun.mp3'
* - "/99-AFR*MP3" for first file matching '/99-Afr*.mp3'
* - "/10*" for first audio file matching '/10*.*'
* - "/10 /20 WAV" for the file '/10/20.wav'
* (first means first in "file copy order")
*
* @param[in] path Full path of the audio file
* @param[in] drive Drive, where file is stored: Drive DFR0534::DRIVEUSB, DFR0534::DRIVESD or DFR0534::DRIVEFLASH (=default)
*/
void DFR0534::playFileByName(char *path, byte drive)
{
if (m_ptrStream == NULL) return; // Should not happen
if (path == NULL) return;
if (drive >= DRIVEUNKNOWN) return;
sendStartingCode();
sendDataByte(0x08);
sendDataByte(strlen(path)+1);
sendDataByte(drive);
for (int i=0;i<strlen(path);i++) {
sendDataByte(path[i]);
}
sendCheckSum();
}
/**@brief
* Checks which drives are ready/online
*
* Returned value is a bit pattern that shows which drives are ready/online (1=online,0=offline):
* - Bit 0 = DFR0534::DRIVEUSB
* - Bit 1 = DFR0534::DRIVESD
* - Bit 2 = DFR0534::DRIVEFLASH
* @returns Bit pattern for drives
* @retval DFR0534::DRIVEUNKNOWN Error (for example request timeout)
*/
byte DFR0534::getDrivesStates()
{
#define COMMAND 0x09
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEFAILED DRIVEUNKNOWN
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) return RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff, result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 1) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
result = data;
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Get current drive
*
* @retval DFR0534::DRIVEUSB USB drive
* @retval DFR0534::DRIVESD SD card
* @retval DFR0534::DRIVEFLASH Flash memory chip
* @retval DFR0534::DRIVENO No drive found
* @retval DFR0534::DRIVEUNKNOWN Error (for example request timeout)
*/
byte DFR0534::getDrive()
{
#define COMMAND 0x0A
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEFAILED DRIVEUNKNOWN
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) return RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff, result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 1) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
result = data;
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Switch to drive
*
* @param[in] drive Drive DFR0534::DRIVEUSB, DFR0534::DRIVESD or DFR0534::DRIVEFLASH
*/
void DFR0534::setDrive(byte drive)
{
if (m_ptrStream == NULL) return; // Should not happen
if (drive >= DRIVEUNKNOWN) return;
sendStartingCode();
sendDataByte(0x0B);
sendDataByte(0x01);
sendDataByte(drive);
sendCheckSum();
}
/**@brief
* Get file number of current file
*
* File number is in "file copy order". First audio file copied to the drive get number 1...
*
* @returns File number
* @retval 0 Error (for example request timeout)
*/
word DFR0534::getFileNumber()
{
#define COMMAND 0x0D
#define RECEIVEFAILED 0
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) return RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff;
word result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 2) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
switch (i-RECEIVEHEADERLENGTH-1) {
case 0:
result=data<<8;
break;
case 1:
result+=data;
break;
}
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Get total number of supported audio files on current drive
*
* @returns Number of files
* @retval -1 Error (for example request timeout)
*/
int DFR0534::getTotalFiles()
{
#define COMMAND 0x0C
#define RECEIVEFAILED -1
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) return RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff;
word result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 2) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
switch (i-RECEIVEHEADERLENGTH-1) {
case 0:
result=data<<8;
break;
case 1:
result+=data;
break;
}
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Play last file in directory (in "file copy order")
*/
void DFR0534::playLastInDirectory()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x0E);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Play first file in next directory (in "file copy order")
*/
void DFR0534::playNextDirectory()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x0F);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Get number of first file in current directory
*
* @returns File number
* @retval -1 Error (for example request timeout)
*/
int DFR0534::getFirstFileNumberInCurrentDirectory()
{
#define COMMAND 0x11
#define RECEIVEFAILED -1
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff;
word result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 2) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
switch (i-RECEIVEHEADERLENGTH-1) {
case 0:
result=data<<8;
break;
case 1:
result+=data;
break;
}
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Count all audio files for the current directory
*
* @returns File count
* @retval -1 Error (for example request timeout)
*/
int DFR0534::getTotalFilesInCurrentDirectory()
{
#define COMMAND 0x12
#define RECEIVEFAILED -1
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) RECEIVEFAILED; // Should not happen
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff;
word result = 0;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) {
length = data; // Length of receiving data
if (length != 2) {
// Invalid length => reset receive
i=0;
firstByte = 0;
}
}
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
switch (i-RECEIVEHEADERLENGTH-1) {
case 0:
result=data<<8;
break;
case 1:
result+=data;
break;
}
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
if (data != sum) return RECEIVEFAILED; // Does checksum matches?
return result;
}
/**@brief
* Increase volume by one step
*/
void DFR0534::increaseVolume()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x14);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Decrease volume by one step
*/
void DFR0534::decreaseVolume()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x15);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Pause current file and play another file by number
*
* File number order is "file copy order". Continue original file when this file stops
*
* @param[in] track File number of the audio file
* @param[in] drive Drive, where file is stored: Drive DFR0534::DRIVEUSB, DFR0534::DRIVESD or DFR0534::DRIVEFLASH (=default)
*/
void DFR0534::insertFileByNumber(word track, byte drive)
{
if (m_ptrStream == NULL) return; // Should not happen
if (drive >= DRIVEUNKNOWN) return;
sendStartingCode();
sendDataByte(0x16);
sendDataByte(0x03);
sendDataByte(drive);
sendDataByte((track >> 8) & 0xff);
sendDataByte(track & 0xff);
sendCheckSum();
}
/**@brief
* Stop inserted file
*
* Continue original file
*/
void DFR0534::stopInsertedFile()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x10);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Should set directory, but does not work for me
*
* @param[in] path Directory
* @param[in] drive Drive, where directory is stored: Drive DFR0534::DRIVEUSB, DFR0534::DRIVESD or DFR0534::DRIVEFLASH (=default)
*/
void DFR0534::setDirectory(char *path, byte drive)
{
if (m_ptrStream == NULL) return; // Should not happen
if (path == NULL) return;
if (drive >= DRIVEUNKNOWN) return;
sendStartingCode();
sendDataByte(0x17);
sendDataByte(strlen(path)+1);
sendDataByte(drive);
for (int i=0;i<strlen(path);i++) {
sendDataByte(path[i]);
}
sendCheckSum();
}
/**@brief
* Set loop mode
*
* @param[in] mode Loop mode: DFR0534::LOOPBACKALL, DFR0534::SINGLEAUDIOLOOP, DFR0534::SINGLEAUDIOSTOP, DFR0534::PLAYRANDOM, DFR0534::DIRECTORYLOOP, DFR0534::RANDOMINDIRECTORY, DFR0534::SEQUENTIALINDIRECTORY or DFR0534::SEQUENTIAL
*/
void DFR0534::setLoopMode(byte mode)
{
if (m_ptrStream == NULL) return; // Should not happen
if (mode >= PLAYMODEUNKNOWN) return;
sendStartingCode();
sendDataByte(0x18);
sendDataByte(0x01);
sendDataByte(mode);
sendCheckSum();
}
/**@brief
* Set repeat loops
*
* Only valid for loop modes DFR0534::LOOPBACKALL, DFR0534::SINGLEAUDIOLOOP or DFR0534::DIRECTORYLOOP
*
* @param[in] loops Number of loops
*/
void DFR0534::setRepeatLoops(word loops)
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x19);
sendDataByte(0x02);
sendDataByte((loops >> 8) & 0xff);
sendDataByte(loops & 0xff);
sendCheckSum();
}
/**@brief
* Combined/concatenated play of files
*
* Combined is like a playlist, for example playCombined("0103") for
* the two files 01 and 03.
* The Filenames must be two chars long and the files must
* be in a directory called /ZH
* Combined playback ignores loop mode and stops after last file.
*
* @param[in] list Concatenated list of all files to play
*/
void DFR0534::playCombined(char* list)
{
if (m_ptrStream == NULL) return; // Should not happen
if (list == NULL) return;
if ((strlen(list) % 2) != 0) return;
sendStartingCode();
sendDataByte(0x1B);
sendDataByte(strlen(list));
for (int i=0;i<strlen(list);i++) {
sendDataByte(list[i]);
}
sendCheckSum();
}
/**@brief
* Stop combined play (playlist)
*/
void DFR0534::stopCombined()
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x1C);
sendDataByte(0x00);
sendCheckSum();
}
/**@brief
* Set output for DAC to channel MP3, AUX or both
*
* I found not P26/P27 for AUX on my DFR0534 => Only DFR0534::CHANNELMP3 makes sense (and is already set by default)
* Perhaps this function works on other audio modules with the same chip.
*
* @param[in] channel Output channel: DFR0534::CHANNELMP3, DFR0534::CHANNELAUX or DFR0534::CHANNELMP3AUX
*/
void DFR0534::setChannel(byte channel)
{
if (m_ptrStream == NULL) return; // Should not happen
if (channel >= CHANNELUNKNOWN) return;
sendStartingCode();
sendDataByte(0x1D);
sendDataByte(0x01);
sendDataByte(channel);
sendCheckSum();
}
/**@brief
* Get name for current file
*
* File name is in 8+3 format in upper case, with spaces
* without the dot "." between name and extension,
* e.g. "TEST WAV" for the file test.wav
*
* @param[out] name Filename. You have to allocate at least 12 chars memory for this variable.
*/
bool DFR0534::getFileName(char *name)
{
#define COMMAND 0x1E
#define RECEIVEBYTETIMEOUTMS 100
#define RECEIVEGLOBALTIMEOUTMS 500
#define RECEIVEFAILED false
#define RECEIVEHEADERLENGTH 2 // startingcode+command
if (m_ptrStream == NULL) return false; // Should not happen
if (name == NULL) return false;
name[0] = '\0';
sendStartingCode();
sendDataByte(COMMAND);
sendDataByte(0x00);
sendCheckSum();
// Receive
int i=0;
byte data, firstByte = 0, sum, length=0xff;
unsigned long receiveStartMS = millis();
do {
byte dataReady = 0;
unsigned long lastMS = millis();
// Wait for response or timeout
while ((millis()-lastMS < RECEIVEBYTETIMEOUTMS) && (dataReady==0)) dataReady = m_ptrStream->available();
if (dataReady == 0) return RECEIVEFAILED; // Timeout
data = m_ptrStream->read();
if (i==0) { // Begin of transmission
firstByte=data;
sum = 0;
}
if ((i == 1) && (data != COMMAND)) {
// Invalid signal => reset receive
i=0;
firstByte = 0;
}
if (i == RECEIVEHEADERLENGTH) length = data; // Length of receiving string
if ((i > RECEIVEHEADERLENGTH) && (i-RECEIVEHEADERLENGTH-1<length)) {
if ((i-RECEIVEHEADERLENGTH) < 12) { // I expect no longer file names than 8+3 chars plus '\0'
name[i-RECEIVEHEADERLENGTH-1] = data;
name[i-RECEIVEHEADERLENGTH] = '\0';
}
}
if (firstByte == STARTINGCODE) {
if (i-RECEIVEHEADERLENGTH<=length) sum+=data; // Update checksum
i++;
}
if (millis()-receiveStartMS > RECEIVEGLOBALTIMEOUTMS) return RECEIVEFAILED; // Timeout
} while (i<length+RECEIVEHEADERLENGTH+2);
return (data == sum); // Does checksum matches?
}
/**@brief
* Select file by number, but not start playing
*
* @param[in] track Number for file
*/
void DFR0534::prepareFileByNumber(word track)
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x1F);
sendDataByte(0x02);
sendDataByte((track >> 8) & 0xff);
sendDataByte(track & 0xff);
sendCheckSum();
}
/**@brief
* Repeat part of the current file
*
* Repeat between time start and stop position
*
* @param[in] startMinute Minute for start position
* @param[in] startSecond Second for start position
* @param[in] stopMinute Minute for stop position
* @param[in] stopSecond Seconde for stop position
*/
void DFR0534::repeatPart(byte startMinute, byte startSecond, byte stopMinute, byte stopSecond )
{
if (m_ptrStream == NULL) return; // Should not happen
sendStartingCode();
sendDataByte(0x20);
sendDataByte(0x04);
sendDataByte(startMinute);
sendDataByte(startSecond);