-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBY8001.cpp
executable file
·268 lines (223 loc) · 6.14 KB
/
BY8001.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
// BY8001.cpp, BY8001 Mp3 Player Function Library
// Date: June 2015
// Author: borland of Arduino forum
// No affiliation with Balway Electronics Technology or Shenzhen Electronics Technology Ltd.
// Released in public domain
#include "Arduino.h"
#include "BY8001.h"
BY8001::BY8001() {
sendBuffer[0] = 0x7E;
sendBuffer[6] = 0xEF;
}
void BY8001::setup(Stream& serial){
mp3Serial = &serial;
}
void BY8001::sendCommand(byte cmd){
sendBuffer[1] = 0x03; // length
sendBuffer[2] = cmd;
fillCheckCode();
sendBuffer[4] = 0xEF;
txFunc();
}
void BY8001::sendCommand(byte cmd, byte pmtr){
sendBuffer[1] = 0x04; // length
sendBuffer[2] = cmd;
sendBuffer[3] = pmtr;
fillCheckCode();
sendBuffer[5] = 0xEF;
txFunc();
}
void BY8001::sendCommand(byte cmd, word pmtr){
sendBuffer[1] = 0x05; // length
sendBuffer[2] = cmd;
sendBuffer[3] = highByte(pmtr); sendBuffer[4] = lowByte(pmtr);
fillCheckCode();
txFunc();
}
void BY8001::sendCommand(byte cmd, byte pmtr1, byte pmtr2){
sendBuffer[1] = 0x05; // length
sendBuffer[2] = cmd;
sendBuffer[3] = pmtr1; sendBuffer[4] = pmtr2;
fillCheckCode();
txFunc();
}
void BY8001::fillCheckCode(){
byte checkCode = getCheckCode();
sendBuffer[(int)sendBuffer[1]] = checkCode;
}
byte BY8001::getCheckCode(){
byte count = 0;
int j = sendBuffer[1];
for(int i=1; i<j; i++){
count = count ^ sendBuffer[i]; // XOR checksum or check code
}
return count;
}
void BY8001::txFunc(){
while ( mp3Serial->available() >0 ) mp3Serial->read();
mp3Serial->write(sendBuffer, (int)sendBuffer[1] + 2);
}
void BY8001::rxFunc(int length){
// delay 130ms
unsigned long mTimer = millis(); while (millis() - mTimer < 130) {}
mp3Serial->readBytes(receiveBuffer, length);
}
word BY8001::getReply(int length, int offset){
rxFunc(length);
word value = strtoul(receiveBuffer + offset, NULL, 16);
return value;
}
// User Command Functions:
void BY8001::play(){
sendCommand(0x01);
}
void BY8001::pause(){
sendCommand(0x02);
}
void BY8001::nextTrack(){
sendCommand(0x03);
}
void BY8001::previousTrack(){
sendCommand(0x04);
}
void BY8001::increaseVolume(){
sendCommand(0x05);
}
void BY8001::decreaseVolume(){
sendCommand(0x06);
}
void BY8001::toggleStandbyMode(){
sendCommand(0x07);
}
void BY8001::resetModule(){
sendCommand(0x09);
}
void BY8001::fastForward(){
sendCommand(0x0A);
}
void BY8001::fastRewind(){
sendCommand(0x0B);
}
void BY8001::stopPlayback(){
sendCommand(0x0E);
}
void BY8001::setVolume(int volume){
sendCommand(0x31, (byte) volume);
// delay 100ms
unsigned long mTimer = millis(); while (millis() - mTimer < 100) {}
}
void BY8001::setEqualizerProfile(int equalizerProfile){
sendCommand(0x32,(byte) equalizerProfile );
// delay 100ms
unsigned long mTimer = millis(); while (millis() - mTimer < 100) {}
}
void BY8001::setLoopPlaybackMode(int loopPlaybackMode){
sendCommand(0x33,(byte) loopPlaybackMode );
// delay 100ms
unsigned long mTimer = millis(); while (millis() - mTimer < 100) {}
}
void BY8001::switchFolder(int swDirection){
sendCommand(0x34, (byte) swDirection);
}
void BY8001::switchDevice(int device){
sendCommand(0x35,(byte) device );
}
void BY8001::playTrackByIndexNumber(word trackNumber){
sendCommand(0x41, trackNumber);
}
void BY8001::playTrackFromFolder(int folderNumber, int trackNumber){
sendCommand(0x42, (byte) folderNumber, (byte) trackNumber);
}
void BY8001::playInsertionSpot(int trackNumber){
sendCommand(0x43, (byte) trackNumber);
}
void BY8001::playInsertionFromFolder(int folderNumber, int trackNumber){
sendCommand(0x44, (byte) folderNumber, (byte) trackNumber);
}
// User Query Commands:
byte BY8001::getPlaybackStatus(){
sendCommand(0x10);
byte currentStatus = getReply(8,0);
return currentStatus;
}
byte BY8001::getVolumeSetting(){
sendCommand(0x11);
byte volume = getReply(8,0);
return volume;
}
byte BY8001::getEqualizerProfile(){
sendCommand(0x12);
word profile = getReply(8,0);
return (byte)profile;
}
byte BY8001::getLoopPlaybackMode(){
sendCommand(0x13);
byte loopPlaybackMode = getReply(8,0);
return loopPlaybackMode;
}
char* BY8001::getFirmwareVersion(){
sendCommand(0x14);
rxFunc(4);
String versionReturned = receiveBuffer;
char * fwVersion = new char[5];
versionReturned = versionReturned.substring(0,4);
versionReturned.toCharArray(fwVersion,5);
return fwVersion;
}
word BY8001::getNumberOfTracksTF(){
sendCommand(0x15);
word numberOfTracksTF = getReply(8,0);
return numberOfTracksTF;
}
word BY8001::getNumberOfTracksUSB(){
sendCommand(0x16);
word numberOfTracksUSB = getReply(8,0);
return numberOfTracksUSB;
}
byte BY8001::getCurrentPlaybackDevice(){
sendCommand(0x18);
byte device = getReply(8,0);
return device;
}
word BY8001::getCurrentTrackTF(){
sendCommand(0x19);
word currentTrackTF = getReply(8,0);
return currentTrackTF;
}
word BY8001::getCurrentTrackUSB(){
sendCommand(0x1A);
word currentTrackUSB = getReply(8,0);
return currentTrackUSB;
}
word BY8001::getElapsedTrackPlaybackTime(){
sendCommand(0x1C);
word elapsedTrackPlaybackTime = getReply(8,2);
return elapsedTrackPlaybackTime;
}
word BY8001::getTotalTrackPlaybackTime(){
sendCommand(0x1D);
word totalTrackPlaybackTime = getReply(8,2);
return totalTrackPlaybackTime;
}
char* BY8001::getFileNameCurrentTrack(){
// delay 130ms
unsigned long mTimer = millis(); while (millis() - mTimer < 130) {}
sendCommand(0x1E);
rxFunc(15);
String nameReturned = receiveBuffer;
char * name = new char[10];
nameReturned = nameReturned.substring(0,15);
if (nameReturned.endsWith("\r\n") == true) {
nameReturned = nameReturned.substring(2,10);
}
else {
nameReturned = nameReturned.substring(0,8);
}
nameReturned.toCharArray(name,9);
return name;
}
word BY8001::getNumberOfTracksCurrentFolder(){
sendCommand(0x1F);
word numberTracksCurrentFolder = getReply(8,2);
return numberTracksCurrentFolder;
}