SDcard.ino 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. //Serial.println("readline");
  57. //check selector (1st word of line) and execute corresponding function
  58. String selector = getSubstring(buf, ' ', 0);
  59. //Serial.println(selector);
  60. if (selector == "sound") {
  61. String track = getSubstring(buf, ' ', 2) ;
  62. String out = getSubstring(buf, ' ', 1) ;
  63. if (out == "stop") { tsunami.stopAllTracks(); Serial.println("Stopping all sounds");}
  64. else if (out == "fadeout") {
  65. Serial.println("Fading all sounds") ;
  66. for (int track ; track < 60 ; track++) {
  67. tsunami.trackFade(track, 0, 500, 1) ;
  68. }
  69. }
  70. else {
  71. Serial.println("Playing track " + track + " to output " + out);
  72. tsunami.trackPlayPoly(track.toInt(), out.toInt()-1, true);
  73. }
  74. }
  75. else if (selector == "led") {
  76. //Serial.println("led");
  77. String zone = getSubstring(buf, ' ', 1);
  78. String palette = getSubstring(buf, ' ', 2);
  79. String bright = getSubstring(buf, ' ', 3);
  80. String fadeTime = getSubstring(buf, ' ', 4);
  81. //Serial.println(getSubstring(buf, ' ', 2));
  82. if (zone == "lustre"){
  83. if (palette == "BEAT"){palette_BEAT = true; lustre_currentBrightness = bright.toInt() ; BPM = fadeTime.toInt() ;paletteBeatStart = millis();Serial.println("Leds BEAT");}
  84. else if (palette == "MOVE") {Serial.println("Leds MOVE");palette_MOVE = true; lustre_currentBrightness = bright.toInt() ; led_move = fadeTime.toInt();}
  85. else {
  86. palette_BEAT = false ;
  87. palette_MOVE = false;
  88. if (palette == "MER"){lustre_targetPalette = palette_MER ;}
  89. else if (palette == "SOLEIL"){lustre_targetPalette = palette_SOLEIL ;}
  90. else if (palette == "CREPUSCULE"){lustre_targetPalette = palette_CREPUSCULE ;}
  91. else if (palette == "FLAMINGO"){lustre_targetPalette = palette_FLAMINGO ;}
  92. lustre_targetBrightness = bright.toInt();
  93. lustre_stepBrightness = constrain( abs((lustre_targetBrightness - lustre_currentBrightness)) / (float)((fadeTime.toInt()+1)/refreshRate), 0, 255) ;
  94. if(lustre_currentBrightness > lustre_targetBrightness) { lustre_stepBrightness *= -1 ;}
  95. }
  96. Serial.print ("LEDS : zone ");
  97. Serial.print (zone);
  98. Serial.print (" - palette ");
  99. Serial.print (palette);
  100. Serial.print (" - brightness : ");
  101. Serial.print (lustre_targetBrightness);
  102. Serial.print (" - fade time : ");
  103. Serial.print (fadeTime);
  104. Serial.print (" - steps : ");
  105. Serial.println (lustre_stepBrightness);
  106. }
  107. else if (zone == "table"){
  108. if (palette == "MER"){table_targetPalette = palette_MER ;}
  109. else if (palette == "SOLEIL"){table_targetPalette = palette_SOLEIL ;}
  110. else if (palette == "CREPUSCULE"){table_targetPalette = palette_CREPUSCULE ;}
  111. else if (palette == "FLAMINGO"){table_targetPalette = palette_FLAMINGO ;}
  112. table_targetBrightness = bright.toInt();
  113. table_stepBrightness = constrain( abs((table_targetBrightness - table_currentBrightness)) / (float)(fadeTime.toInt()/refreshRate), 0, 255) ;
  114. if(table_currentBrightness > table_targetBrightness) { table_stepBrightness *= -1 ;}
  115. Serial.print ("LEDS : zone ");
  116. Serial.print (zone);
  117. Serial.print (" - palette ");
  118. Serial.print (palette);
  119. Serial.print (" - brightness : ");
  120. Serial.print (table_targetBrightness);
  121. Serial.print (" - fade time : ");
  122. Serial.print (fadeTime);
  123. Serial.print (" - steps : ");
  124. Serial.println (table_stepBrightness);
  125. }
  126. //Serial.println("Led mode " + getSubstring(buf, ' ', 1));
  127. }
  128. else if (selector == "dimmer") {
  129. String dim_val = getSubstring(buf, ' ', 1);
  130. if (dim_val == "RANDOM") {dimmer_random = true ; Serial.println("dimmer random mode");}
  131. else if (dim_val == "MOVE" ) { dimmer_move = getSubstring(buf, ' ', 2).toInt(); }
  132. else {
  133. dimmer_random = false ;
  134. dimmer_move = 0 ;
  135. int dimmer_fade = constrain(getSubstring(buf, ' ', 2).toInt(), 50, 50000); // si 0, pas d'update
  136. Serial.println("Dimmer fade à " + dim_val + " en " + dimmer_fade + "ms");
  137. if (dim_val == "LOW") {dimmer_value = dimmer_low ;}
  138. else if (dim_val == "MID") {dimmer_value = dimmer_mid ;}
  139. else if (dim_val == "HIGH") {dimmer_value = dimmer_high ;}
  140. else if (dim_val == "OFF") {dimmer_value = 0 ;}
  141. else { dimmer_value = dim_val.toInt();} //value%
  142. dimmer.setTime(dimmer_fade, true);
  143. dimmer.set(percent2PWM(dimmer_value));
  144. //Serial.println(percent2PWM(dimmer_value));
  145. }
  146. // Serial.println(dimmer_delta);
  147. }
  148. else if (selector == "delay") {
  149. String wait = getSubstring(buf, ' ', 1);
  150. Serial.println("delai " + wait + " ms");
  151. inibSDreading = true ;
  152. delay_start = millis();
  153. delay_length = wait.toInt() ;
  154. // delay_flag = 0 ;
  155. mgr.addListener(new EvtTimeListener(50 , true, (EvtAction)checkDelay));
  156. //delay(wait.toInt());
  157. }
  158. else if (selector == ""){}
  159. else {
  160. Serial.print(selector);
  161. Serial.println(": pas compris");
  162. }
  163. }
  164. else {
  165. Serial.println("EOF");
  166. inibSDreading = true;
  167. //btnFile.close();
  168. //EvtResetButtonContext();
  169. }
  170. }
  171. // else{
  172. // Serial.println("not reading");
  173. // }
  174. return false ;
  175. }