SDcard.ino 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ///////////////////////// SD ////////////////////////////////////////////
  2. String getSubstring(String dataString, char separator, int index)
  3. {
  4. int found = 0;
  5. int strIndex[] = { 0, -1 };
  6. int maxIndex = dataString.length() - 1;
  7. for (int i = 0; i <= maxIndex && found <= index; i++) {
  8. if (dataString.charAt(i) == separator || i == maxIndex) {
  9. found++;
  10. strIndex[0] = strIndex[1] + 1;
  11. strIndex[1] = (i == maxIndex) ? i+1 : i;
  12. }
  13. }
  14. return found > index ? dataString.substring(strIndex[0], strIndex[1]) : "";
  15. }
  16. bool readSDconfig(){
  17. File configFile = SD.open("config.txt");
  18. while (configFile.available()) { //as long as there is data available
  19. String buf = configFile.readStringUntil('\n'); // read a line
  20. String keyword = getSubstring(buf, ' ', 0); // update variable according to keyword
  21. if (keyword == "LOW") { dimmer_low = getSubstring(buf, ' ', 1).toInt() ; }
  22. if (keyword == "HIGH") { dimmer_high = getSubstring(buf, ' ', 1).toInt() ; }
  23. if (keyword == "MID") { dimmer_mid = getSubstring(buf, ' ', 1).toInt() ; }
  24. if (keyword == "MER") { fill_solid( palette_MER, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
  25. if (keyword == "SOLEIL") { fill_solid( palette_SOLEIL, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
  26. if (keyword == "CREPUSCULE") { fill_solid( palette_CREPUSCULE, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
  27. if (keyword == "FLAMINGO") { fill_solid( palette_FLAMINGO, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
  28. }
  29. Serial.println("Configuration : ");
  30. Serial.print("Niveaux dimmer (LOW / MID / HIGH) : ");
  31. Serial.print(dimmer_low);
  32. Serial.print(" ");
  33. Serial.print(dimmer_mid);
  34. Serial.print(" ");
  35. Serial.print(dimmer_high);
  36. Serial.println("");
  37. Serial.print("Palette MER : ");
  38. Serial.print(ColorFromPalette(palette_MER, 0));
  39. Serial.print("Palette SOLEIL : ");
  40. Serial.print(ColorFromPalette(palette_SOLEIL, 0));
  41. Serial.print("Palette CREPUSCULE : ");
  42. Serial.print(ColorFromPalette(palette_CREPUSCULE, 0));
  43. Serial.print("Palette FLAMINGO : ");
  44. Serial.print(ColorFromPalette(palette_FLAMINGO, 0));
  45. // Serial.print(" ");
  46. // Serial.print(dimmer_mid);
  47. // Serial.print(" ");
  48. // Serial.print(dimmer_high);
  49. Serial.println("");
  50. configFile.close();
  51. }
  52. bool readSDFile() {
  53. if (!inibSDreading) { // if currently delaying, don't read a line
  54. if (btnFile.available()) {
  55. buf = btnFile.readStringUntil('\n');
  56. //check selector (1st word of line) and execute corresponding function
  57. String selector = getSubstring(buf, ' ', 0);
  58. //Serial.println(selector);
  59. if (selector == "sound") {
  60. String track = getSubstring(buf, ' ', 2) ;
  61. String out = getSubstring(buf, ' ', 1) ;
  62. if (out == "stop") { tsunami.stopAllTracks(); Serial.println("Stopping all sounds");}
  63. else if (out == "fadeout") {
  64. Serial.println("Fading all sounds") ;
  65. for (int track ; track < 60 ; track++) {
  66. tsunami.trackFade(track, 0, 500, 1) ;
  67. }
  68. }
  69. else {
  70. Serial.println("Playing track " + track + " to output " + out);
  71. tsunami.trackPlayPoly(track.toInt(), out.toInt()-1, true);
  72. }
  73. }
  74. else if (selector == "led") {
  75. //Serial.println("led");
  76. String zone = getSubstring(buf, ' ', 1);
  77. String palette = getSubstring(buf, ' ', 2);
  78. String bright = getSubstring(buf, ' ', 3);
  79. String fadeTime = getSubstring(buf, ' ', 4);
  80. //Serial.println(getSubstring(buf, ' ', 2));
  81. if (zone == "lustre"){
  82. if (palette == "BEAT"){palette_BEAT = true; lustre_currentBrightness = bright.toInt() ; BPM = fadeTime.toInt() ;paletteBeatStart = millis();Serial.println("Leds BEAT");}
  83. else if (palette == "MOVE") {Serial.println("Leds MOVE");palette_MOVE = true; lustre_currentBrightness = bright.toInt() ; led_move = fadeTime.toInt();}
  84. else {
  85. palette_BEAT = false ;
  86. palette_MOVE = false;
  87. if (palette == "MER"){lustre_targetPalette = palette_MER ;}
  88. else if (palette == "SOLEIL"){lustre_targetPalette = palette_SOLEIL ;}
  89. else if (palette == "CREPUSCULE"){lustre_targetPalette = palette_CREPUSCULE ;}
  90. else if (palette == "FLAMINGO"){lustre_targetPalette = palette_FLAMINGO ;}
  91. lustre_targetBrightness = bright.toInt();
  92. lustre_stepBrightness = constrain( abs((lustre_targetBrightness - lustre_currentBrightness)) / (float)((fadeTime.toInt()+1)/refreshRate), 0, 255) ;
  93. if(lustre_currentBrightness > lustre_targetBrightness) { lustre_stepBrightness *= -1 ;}
  94. }
  95. }
  96. else if (zone == "table"){
  97. if (palette == "MER"){table_targetPalette = palette_MER ;}
  98. else if (palette == "SOLEIL"){table_targetPalette = palette_SOLEIL ;}
  99. else if (palette == "CREPUSCULE"){table_targetPalette = palette_CREPUSCULE ;}
  100. else if (palette == "FLAMINGO"){table_targetPalette = palette_FLAMINGO ;}
  101. table_targetBrightness = bright.toInt();
  102. table_stepBrightness = constrain( abs((table_targetBrightness - table_currentBrightness)) / (float)(fadeTime.toInt()/refreshRate), 1, 255) ;
  103. if(table_currentBrightness > table_targetBrightness) { table_stepBrightness *= -1 ;}
  104. }
  105. Serial.print ("LEDS : zone ");
  106. Serial.print (zone);
  107. Serial.print (" - palette ");
  108. Serial.print (palette);
  109. Serial.print (" - brightness : ");
  110. Serial.print (lustre_targetBrightness);
  111. Serial.print (" - fade time : ");
  112. Serial.print (fadeTime);
  113. Serial.print (" - steps : ");
  114. Serial.println (lustre_stepBrightness);
  115. //Serial.println("Led mode " + getSubstring(buf, ' ', 1));
  116. }
  117. else if (selector == "dimmer") {
  118. String dim_val = getSubstring(buf, ' ', 1);
  119. if (dim_val == "RANDOM") {dimmer_random = true ; Serial.println("dimmer random mode");}
  120. else if (dim_val == "MOVE" ) { dimmer_move = getSubstring(buf, ' ', 2).toInt(); }
  121. else {
  122. dimmer_random = false ;
  123. dimmer_move = 0 ;
  124. int dimmer_fade = constrain(getSubstring(buf, ' ', 2).toInt(), 50, 50000); // si 0, pas d'update
  125. Serial.println("Dimmer fade à " + dim_val + " en " + dimmer_fade + "ms");
  126. if (dim_val == "LOW") {dimmer_value = dimmer_low ;}
  127. else if (dim_val == "MID") {dimmer_value = dimmer_mid ;}
  128. else if (dim_val == "HIGH") {dimmer_value = dimmer_high ;}
  129. else if (dim_val == "OFF") {dimmer_value = 0 ;}
  130. else { dimmer_value = dim_val.toInt();} //value%
  131. dimmer.setTime(dimmer_fade, true);
  132. dimmer.set(percent2PWM(dimmer_value));
  133. //Serial.println(percent2PWM(dimmer_value));
  134. }
  135. // Serial.println(dimmer_delta);
  136. }
  137. else if (selector == "delay") {
  138. String wait = getSubstring(buf, ' ', 1);
  139. Serial.println("delai " + wait + " ms");
  140. inibSDreading = true ;
  141. delay_start = millis();
  142. delay_length = wait.toInt() ;
  143. // delay_flag = 0 ;
  144. mgr.addListener(new EvtTimeListener(50 , true, (EvtAction)checkDelay));
  145. //delay(wait.toInt());
  146. }
  147. else if (selector == ""){}
  148. else {
  149. Serial.print(selector);
  150. Serial.println(": pas compris");
  151. }
  152. }
  153. else {
  154. Serial.println("EOF");
  155. inibSDreading = true;
  156. // btnFile.close();
  157. EvtResetButtonContext();
  158. }
  159. }
  160. // else{
  161. // Serial.println("not reading");
  162. // }
  163. return false ;
  164. }