AdmiralCrunch
Well-known member
hi
on my SDCard (T_3.6) I have the following structure:
| - startCfg.txt
| configs \ default.txt
| configs \ myConf.txt
| pattern \ default.txt
| pattern \ myPattern.txt
I read a main-configfile startCfg.txt to know which further config-files I need ro load.
Everything worked well until I added the pattern-folder & files to the SD-Card .. oO .. I really did NO change to the code.. anyway, now the SD.open(fileToLoad.c_str()) does not work. When I write the path and file directly in SD.open() it works..
on my SDCard (T_3.6) I have the following structure:
| - startCfg.txt
| configs \ default.txt
| configs \ myConf.txt
| pattern \ default.txt
| pattern \ myPattern.txt
I read a main-configfile startCfg.txt to know which further config-files I need ro load.
Everything worked well until I added the pattern-folder & files to the SD-Card .. oO .. I really did NO change to the code.. anyway, now the SD.open(fileToLoad.c_str()) does not work. When I write the path and file directly in SD.open() it works..
Code:
#include <SD.h>
#include <SPI.h>
#include <string.h>
#include <ctype.h>
#include <cfg.h>
bool debug = true;
/* #### READ SD #### */
File root;
File startCfg;
File loadedCfg;
File loadedPattern;
const int chipSelect = BUILTIN_SDCARD;
String cfgName;
String cfgValue;
String helpVarString;
void setup() {
// SERIAL PORT
Serial.begin(9600);
while ( !Serial && (millis() < 4000) ) {
// ..
}
// INIT SD & LOAD CONFIG
Serial.println(".................. LOADING SD CONFIG ");
if (!SD.begin(chipSelect)) {
if(debug == true) {
Serial.println("SD initialization failed!");
}
return;
}
// READ INIT-CONFIG & LOAD INIT-MAPPING
startCfg = SD.open("startCfg.txt");
if (startCfg) {
Serial.println(".................. LOADING startCfg ");
while (startCfg.available()) {
helpVarString = startCfg.readStringUntil('\n');
cfgName = getValue(helpVarString, ':', 0);
cfgValue = getValue(helpVarString, ':', 1);
if(cfgName == "MappingPreset") {
// load cfg file
loadCfgFile(cfgValue);
}
}
startCfg.close();
} else {
if(debug == true) {
Serial.println("error opening startCfg.txt");
}
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void loadCfgFile(String cfgFile) {
String fileToLoad = "/configs/" + cfgFile + ".txt";
int trackCount = 0;
loadedCfg = SD.open(fileToLoad.c_str());
if (loadedCfg) {
while (loadedCfg.available()) {
helpVarString = loadedCfg.readStringUntil('\n');
cfgName = getValue(helpVarString, ':', 0);
cfgValue = getValue(helpVarString, ':', 1);
if(cfgName == "END") {
trackCount++;
}
int b = atoi(cfgValue.c_str());
//trackMidiMapping[trackCount].setTrackCtrl(cfgName, b, trackCount);
Serial.println(cfgName);
}
loadedCfg.close();
} else {
Serial.print("error opening "); Serial.print(cfgFile); Serial.println(".txt");
}
}
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}