/
player.cpp
286 lines (231 loc) · 5.6 KB
/
player.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
#include "filesys.h"
#include "storage.h"
#include "string.h"
#include "mmc.h"
#include "player.h"
#include "config.h"
#include "ui.h"
#include "vs10xx.h"
#include "SoftwareSerial.h"
extern SoftwareSerial mySerial;
/** Playing State Global */
playingstatetype playingState = PS_NORMAL;
unsigned char playStop = 1; // play or stop flag,1-play,0-stop
unsigned char currentFile = 0;
/** Plays a disk file. Returns 1) if the file ends or 2) if the global
variable playingState is not PS_NORMAL i.e. user has requested
stop or next or previous.*/
void PlayCurrentFile()
{
char c, nFragments;
playingState = PS_NORMAL; /* Request to play normally */
//uiMode = UI_SPEC; /* User interface: show title SPECANA FOR VS1003*/
///LcdLocateHome();
///LcdPutConstantString("Opening ");
//Serial.print("\r\nBuilding file fragment table...");
//sectorAddress.l = album[currentAlbumCnt].track[currentFile].trackAddr.l;
delay(100);//delay here is very important, give some time to sd card.---by Icing
nFragments = BuildFragmentTable(); /* Too slow, rewrite! */
//Serial.print("Fragments: ");
//Serial.print(nFragments,DEC);
///LcdLocateHome();
///LcdPutConstantString("Playing ");
for (c=0; c<nFragments; c++){
sectorAddress.l = fragment[c].start;
//ConsoleWrite ("\r\nPlayer: Playing from sector ");
//ConsolePutUInt (sectorAddress.l);
if (PlayDiskSectors(fragment[c].length)!=0){
Mp3WriteRegister(SPI_MODE,0,SM_OUTOFWAV);
SendZerosToVS10xx();
return; //return without touching the value of playingState
}
}
SendZerosToVS10xx();
// After finishing normally default to requesting to play next song
playingState = PS_NEXT_SONG;
}
unsigned char g_volume = 40;//used for controling the volume
int redPwm = 200;//used for controling the brightness of red led
unsigned int greenFreq = 5000;//used for controling the flash frequency of green led
void ControlLed()
{
static unsigned char greenOnOff = 1;
if( 0 == greenFreq--)
{
greenOnOff = 1-greenOnOff;
greenFreq = 5000;
}
if(greenOnOff)
{
GREEN_LED_ON();
}
else
{
GREEN_LED_OFF();
}
analogWrite(9,redPwm);
}
void CheckKey()
{
//static unsigned char volume = 40;
static unsigned int vu_cnt = 1000;//volume up interval
static unsigned int vd_cnt = 1000;//volume down interval
if(0 == PSKey)
{
playStop = 1-playStop;
delay(20);
while(0 == PSKey);
delay(20);
}
if(0 == NTKey)
{
playingState = PS_NEXT_SONG;
delay(20);
while(0 == NTKey);
delay(20);
}
else if(0 == BKKey)
{
playingState = PS_PREVIOUS_SONG;
delay(20);
while(0 == BKKey);
delay(20);
}
else if(0 == VUKey)
{
if(--vu_cnt == 0)
{
if (g_volume-- == 0) g_volume = 0; //Change + limit to 0 (maximum volume)
Mp3SetVolume(g_volume,g_volume);
redPwm = (175-g_volume)*3>>1;
if(redPwm >255)
{
redPwm = 255;
}
if(redPwm < 0)
{
redPwm = 0;
}
//Serial.println(redPwm,DEC);
vu_cnt = 1000;
}
}
else if (0 == VDKey)
{
if(--vd_cnt == 0)
{
if (g_volume++ == 254) g_volume = 254; //Change + limit to 254 (minimum vol)
Mp3SetVolume(g_volume,g_volume);
redPwm = 305-(g_volume<<1);
if(redPwm >255)
{
redPwm = 255;
}
if(redPwm < 0)
{
redPwm = 0;
}
//Serial.println(redPwm,DEC);
vd_cnt = 1000;
}
}
}
void IPODCommandProcess()
{
if(mySerial.available())
{
delay(10);
int cmdBuffer[40];
int len = mySerial.available();
int i = 0;
Serial.print(len,HEX);
while(len--)
{
cmdBuffer[i++] = mySerial.read();
//Serial.print(cmdBuffer[i-1],HEX);
}
if(0xff == cmdBuffer[0] && 0x55 == cmdBuffer[1] && 0x03 == cmdBuffer[2] && 0x02 == cmdBuffer[3])
{
switch (cmdBuffer[5])
{
case 0x01:playStop = 1-playStop;break;
case 0x02:if (g_volume-- == 0) g_volume = 0;Mp3SetVolume(g_volume,g_volume);break;
case 0x04:if (g_volume++ == 254) g_volume = 254;Mp3SetVolume(g_volume,g_volume);break;
case 0x08:playingState = PS_NEXT_SONG;break;
case 0x10:playingState = PS_PREVIOUS_SONG;break;
case 0x20:playingState = PS_NEXT_SONG;break;//it should be next Album
case 0x40:playingState = PS_PREVIOUS_SONG;break;//it shuld be previous Album
case 0x80:playStop = 1-playStop;break;
default: break;
}
//wait remote button release
unsigned char continueZeros = 0;
while(1)
{
if(mySerial.available())
{
if(mySerial.read() == 0)
{
continueZeros++;
//Serial.print(continueZeros,DEC);
if(5 == continueZeros)
break;
}
else
{
continueZeros = 0;
}
}
}
}
}
}
/** This function is called when the player is playing a song
and there is free processor time. The basic task of this
function is to implement the player user interface. */
void AvailableProcessorTime()
{
do
{
CheckKey();
IPODCommandProcess();
if(0 == playStop)
{
GREEN_LED_ON();
}
}while(0 == playStop);
//do other things
ControlLed();
}
void Play()
{
playingState = PS_NEXT_SONG;
currentFile = 1;
//cyclely play
while(1)
{
//CheckPlay();
//CheckKey();
AvailableProcessorTime();
if(1 ==playStop)
{
if(OpenFile(currentFile))
{
//if open failed, then try it again
if(OpenFile(currentFile))
{
playStop = 0;
playingState = PS_NEXT_SONG;
currentFile = 1;
continue;
}
}
PlayCurrentFile();
if (playingState == PS_PREVIOUS_SONG) currentFile--;
if (playingState == PS_NEXT_SONG) currentFile++;
if (currentFile==0) currentFile = 1;
//if (playingState == PS_END_OF_SONG) playingState = PS_NORMAL;
Mp3SoftReset();
}
}
}