-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.cpp
More file actions
230 lines (193 loc) · 4.47 KB
/
player.cpp
File metadata and controls
230 lines (193 loc) · 4.47 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
// File player.cpp
// Author: Renzo Dani
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
#include <Arduino.h>
#include <SD.h>
#include "string.h"
#include "player.h"
#include "config.h"
#include "vs10xx.h"
#include "newSDLib.h"
#include "Keypad.h"
#define fileNameSize 50
unsigned char playStop = 1; // play or stop flag,1-play,0-stop
int sameFileCheck = 0;
bool type_wav = true;
int dirN = 0;
int fileN = 0;
File currentFile;
/**
* Restart play list
*/
void rewind(){
fileN = 0;
}
void nextdir(){
dirN++;
if(dirN > 10) dirN = 0;
}
void nextFile(){
fileN++;
}
/**
* Initialize current playing:
* 1. create file name
* 2. check file exists
* 3. if not try the other extension
* 4. if already tried the other extension restart play list
*/
void initP(){
playStop = 1;
Serial.print("open dir:");
Serial.print(dirN);
Serial.print(" file:");
Serial.println(fileN);
char fileName[fileNameSize];
String fnS = String(dirN);
fnS += "/";
fnS += fileN;
if(type_wav){
fnS += ".wav";
}else{
fnS += ".mp3";
}
fnS.toCharArray(fileName, fileNameSize);
currentFile = SD.open(fileName);
if(!currentFile){//file does not exists
sameFileCheck++;
playStop = 0;//stop playing and do not skip to next file
type_wav = !type_wav; //try other extension
if(sameFileCheck >= 2){
rewind(); //all extensions checked: restart play list
}
return;
}else{
sameFileCheck = 0;
}
}
/**
* Close current file and point to the next one.
*/
void closeP(){
if(currentFile != NULL && currentFile) currentFile.close();
if(playStop == 1) nextFile();
}
/**
* Copy content of the file to the buffer.
*/
int readFile(byte *buffer, int len) {
int readLen = 0;
readLen = currentFile.read((char*)buffer,len);
return readLen;
}
/*******************************************************************/
void scanKey() {
String msg;
// Fills kpd.key[ ] array with up-to 10 active keys.
// Returns true if there are ANY active keys.
if (kpd.getKeys()) {
for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
{
if (kpd.key[i].stateChanged) // Only find keys that have changed state.
{
switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
case PRESSED:
msg = " PRESSED.";
pressed(kpd.key[i].kchar);
break;
case HOLD:
msg = " HOLD.";
break;
case RELEASED:
msg = " RELEASED.";
break;
case IDLE:
msg = " IDLE.";
}
Serial.print("Key ");
Serial.print(kpd.key[i].kchar);
Serial.println(msg);
}
}
}
}
void pressed(char presseChar) {
if(presseChar == '9') return; //there is currently a problem on hardware, so skip button 9
int dirPressed;
if(presseChar == '*'){
dirPressed = 9;
}else if(presseChar == '#'){
dirPressed = 10;
}else{
dirPressed = presseChar - '0';
}
Serial.print("choose:");
Serial.println(dirPressed);
if(dirPressed == dirN){
nextFile();
}else{
dirN = dirPressed;
rewind();
}
playStop = 0;
}
//do some interactive things when vs1053 is busy
void AvailableProcessorTime() {
scanKey();
}
int playFile() {
initP();
AvailableProcessorTime();
if (0 == playStop){
closeP();
return 0;
}
Mp3SoftReset(); //it is necessary to play each music file
int readLen = 0;
byte readBuf[READ_BUF_LEN];
byte *tp = readBuf;
while (1) {
readLen = readFile(readBuf, READ_BUF_LEN); //read file content, 32 byte every time
tp = readBuf;
//Serial.println(readLen);
Mp3SelectData();
while (tp < readBuf + readLen) {
if (!MP3_DREQ) {
while (!MP3_DREQ) {
Mp3DeselectData();
while (1) {
AvailableProcessorTime(); //here vs1053 is busy
if (0 == playStop){
closeP();
return 0;
}else{
break;
}
}
Mp3SelectData();
}
}
// Send music content data to VS10xx
SPIPutChar(*tp++);
}
SPIWait();
Mp3DeselectData();
if (readLen < READ_BUF_LEN) {
Mp3WriteRegister(SPI_MODE, 0, SM_OUTOFWAV);
SendZerosToVS10xx();
break;
}
};
Serial.println("played over\r\n");
closeP();
return 0; //OK Exit
}