123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- /////////////////////////////Events/////////////////////////////////
- ///*resetButtonContext create a listener for each button
- // * when triggered, thoses listener execute the same functions with the index of the pressed button :
- // * resetButtonContext and kill any running sequence
- // * buttonFocus -> lights the current button and shuts the others
- // * readSequence -> reads a text file on the SD card with the sequence of actions to execute
- // */
- bool checkDelay() {
- //Serial.println("checkDelay");
- //if (delay_flag == 0 ) { delay_flag = 1 ;}
- if (millis() - delay_start > delay_length ) {
- inibSDreading = false ;
- }
- //else {inibSDreading = false ;}
- return false ;
- }
- bool updateAll() {
- //tsunami.update();
- updateLeds() ;
- if (dimmer_random){
- dimmer.setTime(50);
- if (random8(100) > 50) {
- dimmer.set(percent2PWM(random8(10, 30)));
- }
- }
- else if (dimmer_move) {
- if (random8(100) > 20) {
- int timer = millis();
- int delta = 2* dimmer_move * (sin8( timer / 9 )-127) * (sin8(timer / 7 )-127) / 65536 ; //- led_move/2
- dimmer.setTime(150);
- dimmer.set(percent2PWM(dimmer_value + delta));
- //Serial.println(dimmer_value - delta );
- }
- }
- FadeLed::update() ;
-
- return false;
-
-
- }
- void tlcWrite(){
- for (int ledIndex ; ledIndex < 12 ; ledIndex++) { //sizeof(ledState)/sizeof(uint32_t)
-
- int indexDiv = ledIndex / 3 ;
-
- tlc.setLED(indexDiv, ledState[indexDiv*3], ledState[indexDiv*3+1], ledState[indexDiv*3+2]);
- // Serial.print(ledState[ledIndex]);
- // Serial.print(" ");
- }
- // Serial.println();
- tlc.write();
- }
- void buttonFocus(int index) {
- btnFile.close();
- //delay(50);
-
- currentButton = index ;
- //Serial.println(currentButton);
- //turn off all leds but currentButton one
- for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
- if (i == currentButton ) {
- ledState[11-i] = 10000 ; //65536 max
- }
- else { ledState[11-i] = 50 ; }
- tlcWrite();
- }
- //et après ya plein de trucs à faire
-
- String file = "button";
- file+=index ;
- file+=".txt";
- btnFile = SD.open(file.c_str(), FILE_READ) ;
- btnFile.setTimeout(100);
- //btnFile = SD.open("button0.txt");
- Serial.println("");
- Serial.println("opening " + file);
- if (!btnFile) {
- Serial.println("The text file cannot be opened");
- // return false;
- }
- // while(
-
- EvtResetButtonContext() ; //clean context before starting reading the file
- //Serial.println("ended buttonReset");
- delay_length = 0 ;
- inibSDreading = false ;
-
- //Serial.println("ended focus");
- //mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
- //mgr.addListener(new EvtTimeListener(50, true, (EvtAction)fadeDimmer));
- //readSDFile();
- }
- void stopAll() {
- tsunami.stopAllTracks();
- dimmer.setTime(1);
- dimmer.off();
- for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
- lustreLeds[i] = CRGB::Black ;
- tableLeds[i] = CRGB::Black ;
- }
- FastLED.show();
- for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
- if (i == 4 ) {
- ledState[11-i] = 65535 ;
- }
- else { ledState[11-i] = 50 ; }
- tlcWrite();
- }
-
-
- EvtResetButtonContext();
- }
- void audioVolume(){
- int targetVolume = map (analogRead(audioVolumePin), 0, 1024, 0, -40) ;
- //Serial.println(analogRead(A21));
- if (targetVolume != audioMasterVolume){
- audioMasterVolume = targetVolume;
- Serial.print("Setting audio volume to : ");
- Serial.println(audioMasterVolume);
- for (int i ; i<4 ; i++) {
- tsunami.masterGain(i, audioMasterVolume);
- }
- }
- }
- ///////////////////// CONTEXT ///////////////////////////////
- bool button0(){
- buttonFocus(0) ;
- return false;
- }
- bool button1(){
- buttonFocus(1) ;
- return false;
- }
- bool button2(){
- buttonFocus(2) ;
- return false;
- }
- bool button3(){
- buttonFocus(3) ;
- return false;
- }
- bool button4(){
- buttonFocus(4) ;
- return false;
- }
- bool button5(){
- buttonFocus(5) ;
- return false;
- }
- bool button6(){
- buttonFocus(6) ;
- return false;
- }
- bool button7(){
- buttonFocus(7) ;
- return false;
- }
- bool button8(){
- buttonFocus(8) ;
- return false;
- }
- bool button9(){
- buttonFocus(9) ;
- return false;
- }
- void EvtResetButtonContext () {
- Serial.println("Resetting event context");
- mgr.resetContext();
- //delay (50);
- // button listeners
- int debounce = 10 ;
- mgr.addListener(new EvtPinListener(BUTTON_PIN[0], debounce, (EvtAction)button0));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[1], debounce, (EvtAction)button1));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[2], debounce, (EvtAction)button2));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[3], debounce, (EvtAction)button3));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[4], debounce, (EvtAction)button4));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[5], debounce, (EvtAction)button5));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[6], debounce, (EvtAction)button6));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[7], debounce, (EvtAction)button7));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[8], debounce, (EvtAction)button8));
- mgr.addListener(new EvtPinListener(BUTTON_PIN[9], debounce, (EvtAction)button9));
- // controlled time listeners
- // pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);
- inibSDreading = true ;
- mgr.addListener(new EvtTimeListener(5, true, (EvtAction)readSDFile));
- mgr.addListener(new EvtTimeListener(refreshRate, true, (EvtAction)updateAll));
- //mgr.addListener(new EvtTimeListener(40, true, (EvtAction)fastLeds));
- mgr.addListener(new EvtTimeListener(refreshRate, true, (EvtAction)audioVolume));
- Serial.println("Done resetting ");
-
- }
|