SDcard.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 readSDFile() {
  17. if (!inibSDreading) { // if currently delaying, don't read a line
  18. if (btnFile.available()) {
  19. buf = btnFile.readStringUntil('\n');
  20. //check selector (1st word of line) and execute corresponding function
  21. String selector = getSubstring(buf, ' ', 0);
  22. //Serial.println(selector);
  23. if (selector == "sound") {
  24. String track = getSubstring(buf, ' ', 2) ;
  25. String out = getSubstring(buf, ' ', 1) ;
  26. Serial.println("Playing track " + track + " to output " + out);
  27. tsunami.trackPlayPoly(track.toInt(), out.toInt()-1, true);
  28. }
  29. else if (selector == "led") {
  30. // String mode = getSubstring(buf, ' ', 1);
  31. // if (mode == "L"){
  32. // int R = getSubstring(buf, ' ', 2).toInt();
  33. // int G = getSubstring(buf, ' ', 3).toInt();
  34. // int B = getSubstring(buf, ' ', 4).toInt();
  35. // for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
  36. // lustreLeds[i].setRGB(R, G, B) ;
  37. // }
  38. // }
  39. // if (mode == "T"){
  40. // int R = getSubstring(buf, ' ', 2).toInt();
  41. // int G = getSubstring(buf, ' ', 3).toInt();
  42. // int B = getSubstring(buf, ' ', 4).toInt();
  43. // for (int i ; i<NUM_LEDS_TABLE ; i++) {
  44. // tableLeds[i].setRGB(R, G, B) ;
  45. // }
  46. // }
  47. // Serial.println("Led mode " + getSubstring(buf, ' ', 1));
  48. }
  49. else if (selector == "dimmer") {
  50. Serial.println("Dimmer fade à " + getSubstring(buf, ' ', 1) + " en " + getSubstring(buf, ' ', 2) + "ms");
  51. int dimmer_value = getSubstring(buf, ' ', 1).toInt();
  52. int dimmer_fade = getSubstring(buf, ' ', 2).toInt();
  53. dimmer.setTime(dimmer_fade);
  54. dimmer.set(dimmer_value);
  55. // Serial.println(dimmer_delta);
  56. }
  57. else if (selector == "delay") {
  58. String wait = getSubstring(buf, ' ', 1);
  59. Serial.println("delai " + wait + " ms");
  60. inibSDreading = true ;
  61. delay_start = millis();
  62. delay_length = wait.toInt() ;
  63. // delay_flag = 0 ;
  64. mgr.addListener(new EvtTimeListener(50 , true, (EvtAction)checkDelay));
  65. //delay(wait.toInt());
  66. }
  67. else if (selector == ""){}
  68. else {
  69. Serial.print(selector);
  70. Serial.println(": pas compris");
  71. }
  72. }
  73. else {
  74. Serial.println("EOF");
  75. btnFile.close();
  76. inibSDreading = true;
  77. EvtResetButtonContext();
  78. }
  79. }
  80. // else{Serial.println("not reading");}
  81. return false ;
  82. }