forked from electron1979/Arduino_XY-V17B_mp3_wav_player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXY-V17B.ino
207 lines (174 loc) · 8.86 KB
/
XY-V17B.ino
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
/*
Arduino sketch to drive XY-V17B mp3/wav player
http://www.icstation.com/mini-player-module-audio-voice-board-8bit-uart-contorl-support-card-card-p-13279.html
http://attach01.oss-us-west-1.aliyuncs.com/IC/Datasheet/13288.pdf
5.1>. Communication format
Adopt full duplex serial port communication;
Baud rate 9600, data bits 8, stop bit 1, check bit N.
Start code - command type - data length (n) - data 1- data n- and test (SM)
Command code: fixed to 0xAA.
Command type: used to distinguish the type of command.
Data length: the number of bytes of data in an command.
Data: the relevant data in the command, when the length of data is 1, means
that there is only CMD and no data bits.
Test: it is low 8 bits of the sum of all the bytes. that is, When the starting code
and the data are added, take out low 8 bits.
Data format: sent data or command, high 8-bit data is in front, low 8-bit is in
the back.
5.2>. Communication protocol
The following is a data definition for the return and identification of the chip.
A. Playing State definition: the system is on the stop state when power on.
00(stop) 01(play) 02(pause)
B. Disk character definition: it is stopped after the switch disk.
USB:00 SD:01 FLASH:02 NO_DEVICE: FF.
C. Volume: the volume is 31grades, 0-30.The default is 20grade.
D. Play mode: the default is the single stop when power on.
Cycle for all songs (00) : play the whole songs in sequence and play it
after the play.
Single cycle (01) : play the current song all the time.
Single stop (02) : Only play current song once and then stop.
Random play (03) : random play.
Directory loop (04) : play the songs in the current folder in order, and
then play them after the play. The directory does not contain subdirectory.
Directory random (05): random play in the current folder, and the
directory does not contain subdirectory.
Directory order play (06) : play the songs in the current folder in order
and stop after the play. The directory does not contain subdirectory.
Sequential play (07) : play the whole songs in order and stop after it is
played.
E. EQ definition: the default EQ is NORMAL(00).
NORMAL(00) POP(01) ROCK(02) JAZZ(03) CLASSIC(04)
F. Composition play definition: combination play is combined by filename. The
file requirements are stored under the "XY" file. You can change the name of the
file you want to combine to two bytes, which is generally recommended as a
number. Such as: 01. Mp3, 02. Mp3.
By Peter Barsznica
Sept 2018
*****Currently only written to select from 255 tracks*****
*/
// Play selected track examples
byte playXX[] = {0xAA, 0x07, 0x02, 0x00, 0x02, 0xB5};
byte play01[] = {0xAA, 0x07, 0x02, 0x00, 0x01, 0xB4};
byte play02[] = {0xAA, 0x07, 0x02, 0x00, 0x02, 0xB5};
// Control Commands
byte play[] = {0xAA, 0x02, 0x00, 0xAC};
byte pause[] = {0xAA, 0x03, 0x00, 0xAD};
byte stop[] = {0xAA, 0x04, 0x00, 0xAE};
byte previous[] = {0xAA, 0x05, 0x00, 0xAF};
byte next[] = {0xAA, 0x06, 0x00, 0xB0};
byte volPlus[] = {0xAA, 0x14, 0x00, 0xBE};
byte volMinus[] = {0xAA, 0x15, 0x00, 0xBF};
byte prevFile[] = {0xAA, 0x0E, 0x00, 0xB8};
byte nextFile[] = {0xAA, 0x0F, 0x00, 0xB9};
byte stopPlay[] = {0xAA, 0x10, 0x00, 0xBA};
// Setting Commands
byte setVol[] = {0xAA, 0x13, 0x01}; // {0xAA, 0x13, 0x01, VOL, SM} VOL: 0x00-0xFF (0-30)
byte setLoopMode[] = {0xAA, 0x18, 0x01}; // {0xAA, 0x18, 0x01, LM, SM} LM: 0x00-0x07
byte setCycleTimes[] = {0xAA, 0x19, 0x02}; // {0xAA, 0x19, 0x02, H, L, SM} L&H: 0x00-0xFF
byte setEQ[] = {0xAA, 0x1A, 0x01}; // {0xAA, 0x1A, 0x01, EQ, SM} EQ: 0x00-0x04
byte specifySong[] = {0xAA, 0x07, 0x02}; // {0xAA, 0x07, 0x02, H, L, SM} L&H: 0x00-0xFF
byte specifyPath[] = {0xAA, 0x08}; // {0xAA, 0x08, Length, Drive, Path, SM} L,D&P: 0x00-0xFF
byte switchDrive[] = {0xAA, 0x0B, 0x01}; // {0xAA, 0x0B, 0x01, Drive, SM} L&H: 0x00-0xFF
byte specifySongInterplay[] = {0xAA, 0x16, 0x03}; // {0xAA, 0x16, 0x03, H, L, SM} L&H: 0x00-0xFF
byte specifyPathInterplay[] = {0xAA, 0x17}; // {0xAA, 0x17, Length, Drive, Path, SM} L,D&P: 0x00-0xFF
byte selectSong[] = {0xAA, 0x1F, 0x02}; // {0xAA, 0x1F, 0x02, H, L, SM} L&H: 0x00-0xFF (Don't play)
// Query Commands
// Command // Command Code // Return
byte playStatus[] = {0xAA, 0x01, 0x00, 0xAB}; // AA, 01 01, play status, SM
byte currentOnlineDrive[] = {0xAA, 0x09, 0x00, 0xB3}; // AA, 09 01, drive, SM
byte currentPlayDrive[] = {0xAA, 0x0A, 0x00, 0xB4}; // AA, 0A 01, drive, SM
byte NumberOfSongs[] = {0xAA, 0x0C, 0x00, 0xB6}; // AA, 0C 02, S.N.H S.N.L SM
byte currentSong[] = {0xAA, 0x0D, 0x00, 0xB7}; // AA, 0D 02, S.N.H S.N.L SM
byte folderDirectorySong[] = {0xAA, 0x11, 0x00, 0xBB}; // AA, 11 02, S.N.H S.N.L SM
byte folderNumberOfSongs[] = {0xAA, 0x12, 0x00, 0xBC}; // AA, 12 02, S.N.H S.N.L SM
// globals
#define busyPin 2 // YOUR "busy" pin
#define randomSeedPin 0 // YOUR random seed ANALOG INPUT *Do not connect to anything*
uint8_t noOfTracks = 0; // Number of tracks
uint8_t count = 1; // Count songs played once (before playing all)
uint8_t playedAll = 0; // played all tracks flag
uint8_t lastTrackAll = 0; // last track played in random group (== noOfTracks)
uint8_t randTrack = 0; // random track to be played
uint8_t busy = 0;
bool played[256] = {LOW}; // keeps track of tracks played
void setup() {
uint8_t noTracks[6] = {0}; // used to build Number of tracks (noOfTracks)
uint8_t indx = 0; // used to read Serial noOfTracks
pinMode(busyPin, INPUT); // sets the "busy" pin as INPUT
pinMode(randomSeedPin, INPUT); // sets the "randomSeedPin" pin as INPUT
randomSeed(analogRead(randomSeedPin));
Serial.begin(115200);
Serial1.begin(9600);
Serial.println("STARTING...");
// Detect & print number of tracks ///////////////////////////
Serial1.write(NumberOfSongs, sizeof(NumberOfSongs));
Serial1.flush();
while (!Serial1.available()) {
Serial.print('.');
delay(500);
}
Serial.println("");
while (Serial1.available()) {
noTracks[indx] = Serial1.read();
// Serial.println(noTracks[indx++], HEX);
indx++;
}
Serial.print("No. of songs: ");
noOfTracks = ( (256*noTracks[3]) + (noTracks[4]));
Serial.println(noOfTracks);
for (uint8_t initial=0; initial<noOfTracks; initial++) {
played[initial] = LOW;
}
// Play //////////////////////////////////////////////////////
// Serial1.write(play, sizeof(play));
// Serial1.write(play01, sizeof(play01));
// Serial1.flush();
// delay(3000);
// Serial1.write(play02, sizeof(play02));
// Serial1.flush();
}
void loop() {
// Play random track (if not busy) //////////////////////////////////////////////////
busy = (digitalRead(busyPin));
if (busy == LOW) { // !busy
// Serial.println("!busy");
// Check if played all tracks. //////////////////////////////////////////////////////
playedAll = HIGH;
for (int8_t chck=0; chck<noOfTracks; chck++) {
// Serial.print("played[");
// Serial.print(chck);
// Serial.print("]: ");
// Serial.print(played[chck]);
// Serial.print(", ");
if (played[chck] == LOW) {
playedAll = LOW;
}
}
// Serial.print("\n");
// Reset if played all. /////////////////////////////////////////////////////////////
if (playedAll == HIGH) {
Serial.println("Played all\n\t");
count = 1;
for (uint8_t initial=0; initial<noOfTracks; initial++) {
// for (uint8_t initial=0; initial<sizeof(played); initial++) {
played[initial] = LOW;
}
}
// Pick random track (not already played) ///////////////////////////////////////////
randTrack = random(1, noOfTracks+1);
while ( (played[randTrack-1]==HIGH) || (randTrack==lastTrackAll) ) {
randTrack = random(1, noOfTracks+1);
}
lastTrackAll = randTrack;
// Play chosen track ////////////////////////////////////////////////////////////////
Serial.print("Playing track[");
Serial.print(count++);
Serial.print("]: ");
Serial.println(randTrack);
playXX[4] = randTrack; // playXX[4] set to 0
playXX[5] = ( playXX[0] + playXX[1] + playXX[2] + playXX[3] + playXX[4] ); // Should be "low 8 bits" only!
Serial1.write(playXX, sizeof(playXX));
played[randTrack-1] = HIGH;
delay(500);
}
}