From b6708350814ed3c7f8fad767b17f3e871ea01824 Mon Sep 17 00:00:00 2001
From: Minecon724 <git@m724.eu>
Date: Tue, 14 May 2024 18:42:12 +0200
Subject: [PATCH] improve

---
 ws2811.py | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/ws2811.py b/ws2811.py
index 27eb058..4cd2e2f 100644
--- a/ws2811.py
+++ b/ws2811.py
@@ -3,8 +3,9 @@ from rp2 import PIO, StateMachine, asm_pio
 from time import sleep_ms, sleep_us
 from random import randint
 
-LEDS = 3
+LEDS = 5
 PIN = Pin(16)
+DELAY = 5
 
 """
 inspired by https://github.com/raspberrypi/pico-micropython-examples/blob/master/pio/pio_ws2812.py
@@ -58,8 +59,25 @@ def prog():
 sm = StateMachine(0, prog, sideset_base=PIN)
 sm.active(1)
 
+def transition_step(src_hex: int, tgt_hex: int, prog: float):
+    src_hex += int(( (tgt_hex >> 16) - (src_hex >> 16) ) * prog) << 16
+    src_hex += int(( (tgt_hex >> 8 & 0xFF) - (src_hex >> 8 & 0xFF) ) * prog) << 8
+    src_hex += int(( (tgt_hex & 0xFF) - (src_hex & 0xFF) ) * prog)
+    
+    return src_hex
+
+cols = [randint(0, 0xFFFFFF) for i in range(LEDS)]
+cols_new = cols[1:] + [randint(0, 0xFFFFFF)]
+
 while True:
-    for i in range(LEDS):
-        sm.put(randint(0, 0xFFFFFF), 8) # I still don't know why 8 not 24
-        
-    sleep_ms(500)
\ No newline at end of file
+    cols.pop(0)
+    cols.append(cols_new[-1])
+    cols_new.pop(0)
+    cols_new.append(randint(0, 0xFFFFFF))
+    
+    for i in range(0, 100):
+        for l in range(LEDS):
+            cold = transition_step(cols[l], cols_new[l], i / 100)
+            sm.put(cold, 8) # I don't know why 8 not 24
+        sleep_ms(DELAY)
+        
\ No newline at end of file