forked from canvasus/CreatorMaster2k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdMgr.cpp
236 lines (214 loc) · 6.13 KB
/
sdMgr.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
#include "sdMgr.h"
const int chipSelect = BUILTIN_SDCARD;
bool sdStatus = false;
ProjectInfo projectInfo[NR_PROJECTS];
void initSDcard()
{
if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); }
else
{
for (uint8_t projectId = 0; projectId < NR_PROJECTS; projectId++)
{
setProjectfolder(projectId);
loadProjectInfo();
}
sdStatus = true;
setProjectfolder(0);
}
}
void loadProject()
{
//Serial.printf("load project %d\n", currentProject);
loadPatterns();
loadArrangementSettings();
loadTransport();
loadProjectInfo();
}
void loadPatterns()
{
for (uint8_t patternId = 0; patternId < NR_PATTERNS; patternId++)
{
loadTrackEvents(patternId);
loadTrackSettings(patternId);
}
}
void saveProject()
{
for (uint8_t patternId = 0; patternId < NR_PATTERNS; patternId++)
{
saveTrackEvents(patternId);
saveTrackSettings(patternId);
}
saveArrangementSettings();
saveTransport();
saveProjectInfo();
}
void loadTrackEvents(uint8_t patternNr)
{
char fileName[18];
sprintf(fileName, "CM2K_PATTERN_%03d", patternNr);
if (SD.exists(fileName))
{
File dataFile = SD.open(fileName, FILE_READ);
for (uint8_t trackNr = 0; trackNr < NR_TRACKS; trackNr++)
{
uint16_t nrEvents = 0;
dataFile.read((uint8_t *)&trackNr, sizeof(trackNr));
dataFile.read((uint8_t *)&nrEvents, sizeof(nrEvents));
if (nrEvents > 0)
{
event * loadedEvents = (event*)malloc(nrEvents * sizeof(event));
memset(loadedEvents, 0, nrEvents * sizeof(event));
dataFile.read((uint8_t *)loadedEvents, nrEvents * sizeof(event));
patterns[patternNr].tracks[trackNr].paste(loadedEvents, nrEvents);
free(loadedEvents);
}
else patterns[patternNr].tracks[trackNr].clear();
}
dataFile.close();
}
}
uint16_t getNrEvents(event * eventBuffer, uint16_t bufferSizeBlocks)
{
uint16_t nrEvents = 0;
for (uint16_t eventId = 0; eventId < bufferSizeBlocks * NR_EVENTS; eventId++)
{
if (eventBuffer[eventId].type != 0) nrEvents++;
}
return nrEvents;
}
void saveTrackEvents(uint8_t patternNr)
{
char fileName[18];
sprintf(fileName, "CM2K_PATTERN_%03d", patternNr);
SD.remove(fileName);
File dataFile = SD.open(fileName, FILE_WRITE);
for (uint8_t trackNr = 0; trackNr < NR_TRACKS; trackNr++)
{
uint16_t nrEvents = patterns[patternNr].tracks[trackNr]._nrEvents;
event * trackEvents = patterns[patternNr].tracks[trackNr].copy();
dataFile.write((uint8_t *)&trackNr, sizeof(trackNr));
dataFile.write((uint8_t *)&nrEvents, sizeof(nrEvents));
if (nrEvents > 0) dataFile.write((uint8_t *)trackEvents, nrEvents * sizeof(event));
}
dataFile.close();
}
void loadSystemSettings()
{
char fileName[12] = "CM2K_SYSTEM";
if (SD.exists(fileName))
{
File dataFile = SD.open(fileName, FILE_READ);
//dataFile.read((uint8_t *)&projectInfo[currentProject], sizeof(ProjectInfo));
dataFile.close();
}
}
void saveSystemSettings()
{
char fileName[12] = "CM2K_SYSTEM";
SD.remove(fileName);
File dataFile = SD.open(fileName, FILE_WRITE);
//dataFile.write((uint8_t *)&projectInfo[currentProject], sizeof(ProjectInfo));
dataFile.close();
}
void loadProjectInfo()
{
char fileName[14] = "CM2K_PROJINFO";
if (SD.exists(fileName))
{
File dataFile = SD.open(fileName, FILE_READ);
dataFile.read((uint8_t *)&projectInfo[currentProject], sizeof(ProjectInfo));
dataFile.close();
}
}
void saveProjectInfo()
{
char fileName[14] = "CM2K_PROJINFO";
SD.remove(fileName);
File dataFile = SD.open(fileName, FILE_WRITE);
dataFile.write((uint8_t *)&projectInfo[currentProject], sizeof(ProjectInfo));
dataFile.close();
}
void loadTrackSettings(uint8_t patternNr)
{
char fileName[20];
sprintf(fileName, "CM2K_PATTERNSET_%03d", patternNr);
if (SD.exists(fileName))
{
File dataFile = SD.open(fileName, FILE_READ);
dataFile.read((uint8_t *)&patterns[patternNr].config, sizeof(PatternConfig));
for (uint8_t trackNr = 0; trackNr < NR_TRACKS; trackNr++)
{
dataFile.read((uint8_t *)&patterns[patternNr].tracks[trackNr].config, sizeof(TrackConfig));
patterns[patternNr].tracks[trackNr].syncSettings();
patterns[patternNr].syncPortSettings(trackNr);
}
dataFile.close();
}
}
void saveTrackSettings(uint8_t patternNr)
{
char fileName[20];
sprintf(fileName, "CM2K_PATTERNSET_%03d", patternNr);
SD.remove(fileName);
File dataFile = SD.open(fileName, FILE_WRITE);
dataFile.write((uint8_t *)&patterns[patternNr].config, sizeof(PatternConfig));
for (uint8_t trackNr = 0; trackNr < NR_TRACKS; trackNr++)
{
dataFile.write((uint8_t *)&patterns[patternNr].tracks[trackNr].config, sizeof(TrackConfig));
}
dataFile.close();
}
void loadArrangementSettings()
{
const char fileName[9] = "CM2K_ARR";
if (SD.exists(fileName))
{
File dataFile = SD.open(fileName, FILE_READ);
for (uint8_t arrItem = 0; arrItem < NR_ARRITEMS; arrItem++)
{
dataFile.read((uint8_t *)&arrangement.arrangementItems_a[arrItem], sizeof(ArrangementItem));
}
dataFile.close();
}
}
void saveArrangementSettings()
{
const char fileName[9] = "CM2K_ARR";
SD.remove(fileName);
File dataFile = SD.open(fileName, FILE_WRITE);
for (uint8_t arrItem = 0; arrItem < NR_ARRITEMS; arrItem++)
{
dataFile.write((uint8_t *)&arrangement.arrangementItems_a[arrItem], sizeof(ArrangementItem));
}
dataFile.close();
}
void loadTransport()
{
const char fileName[15] = "CM2K_TRANSPORT";
if (SD.exists(fileName))
{
File dataFile = SD.open(fileName, FILE_READ);
dataFile.read((uint8_t *)&transport, sizeof(Transport));
dataFile.close();
}
syncTransportSettings();
}
void saveTransport()
{
const char fileName[15] = "CM2K_TRANSPORT";
SD.remove(fileName);
File dataFile = SD.open(fileName, FILE_WRITE);
dataFile.write((uint8_t *)&transport, sizeof(Transport));
dataFile.close();
}
void setProjectfolder(uint8_t projectId)
{
//Serial.printf("Set folder %d\n", projectId);
SD.sdfs.chdir("/");
char fileName[17];
sprintf(fileName, "CM2K_PROJECT_%03d", projectId);
if (!SD.exists(fileName)) SD.mkdir(fileName);
SD.sdfs.chdir(fileName);
currentProject = projectId;
}