///////////////////////// SD //////////////////////////////////////////// String getSubstring(String dataString, char separator, int index) { int found = 0; int strIndex[] = { 0, -1 }; int maxIndex = dataString.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (dataString.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found > index ? dataString.substring(strIndex[0], strIndex[1]) : ""; } bool readSDconfig(){ File configFile = SD.open("config.txt"); while (configFile.available()) { //as long as there is data available String buf = configFile.readStringUntil('\n'); // read a line String keyword = getSubstring(buf, ' ', 0); // update variable according to keyword if (keyword == "LOW") { dimmer_low = getSubstring(buf, ' ', 1).toInt() ; } if (keyword == "HIGH") { dimmer_high = getSubstring(buf, ' ', 1).toInt() ; } if (keyword == "MID") { dimmer_mid = getSubstring(buf, ' ', 1).toInt() ; } if (keyword == "MER") { fill_solid( palette_MER, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; } if (keyword == "SOLEIL") { fill_solid( palette_SOLEIL, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; } if (keyword == "CREPUSCULE") { fill_solid( palette_CREPUSCULE, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; } if (keyword == "FLAMINGO") { fill_solid( palette_FLAMINGO, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; } } Serial.println("Configuration : "); Serial.print("Niveaux dimmer (LOW / MID / HIGH) : "); Serial.print(dimmer_low); Serial.print(" "); Serial.print(dimmer_mid); Serial.print(" "); Serial.print(dimmer_high); Serial.println(""); Serial.print("Palette MER : "); Serial.print(ColorFromPalette(palette_MER, 0)); Serial.print("Palette SOLEIL : "); Serial.print(ColorFromPalette(palette_SOLEIL, 0)); Serial.print("Palette CREPUSCULE : "); Serial.print(ColorFromPalette(palette_CREPUSCULE, 0)); Serial.print("Palette FLAMINGO : "); Serial.print(ColorFromPalette(palette_FLAMINGO, 0)); // Serial.print(" "); // Serial.print(dimmer_mid); // Serial.print(" "); // Serial.print(dimmer_high); Serial.println(""); configFile.close(); } bool readSDFile() { if (!inibSDreading) { // if currently delaying, don't read a line if (btnFile.available()) { buf = btnFile.readStringUntil('\n'); //check selector (1st word of line) and execute corresponding function String selector = getSubstring(buf, ' ', 0); //Serial.println(selector); if (selector == "sound") { String track = getSubstring(buf, ' ', 2) ; String out = getSubstring(buf, ' ', 1) ; if (out == "stop") { tsunami.stopAllTracks(); Serial.println("Stopping all sounds");} else if (out == "fadeout") { Serial.println("Fading all sounds") ; for (int track ; track < 60 ; track++) { tsunami.trackFade(track, 0, 500, 1) ; } } else { Serial.println("Playing track " + track + " to output " + out); tsunami.trackPlayPoly(track.toInt(), out.toInt()-1, true); } } else if (selector == "led") { //Serial.println("led"); String zone = getSubstring(buf, ' ', 1); String palette = getSubstring(buf, ' ', 2); String bright = getSubstring(buf, ' ', 3); String fadeTime = getSubstring(buf, ' ', 4); //Serial.println(getSubstring(buf, ' ', 2)); if (zone == "lustre"){ if (palette == "BEAT"){palette_BEAT = true; lustre_currentBrightness = bright.toInt() ; BPM = fadeTime.toInt() ;paletteBeatStart = millis();Serial.println("Leds BEAT");} else if (palette == "MOVE") {Serial.println("Leds MOVE");palette_MOVE = true; lustre_currentBrightness = bright.toInt() ; led_move = fadeTime.toInt();} else { palette_BEAT = false ; palette_MOVE = false; if (palette == "MER"){lustre_targetPalette = palette_MER ;} else if (palette == "SOLEIL"){lustre_targetPalette = palette_SOLEIL ;} else if (palette == "CREPUSCULE"){lustre_targetPalette = palette_CREPUSCULE ;} else if (palette == "FLAMINGO"){lustre_targetPalette = palette_FLAMINGO ;} lustre_targetBrightness = bright.toInt(); lustre_stepBrightness = constrain( abs((lustre_targetBrightness - lustre_currentBrightness)) / (float)((fadeTime.toInt()+1)/refreshRate), 0, 255) ; if(lustre_currentBrightness > lustre_targetBrightness) { lustre_stepBrightness *= -1 ;} } } else if (zone == "table"){ if (palette == "MER"){table_targetPalette = palette_MER ;} else if (palette == "SOLEIL"){table_targetPalette = palette_SOLEIL ;} else if (palette == "CREPUSCULE"){table_targetPalette = palette_CREPUSCULE ;} else if (palette == "FLAMINGO"){table_targetPalette = palette_FLAMINGO ;} table_targetBrightness = bright.toInt(); table_stepBrightness = constrain( abs((table_targetBrightness - table_currentBrightness)) / (float)(fadeTime.toInt()/refreshRate), 1, 255) ; if(table_currentBrightness > table_targetBrightness) { table_stepBrightness *= -1 ;} } Serial.print ("LEDS : zone "); Serial.print (zone); Serial.print (" - palette "); Serial.print (palette); Serial.print (" - brightness : "); Serial.print (lustre_targetBrightness); Serial.print (" - fade time : "); Serial.print (fadeTime); Serial.print (" - steps : "); Serial.println (lustre_stepBrightness); //Serial.println("Led mode " + getSubstring(buf, ' ', 1)); } else if (selector == "dimmer") { String dim_val = getSubstring(buf, ' ', 1); if (dim_val == "RANDOM") {dimmer_random = true ; Serial.println("dimmer random mode");} else if (dim_val == "MOVE" ) { dimmer_move = getSubstring(buf, ' ', 2).toInt(); } else { dimmer_random = false ; dimmer_move = 0 ; int dimmer_fade = constrain(getSubstring(buf, ' ', 2).toInt(), 50, 50000); // si 0, pas d'update Serial.println("Dimmer fade à " + dim_val + " en " + dimmer_fade + "ms"); if (dim_val == "LOW") {dimmer_value = dimmer_low ;} else if (dim_val == "MID") {dimmer_value = dimmer_mid ;} else if (dim_val == "HIGH") {dimmer_value = dimmer_high ;} else if (dim_val == "OFF") {dimmer_value = 0 ;} else { dimmer_value = dim_val.toInt();} //value% dimmer.setTime(dimmer_fade, true); dimmer.set(percent2PWM(dimmer_value)); //Serial.println(percent2PWM(dimmer_value)); } // Serial.println(dimmer_delta); } else if (selector == "delay") { String wait = getSubstring(buf, ' ', 1); Serial.println("delai " + wait + " ms"); inibSDreading = true ; delay_start = millis(); delay_length = wait.toInt() ; // delay_flag = 0 ; mgr.addListener(new EvtTimeListener(50 , true, (EvtAction)checkDelay)); //delay(wait.toInt()); } else if (selector == ""){} else { Serial.print(selector); Serial.println(": pas compris"); } } else { Serial.println("EOF"); inibSDreading = true; // btnFile.close(); EvtResetButtonContext(); } } // else{ // Serial.println("not reading"); // } return false ; }