eventually.ino 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. //tsunami.update();
  19. updateLeds() ;
  20. if (dimmer_random){
  21. dimmer.setTime(50);
  22. if (random8(100) > 50) {
  23. dimmer.set(percent2PWM(random8(10, 30)));
  24. }
  25. }
  26. else if (dimmer_move) {
  27. if (random8(100) > 20) {
  28. int timer = millis();
  29. int delta = 2* dimmer_move * (sin8( timer / 9 )-127) * (sin8(timer / 7 )-127) / 65536 ; //- led_move/2
  30. dimmer.setTime(150);
  31. dimmer.set(percent2PWM(dimmer_value + delta));
  32. //Serial.println(dimmer_value - delta );
  33. }
  34. }
  35. FadeLed::update() ;
  36. return false;
  37. }
  38. void tlcWrite(){
  39. for (int ledIndex ; ledIndex < 12 ; ledIndex++) { //sizeof(ledState)/sizeof(uint32_t)
  40. int indexDiv = ledIndex / 3 ;
  41. tlc.setLED(indexDiv, ledState[indexDiv*3], ledState[indexDiv*3+1], ledState[indexDiv*3+2]);
  42. // Serial.print(ledState[ledIndex]);
  43. // Serial.print(" ");
  44. }
  45. // Serial.println();
  46. tlc.write();
  47. }
  48. void buttonFocus(int index) {
  49. btnFile.close();
  50. //delay(50);
  51. currentButton = index ;
  52. //Serial.println(currentButton);
  53. //turn off all leds but currentButton one
  54. for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
  55. if (i == currentButton ) {
  56. ledState[11-i] = 10000 ; //65536 max
  57. }
  58. else { ledState[11-i] = 50 ; }
  59. tlcWrite();
  60. }
  61. //et après ya plein de trucs à faire
  62. String file = "button";
  63. file+=index ;
  64. file+=".txt";
  65. btnFile = SD.open(file.c_str(), FILE_READ) ;
  66. btnFile.setTimeout(100);
  67. //btnFile = SD.open("button0.txt");
  68. Serial.println("");
  69. Serial.println("opening " + file);
  70. if (!btnFile) {
  71. Serial.println("The text file cannot be opened");
  72. // return false;
  73. }
  74. // while(
  75. EvtResetButtonContext() ; //clean context before starting reading the file
  76. //Serial.println("ended buttonReset");
  77. delay_length = 0 ;
  78. inibSDreading = false ;
  79. //Serial.println("ended focus");
  80. //mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
  81. //mgr.addListener(new EvtTimeListener(50, true, (EvtAction)fadeDimmer));
  82. //readSDFile();
  83. }
  84. void stopAll() {
  85. tsunami.stopAllTracks();
  86. dimmer.setTime(1);
  87. dimmer.off();
  88. for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
  89. lustreLeds[i] = CRGB::Black ;
  90. tableLeds[i] = CRGB::Black ;
  91. }
  92. FastLED.show();
  93. for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
  94. if (i == 4 ) {
  95. ledState[11-i] = 65535 ;
  96. }
  97. else { ledState[11-i] = 50 ; }
  98. tlcWrite();
  99. }
  100. EvtResetButtonContext();
  101. }
  102. void audioVolume(){
  103. int targetVolume = map (analogRead(audioVolumePin), 0, 1024, 0, -40) ;
  104. //Serial.println(analogRead(A21));
  105. if (targetVolume != audioMasterVolume){
  106. audioMasterVolume = targetVolume;
  107. Serial.print("Setting audio volume to : ");
  108. Serial.println(audioMasterVolume);
  109. for (int i ; i<4 ; i++) {
  110. tsunami.masterGain(i, audioMasterVolume);
  111. }
  112. }
  113. }
  114. ///////////////////// CONTEXT ///////////////////////////////
  115. bool button0(){
  116. buttonFocus(0) ;
  117. return false;
  118. }
  119. bool button1(){
  120. buttonFocus(1) ;
  121. return false;
  122. }
  123. bool button2(){
  124. buttonFocus(2) ;
  125. return false;
  126. }
  127. bool button3(){
  128. buttonFocus(3) ;
  129. return false;
  130. }
  131. bool button4(){
  132. buttonFocus(4) ;
  133. return false;
  134. }
  135. bool button5(){
  136. buttonFocus(5) ;
  137. return false;
  138. }
  139. bool button6(){
  140. buttonFocus(6) ;
  141. return false;
  142. }
  143. bool button7(){
  144. buttonFocus(7) ;
  145. return false;
  146. }
  147. bool button8(){
  148. buttonFocus(8) ;
  149. return false;
  150. }
  151. bool button9(){
  152. buttonFocus(9) ;
  153. return false;
  154. }
  155. void EvtResetButtonContext () {
  156. Serial.println("Resetting event context");
  157. mgr.resetContext();
  158. //delay (50);
  159. // button listeners
  160. int debounce = 10 ;
  161. mgr.addListener(new EvtPinListener(BUTTON_PIN[0], debounce, (EvtAction)button0));
  162. mgr.addListener(new EvtPinListener(BUTTON_PIN[1], debounce, (EvtAction)button1));
  163. mgr.addListener(new EvtPinListener(BUTTON_PIN[2], debounce, (EvtAction)button2));
  164. mgr.addListener(new EvtPinListener(BUTTON_PIN[3], debounce, (EvtAction)button3));
  165. mgr.addListener(new EvtPinListener(BUTTON_PIN[4], debounce, (EvtAction)button4));
  166. mgr.addListener(new EvtPinListener(BUTTON_PIN[5], debounce, (EvtAction)button5));
  167. mgr.addListener(new EvtPinListener(BUTTON_PIN[6], debounce, (EvtAction)button6));
  168. mgr.addListener(new EvtPinListener(BUTTON_PIN[7], debounce, (EvtAction)button7));
  169. mgr.addListener(new EvtPinListener(BUTTON_PIN[8], debounce, (EvtAction)button8));
  170. mgr.addListener(new EvtPinListener(BUTTON_PIN[9], debounce, (EvtAction)button9));
  171. // controlled time listeners
  172. // pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);
  173. inibSDreading = true ;
  174. mgr.addListener(new EvtTimeListener(5, true, (EvtAction)readSDFile));
  175. mgr.addListener(new EvtTimeListener(refreshRate, true, (EvtAction)updateAll));
  176. //mgr.addListener(new EvtTimeListener(40, true, (EvtAction)fastLeds));
  177. mgr.addListener(new EvtTimeListener(refreshRate, true, (EvtAction)audioVolume));
  178. Serial.println("Done resetting ");
  179. }