-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_custom_SerialMp3.cpp
More file actions
332 lines (250 loc) · 7.65 KB
/
_custom_SerialMp3.cpp
File metadata and controls
332 lines (250 loc) · 7.65 KB
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
/*
* SerialMP3Player.h - Library for Serial MP3 Player board from Catalex (YX5300 chip)
* Created by Salvador Rueda Pau, July 23, 2016.
* License as GPL v3 (http://www.gnu.org/licenses/gpl.html)
*
* >>>>>>>>>>>> Modified to remove delay from sendCommand() <<<<<<<<<<<<<
*
*/
#include "Arduino.h"
#include "_custom_SerialMp3.h"
#define NO_SERIALMP3_DELAY // Comment this line to add a delay (1200 ms) to each command sent
#ifdef NO_SERIALMP3_DELAY
#define _mp3_delay(m) asm("nop")
#else
#define _mp3_delay(m) delay(m)
#endif
// Uncomment next line if you are using an Arduino Mega.
//#define mp3 Serial3 // Connect the MP3 Serial Player to the Arduino MEGA Serial3 (14 TX3 -> RX, 15 RX3 -> TX)
SerialMP3Player::SerialMP3Player(){
//SoftwareSerial Serial3 = SoftwareSerial(10, 11);
Serial3 = new SoftwareSerial(10, 11);
_showDebugMessages = false;
}
SerialMP3Player::SerialMP3Player(int RX, int TX){
//SoftwareSerial Serial3 = SoftwareSerial(RX, TX);
Serial3 = new SoftwareSerial(RX, TX);
_showDebugMessages = false;
}
void SerialMP3Player::showDebug(bool op){
// showDebug (op) 0:OFF 1:ON
_showDebugMessages = op;
}
void SerialMP3Player::begin(int bps){
Serial3->begin(bps);
}
int SerialMP3Player::available(){
return Serial3->available();
}
char SerialMP3Player::read(){
return Serial3->read();
}
void SerialMP3Player::playNext(){
sendCommand(CMD_NEXT);
}
void SerialMP3Player::playPrevious(){
sendCommand(CMD_PREV);
}
void SerialMP3Player::volUp(){
sendCommand(CMD_VOL_UP);
}
void SerialMP3Player::volDown(){
sendCommand(CMD_VOL_DOWN);
}
void SerialMP3Player::setVol(byte v){
// Set volumen (0-30)
sendCommand(CMD_SET_VOL, v);
}
void SerialMP3Player::playSL(byte n){
// Play single loop n file
sendCommand(CMD_PLAY_SLOOP, n);
}
void SerialMP3Player::playSL(byte f, byte n){
// Single loop play n file from f folder
sendCommand(CMD_PLAY_SLOOP, f, n);
}
void SerialMP3Player::play(){
sendCommand(CMD_PLAY);
}
void SerialMP3Player::pause(){
sendCommand(CMD_PAUSE);
}
void SerialMP3Player::play(byte n){
// n number of the file that must be played.
// n possible values (1-255)
sendCommand(CMD_PLAYN, n);
}
void SerialMP3Player::play(byte n, byte vol){
// n number of the file that must be played
sendCommand(CMD_PLAY_W_VOL, vol, n);
}
void SerialMP3Player::playF(byte f){
// Play all files in the f folder
sendCommand(CMD_FOLDER_CYCLE, f, 0);
}
void SerialMP3Player::stop(){
sendCommand(CMD_STOP_PLAY);
}
void SerialMP3Player::qPlaying(){
// Ask for the file is playing
sendCommand(CMD_PLAYING_N);
}
void SerialMP3Player::qStatus(){
// Ask for the status.
sendCommand(CMD_QUERY_STATUS);
}
void SerialMP3Player::qVol(){
// Ask for the volumen
sendCommand(CMD_QUERY_VOLUME);
}
void SerialMP3Player::qFTracks(){ // !!! Nonsense answer
// Ask for the number of tracks folders
sendCommand(CMD_QUERY_FLDR_TRACKS);
}
void SerialMP3Player::qTTracks(){
// Ask for the total of tracks
sendCommand(CMD_QUERY_TOT_TRACKS);
}
void SerialMP3Player::qTFolders(){
// Ask for the number of folders
sendCommand(CMD_QUERY_FLDR_COUNT);
}
void SerialMP3Player::sleep(){
// Send sleep command
sendCommand(CMD_SLEEP_MODE);
}
void SerialMP3Player::wakeup(){
// Send wake up command
sendCommand(CMD_WAKE_UP);
}
void SerialMP3Player::reset(){
// Send reset command
sendCommand(CMD_RESET);
}
void SerialMP3Player::sendCommand(byte command){
sendCommand(command, 0, 0);
}
void SerialMP3Player::sendCommand(byte command, byte dat2){
sendCommand(command, 0, dat2);
}
void SerialMP3Player::sendCommand(byte command, byte dat1, byte dat2){
byte Send_buf[8] = {0}; // Buffer for Send commands.
String mp3send = "";
// Command Structure 0x7E 0xFF 0x06 CMD FBACK DAT1 DAT2 0xEF
_mp3_delay(20);
Send_buf[0] = 0x7E; // Start byte
Send_buf[1] = 0xFF; // Version
Send_buf[2] = 0x06; // Command length not including Start and End byte.
Send_buf[3] = command; // Command
Send_buf[4] = 0x01; // Feedback 0x00 NO, 0x01 YES
Send_buf[5] = dat1; // DATA1 datah
Send_buf[6] = dat2; // DATA2 datal
Send_buf[7] = 0xEF; // End byte
for(int i=0; i<8; i++)
{
Serial3->write(Send_buf[i]) ;
mp3send+=sbyte2hex(Send_buf[i]);
}
if (_showDebugMessages){
Serial.print("Sending: ");
Serial.println(mp3send); // watch what are we sending
}
_mp3_delay(1000); // Wait between sending commands.
}
//String sanswer(void);
//int iansbuf = 0; // Index for answer buffer.
//static uint8_t ansbuf[10] = {0}; // Buffer for the answers.
String SerialMP3Player::decodeMP3Answer(){
// Response Structure 0x7E 0xFF 0x06 RSP 0x00 0x00 DAT 0xFE 0xBA 0xEF
//
// RSP Response code
// DAT Response additional data
String decodedMP3Answer="";
decodedMP3Answer=sanswer();
switch (ansbuf[3])
{
case 0x3A:
decodedMP3Answer += " -> Memory card inserted.";
break;
case 0x3D:
decodedMP3Answer += " -> Completed play num " + String(ansbuf[6], DEC);
break;
case 0x40:
decodedMP3Answer += " -> Error";
break;
case 0x41:
decodedMP3Answer += " -> Data recived correctly. ";
break;
case 0x42:
switch(ansbuf[6]){
case 0: decodedMP3Answer += " -> Status: stopped"; break;
case 1: decodedMP3Answer += " -> Status: playing"; break;
case 2: decodedMP3Answer += " -> Status: paused"; break;
}
break;
case 0x43:
decodedMP3Answer += " -> Vol playing: " + String(ansbuf[6], DEC);
break;
case 0x48:
decodedMP3Answer += " -> File count: " + String(ansbuf[6], DEC);
break;
case 0x4C:
decodedMP3Answer += " -> Playing: " + String(ansbuf[6], DEC);
break;
case 0x4E:
decodedMP3Answer += " -> Folder file count: " + String(ansbuf[6], DEC);
break;
case 0x4F:
decodedMP3Answer += " -> Folder count: " + String(ansbuf[6], DEC);
break;
}
ansbuf[3] = 0; // Clear ansbuff.
return decodedMP3Answer;
}
/********************************************************************************/
/*Function: sbyte2hex. Returns a byte data in HEX format. */
/*Parameter:- uint8_t b. Byte to convert to HEX. */
/*Return: String */
String SerialMP3Player::sbyte2hex(byte b)
{
String shex;
//Serial.print("0x");
shex="0X";
//if (b < 16) Serial.print("0");
if (b < 16) shex+="0";
//Serial.print(b, HEX);
shex+=String(b,HEX);
//Serial.print(" ");
shex+=" ";
return shex;
}
/********************************************************************************/
/*Function: sanswer. Returns a String answer from mp3 UART module. */
/*Return: String. the answer */
String SerialMP3Player::sanswer(void){
// Response Structure 0x7E 0xFF 0x06 RSP 0x00 0x00 DAT 0xFE 0xBA 0xEF
//
// RSP Response code
// DAT Response additional data
// if there are something available start to read from mp3 serial.
// if there are "0x7E" it's a beginning.
//
// read while something readed and it's not the end "0xEF"
byte b;
String mp3answer=""; // Answer from the Serial3.
int iansbuf = 0;
if (Serial3->available()){
do{
b = Serial3->read();
if(b == 0x7E){ // if there are "0x7E" it's a beginning.
iansbuf=0; // ansbuf index to zero.
mp3answer="";
}
ansbuf[iansbuf] = b;
mp3answer+=sbyte2hex(ansbuf[iansbuf]);
iansbuf++; // increase this index.
}while(b != 0xEF);
// while there are something to read and it's not the end "0xEF"
}
return mp3answer;
}