eventually.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /////////////////////////////Events/////////////////////////////////
  2. ///*resetButtonContext create a listener for each button
  3. // * when triggered, thoses listener execute the same functions with the index of the pressed button :
  4. // * resetButtonContext and kill any running sequence
  5. // * buttonFocus -> lights the current button and shuts the others
  6. // * readSequence -> reads a text file on the SD card with the sequence of actions to execute
  7. // */
  8. bool checkDelay() {
  9. //Serial.println("checkDelay");
  10. //if (delay_flag == 0 ) { delay_flag = 1 ;}
  11. if (millis() - delay_start > delay_length ) {
  12. inibSDreading = false ;
  13. }
  14. //else {inibSDreading = false ;}
  15. return false ;
  16. }
  17. bool updateAll() {
  18. FadeLed::update();
  19. //tsunami.update();
  20. updateLeds() ;
  21. // FastLED.show();
  22. return false;
  23. }
  24. void tlcWrite(){
  25. for (int ledIndex ; ledIndex < 12 ; ledIndex++) { //sizeof(ledState)/sizeof(uint32_t)
  26. int indexDiv = ledIndex / 3 ;
  27. tlc.setLED(indexDiv, ledState[indexDiv*3], ledState[indexDiv*3+1], ledState[indexDiv*3+2]);
  28. // Serial.print(ledState[ledIndex]);
  29. // Serial.print(" ");
  30. }
  31. // Serial.println();
  32. tlc.write();
  33. }
  34. void buttonFocus(int index) {
  35. currentButton = index ;
  36. //Serial.println(currentButton);
  37. //turn off all leds but currentButton one
  38. for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
  39. if (i == currentButton ) {
  40. ledState[11-i] = 10000 ; //65536 max
  41. }
  42. else { ledState[11-i] = 50 ; }
  43. tlcWrite();
  44. }
  45. //et après ya plein de trucs à faire
  46. btnFile.close();
  47. delay(50);
  48. String file = "button";
  49. file+=index ;
  50. file+=".txt";
  51. btnFile = SD.open(file.c_str()) ;
  52. //btnFile = SD.open("button0.txt");
  53. Serial.println("");
  54. Serial.println("opening " + file);
  55. if (!btnFile) {
  56. Serial.println("The text file cannot be opened");
  57. // return false;
  58. }
  59. // while(
  60. EvtResetButtonContext() ; //clean context before starting reading the file
  61. inibSDreading = false ;
  62. //mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
  63. //mgr.addListener(new EvtTimeListener(50, true, (EvtAction)fadeDimmer));
  64. //readSDFile();
  65. }
  66. void stopAll() {
  67. tsunami.stopAllTracks();
  68. dimmer.setTime(1);
  69. dimmer.off();
  70. for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
  71. lustreLeds[i] = CRGB::Black ;
  72. tableLeds[i] = CRGB::Black ;
  73. }
  74. FastLED.show();
  75. for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
  76. if (i == 4 ) {
  77. ledState[11-i] = 65535 ;
  78. }
  79. else { ledState[11-i] = 50 ; }
  80. tlcWrite();
  81. }
  82. EvtResetButtonContext();
  83. }
  84. ///////////////////// CONTEXT ///////////////////////////////
  85. bool button0(){
  86. buttonFocus(0) ;
  87. //lustre_targetPalette = palette_FLAMINGO ;
  88. //lustre_currentBrightness = 255;
  89. return false;
  90. }
  91. bool button1(){
  92. buttonFocus(1) ;
  93. //lustre_targetPalette = palette_SOLEIL;
  94. //lustre_currentBrightness = 0;
  95. // testAll();
  96. return false;
  97. }
  98. bool button2(){
  99. buttonFocus(2) ;
  100. //lustre_targetPalette = palette_CREPUSCULE ;
  101. //lustre_currentBrightness = 0;
  102. return false;
  103. }
  104. bool button3(){
  105. buttonFocus(3) ;
  106. //lustre_targetPalette =palette_MER;
  107. //lustre_currentBrightness = 0;
  108. return false;
  109. }
  110. bool button4(){
  111. buttonFocus(4) ;
  112. //lustre_currentBrightness = 0;
  113. // stopAll();
  114. return false;
  115. }
  116. bool button5(){
  117. buttonFocus(5) ;
  118. return false;
  119. }
  120. bool button6(){
  121. buttonFocus(6) ;
  122. return false;
  123. }
  124. bool button7(){
  125. buttonFocus(7) ;
  126. return false;
  127. }
  128. bool button8(){
  129. buttonFocus(8) ;
  130. return false;
  131. }
  132. bool button9(){
  133. buttonFocus(9) ;
  134. return false;
  135. }
  136. void EvtResetButtonContext () {
  137. Serial.println("Resetting event context");
  138. mgr.resetContext();
  139. delay (50);
  140. // button listeners
  141. mgr.addListener(new EvtPinListener(BUTTON_PIN[0], (EvtAction)button0));
  142. mgr.addListener(new EvtPinListener(BUTTON_PIN[1], (EvtAction)button1));
  143. mgr.addListener(new EvtPinListener(BUTTON_PIN[2], (EvtAction)button2));
  144. mgr.addListener(new EvtPinListener(BUTTON_PIN[3], (EvtAction)button3));
  145. mgr.addListener(new EvtPinListener(BUTTON_PIN[4], (EvtAction)button4));
  146. mgr.addListener(new EvtPinListener(BUTTON_PIN[5], (EvtAction)button5));
  147. mgr.addListener(new EvtPinListener(BUTTON_PIN[6], (EvtAction)button6));
  148. mgr.addListener(new EvtPinListener(BUTTON_PIN[7], (EvtAction)button7));
  149. mgr.addListener(new EvtPinListener(BUTTON_PIN[8], (EvtAction)button8));
  150. mgr.addListener(new EvtPinListener(BUTTON_PIN[9], (EvtAction)button9));
  151. // controlled time listeners
  152. // pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);
  153. inibSDreading = true ;
  154. mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
  155. mgr.addListener(new EvtTimeListener(refreshRate, true, (EvtAction)updateAll));
  156. //mgr.addListener(new EvtTimeListener(40, true, (EvtAction)fastLeds));
  157. Serial.println("Done resetting ");
  158. }