-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.ino
203 lines (168 loc) · 4.07 KB
/
Storage.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
#include <SD.h>
#define SDCARD_DETECT_PIN 9
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 11
#define SDCARD_MISO_PIN 12
#define SDCARD_SCK_PIN 13
const int maxTestFiles = 10; // Adjust if more test files are added
String testFileNames[maxTestFiles];
int testFileCount = 0;
int currentTestIndex = 0;
String currentPlayingFile = "No test file";
void setupStorage()
{
pinMode(SDCARD_DETECT_PIN, INPUT_PULLUP);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setMISO(SDCARD_MISO_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN)))
{
Serial.println("SD Card initialization failed!");
pixels.setPixelColor(0, pixels.Color(10, 0, 0));
pixels.show();
sdFound = false;
}
else
{
sdFound = true;
if (dev_mode)
{
File root = SD.open("/");
File testFile = root.openNextFile();
while (testFile)
{
if (!testFile.isDirectory() && String(testFile.name()).endsWith(".wav"))
{
if (testFileCount < maxTestFiles) // Prevents overflow
{
testFileNames[testFileCount++] = testFile.name();
}
}
testFile = root.openNextFile(); // Check the next file
}
root.close();
}
}
}
void playFile()
{
if (testFileCount == 0)
{
Serial.println("No .wav files found.");
return;
}
if (!player.isPlaying()) // Only play the file if we aren't already playing something
{
String fileName = testFileNames[currentTestIndex];
Serial.print("Playing: ");
Serial.println(fileName);
currentPlayingFile = fileName;
player.play(fileName.c_str());
// Delay to wait for the player to start playing and toggle on the isPlaying() state.
delay(25);
}
}
void playNextFile()
{
if (testFileCount == 0)
{
Serial.println("No .wav files found.");
return;
}
if (player.isPlaying())
{
player.stop();
}
currentTestIndex = (currentTestIndex + 1) % testFileCount; // Select the next index, looping back if we're at the end
dirtyDisplay = true;
playFile();
}
String getCurrentPlayingFileName()
{
return currentPlayingFile;
}
// const char *getTestFile()
// {
// File root = SD.open("/");
// int index = 0;
// File firstTest = root.openNextFile();
// if (!firstTest)
// {
// Serial.println("1) Failed to open a test file.");
// root.close();
// return 0;
// }
// while (firstTest.name()[0] == '.')
// {
// firstTest.close();
// firstTest = root.openNextFile();
// if (!firstTest)
// {
// Serial.println("2) Failed to open a test file.");
// root.close();
// return 0;
// }
// }
// if(currentTestIndex == 0)
// {
// const char *name = firstTest.name();
// firstTest.close();
// root.close();
// return name;
// }
// File test;
// do
// {
// test = root.openNextFile();
// index++;
// if(!test)
// {
// Serial.println("3) Failed to open a test file.");
// const char *name = firstTest.name();
// root.close();
// currentTestIndex = 0;
// firstTest.close();
// return name;
// }
// while (test.name()[0] == '.')
// {
// test = root.openNextFile();
// if(!test)
// {
// Serial.println("4) Failed to open a test file.");
// const char *name = firstTest.name();
// root.close();
// currentTestIndex = 0;
// firstTest.close();
// return name;
// }
// }
// }
// while(index < currentTestIndex);
// const char *name = test.name();
// root.close();
// firstTest.close();
// test.close();
// return name;
// }
// void incrementCurrentTest()
// {
// player.stop();
// currentTestIndex++;
// dirtyDisplay = true;
// }
// void playSdAudio(const char *filename)
// {
// if (!filename)
// {
// return;
// }
// if (!player.isPlaying())
// {
// Serial.print("Playing ");
// Serial.println(filename);
// player.play(filename);
// // Delay to wait for the player to start playing and toggle on the isPlaying() state.
// delay(25);
// }
// }