فهرست منبع

refactor led updates

Etienne Landon 8 ماه پیش
والد
کامیت
847e24970c
2فایلهای تغییر یافته به همراه179 افزوده شده و 52 حذف شده
  1. 175 52
      Atm_fade_WS2812.cpp
  2. 4 0
      Atm_fade_WS2812.hpp

+ 175 - 52
Atm_fade_WS2812.cpp

@@ -67,6 +67,8 @@ Atm_fade_WS2812& Atm_fade_WS2812::pause( uint32_t duration ) {  // Pause between
   return *this;
 }
 
+
+
 Atm_fade_WS2812& Atm_fade_WS2812::fade( int fade_length){
   if(fade_length>=0){
     fade_duration = fade_length  ;
@@ -93,6 +95,19 @@ Atm_fade_WS2812& Atm_fade_WS2812::fade( int fade_length, bool fade_light, int fa
   return *this;
 }
 
+Atm_fade_WS2812& Atm_fade_WS2812::fadeSpeed( float fade_speed){
+  //define fade rate by leds/s
+  fade((num_leds/fade_speed)*1000);
+  return *this;
+  
+}
+
+Atm_fade_WS2812& Atm_fade_WS2812::fadeSpeed( float fade_speed, bool fade_light, int fade_chase){
+  //define fade rate by leds/s
+  fade((num_leds/fade_speed)*1000, fade_light, fade_chase);
+  return *this;
+}
+
 Atm_fade_WS2812& Atm_fade_WS2812::repeat( uint16_t repeat ) {
   counter_repeat.set( repeat_count = repeat );
   return *this;
@@ -214,6 +229,11 @@ Atm_fade_WS2812& Atm_fade_WS2812::update(){
   return *this;
 }
 
+Atm_fade_WS2812& Atm_fade_WS2812::reversed(bool _reverse){
+  reverse = _reverse ;
+  return *this ;
+}
+
 Atm_fade_WS2812& Atm_fade_WS2812::trace( Stream& stream ) {
   setTrace( &stream, atm_serial_debug::trace,
             "FADE\0EVT_CNT_FADE\0EVT_TM_FADE\0EVT_TM_ON\0EVT_TM_OFF\0EVT_CNT_RPT\0EVT_ON\0EVT_OFF\0EVT_BLINK\0EVT_TOGGLE\0EVT_TOGGLE_BLINK\0ELSE\0"
@@ -226,72 +246,175 @@ Atm_fade_WS2812& Atm_fade_WS2812::trace( Stream& stream ) {
 void Atm_fade_WS2812::update_leds(float fade_value){
 
   if(update_flag){
-      // Serial.println("loop");
-    int current_step = fade_value * FADE_STEPS ;
 
     CHSV temp_led[this->num_leds] ;
-    int current_value = color.val; 
-
-    if(_fade_light){ current_value *= fade_value ;}
-
-    current_value = constrain(current_value, 0, 255 ) ;
-    // Serial.println(current_value);
-    int chase_led = num_leds;
-    for(int led=0; led<num_leds; led++){
-      // Serial.println(led);
-      fade_value = _fade_chase ? fade_value : 1. ;
-      if(led<fade_value*num_leds){
-        temp_led[led].value = current_value ;
-        temp_led[led].saturation = color.saturation ;
-        temp_led[led].hue = color.hue ;
-      }
-      else{
-        temp_led[led].value = 0 ;
-        temp_led[led].saturation = color.saturation ;
-        temp_led[led].hue = color.hue ;
+    int current_value = color.val ; 
+
+    if(_replace_array){
+      if(_fade_light){ current_value = color.val*fade_value ;}
+      current_value = constrain(current_value, 0, 255 ) ;
+      for(int led=0; led<num_leds; led++){
+        // Serial.println(led);
+        fade_value = _fade_chase ? fade_value : 1. ;
+        if(led<fade_value*num_leds){
+          temp_led[led].value = current_value ;
+          temp_led[led].saturation = color.saturation ;
+          temp_led[led].hue = color.hue ;
+        }
+        else{
+          temp_led[led].value = 0 ;
+          temp_led[led].saturation = color.saturation ;
+          temp_led[led].hue = color.hue ;
+        }
+        if(reverse){leds[num_leds-led-1] = CRGB(temp_led[led]);}
+        else{leds[led] = CRGB(temp_led[led]);}
       }
-
-      if(_replace_array){leds[led] = CRGB(temp_led[led]);}
-      if(!_replace_array){
-        //add or remove one step of color 
-        // current_value = color.val / FADE_STEPS ;
-        // temp_led[led].value = current_value ;
-        // temp_led[led].saturation = color.saturation ;
-        // temp_led[led].hue = color.hue ;
-        if(update_flag){
-          CRGB rgb_step = CRGB(temp_led[led]);
+    }
+    if(!_replace_array){
+      CRGB colorRGB = CRGB(color);
+      
+
+      // current_value = constrain(current_value, 0, 255 ) ;
+      CRGB temp_led_RGB[num_leds] ;
+      for(int led=0; led<num_leds; led++){
+        if(_fade_light){ colorRGB = colorRGB*fade_value/(FADE_STEPS*(1-led/num_leds));}
+        fade_value = _fade_chase ? fade_value : 1. ;
+        
+        if(led<fade_value*num_leds){
+          int current_led = reverse ? num_leds-led-1 : led ;
           if(this->state() == DOWN || this->state() == ODOWN) {
-          Serial.println("down");
-            leds[led].r = 	qsub8 (leds[led].r, rgb_step.r);
-            leds[led].g = 	qsub8 (leds[led].g, rgb_step.g);
-            leds[led].b = 	qsub8 (leds[led].b, rgb_step.b);
+            leds[current_led].r = 	qsub8 (leds[current_led].r, colorRGB.r+1);
+            leds[current_led].g = 	qsub8 (leds[current_led].g, colorRGB.g+1);
+            leds[current_led].b = 	qsub8 (leds[current_led].b, colorRGB.b+1);
           }
           else{
-          leds[led].r = 	qadd8 (leds[led].r, rgb_step.r);
-          leds[led].g = 	qadd8 (leds[led].g, rgb_step.g);
-          leds[led].b = 	qadd8 (leds[led].b, rgb_step.b);
+            leds[current_led].r = 	qadd8 (leds[current_led].r, colorRGB.r);
+            leds[current_led].g = 	qadd8 (leds[current_led].g, colorRGB.g);
+            leds[current_led].b = 	qadd8 (leds[current_led].b, colorRGB.b);
           }
-          // leds[led] = blend(leds[led], CRGB(temp_led[led]), 255);
         }
         
-        // CRGB rgb_step = CRGB(temp_led[led]);
-        // Serial.println(this->state());
-        // if(this->state() == DOWN || this->state() == ODOWN) {
-        //   Serial.println("down");
-        //   leds[led].r = 	qsub8 (leds[led].r, rgb_step.r);
-        //   leds[led].g = 	qsub8 (leds[led].g, rgb_step.g);
-        //   leds[led].b = 	qsub8 (leds[led].b, rgb_step.b);
-        // }
-        // else{
-        // leds[led].r = 	qadd8 (leds[led].r, rgb_step.r);
-        // leds[led].g = 	qadd8 (leds[led].g, rgb_step.g);
-        // leds[led].b = 	qadd8 (leds[led].b, rgb_step.b);
-        // }
+        
+
       }
     }
+
+    // if(!_replace_array){
+    //   //add or remove one step of color 
+    //   current_value = color.val / (FADE_STEPS-1) ;
+    //   if(reverse){
+    //     if(led>fade_value*num_leds){temp_led[led].value = current_value ;}
+    //     else{temp_led[led].value = 0 ;}
+    //   }
+    //   else{
+    //     if(led<fade_value*num_leds){temp_led[led].value = current_value ;}
+    //     else{temp_led[led].value = 0 ;}
+    //   }
+      
+    //   // temp_led[led].value = current_value ;
+    //   // temp_led[led].value = temp_led[led].value / FADE_STEPS ;
+    //   temp_led[led].saturation = color.saturation ;
+    //   temp_led[led].hue = color.hue ;
+    //   if(update_flag){
+    //     CRGB rgb_step ;
+    //     if(reverse){ rgb_step= CRGB(temp_led[num_leds-led])  ;}
+    //     else{ rgb_step = CRGB(temp_led[led])  ;}
+        
+    //     if(this->state() == DOWN || this->state() == ODOWN) {
+    //     // Serial.println("down");
+    //       leds[led].r = 	qsub8 (leds[led].r, rgb_step.r);
+    //       leds[led].g = 	qsub8 (leds[led].g, rgb_step.g);
+    //       leds[led].b = 	qsub8 (leds[led].b, rgb_step.b);
+    //     }
+    //     else{
+    //       leds[led].r = 	qadd8 (leds[led].r, rgb_step.r);
+    //       leds[led].g = 	qadd8 (leds[led].g, rgb_step.g);
+    //       leds[led].b = 	qadd8 (leds[led].b, rgb_step.b);
+    //     }
+    //     // leds[led] = blend(leds[led], CRGB(temp_led[led]), 255);
+    //   }  
+      
+
+    // }
+    Serial.println(leds[0].r);
     update_flag=true ;
   }
 
+
+//was about working
+// if(update_flag){
+//       // Serial.println("loop");
+//     int current_step = fade_value * FADE_STEPS ;
+
+//     CHSV temp_led[this->num_leds] ;
+//     int current_value = color.val; 
+
+//     if(_fade_light){ current_value *= fade_value ;}
+
+//     current_value = constrain(current_value, 0, 255 ) ;
+//     // Serial.println(current_value);
+//     int chase_led = num_leds;
+//     for(int led=0; led<num_leds; led++){
+//       // Serial.println(led);
+//       fade_value = _fade_chase ? fade_value : 1. ;
+//       if(led<fade_value*num_leds){
+//         temp_led[led].value = current_value ;
+//         temp_led[led].saturation = color.saturation ;
+//         temp_led[led].hue = color.hue ;
+//       }
+//       else{
+//         temp_led[led].value = 0 ;
+//         temp_led[led].saturation = color.saturation ;
+//         temp_led[led].hue = color.hue ;
+//       }
+
+//       if(_replace_array){
+//         if(reverse){leds[led] = CRGB(temp_led[num_leds-led]);}
+//         else{leds[led] = CRGB(temp_led[led]);}
+//       }
+//       if(!_replace_array){
+//         //add or remove one step of color 
+//         current_value = color.val / (FADE_STEPS-1) ;
+//         if(reverse){
+//           if(led>fade_value*num_leds){temp_led[led].value = current_value ;}
+//           else{temp_led[led].value = 0 ;}
+//         }
+//         else{
+//           if(led<fade_value*num_leds){temp_led[led].value = current_value ;}
+//           else{temp_led[led].value = 0 ;}
+//         }
+        
+//         // temp_led[led].value = current_value ;
+//         // temp_led[led].value = temp_led[led].value / FADE_STEPS ;
+//         temp_led[led].saturation = color.saturation ;
+//         temp_led[led].hue = color.hue ;
+//         if(update_flag){
+//           CRGB rgb_step ;
+//           if(reverse){ rgb_step= CRGB(temp_led[num_leds-led])  ;}
+//           else{ rgb_step = CRGB(temp_led[led])  ;}
+          
+//           if(this->state() == DOWN || this->state() == ODOWN) {
+//           // Serial.println("down");
+//             leds[led].r = 	qsub8 (leds[led].r, rgb_step.r);
+//             leds[led].g = 	qsub8 (leds[led].g, rgb_step.g);
+//             leds[led].b = 	qsub8 (leds[led].b, rgb_step.b);
+//           }
+//           else{
+//             leds[led].r = 	qadd8 (leds[led].r, rgb_step.r);
+//             leds[led].g = 	qadd8 (leds[led].g, rgb_step.g);
+//             leds[led].b = 	qadd8 (leds[led].b, rgb_step.b);
+//           }
+//           // leds[led] = blend(leds[led], CRGB(temp_led[led]), 255);
+//         }
+//         Serial.println(leds[0].r);
+        
+
+//       }
+//     }
+//     update_flag=true ;
+  
+
+
 }
 
 // #endif

+ 4 - 0
Atm_fade_WS2812.hpp

@@ -21,6 +21,8 @@ class Atm_fade_WS2812 : public Machine {
   Atm_fade_WS2812& pause( uint32_t duration );
   Atm_fade_WS2812& fade( int fade_length);
   Atm_fade_WS2812& fade( int fade_length, bool fade_light, int fade_chase);
+  Atm_fade_WS2812& fadeSpeed( float fade_speed);
+  Atm_fade_WS2812& fadeSpeed( float fade_speed, bool fade_light, int fade_chase);
   Atm_fade_WS2812& repeat( uint16_t repeat );
   Atm_fade_WS2812& on( void );
   Atm_fade_WS2812& off( void );
@@ -34,12 +36,14 @@ class Atm_fade_WS2812 : public Machine {
   Atm_fade_WS2812& replaceValues(bool replace); // if true replace the array, else just add values
   Atm_fade_WS2812& updateRate(int new_update_rate); // change update rate
   Atm_fade_WS2812& update();
+  Atm_fade_WS2812& reversed(bool _reverse);
 
  private:
   CRGB * leds;
   int num_leds ;
   int offset_leds ;
   CHSV color ;
+  bool reverse = false ;
   int FADE_STEPS = 254 ;
   uint32_t fade_duration ; //last led to update
   unsigned long fade_start_millis ;