eventually.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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] = 65535 ;
  41. }
  42. else { ledState[11-i] = 50 ; }
  43. tlcWrite();
  44. }
  45. EvtResetButtonContext() ; //clean context before starting reading the file
  46. //et après ya plein de trucs à faire
  47. String file = "button";
  48. file+=index ;
  49. file+=".txt";
  50. btnFile = SD.open(file.c_str()) ;
  51. // btnFile = SD.open("button0.txt");
  52. Serial.println("opening " + file);
  53. if (!btnFile) {
  54. Serial.println("The text file cannot be opened");
  55. // return false;
  56. }
  57. // while(
  58. inibSDreading = false ;
  59. //mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
  60. //mgr.addListener(new EvtTimeListener(50, true, (EvtAction)fadeDimmer));
  61. //readSDFile();
  62. }
  63. void stopAll() {
  64. tsunami.stopAllTracks();
  65. dimmer.setTime(1);
  66. dimmer.off();
  67. for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
  68. lustreLeds[i] = CRGB::Black ;
  69. tableLeds[i] = CRGB::Black ;
  70. }
  71. FastLED.show();
  72. for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
  73. if (i == 4 ) {
  74. ledState[11-i] = 65535 ;
  75. }
  76. else { ledState[11-i] = 50 ; }
  77. tlcWrite();
  78. }
  79. EvtResetButtonContext();
  80. }
  81. ///////////////////// CONTEXT ///////////////////////////////
  82. bool button0(){
  83. buttonFocus(0) ;
  84. lustre_targetPalette = FLAMINGO ;
  85. //lustre_currentBrightness = 0;
  86. return false;
  87. }
  88. bool button1(){
  89. buttonFocus(1) ;
  90. lustre_targetPalette = SOLEIL;
  91. //lustre_currentBrightness = 0;
  92. // testAll();
  93. return false;
  94. }
  95. bool button2(){
  96. buttonFocus(2) ;
  97. lustre_targetPalette = CREPUSCULE ;
  98. //lustre_currentBrightness = 0;
  99. return false;
  100. }
  101. bool button3(){
  102. buttonFocus(3) ;
  103. lustre_targetPalette =TIGER_DOWN_MER;
  104. //lustre_currentBrightness = 0;
  105. return false;
  106. }
  107. bool button4(){
  108. buttonFocus(4) ;
  109. lustre_currentBrightness = 0;
  110. // stopAll();
  111. return false;
  112. }
  113. bool button5(){
  114. buttonFocus(5) ;
  115. return false;
  116. }
  117. bool button6(){
  118. buttonFocus(6) ;
  119. return false;
  120. }
  121. bool button7(){
  122. buttonFocus(7) ;
  123. return false;
  124. }
  125. bool button8(){
  126. buttonFocus(8) ;
  127. return false;
  128. }
  129. bool button9(){
  130. buttonFocus(9) ;
  131. return false;
  132. }
  133. void EvtResetButtonContext () {
  134. Serial.println("Resetting event context");
  135. mgr.resetContext();
  136. // button listeners
  137. mgr.addListener(new EvtPinListener(BUTTON_PIN[0], (EvtAction)button0));
  138. mgr.addListener(new EvtPinListener(BUTTON_PIN[1], (EvtAction)button1));
  139. mgr.addListener(new EvtPinListener(BUTTON_PIN[2], (EvtAction)button2));
  140. mgr.addListener(new EvtPinListener(BUTTON_PIN[3], (EvtAction)button3));
  141. mgr.addListener(new EvtPinListener(BUTTON_PIN[4], (EvtAction)button4));
  142. mgr.addListener(new EvtPinListener(BUTTON_PIN[5], (EvtAction)button5));
  143. mgr.addListener(new EvtPinListener(BUTTON_PIN[6], (EvtAction)button6));
  144. mgr.addListener(new EvtPinListener(BUTTON_PIN[7], (EvtAction)button7));
  145. mgr.addListener(new EvtPinListener(BUTTON_PIN[8], (EvtAction)button8));
  146. mgr.addListener(new EvtPinListener(BUTTON_PIN[9], (EvtAction)button9));
  147. // controlled time listeners
  148. // pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);
  149. inibSDreading = true ;
  150. mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
  151. mgr.addListener(new EvtTimeListener(25, true, (EvtAction)updateAll));
  152. //mgr.addListener(new EvtTimeListener(40, true, (EvtAction)fastLeds));
  153. }