Selaa lähdekoodia

config file + bugfix

added a config file to give acces to HIGH, MID and LOW dimmer values and
to led color palettes
fixed a bug with sd reading hanging after one or two pushes : close the
precedent file just before reading a nex one, when sd listener is not
running
Etienne Landon 8 vuotta sitten
vanhempi
commit
37db1a4449
4 muutettua tiedostoa jossa 94 lisäystä ja 25 poistoa
  1. 12 8
      DownTigerDown.ino
  2. 57 7
      SDcard.ino
  3. 16 9
      eventually.ino
  4. 9 1
      fastLed.ino

+ 12 - 8
DownTigerDown.ino

@@ -50,7 +50,9 @@ bool delay_flag ; //prevent first event to fire
 //Dimmer
 #define DIMMER_PIN 9
 FadeLed dimmer(DIMMER_PIN);
-
+byte dimmer_low = 20;
+byte dimmer_mid = 60;
+byte dimmer_high = 80;
 
 // FastLED
 #define NUM_LEDS_LUSTRE 240
@@ -70,10 +72,10 @@ CRGBPalette16 table_currentPalette( CRGB::Pink);
 CRGBPalette16 table_targetPalette( CRGB::Blue );
 
 // FastLED Palettes
-CRGBPalette16 TIGER_DOWN_MER (CRGB::DarkBlue);
-CRGBPalette16 SOLEIL (CRGB::Orange);
-CRGBPalette16 CREPUSCULE (CRGB::YellowGreen);
-CRGBPalette16 FLAMINGO (CRGB::LightCoral);
+CRGBPalette16 palette_MER (CRGB::DarkBlue);
+CRGBPalette16 palette_SOLEIL (CRGB::Orange);;
+CRGBPalette16 palette_CREPUSCULE (CRGB::YellowGreen);
+CRGBPalette16 palette_FLAMINGO (CRGB::LightCoral);
 
 
 //Functions prototypes
@@ -101,7 +103,8 @@ void setup() {
     }
   }
   SDfound = 1;
-
+  readSDconfig();
+  
   // DIMMER
   dimmer.off();
 
@@ -151,8 +154,9 @@ void setup() {
 //  }
   delay (3000) ;
   
-  buttonFocus(0) ;
-//  EvtResetButtonContext ();
+  
+  EvtResetButtonContext ();
+//  buttonFocus(0) ;
   
 }
 

+ 57 - 7
SDcard.ino

@@ -20,6 +20,46 @@ String getSubstring(String dataString, char separator, int index)
 
 
 
