Преглед на файлове

FastLED rework

created an updateLed() function that will handle updating colors through
palettes with linear blend, and then apply to the leds with separate
master brightness for lustre and table
Etienne Landon преди 8 години
родител
ревизия
a40add3ff7
променени са 1 файла, в които са добавени 202 реда и са изтрити 97 реда
  1. 202 97
      DownTigerDown/DownTigerDown.ino

+ 202 - 97
DownTigerDown/DownTigerDown.ino

@@ -1,3 +1,11 @@
+/*TODO
+ * Fastled and sd reading ?
+ * stop state
+ * stop running state 
+ * fade leds
+ */
+
+
 #include <Eventually.h>
 #include "Adafruit_TLC59711.h"
 #include <SD.h>
@@ -43,24 +51,30 @@ bool delay_flag ; //prevent first event to fire
 #define DIMMER_PIN 9
 FadeLed dimmer(DIMMER_PIN);
 
-//// OctoWS2811 settings
-//const int ledsPerStrip = 10; // change for your setup
-//const byte numStrips= 2; // change for your setup
-//DMAMEM int displayMemory[ledsPerStrip*numStrips];
-//int drawingMemory[ledsPerStrip*numStrips];
-//const int config = WS2811_GRB | WS2811_800kHz;
-//OctoWS2811 leds(10, displayMemory, drawingMemory, config);
 
 // FastLED
 #define NUM_LEDS_LUSTRE 240
-#define NUM_LEDS_TABLE 50
 CRGB lustreLeds[NUM_LEDS_LUSTRE];
+byte lustre_currentBrightness ;     //for fading
+byte lustre_targetBrightness ;
+int lustre_stepBrightness;
+CRGBPalette16 lustre_currentPalette( CRGB::Pink);
+CRGBPalette16 lustre_targetPalette( CRGB::Blue );
+
+#define NUM_LEDS_TABLE 50
 CRGB tableLeds[NUM_LEDS_TABLE];
+byte table_currentBrightness ;
+byte table_targetBrightness ;
+int table_stepBrightness;
+CRGBPalette16 table_currentPalette( CRGB::Pink);
+CRGBPalette16 table_targetPalette( CRGB::Blue );
 
 //Functions prototypes
 void EvtResetButtonContext ();
 void tlcWrite();
 void buttonFocus(int);
+
+
 //////////////////////// SETUP /////////////////////////////////
 
 void setup() {
@@ -69,15 +83,7 @@ void setup() {
   // TLC
   tlc.begin();
   tlc.write();
-  Serial.println("Test boutons");
-   for (int i ; i<12; i++) {
-      
-      ledState[i] = 65535 ;
-      tlcWrite();
-      delay (100);
-      ledState[i] = 0 ;
-      tlcWrite();
-   }  
+
 
    // SD
    Serial.println("Lecture carte SD");
@@ -90,42 +96,12 @@ void setup() {
   SDfound = 1;
 
   // DIMMER
-  Serial.println("Test dimmer");
-  dimmer.setTime(1);
-  dimmer.off();
-  while (!dimmer.done()) {FadeLed::update();}
-  delay(2000);
-  Serial.println("fading up");
-  dimmer.setTime(2000);
-  dimmer.on();
-  while (!dimmer.done()) {FadeLed::update();}
-  Serial.println("fading down");
   dimmer.off();
-  while (!dimmer.done()) {FadeLed::update();}
 
-  // OctoWS2811
-//  leds.begin();
-//  delay(500);
-//  for (int i ; i< ledsPerStrip * numStrips ; i++) {
-//    leds.setPixel(i, 255, 255, 255);
-//    leds.show();
-//    delay(50);
-//    leds.setPixel(i, 0, 0, 0);
-//    leds.show();
-//  }
-
-//FastLED
+  // LEDS
   FastLED.addLeds<NEOPIXEL, 2>(lustreLeds, NUM_LEDS_LUSTRE);
   FastLED.addLeds<NEOPIXEL, 14>(tableLeds, NUM_LEDS_TABLE);
-  for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
-    lustreLeds[i] = CRGB::White ;
-    tableLeds[i] = CRGB::White ;
-    FastLED.show();
-    delay(10);
-    lustreLeds[i] = CRGB::Black ;
-    tableLeds[i] = CRGB::Black ;
-    FastLED.show();
-  }
+
   
   // TSUNAMI
   // We should wait for the Tsunami to finish reset 
@@ -147,22 +123,9 @@ void setup() {
   }
   else
     Serial.print("WAV Trigger response not available");
-  tsunami.trackPlayPoly(1, 0, true);
-  delay(500);
-  tsunami.trackPlayPoly(2, 0, true);
-  delay(500);
-  tsunami.trackPlayPoly(3, 1, true);
-  delay(500);
-  tsunami.trackPlayPoly(4, 1, true);
-  delay(500);
-  tsunami.trackPlayPoly(5, 2, true);
-  delay(500);
-  tsunami.trackPlayPoly(6, 2, true);
-  delay(500);
-  tsunami.trackPlayPoly(7, 3, true);
-  delay(500);
-  tsunami.trackPlayPoly(8, 3, true);
-
+  
+  tsunami.setReporting(false);
+  
 
   
   
@@ -171,25 +134,128 @@ void setup() {
     pinMode(BUTTON_PIN[i],INPUT_PULLUP) ; 
   }
 
-  // Create timers for controlled events
-  
-
-  
   //EvtResetButtonContext();
   Serial.println();
   Serial.println();
   Serial.println("Demarrage termine, activation etat 0");
+//  btnFile = SD.open("button0.txt");
+//  while(!btnFile.available()){
+//    Serial.println("bah alors");
+//  }
+  delay (3000) ;
   
   buttonFocus(0) ;
+//  EvtResetButtonContext ();
+  
+}
+
+///////////////////////// TEST ALL DEVICES  /////////////////////////////
+
+void testAll(){
+      
+  mgr.resetContext();
+  // TLC
+    Serial.println("Test boutons");
+   for (int i ; i<12; i++) {
+      
+      ledState[i] = 65535 ;
+      tlcWrite();
+      delay (100);
+      ledState[i] = 0 ;
+      tlcWrite();
+      
+   }  
+   for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
+
+    if (i == 1 ) { 
+      ledState[11-i] = 65535 ;
+    }
+    else { ledState[11-i] =  50 ; }
+    tlcWrite();
+  }
+
+  // DIMMER
+  Serial.println("Test dimmer");
+  dimmer.off();
+  while (!dimmer.done()) {FadeLed::update();}
+  //delay(2000);
+  Serial.println("fading up");
+  dimmer.setTime(2000);
+  dimmer.on();
+  while (!dimmer.done()) {FadeLed::update();}
+  Serial.println("fading down");
+  dimmer.off();
+  while (!dimmer.done()) {FadeLed::update();}
 
+//  FastLED
+  Serial.println("Test leds");
+  for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
+    lustreLeds[i] = CRGB::White ;
+    tableLeds[i] = CRGB::White ;
+    FastLED.show();
+//    delay(1);
+//    lustreLeds[i] = CRGB::Black ;
+//    tableLeds[i] = CRGB::Black ;
+//    FastLED.show();
+  }
   
