TuXaMaT

Led strip effects

Dec 3rd, 2023
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 20.72 KB | None | 0 0
  1. light:
  2.   - platform: neopixelbus
  3.     power_supply: power
  4.     variant: WS2813
  5.     pin: GPIO23
  6.     num_leds: ${num_leds}
  7.     type: RGB
  8.     color_correct: [50%, 50%, 50%]
  9.     default_transition_length: 1s
  10.     restore_mode: always_off
  11.     name: "Striplight"
  12.     id: "Striplight"    
  13.     effects:                    
  14.       - addressable_lambda:
  15.           name: "Exit"
  16.           update_interval: 10ms
  17.           lambda: |-
  18.             static Color led[${num_leds}];
  19.             static uint16_t progress = 0;
  20.             static int middle = ${num_leds} / 2;
  21.  
  22.             const uint8_t speed = 4;
  23.             const uint8_t steps = 30;
  24.  
  25.             const bool revers = false;
  26.             const bool twoside = false;
  27.             const bool fade = false;
  28.  
  29.             if (initial_run)
  30.             {
  31.               ESP_LOGD("effect", "Rise and Fade x%i / %i", speed, steps);
  32.  
  33.               for (int i = 0; i < it.size(); i++)
  34.               {
  35.                 led[i] = fade ? current_color : Color::BLACK;
  36.               }
  37.               progress = 0;
  38.             }
  39.  
  40.             uint8_t r = max(1, current_color.r / steps);
  41.             uint8_t g = max(1, current_color.g / steps);
  42.             uint8_t b = max(1, current_color.b / steps);
  43.            
  44.             // Let's light up the ones who got their turn.
  45.             for (int i = 0; i < speed; i++)
  46.             {
  47.               Color color = fade ? Color(current_color.r - r, current_color.g - g, current_color.b - b) : Color(r, g, b);
  48.  
  49.               if (twoside)
  50.               {
  51.                 if (progress < middle)
  52.                 {
  53.                   if ((revers && progress == middle - 1) || (progress == 0 && !revers))
  54.                   {
  55.                     led[middle] = color;
  56.                   }
  57.                   led[revers ? max(0, (int) progress) : min((int) (middle + progress + 1), (int) it.size())] = color;
  58.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : max(0, (int) (middle - progress - 1))] = color;
  59.                 }
  60.               }
  61.               else
  62.               {
  63.                 if (progress < it.size())
  64.                 {
  65.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : min((int) progress, (int) it.size())] = color;
  66.                 }
  67.               }
  68.  
  69.               progress++;
  70.             }
  71.  
  72.             bool complete = true;
  73.  
  74.             // Increase or Decrease the brightness of those already in action.
  75.             for (int i = 0; i < it.size(); i++)
  76.             {
  77.               if ( !(led[i].r == 0 && led[i].g == 0 && led[i].b == 0) &&
  78.                   !(led[i].r == current_color.r && led[i].g == current_color.g && led[i].b == current_color.b) )
  79.               {
  80.                 if (fade)
  81.                 {
  82.                   led[i].r = max(0, led[i].r - r);
  83.                   led[i].g = max(0, led[i].g - g);
  84.                   led[i].b = max(0, led[i].b - b);
  85.                 }
  86.                 else
  87.                 {
  88.                   led[i].r = min(led[i].r + r, (int) current_color.r);
  89.                   led[i].g = min(led[i].g + g, (int) current_color.g);
  90.                   led[i].b = min(led[i].b + b, (int) current_color.b);
  91.                 }
  92.  
  93.                 complete = false;
  94.               }
  95.             }
  96.  
  97.             // Everyone's already on burn, mission over
  98.             if (complete)
  99.             {
  100.               if (fade)
  101.               {
  102.                 auto call = id(Striplight).turn_off();
  103.                 call.perform();
  104.               }
  105.               else
  106.               {
  107.                 it.all() = current_color;
  108.                 auto call = id(Striplight).turn_on();
  109.                 call.set_effect("None");
  110.                 call.perform();
  111.               }
  112.  
  113.               ESP_LOGD("effect", "Rise and Fade x%i / %i - Complete", speed, steps);
  114.             }
  115.             else // Transfer the current state to the led strip
  116.             {
  117.               for (int i = 0; i < it.size(); i++)
  118.               {
  119.                 it[i] = led[i];
  120.               }
  121.             }
  122.       - addressable_lambda:
  123.           name: "Enter"
  124.           update_interval: 10ms
  125.           lambda: |-
  126.             static Color led[${num_leds}];
  127.             static uint16_t progress = 0;
  128.             static int middle = ${num_leds} / 2;
  129.  
  130.             const uint8_t speed = 4;
  131.             const uint8_t steps = 30;
  132.  
  133.             const bool revers = true;
  134.             const bool twoside = false;
  135.             const bool fade = false;
  136.  
  137.             if (initial_run)
  138.             {
  139.               ESP_LOGD("effect", "Rise and Fade x%i / %i", speed, steps);
  140.  
  141.               for (int i = 0; i < it.size(); i++)
  142.               {
  143.                 led[i] = fade ? current_color : Color::BLACK;
  144.               }
  145.               progress = 0;
  146.             }
  147.  
  148.             uint8_t r = max(1, current_color.r / steps);
  149.             uint8_t g = max(1, current_color.g / steps);
  150.             uint8_t b = max(1, current_color.b / steps);
  151.            
  152.             // Let's light up the ones who got their turn.
  153.             for (int i = 0; i < speed; i++)
  154.             {
  155.               Color color = fade ? Color(current_color.r - r, current_color.g - g, current_color.b - b) : Color(r, g, b);
  156.  
  157.               if (twoside)
  158.               {
  159.                 if (progress < middle)
  160.                 {
  161.                   if ((revers && progress == middle - 1) || (progress == 0 && !revers))
  162.                   {
  163.                     led[middle] = color;
  164.                   }
  165.                   led[revers ? max(0, (int) progress) : min((int) (middle + progress + 1), (int) it.size())] = color;
  166.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : max(0, (int) (middle - progress - 1))] = color;
  167.                 }
  168.               }
  169.               else
  170.               {
  171.                 if (progress < it.size())
  172.                 {
  173.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : min((int) progress, (int) it.size())] = color;
  174.                 }
  175.               }
  176.  
  177.               progress++;
  178.             }
  179.  
  180.             bool complete = true;
  181.  
  182.             // Increase or Decrease the brightness of those already in action.
  183.             for (int i = 0; i < it.size(); i++)
  184.             {
  185.               if ( !(led[i].r == 0 && led[i].g == 0 && led[i].b == 0) &&
  186.                   !(led[i].r == current_color.r && led[i].g == current_color.g && led[i].b == current_color.b) )
  187.               {
  188.                 if (fade)
  189.                 {
  190.                   led[i].r = max(0, led[i].r - r);
  191.                   led[i].g = max(0, led[i].g - g);
  192.                   led[i].b = max(0, led[i].b - b);
  193.                 }
  194.                 else
  195.                 {
  196.                   led[i].r = min(led[i].r + r, (int) current_color.r);
  197.                   led[i].g = min(led[i].g + g, (int) current_color.g);
  198.                   led[i].b = min(led[i].b + b, (int) current_color.b);
  199.                 }
  200.  
  201.                 complete = false;
  202.               }
  203.             }
  204.  
  205.             // Everyone's already on burn, mission over
  206.             if (complete)
  207.             {
  208.               if (fade)
  209.               {
  210.                 auto call = id(Striplight).turn_off();
  211.                 call.perform();
  212.               }
  213.               else
  214.               {
  215.                 it.all() = current_color;
  216.                 auto call = id(Striplight).turn_on();
  217.                 call.set_effect("None");
  218.                 call.perform();
  219.               }
  220.  
  221.               ESP_LOGD("effect", "Rise and Fade x%i / %i - Complete", speed, steps);
  222.             }
  223.             else // Transfer the current state to the led strip
  224.             {
  225.               for (int i = 0; i < it.size(); i++)
  226.               {
  227.                 it[i] = led[i];
  228.               }
  229.             }
  230.       - addressable_lambda:
  231.           name: "Side"
  232.           update_interval: 10ms
  233.           lambda: |-
  234.             static Color led[${num_leds}];
  235.             static uint16_t progress = 0;
  236.             static int middle = ${num_leds} / 2;
  237.  
  238.             const uint8_t speed = 3.2;
  239.             const uint8_t steps = 30;
  240.  
  241.             const bool revers = false;
  242.             const bool twoside = true;
  243.             const bool fade = false;
  244.  
  245.             if (initial_run)
  246.             {
  247.               ESP_LOGD("effect", "Rise and Fade x%i / %i", speed, steps);
  248.  
  249.               for (int i = 0; i < it.size(); i++)
  250.               {
  251.                 led[i] = fade ? current_color : Color::BLACK;
  252.               }
  253.               progress = 0;
  254.             }
  255.  
  256.             uint8_t r = max(1, current_color.r / steps);
  257.             uint8_t g = max(1, current_color.g / steps);
  258.             uint8_t b = max(1, current_color.b / steps);
  259.            
  260.             // Let's light up the ones who got their turn.
  261.             for (int i = 0; i < speed; i++)
  262.             {
  263.               Color color = fade ? Color(current_color.r - r, current_color.g - g, current_color.b - b) : Color(r, g, b);
  264.  
  265.               if (twoside)
  266.               {
  267.                 if (progress < middle)
  268.                 {
  269.                   if ((revers && progress == middle - 1) || (progress == 0 && !revers))
  270.                   {
  271.                     led[middle] = color;
  272.                   }
  273.                   led[revers ? max(0, (int) progress) : min((int) (middle + progress + 1), (int) it.size())] = color;
  274.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : max(0, (int) (middle - progress - 1))] = color;
  275.                 }
  276.               }
  277.               else
  278.               {
  279.                 if (progress < it.size())
  280.                 {
  281.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : min((int) progress, (int) it.size())] = color;
  282.                 }
  283.               }
  284.  
  285.               progress++;
  286.             }
  287.  
  288.             bool complete = true;
  289.  
  290.             // Increase or Decrease the brightness of those already in action.
  291.             for (int i = 0; i < it.size(); i++)
  292.             {
  293.               if ( !(led[i].r == 0 && led[i].g == 0 && led[i].b == 0) &&
  294.                   !(led[i].r == current_color.r && led[i].g == current_color.g && led[i].b == current_color.b) )
  295.               {
  296.                 if (fade)
  297.                 {
  298.                   led[i].r = max(0, led[i].r - r);
  299.                   led[i].g = max(0, led[i].g - g);
  300.                   led[i].b = max(0, led[i].b - b);
  301.                 }
  302.                 else
  303.                 {
  304.                   led[i].r = min(led[i].r + r, (int) current_color.r);
  305.                   led[i].g = min(led[i].g + g, (int) current_color.g);
  306.                   led[i].b = min(led[i].b + b, (int) current_color.b);
  307.                 }
  308.  
  309.                 complete = false;
  310.               }
  311.             }
  312.  
  313.             // Everyone's already on burn, mission over
  314.             if (complete)
  315.             {
  316.               if (fade)
  317.               {
  318.                 auto call = id(Striplight).turn_off();
  319.                 call.perform();
  320.               }
  321.               else
  322.               {
  323.                 it.all() = current_color;
  324.                 auto call = id(Striplight).turn_on();
  325.                 call.set_effect("None");
  326.                 call.perform();
  327.               }
  328.  
  329.               ESP_LOGD("effect", "Rise and Fade x%i / %i - Complete", speed, steps);
  330.             }
  331.             else // Transfer the current state to the led strip
  332.             {
  333.               for (int i = 0; i < it.size(); i++)
  334.               {
  335.                 it[i] = led[i];
  336.               }
  337.             }
  338.       - addressable_lambda:
  339.           name: "Side_fade"
  340.           update_interval: 10ms
  341.           lambda: |-
  342.             static Color led[${num_leds}];
  343.             static uint16_t progress = 0;
  344.             static int middle = ${num_leds} / 2;
  345.  
  346.             const uint8_t speed = 3.2;
  347.             const uint8_t steps = 30;
  348.  
  349.             const bool revers = false;
  350.             const bool twoside = true;
  351.             const bool fade = true;
  352.  
  353.             if (initial_run)
  354.             {
  355.               ESP_LOGD("effect", "Rise and Fade x%i / %i", speed, steps);
  356.  
  357.               for (int i = 0; i < it.size(); i++)
  358.               {
  359.                 led[i] = fade ? current_color : Color::BLACK;
  360.               }
  361.               progress = 0;
  362.             }
  363.  
  364.             uint8_t r = max(1, current_color.r / steps);
  365.             uint8_t g = max(1, current_color.g / steps);
  366.             uint8_t b = max(1, current_color.b / steps);
  367.            
  368.             // Let's light up the ones who got their turn.
  369.             for (int i = 0; i < speed; i++)
  370.             {
  371.               Color color = fade ? Color(current_color.r - r, current_color.g - g, current_color.b - b) : Color(r, g, b);
  372.  
  373.               if (twoside)
  374.               {
  375.                 if (progress < middle)
  376.                 {
  377.                   if ((revers && progress == middle - 1) || (progress == 0 && !revers))
  378.                   {
  379.                     led[middle] = color;
  380.                   }
  381.                   led[revers ? max(0, (int) progress) : min((int) (middle + progress + 1), (int) it.size())] = color;
  382.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : max(0, (int) (middle - progress - 1))] = color;
  383.                 }
  384.               }
  385.               else
  386.               {
  387.                 if (progress < it.size())
  388.                 {
  389.                   led[revers ? max(0, (int) (it.size() - progress - 1)) : min((int) progress, (int) it.size())] = color;
  390.                 }
  391.               }
  392.  
  393.               progress++;
  394.             }
  395.  
  396.             bool complete = true;
  397.  
  398.             // Increase or Decrease the brightness of those already in action.
  399.             for (int i = 0; i < it.size(); i++)
  400.             {
  401.               if ( !(led[i].r == 0 && led[i].g == 0 && led[i].b == 0) &&
  402.                   !(led[i].r == current_color.r && led[i].g == current_color.g && led[i].b == current_color.b) )
  403.               {
  404.                 if (fade)
  405.                 {
  406.                   led[i].r = max(0, led[i].r - r);
  407.                   led[i].g = max(0, led[i].g - g);
  408.                   led[i].b = max(0, led[i].b - b);
  409.                 }
  410.                 else
  411.                 {
  412.                   led[i].r = min(led[i].r + r, (int) current_color.r);
  413.                   led[i].g = min(led[i].g + g, (int) current_color.g);
  414.                   led[i].b = min(led[i].b + b, (int) current_color.b);
  415.                 }
  416.  
  417.                 complete = false;
  418.               }
  419.             }
  420.  
  421.             // Everyone's already on burn, mission over
  422.             if (complete)
  423.             {
  424.               if (fade)
  425.               {
  426.                 auto call = id(Striplight).turn_off();
  427.                 call.perform();
  428.               }
  429.               else
  430.               {
  431.                 it.all() = current_color;
  432.                 auto call = id(Striplight).turn_on();
  433.                 call.set_effect("None");
  434.                 call.perform();
  435.               }
  436.  
  437.               ESP_LOGD("effect", "Rise and Fade x%i / %i - Complete", speed, steps);
  438.             }
  439.             else // Transfer the current state to the led strip
  440.             {
  441.               for (int i = 0; i < it.size(); i++)
  442.               {
  443.                 it[i] = led[i];
  444.               }
  445.             }
  446.       - addressable_lambda:
  447.           name: "ColorScan"
  448.           update_interval: 10ms
  449.           lambda: |-
  450.  
  451.             // it.size() - Number of LEDs
  452.             // it[num] - Access the LED at index num.
  453.  
  454.             // Set the LED at num to the given r, g, b values
  455.             // it[num] = Color(r, g, b);
  456.  
  457.             // Get the color at index num (Color instance)
  458.             // it[num].get();
  459.  
  460.             static uint16_t num1 = 0;
  461.             static uint16_t red = 255;
  462.             static uint16_t green = 3;
  463.             static uint16_t blue = 3;
  464.             static bool dir = false;
  465.        
  466.             uint16_t num = num1;
  467.             uint16_t numled = ${num_leds};
  468.  
  469.             if(red > 251 && blue < 4 && green < 252){
  470.               green+=5;
  471.  
  472.             } else if (red > 3 && green > 251 && blue < 4){
  473.               red-=5;
  474.  
  475.             } else if (red < 4 && green > 251 && blue < 252){
  476.               blue+=5;
  477.          
  478.             } else if (red < 4 && green > 3 && blue > 251){
  479.               green-=5;
  480.          
  481.             } else if (red < 252 && green < 4 && blue > 251){
  482.               red+=5;
  483.          
  484.             } else if (red > 251 && green < 4 && blue > 3){
  485.               blue-=5;
  486.          
  487.             }
  488.  
  489.             it[num] = Color(red, green, blue);
  490.             if(dir) {
  491.               if(num1 == numled){
  492.                 dir = false;
  493.               } else{
  494.                 num1++;
  495.               }
  496.             } else  {
  497.               if(num1 == 0){
  498.                 dir = true;
  499.               } else{
  500.                 num1--;
  501.               }
  502.             }
  503.       - pulse:
  504.       - pulse:
  505.           name: "Fast Pulse"
  506.           transition_length: 0.5s
  507.           update_interval: 0.5s
  508.           min_brightness: 0%
  509.           max_brightness: 100%
  510.       - pulse:
  511.           name: "Slow Pulse"
  512.           # transition_length: 1s      # defaults to 1s
  513.           update_interval: 2s
  514.       - strobe:
  515.       - strobe:
  516.           name: Strobe Effect With Custom Values
  517.           colors:
  518.             - state: true
  519.               brightness: 100%
  520.               red: 100%
  521.               green: 90%
  522.               blue: 0%
  523.               duration: 500ms
  524.             - state: false
  525.               duration: 250ms
  526.             - state: true
  527.               brightness: 100%
  528.               red: 0%
  529.               green: 100%
  530.               blue: 0%
  531.               duration: 500ms
  532.       - flicker:
  533.       - flicker:
  534.           name: Flicker Effect With Custom Values
  535.           alpha: 95%
  536.           intensity: 1.5%
  537.       - addressable_lambda:
  538.           name: Gold Glitter
  539.           update_interval: 18ms
  540.           lambda:
  541.            static int state = 0;
  542.  
  543.             if (initial_run){
  544.               state = 0;
  545.  
  546.               it.all() = ESPColor(218,165,32);
  547.  
  548.               ESP_LOGD("custom", "Gold Glitter");
  549.             } else {
  550.  
  551.               it.all() = ESPColor(218,165,32);
  552.  
  553.               if(state==0){
  554.                 int i = rand() % it.size();
  555.                 it[i] = ESPColor::WHITE;
  556.                 state += 1;
  557.               } else {
  558.                 state += 1;
  559.                 state = state % 10;
  560.               }
  561.             }
  562.   # from reddit user thedoctor___
  563.   # https://www.reddit.com/r/homeassistant/comments/bua3u8/esphome_what_custom_addressable_lambda_effects/
  564.  
  565.       - addressable_lambda:
  566.             name: "Fire"
  567.             update_interval: 15ms
  568.             lambda: |-
  569.               int Cooling = 55;
  570.               int Sparking = 110;
  571.               static byte heat[188];
  572.               int cooldown;
  573.  
  574.               // Step 1.  Cool down every cell a little
  575.               for( int i = 0; i < it.size(); i++) {
  576.                 cooldown = random(0, ((Cooling * 10) / it.size()) + 2);
  577.  
  578.                 if(cooldown>heat[i]) {
  579.                   heat[i]=0;
  580.                 } else {
  581.                   heat[i]=heat[i]-cooldown;
  582.                 }
  583.               }
  584.  
  585.               // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  586.               for( int k= it.size() - 1; k >= 2; k--) {
  587.                 heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  588.               }
  589.  
  590.               // Step 3.  Randomly ignite new 'sparks' near the bottom
  591.               if( random(255) < Sparking ) {
  592.                 int y = random(7);
  593.                 heat[y] = heat[y] + random(160,255);
  594.               }
  595.  
  596.               // Step 4.  Convert heat to LED colors
  597.               for( int Pixel = 0; Pixel < it.size(); Pixel++) {
  598.                 // Scale 'heat' down from 0-255 to 0-191
  599.                 byte t192 = round((heat[Pixel]/255.0)*191);
  600.  
  601.                 // calculate ramp up from
  602.                 byte heatramp = t192 & 0x3F; // 0..63
  603.                 heatramp <<= 2; // scale up to 0..252
  604.  
  605.                 // figure out which third of the spectrum we're in:
  606.                //this is where you can reverse the effect by switching the commented out lines in all 3 places.
  607.                 if( t192 > 0x80) {                     // hottest
  608.                   //it[it.size() - Pixel - 1] = ESPColor(255, 255, heatramp);
  609.                   it[Pixel] = ESPColor(255, 255, heatramp);
  610.                 } else if( t192 > 0x40 ) {             // middle
  611.                   //it[it.size() - Pixel - 1] = ESPColor(255, heatramp, 0);
  612.                   it[Pixel] = ESPColor(255, heatramp, 0);
  613.                 } else {                               // coolest
  614.                   //it[it.size() - Pixel - 1] = ESPColor(heatramp, 0, 0);
  615.                   it[Pixel] = ESPColor(heatramp, 0, 0);
  616.                 }
  617.               }
Advertisement
Add Comment
Please, Sign In to add comment