+bool readSDconfig(){
+  File configFile = SD.open("config.txt");
+  while (configFile.available()) {                  //as long as there is data available
+    String buf = configFile.readStringUntil('\n');  // read a line
+    String keyword = getSubstring(buf, ' ', 0);     // update variable according to keyword
+    if (keyword == "LOW") { dimmer_low = getSubstring(buf, ' ', 1).toInt() ; }
+    if (keyword == "HIGH") { dimmer_high = getSubstring(buf, ' ', 1).toInt() ; }
+    if (keyword == "MID") { dimmer_mid = getSubstring(buf, ' ', 1).toInt() ; }
+    
+    if (keyword == "MER") { fill_solid( palette_MER, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
+    if (keyword == "SOLEIL") { fill_solid( palette_SOLEIL, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
+    if (keyword == "CREPUSCULE") { fill_solid( palette_CREPUSCULE, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
+    if (keyword == "FLAMINGO") { fill_solid( palette_FLAMINGO, 16, CRGB(getSubstring(buf, ' ', 1).toInt(), getSubstring(buf, ' ', 2).toInt(), getSubstring(buf, ' ', 3).toInt()) ) ; }
+  }
+  
+  Serial.println("Configuration : ");
+  Serial.print("Niveaux dimmer (LOW / MID / HIGH) : ");
+  Serial.print(dimmer_low);
+  Serial.print(" ");
+  Serial.print(dimmer_mid);
+  Serial.print(" ");
+  Serial.print(dimmer_high);
+  Serial.println("");
+  Serial.print("Palette MER : ");
+  Serial.print(ColorFromPalette(palette_MER, 0));
+  Serial.print("Palette SOLEIL : ");
+  Serial.print(ColorFromPalette(palette_SOLEIL, 0));
+  Serial.print("Palette CREPUSCULE : ");
+  Serial.print(ColorFromPalette(palette_CREPUSCULE, 0));
+  Serial.print("Palette FLAMINGO : ");
+  Serial.print(ColorFromPalette(palette_FLAMINGO, 0));
+//  Serial.print(" ");
+//  Serial.print(dimmer_mid);
+//  Serial.print(" ");
+//  Serial.print(dimmer_high);
+  Serial.println("");
+  configFile.close();  
+}
+
+
 bool readSDFile() {
   
   if (!inibSDreading) { // if currently delaying, don't read a line 
@@ -38,6 +78,7 @@ bool readSDFile() {
       }
       
       else if (selector == "led") {
+        Serial.println("led");
 //        String mode = getSubstring(buf, ' ', 1);
 //        if (mode == "L"){ 
 //          int R = getSubstring(buf, ' ', 2).toInt();
@@ -59,12 +100,18 @@ bool readSDFile() {
       }
      
       else if (selector == "dimmer") {
-        Serial.println("Dimmer fade à " + getSubstring(buf, ' ', 1) + " en " + getSubstring(buf, ' ', 2) + "ms");
-        int dimmer_value = getSubstring(buf, ' ', 1).toInt();
+        String dim_val = getSubstring(buf, ' ', 1);
+        int dimmer_value;
         int dimmer_fade = getSubstring(buf, ' ', 2).toInt();
+        Serial.println("Dimmer fade à " + dim_val + " en " + dimmer_fade + "ms");
+        if (dim_val == "LOW") {dimmer_value = dimmer_low ;}
+        else if (dim_val == "MID") {dimmer_value = dimmer_mid ;}
+        else if (dim_val == "HIGH") {dimmer_value = dimmer_high ;}
+        else if (dim_val == "OFF") {dimmer_value = 0 ;}
+        else { dimmer_value = dim_val.toInt();} //value% 
         dimmer.setTime(dimmer_fade);
-        dimmer.set(dimmer_value);
-  //      Serial.println(dimmer_delta);
+        dimmer.set(percent2PWM(dimmer_value));
+//      Serial.println(dimmer_delta);
         
       }
       
@@ -92,13 +139,16 @@ bool readSDFile() {
     
     else {
       Serial.println("EOF");
-
-      btnFile.close();
       inibSDreading = true;
+//      btnFile.close();
       EvtResetButtonContext();
+      
+      
     }
   }
-//  else{Serial.println("not reading");}
+//  else{
+//    Serial.println("not reading");
+//    }
   return false ;
   
 }

+ 16 - 9
eventually.ino

@@ -49,26 +49,30 @@ void buttonFocus(int index) {
   for (int i ; i < sizeof(ledState)/sizeof(uint32_t) ; i++) {
 
     if (i == currentButton ) { 
-      ledState[11-i] = 65535 ;
+      ledState[11-i] = 10000 ; //65536 max
     }
     else { ledState[11-i] =  50 ; }
     tlcWrite();
   }
   
-  EvtResetButtonContext() ; //clean context before starting reading the file
+  
   
   //et après ya plein de trucs à faire
+  btnFile.close();
   String file = "button";
   file+=index ;
   file+=".txt";
   btnFile = SD.open(file.c_str()) ;
-//  btnFile = SD.open("button0.txt");
+  //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
    inibSDreading = false ;
    //mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
    //mgr.addListener(new EvtTimeListener(50, true, (EvtAction)fadeDimmer));
@@ -102,33 +106,33 @@ void stopAll() {
 
 bool button0(){
   buttonFocus(0) ;
-  lustre_targetPalette = FLAMINGO ;
+  //lustre_targetPalette = palette_FLAMINGO ;
   //lustre_currentBrightness = 0;
   return false;
 }
 bool button1(){
   buttonFocus(1) ;
-  lustre_targetPalette = SOLEIL;
+  //lustre_targetPalette = palette_SOLEIL;
   //lustre_currentBrightness = 0;
 //  testAll(); 
   return false;
 }
 bool button2(){
   buttonFocus(2) ;
-  lustre_targetPalette = CREPUSCULE ;
+  //lustre_targetPalette = palette_CREPUSCULE ;
   //lustre_currentBrightness = 0;
   
   return false;
 }
 bool button3(){
   buttonFocus(3) ;
-  lustre_targetPalette =TIGER_DOWN_MER;
+  //lustre_targetPalette =palette_MER;
   //lustre_currentBrightness = 0;
   return false;
 }
 bool button4(){
   buttonFocus(4) ;
-  lustre_currentBrightness = 0;
+  //lustre_currentBrightness = 0;
 //  stopAll();
   return false;
 }
@@ -156,6 +160,7 @@ bool button9(){
 void EvtResetButtonContext () {
   Serial.println("Resetting event context");
   mgr.resetContext();
+  delay (50);
   // button listeners
   mgr.addListener(new EvtPinListener(BUTTON_PIN[0], (EvtAction)button0));
   mgr.addListener(new EvtPinListener(BUTTON_PIN[1], (EvtAction)button1));
@@ -170,10 +175,12 @@ void EvtResetButtonContext () {
 
   // controlled time listeners
 //  pSD = new EvtTimeListener(5, true, (EvtAction)readSDFile);  
+
   inibSDreading = true ;
   mgr.addListener(new EvtTimeListener(10, true, (EvtAction)readSDFile));
-  mgr.addListener(new EvtTimeListener(25, true, (EvtAction)updateAll));
+  mgr.addListener(new EvtTimeListener(50, true, (EvtAction)updateAll));
   //mgr.addListener(new EvtTimeListener(40, true, (EvtAction)fastLeds));
+  Serial.println("Done resetting ");
   
 }
 

+ 9 - 1
fastLed.ino

@@ -12,7 +12,7 @@ void FillLEDsFromPaletteColors( CRGB leds[], int NUM_LED, CRGBPalette16 palette,
 
 byte FadingBrightness (int FastLEDindex, byte currentBrightness, byte targetBrightness, int step) {
   if (currentBrightness != targetBrightness) { currentBrightness += step ;}
-  else Serial.println("fade done");
+  //else Serial.println("fade done");
   FastLED[FastLEDindex].showLeds(currentBrightness) ;
   return currentBrightness ;
 }
@@ -30,3 +30,11 @@ void updateLeds(){
 }
 
 
+
+/////////////////////////////// DIMMER  //////////////////////////////////////
+
+
+byte percent2PWM ( byte valueIn ) {
+  return map (valueIn, 0, 100, 14, 92);
+}
+