-//  plist = new EvtTimeListener(1000, true, (EvtAction)blinkme);
-//  plist2 = new EvtTimeListener(100, true, (EvtAction)blinkme2);
-//  mgr.addListener( plist ); 
-//  //mgr.addListener( plist2 ); 
+  // AUDIO
+  Serial.println("Test son");
+  tsunami.trackPlayPoly(101, 0, true);
+  //delay(500);
+  //tsunami.update();
+  tsunami.trackPlayPoly(102, 0, true);
+  //delay(500);
+  //tsunami.update();
+  tsunami.trackPlayPoly(103, 1, true);
+  //delay(500);
+  //tsunami.update();
+  tsunami.trackPlayPoly(104, 1, true);
+  //delay(500);
+  //tsunami.update();
+  tsunami.trackPlayPoly(105, 2, true);
+  //delay(500);
+  //tsunami.update();
+  tsunami.trackPlayPoly(106, 2, true);
+  //delay(500);
+ // tsunami.update();
+  tsunami.trackPlayPoly(107, 3, true);
+  //delay(500);
+  //tsunami.update();
+  tsunami.trackPlayPoly(108, 3, true);
+  //tsunami.update();
+  //delay(500);
+
+  Serial.println("fin du test");
+  EvtResetButtonContext();
+//  stopAll();  
+
 }
 
 
+///////////////////////// FastLED ///////////////////////////////////////
+
+
+void FillLEDsFromPaletteColors( uint8_t colorIndex){
+  uint8_t brightness = 255;
+  for( int i = 0; i < NUM_LEDS_LUSTRE; i++) {
+    lustreLeds[i] = ColorFromPalette( lustre_currentPalette, colorIndex + sin8(i*16), 255);
+    colorIndex += 3;
+  }
+}
+
+void FadingBrightness (int FastLEDindex, byte currentBrightness, byte targetBrightness, int step) {
+  FastLED[FastLEDindex].showLeds(currentBrightness) ;
+}
+
+void updateLeds(){
+  FillLEDsFromPaletteColors( 0);
+  uint8_t secondHand = (millis() / 100) % 60;
+  FadingBrightness( 0, secondHand*10,100,5);
+}
+
+
+
 ///////////////////////// SD ////////////////////////////////////////////
 
 String getSubstring(String dataString, char separator, int index)
@@ -212,7 +278,7 @@ String getSubstring(String dataString, char separator, int index)
 
 bool readSDFile() {
   
-  if (!inibSDreading) { // if currently delaying, don't read a line
+  if (!inibSDreading) { // if currently delaying, don't read a line 
     if (btnFile.available()) {
       buf = btnFile.readStringUntil('\n');
       
@@ -224,15 +290,28 @@ bool readSDFile() {
         String track = getSubstring(buf, ' ', 2) ;
         String out = getSubstring(buf, ' ', 1) ;
         Serial.println("Playing track " + track + " to output " + out);
-        tsunami.trackPlayPoly(track.toInt(), out.toInt(), true);     
+        tsunami.trackPlayPoly(track.toInt(), out.toInt()-1, true);     
       }
       
       else if (selector == "led") {
-        String mode = getSubstring(buf, ' ', 1);
-        if (mode.toInt() == 0){
-          
-        }
-        Serial.println("Led mode " + getSubstring(buf, ' ', 1));
+//        String mode = getSubstring(buf, ' ', 1);
+//        if (mode == "L"){ 
+//          int R = getSubstring(buf, ' ', 2).toInt();
+//          int G = getSubstring(buf, ' ', 3).toInt();
+//          int B = getSubstring(buf, ' ', 4).toInt();
+//          for (int i ; i<NUM_LEDS_LUSTRE ; i++) {
+//            lustreLeds[i].setRGB(R, G, B) ;
+//          }         
+//        }
+//        if (mode == "T"){ 
+//          int R = getSubstring(buf, ' ', 2).toInt();
+//          int G = getSubstring(buf, ' ', 3).toInt();
+//          int B = getSubstring(buf, ' ', 4).toInt();
+//          for (int i ; i<NUM_LEDS_TABLE ; i++) {
+//            tableLeds[i].setRGB(R, G, B) ;
+//          }         
+//        }
+//        Serial.println("Led mode " + getSubstring(buf, ' ', 1));
       }
      
       else if (selector == "dimmer") {
@@ -269,12 +348,13 @@ bool readSDFile() {
     
     else {
       Serial.println("EOF");
-  //    mgr.removeListener(pSD);
+
       btnFile.close();
       inibSDreading = true;
-      EvtResetButtonContext ();
+      EvtResetButtonContext();
     }
   }
+//  else{Serial.println("not reading");}
   return false ;
   
 }
@@ -297,20 +377,17 @@ bool checkDelay() {
   return false ;
 }
 
-bool fadeDimmer() {
-//  if (dimmer_value != dimmer_target) {
-//    dimmer_value += dimmer_delta ;
-//    Serial.println(dimmer_value);
-//    
-//  }
-//  else 
-//    mgr.removeListener(pDimmer);
+bool updateAll() {
+
   FadeLed::update();
-//  Serial.println("fade");
+  //tsunami.update();
+  updateLeds() ;
+//  FastLED.show();
   return false;
   
 }
 
+
 void tlcWrite(){
   for (int ledIndex ; ledIndex < 12 ; ledIndex++) { //sizeof(ledState)/sizeof(uint32_t)
     
@@ -343,8 +420,8 @@ void buttonFocus(int index) {
   String file = "button";
   file+=index ;
   file+=".txt";
-  //btnFile = SD.open(file.c_str()) ;
-  btnFile = SD.open("button0.txt");
+  btnFile = SD.open(file.c_str()) ;
+//  btnFile = SD.open("button0.txt");
   Serial.println("opening " + file);
   if (!btnFile) {
     Serial.println("The text file cannot be opened");
@@ -358,12 +435,37 @@ void buttonFocus(int index) {
 
 }
 
+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();
+}
+
+///////////////////// CONTEXT ///////////////////////////////
+
 bool button0(){
   buttonFocus(0) ;
   return false;
 }
 bool button1(){
   buttonFocus(1) ;
+//  testAll(); 
   return false;
 }
 bool button2(){
@@ -376,6 +478,7 @@ bool button3(){
 }
 bool button4(){
   buttonFocus(4) ;
+//  stopAll();
   return false;
 }
 bool button5(){
@@ -415,8 +518,11 @@ void EvtResetButtonContext () {
   mgr.addListener(new EvtPinListener(BUTTON_PIN[9], (EvtAction)button9));
 
   // controlled time listeners
-//  pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);
-  //mgr.addListener(new EvtTimeListener(20, true, (EvtAction)fadeDimmer));
+//  pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);  
+  inibSDreading = true ;
+  mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
+  mgr.addListener(new EvtTimeListener(50, true, (EvtAction)updateAll));
+  //mgr.addListener(new EvtTimeListener(40, true, (EvtAction)fastLeds));
   
 }
 
@@ -431,7 +537,6 @@ void EvtResetButtonContext () {
 
 void loop() {
   mgr.loopIteration();
-  readSDFile();
-  FadeLed::update();
+  //updateAll();
 }