Compare commits
10 Commits
c63e907204
...
2a7b5527a5
Author | SHA1 | Date |
---|---|---|
|
2a7b5527a5 | |
|
50545e3170 | |
|
d2826a0f63 | |
|
87fc74bb51 | |
|
03f3f02da8 | |
|
524db5e979 | |
|
279416cded | |
|
fbd14f2e16 | |
|
1989f6f5c9 | |
|
a19b1e86f2 |
42
src/main.py
42
src/main.py
|
@ -9,6 +9,7 @@ import machine
|
||||||
import time
|
import time
|
||||||
import wifi
|
import wifi
|
||||||
import json
|
import json
|
||||||
|
from p2p import p2p
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
@ -24,30 +25,14 @@ async def main():
|
||||||
async def tick():
|
async def tick():
|
||||||
while True:
|
while True:
|
||||||
patterns.tick()
|
patterns.tick()
|
||||||
await asyncio.sleep_ms(1)
|
await asyncio.sleep_ms(0)
|
||||||
|
|
||||||
|
async def system():
|
||||||
async def espnow():
|
while True:
|
||||||
e = aioespnow.AIOESPNow() # Returns AIOESPNow enhanced with async support
|
gc.collect()
|
||||||
e.active(True)
|
for i in range(60):
|
||||||
async for mac, msg in e:
|
wdt.feed()
|
||||||
data = json.loads(msg)
|
await asyncio.sleep(1)
|
||||||
print(data)
|
|
||||||
if settings["id"] in data["ids"] or settings["id"] == 0:
|
|
||||||
settings.set_settings(data["settings"], patterns)
|
|
||||||
print("should not print")
|
|
||||||
|
|
||||||
async def wifi_connect():
|
|
||||||
for i in range(10):
|
|
||||||
config = wifi.connect(settings.get("wifi_ssid", ""),
|
|
||||||
settings.get("wifi_password", ""),
|
|
||||||
settings.get("wifi_ip", ""),
|
|
||||||
settings.get("wifi_gateway", "")
|
|
||||||
)
|
|
||||||
if config:
|
|
||||||
print(config)
|
|
||||||
break
|
|
||||||
await asyncio.sleep_ms(500)
|
|
||||||
|
|
||||||
w = web(settings, patterns)
|
w = web(settings, patterns)
|
||||||
print(settings)
|
print(settings)
|
||||||
|
@ -57,18 +42,11 @@ async def main():
|
||||||
wdt = machine.WDT(timeout=10000)
|
wdt = machine.WDT(timeout=10000)
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
|
|
||||||
|
|
||||||
#asyncio.create_task(wifi_connect())
|
|
||||||
asyncio.create_task(tick())
|
asyncio.create_task(tick())
|
||||||
asyncio.create_task(espnow())
|
asyncio.create_task(p2p(settings, patterns))
|
||||||
|
asyncio.create_task(system())
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
#print(time.localtime())
|
|
||||||
gc.collect()
|
|
||||||
for i in range(20):
|
|
||||||
wdt.feed()
|
|
||||||
await asyncio.sleep_ms(1000)
|
|
||||||
|
|
||||||
# cleanup before ending the application
|
# cleanup before ending the application
|
||||||
await server
|
await server
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
import asyncio
|
||||||
|
import aioespnow
|
||||||
|
import json
|
||||||
|
|
||||||
|
async def p2p(settings, patterns):
|
||||||
|
e = aioespnow.AIOESPNow() # Returns AIOESPNow enhanced with async support
|
||||||
|
e.active(True)
|
||||||
|
async for mac, msg in e:
|
||||||
|
try:
|
||||||
|
data = json.loads(msg)
|
||||||
|
except:
|
||||||
|
print(f"Failed to load espnow data {msg}")
|
||||||
|
continue
|
||||||
|
print(data)
|
||||||
|
if "names" not in data or settings.get("name") in data.get("names", []):
|
||||||
|
if "step" in settings and isinstance(settings["step"], int):
|
||||||
|
patterns.set_pattern_step(settings["step"])
|
||||||
|
else:
|
||||||
|
settings.set_settings(data.get("settings", {}), patterns, data.get("save", False))
|
||||||
|
print("should not print")
|
372
src/patterns.py
372
src/patterns.py
|
@ -18,25 +18,45 @@ class Patterns:
|
||||||
"rainbow_cycle": self.rainbow_cycle_step,
|
"rainbow_cycle": self.rainbow_cycle_step,
|
||||||
"theater_chase": self.theater_chase_step,
|
"theater_chase": self.theater_chase_step,
|
||||||
"blink": self.blink_step,
|
"blink": self.blink_step,
|
||||||
"random_color_wipe": self.random_color_wipe_step,
|
"color_transition": self.color_transition_step, # Added new pattern
|
||||||
"random_rainbow_cycle": self.random_rainbow_cycle_step,
|
"flicker": self.flicker_step,
|
||||||
"random_theater_chase": self.random_theater_chase_step,
|
"scanner": self.scanner_step, # New: Single direction scanner
|
||||||
"random_blink": self.random_blink_step,
|
"bidirectional_scanner": self.bidirectional_scanner_step, # New: Bidirectional scanner
|
||||||
"color_transition": self.color_transition_step,
|
|
||||||
"external": None
|
"external": None
|
||||||
}
|
}
|
||||||
self.selected = selected
|
self.selected = selected
|
||||||
self.color1 = color1
|
# Ensure colors list always starts with at least two for robust transition handling
|
||||||
self.color2 = color2
|
self.colors = [color1, color2] if color1 != color2 else [color1, (255, 255, 255)] # Fallback if initial colors are same
|
||||||
self.transition_duration = delay * 10 # Default transition duration is 10 times the delay
|
if not self.colors: # Ensure at least one color exists
|
||||||
self.transition_step = 0
|
self.colors = [(0, 0, 0)]
|
||||||
self.current_color = self.color1
|
|
||||||
|
self.transition_duration = delay * 50 # Default transition duration
|
||||||
|
self.hold_duration = delay * 10 # Default hold duration at each color
|
||||||
|
self.transition_step = 0 # Current step in the transition
|
||||||
|
self.current_color_idx = 0 # Index of the color currently being held/transitioned from
|
||||||
|
self.current_color = self.colors[self.current_color_idx] # The actual blended color
|
||||||
|
|
||||||
|
self.hold_start_time = utime.ticks_ms() # Time when the current color hold started
|
||||||
|
|
||||||
|
# New attributes for scanner patterns
|
||||||
|
self.scanner_direction = 1 # 1 for forward, -1 for backward
|
||||||
|
self.scanner_tail_length = 3 # Number of trailing pixels
|
||||||
|
|
||||||
def sync(self):
|
def sync(self):
|
||||||
self.pattern_step=0
|
self.pattern_step=0
|
||||||
self.last_update = utime.ticks_ms() - self.delay
|
self.last_update = utime.ticks_ms() - self.delay
|
||||||
|
if self.selected == "color_transition":
|
||||||
|
self.transition_step = 0
|
||||||
|
self.current_color_idx = 0
|
||||||
|
self.current_color = self.colors[self.current_color_idx]
|
||||||
|
self.hold_start_time = utime.ticks_ms() # Reset hold time
|
||||||
|
# Reset scanner specific variables
|
||||||
|
self.scanner_direction = 1
|
||||||
self.tick()
|
self.tick()
|
||||||
|
|
||||||
|
def set_pattern_step(self, step):
|
||||||
|
self.pattern_step = step
|
||||||
|
|
||||||
def tick(self):
|
def tick(self):
|
||||||
if self.patterns[self.selected]:
|
if self.patterns[self.selected]:
|
||||||
self.patterns[self.selected]()
|
self.patterns[self.selected]()
|
||||||
|
@ -48,39 +68,122 @@ class Patterns:
|
||||||
|
|
||||||
def set_delay(self, delay):
|
def set_delay(self, delay):
|
||||||
self.delay = delay
|
self.delay = delay
|
||||||
# Update transition duration when delay changes for color_transition pattern
|
# Update transition duration and hold duration when delay changes
|
||||||
if self.selected == "color_transition":
|
self.transition_duration = self.delay * 50
|
||||||
self.transition_duration = self.delay * 10 # Or some other multiplier
|
self.hold_duration = self.delay * 10
|
||||||
|
|
||||||
|
|
||||||
def set_brightness(self, brightness):
|
def set_brightness(self, brightness):
|
||||||
self.brightness = brightness
|
self.brightness = brightness
|
||||||
|
|
||||||
def set_color1(self, color):
|
def set_color1(self, color):
|
||||||
self.color1 = color
|
if len(self.colors) > 0:
|
||||||
|
self.colors[0] = color
|
||||||
if self.selected == "color_transition":
|
if self.selected == "color_transition":
|
||||||
|
# If the first color is changed, potentially reset transition
|
||||||
|
# to start from this new color if we were about to transition from it
|
||||||
|
if self.current_color_idx == 0:
|
||||||
self.transition_step = 0
|
self.transition_step = 0
|
||||||
self.current_color = self.color1
|
self.current_color = self.colors[0]
|
||||||
|
self.hold_start_time = utime.ticks_ms()
|
||||||
|
else:
|
||||||
|
self.colors.append(color)
|
||||||
|
|
||||||
|
|
||||||
def set_color2(self, color):
|
def set_color2(self, color):
|
||||||
self.color2 = color
|
if len(self.colors) > 1:
|
||||||
|
self.colors[1] = color
|
||||||
|
elif len(self.colors) == 1:
|
||||||
|
self.colors.append(color)
|
||||||
|
else: # List is empty
|
||||||
|
self.colors.append((0,0,0)) # Dummy color
|
||||||
|
self.colors.append(color)
|
||||||
|
|
||||||
|
|
||||||
|
def set_colors(self, colors):
|
||||||
|
if colors and len(colors) >= 2:
|
||||||
|
self.colors = colors
|
||||||
if self.selected == "color_transition":
|
if self.selected == "color_transition":
|
||||||
|
self.sync() # Reset transition if new color list is provided
|
||||||
|
elif colors and len(colors) == 1:
|
||||||
|
self.colors = [colors[0], (255,255,255)] # Add a default second color
|
||||||
|
if self.selected == "color_transition":
|
||||||
|
print("Warning: 'color_transition' requires at least two colors. Adding a default second color.")
|
||||||
|
self.sync()
|
||||||
|
else:
|
||||||
|
print("Error: set_colors requires a list of at least one color.")
|
||||||
|
self.colors = [(0,0,0), (255,255,255)] # Fallback
|
||||||
|
if self.selected == "color_transition":
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
def set_color(self, num, color):
|
||||||
|
# Changed: More robust index check
|
||||||
|
if 0 <= num < len(self.colors):
|
||||||
|
self.colors[num] = color
|
||||||
|
# If the changed color is part of the current or next transition,
|
||||||
|
# restart the transition for smoother updates
|
||||||
|
if self.selected == "color_transition":
|
||||||
|
current_from_idx = self.current_color_idx
|
||||||
|
current_to_idx = (self.current_color_idx + 1) % len(self.colors)
|
||||||
|
if num == current_from_idx or num == current_to_idx:
|
||||||
|
# If we change a color involved in the current transition,
|
||||||
|
# it's best to restart the transition state for smoothness.
|
||||||
self.transition_step = 0
|
self.transition_step = 0
|
||||||
self.current_color = self.color1
|
self.current_color_idx = current_from_idx # Stay at the current starting color
|
||||||
|
self.current_color = self.colors[self.current_color_idx]
|
||||||
|
self.hold_start_time = utime.ticks_ms() # Reset hold
|
||||||
|
return True
|
||||||
|
elif num == len(self.colors): # Allow setting a new color at the end
|
||||||
|
self.colors.append(color)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def add_color(self, color):
|
||||||
|
self.colors.append(color)
|
||||||
|
if self.selected == "color_transition" and len(self.colors) == 2:
|
||||||
|
# If we just added the second color needed for transition
|
||||||
|
self.sync()
|
||||||
|
|
||||||
|
|
||||||
def apply_brightness(self, color):
|
def del_color(self, num):
|
||||||
return tuple(int(c * self.brightness / 255) for c in color)
|
# Changed: More robust index check and using del for lists
|
||||||
|
if 0 <= num < len(self.colors):
|
||||||
|
del self.colors[num]
|
||||||
|
# If the color being deleted was part of the current transition,
|
||||||
|
# re-evaluate the current_color_idx
|
||||||
|
if self.selected == "color_transition":
|
||||||
|
if len(self.colors) < 2: # Need at least two colors for transition
|
||||||
|
print("Warning: Not enough colors for 'color_transition'. Switching to 'on'.")
|
||||||
|
self.select("on") # Or some other default
|
||||||
|
else:
|
||||||
|
# Adjust index if it's out of bounds after deletion or was the one transitioning from
|
||||||
|
self.current_color_idx %= len(self.colors)
|
||||||
|
self.transition_step = 0
|
||||||
|
self.current_color = self.colors[self.current_color_idx]
|
||||||
|
self.hold_start_time = utime.ticks_ms()
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def apply_brightness(self, color, brightness_override=None):
|
||||||
|
effective_brightness = brightness_override if brightness_override is not None else self.brightness
|
||||||
|
return tuple(int(c * effective_brightness / 255) for c in color)
|
||||||
|
|
||||||
def select(self, pattern):
|
def select(self, pattern):
|
||||||
if pattern in self.patterns:
|
if pattern in self.patterns:
|
||||||
self.selected = pattern
|
self.selected = pattern
|
||||||
self.sync() # Reset pattern state when selecting a new pattern
|
self.sync() # Reset pattern state when selecting a new pattern
|
||||||
if pattern == "color_transition":
|
if pattern == "color_transition":
|
||||||
|
if len(self.colors) < 2:
|
||||||
|
print("Warning: 'color_transition' requires at least two colors. Switching to 'on'.")
|
||||||
|
self.selected = "on" # Fallback if not enough colors
|
||||||
|
self.sync() # Re-sync for the new pattern
|
||||||
|
else:
|
||||||
self.transition_step = 0
|
self.transition_step = 0
|
||||||
self.current_color = self.color1
|
self.current_color_idx = 0 # Start from the first color in the list
|
||||||
self.transition_duration = self.delay * 10 # Initialize transition duration
|
self.current_color = self.colors[self.current_color_idx]
|
||||||
|
self.hold_start_time = utime.ticks_ms() # Reset hold timer
|
||||||
|
self.transition_duration = self.delay * 50 # Initialize transition duration
|
||||||
|
self.hold_duration = self.delay * 10 # Initialize hold duration
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -91,7 +194,7 @@ class Patterns:
|
||||||
self.n.write()
|
self.n.write()
|
||||||
|
|
||||||
def fill(self, color=None):
|
def fill(self, color=None):
|
||||||
fill_color = color if color is not None else self.color1
|
fill_color = color if color is not None else self.colors[0]
|
||||||
for i in range(self.num_leds):
|
for i in range(self.num_leds):
|
||||||
self.n[i] = fill_color
|
self.n[i] = fill_color
|
||||||
self.n.write()
|
self.n.write()
|
||||||
|
@ -100,11 +203,10 @@ class Patterns:
|
||||||
self.fill((0, 0, 0))
|
self.fill((0, 0, 0))
|
||||||
|
|
||||||
def on(self):
|
def on(self):
|
||||||
self.fill(self.apply_brightness(self.color1))
|
self.fill(self.apply_brightness(self.colors[0]))
|
||||||
|
|
||||||
|
|
||||||
def color_wipe_step(self):
|
def color_wipe_step(self):
|
||||||
color = self.apply_brightness(self.color1)
|
color = self.apply_brightness(self.colors[0])
|
||||||
current_time = utime.ticks_ms()
|
current_time = utime.ticks_ms()
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
||||||
if self.pattern_step < self.num_leds:
|
if self.pattern_step < self.num_leds:
|
||||||
|
@ -142,7 +244,7 @@ class Patterns:
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
||||||
for i in range(self.num_leds):
|
for i in range(self.num_leds):
|
||||||
if (i + self.pattern_step) % 3 == 0:
|
if (i + self.pattern_step) % 3 == 0:
|
||||||
self.n[i] = self.apply_brightness(self.color1)
|
self.n[i] = self.apply_brightness(self.colors[0])
|
||||||
else:
|
else:
|
||||||
self.n[i] = (0, 0, 0)
|
self.n[i] = (0, 0, 0)
|
||||||
self.n.write()
|
self.n.write()
|
||||||
|
@ -153,110 +255,142 @@ class Patterns:
|
||||||
current_time = utime.ticks_ms()
|
current_time = utime.ticks_ms()
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
||||||
if self.pattern_step % 2 == 0:
|
if self.pattern_step % 2 == 0:
|
||||||
self.fill(self.apply_brightness(self.color1))
|
self.fill(self.apply_brightness(self.colors[0]))
|
||||||
else:
|
else:
|
||||||
self.fill((0, 0, 0))
|
self.fill((0, 0, 0))
|
||||||
self.pattern_step = (self.pattern_step + 1) % 2
|
self.pattern_step = (self.pattern_step + 1) % 2
|
||||||
self.last_update = current_time
|
self.last_update = current_time
|
||||||
|
|
||||||
def random_color_wipe_step(self):
|
|
||||||
current_time = utime.ticks_ms()
|
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
|
||||||
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
|
||||||
if self.pattern_step < self.num_leds:
|
|
||||||
for i in range(self.num_leds):
|
|
||||||
self.n[i] = (0, 0, 0)
|
|
||||||
self.n[self.pattern_step] = self.apply_brightness(color)
|
|
||||||
self.n.write()
|
|
||||||
self.pattern_step += 1
|
|
||||||
else:
|
|
||||||
self.pattern_step = 0
|
|
||||||
self.last_update = current_time
|
|
||||||
|
|
||||||
def random_rainbow_cycle_step(self):
|
|
||||||
current_time = utime.ticks_ms()
|
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
|
||||||
def wheel(pos):
|
|
||||||
if pos < 85:
|
|
||||||
return (pos * 3, 255 - pos * 3, 0)
|
|
||||||
elif pos < 170:
|
|
||||||
pos -= 85
|
|
||||||
return (255 - pos * 3, 0, pos * 3)
|
|
||||||
else:
|
|
||||||
pos -= 170
|
|
||||||
return (0, pos * 3, 255 - pos * 3)
|
|
||||||
|
|
||||||
random_offset = random.randint(0, 255)
|
|
||||||
for i in range(self.num_leds):
|
|
||||||
rc_index = (i * 256 // self.num_leds) + self.pattern_step + random_offset
|
|
||||||
self.n[i] = self.apply_brightness(wheel(rc_index & 255))
|
|
||||||
self.n.write()
|
|
||||||
self.pattern_step = (self.pattern_step + 1) % 256
|
|
||||||
self.last_update = current_time
|
|
||||||
|
|
||||||
def random_theater_chase_step(self):
|
|
||||||
current_time = utime.ticks_ms()
|
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
|
||||||
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
|
||||||
for i in range(self.num_leds):
|
|
||||||
if (i + self.pattern_step) % 3 == 0:
|
|
||||||
self.n[i] = self.apply_brightness(color)
|
|
||||||
else:
|
|
||||||
self.n[i] = (0, 0, 0)
|
|
||||||
self.n.write()
|
|
||||||
self.pattern_step = (self.pattern_step + 1) % 3
|
|
||||||
self.last_update = current_time
|
|
||||||
|
|
||||||
def random_blink_step(self):
|
|
||||||
current_time = utime.ticks_ms()
|
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= self.delay*10:
|
|
||||||
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
|
||||||
if self.pattern_step % 2 == 0:
|
|
||||||
self.fill(self.apply_brightness(color))
|
|
||||||
else:
|
|
||||||
self.fill((0, 0, 0))
|
|
||||||
self.pattern_step = (self.pattern_step + 1) % 2
|
|
||||||
self.last_update = current_time
|
|
||||||
|
|
||||||
def interpolate_color(self, color_a, color_b, factor):
|
|
||||||
"""Interpolates between two colors."""
|
|
||||||
return tuple(int(a + (b - a) * factor) for a, b in zip(color_a, color_b))
|
|
||||||
|
|
||||||
def color_transition_step(self):
|
def color_transition_step(self):
|
||||||
current_time = utime.ticks_ms()
|
current_time = utime.ticks_ms()
|
||||||
# Use delay for how often to update the transition, not for the duration
|
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= 1: # Update frequently for smooth transition
|
# Check for hold duration first
|
||||||
self.transition_step += utime.ticks_diff(current_time, self.last_update)
|
if utime.ticks_diff(current_time, self.hold_start_time) < self.hold_duration:
|
||||||
self.last_update = current_time
|
# Still in hold phase, just display the current solid color
|
||||||
|
self.fill(self.apply_brightness(self.current_color))
|
||||||
|
self.last_update = current_time # Keep updating last_update to avoid skipping frames
|
||||||
|
return
|
||||||
|
|
||||||
|
# If hold duration is over, proceed with transition
|
||||||
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
||||||
|
num_colors = len(self.colors)
|
||||||
|
if num_colors < 2:
|
||||||
|
# Should not happen if select handles it, but as a safeguard
|
||||||
|
self.select("on")
|
||||||
|
return
|
||||||
|
|
||||||
|
from_color = self.colors[self.current_color_idx]
|
||||||
|
to_color_idx = (self.current_color_idx + 1) % num_colors
|
||||||
|
to_color = self.colors[to_color_idx]
|
||||||
|
|
||||||
|
# Calculate interpolation factor (0.0 to 1.0)
|
||||||
|
# transition_step goes from 0 to transition_duration - 1
|
||||||
|
if self.transition_duration > 0:
|
||||||
|
interp_factor = self.transition_step / self.transition_duration
|
||||||
|
else:
|
||||||
|
interp_factor = 1.0 # Immediately transition if duration is zero
|
||||||
|
|
||||||
|
# Interpolate each color component
|
||||||
|
r = int(from_color[0] + (to_color[0] - from_color[0]) * interp_factor)
|
||||||
|
g = int(from_color[1] + (to_color[1] - from_color[1]) * interp_factor)
|
||||||
|
b = int(from_color[2] + (to_color[2] - from_color[2]) * interp_factor)
|
||||||
|
|
||||||
|
self.current_color = (r, g, b)
|
||||||
|
self.fill(self.apply_brightness(self.current_color))
|
||||||
|
|
||||||
|
self.transition_step += self.delay # Advance the transition step by the delay
|
||||||
|
|
||||||
if self.transition_step >= self.transition_duration:
|
if self.transition_step >= self.transition_duration:
|
||||||
# Transition complete, swap colors and restart
|
# Transition complete, move to the next color and reset for hold phase
|
||||||
self.color1, self.color2 = self.color2, self.color1
|
self.current_color_idx = to_color_idx
|
||||||
self.transition_step = 0
|
self.current_color = self.colors[self.current_color_idx] # Ensure current_color is the exact target color
|
||||||
|
self.transition_step = 0 # Reset transition progress
|
||||||
|
self.hold_start_time = current_time # Start hold phase for the new color
|
||||||
|
|
||||||
# Calculate the interpolation factor (0 to 1)
|
self.last_update = current_time
|
||||||
factor = self.transition_step / self.transition_duration
|
|
||||||
|
|
||||||
# Get the interpolated color and apply brightness
|
def flicker_step(self):
|
||||||
interpolated_color = self.interpolate_color(self.color1, self.color2, factor)
|
current_time = utime.ticks_ms()
|
||||||
self.current_color = self.apply_brightness(interpolated_color)
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay/5:
|
||||||
|
base_color = self.colors[0]
|
||||||
|
# Increase the range for flicker_brightness_offset
|
||||||
|
# Changed from self.brightness // 4 to self.brightness // 2 (or even self.brightness for max intensity)
|
||||||
|
flicker_brightness_offset = random.randint(-int(self.brightness // 1.5), int(self.brightness // 1.5))
|
||||||
|
flicker_brightness = max(0, min(255, self.brightness + flicker_brightness_offset))
|
||||||
|
|
||||||
# Fill the LEDs with the current interpolated color
|
flicker_color = self.apply_brightness(base_color, brightness_override=flicker_brightness)
|
||||||
self.fill(self.current_color)
|
self.fill(flicker_color)
|
||||||
|
self.last_update = current_time
|
||||||
|
|
||||||
|
def scanner_step(self):
|
||||||
|
"""
|
||||||
|
Mimics a 'Knight Rider' style scanner, moving in one direction.
|
||||||
|
"""
|
||||||
|
current_time = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay:
|
||||||
|
self.fill((0, 0, 0)) # Clear all LEDs
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# Calculate the head and tail position
|
||||||
p = Patterns(4, 180)
|
head_pos = self.pattern_step
|
||||||
p.set_color1((255,0,0))
|
color = self.apply_brightness(self.colors[0])
|
||||||
p.set_color2((0,255,0))
|
|
||||||
#p.set_delay(10)
|
# Draw the head
|
||||||
try:
|
if 0 <= head_pos < self.num_leds:
|
||||||
while True:
|
self.n[head_pos] = color
|
||||||
for key in p.patterns:
|
|
||||||
print(key)
|
# Draw the trailing pixels with decreasing brightness
|
||||||
p.select(key)
|
for i in range(1, self.scanner_tail_length + 1):
|
||||||
for _ in range(2000):
|
tail_pos = head_pos - i
|
||||||
p.tick()
|
if 0 <= tail_pos < self.num_leds:
|
||||||
utime.sleep_ms(1)
|
# Calculate fading color for tail
|
||||||
except KeyboardInterrupt:
|
# Example: linear fade from full brightness to off
|
||||||
p.fill((0, 0, 0))
|
fade_factor = 1.0 - (i / (self.scanner_tail_length + 1))
|
||||||
|
faded_color = tuple(int(c * fade_factor) for c in color)
|
||||||
|
self.n[tail_pos] = faded_color
|
||||||
|
|
||||||
|
self.n.write()
|
||||||
|
|
||||||
|
self.pattern_step += 1
|
||||||
|
if self.pattern_step >= self.num_leds + self.scanner_tail_length:
|
||||||
|
self.pattern_step = 0 # Reset to start
|
||||||
|
|
||||||
|
self.last_update = current_time
|
||||||
|
|
||||||
|
def bidirectional_scanner_step(self):
|
||||||
|
"""
|
||||||
|
Mimics a 'Knight Rider' style scanner, moving back and forth.
|
||||||
|
"""
|
||||||
|
current_time = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(current_time, self.last_update) >= self.delay/100:
|
||||||
|
self.fill((0, 0, 0)) # Clear all LEDs
|
||||||
|
|
||||||
|
color = self.apply_brightness(self.colors[0])
|
||||||
|
|
||||||
|
# Calculate the head position based on direction
|
||||||
|
head_pos = self.pattern_step
|
||||||
|
|
||||||
|
# Draw the head
|
||||||
|
if 0 <= head_pos < self.num_leds:
|
||||||
|
self.n[head_pos] = color
|
||||||
|
|
||||||
|
# Draw the trailing pixels with decreasing brightness
|
||||||
|
for i in range(1, self.scanner_tail_length + 1):
|
||||||
|
tail_pos = head_pos - (i * self.scanner_direction)
|
||||||
|
if 0 <= tail_pos < self.num_leds:
|
||||||
|
fade_factor = 1.0 - (i / (self.scanner_tail_length + 1))
|
||||||
|
faded_color = tuple(int(c * fade_factor) for c in color)
|
||||||
|
self.n[tail_pos] = faded_color
|
||||||
|
|
||||||
|
self.n.write()
|
||||||
|
|
||||||
|
self.pattern_step += self.scanner_direction
|
||||||
|
|
||||||
|
# Change direction if boundaries are reached
|
||||||
|
if self.scanner_direction == 1 and self.pattern_step >= self.num_leds:
|
||||||
|
self.scanner_direction = -1
|
||||||
|
self.pattern_step = self.num_leds - 1 # Start moving back from the last LED
|
||||||
|
elif self.scanner_direction == -1 and self.pattern_step < 0:
|
||||||
|
self.scanner_direction = 1
|
||||||
|
self.pattern_step = 0 # Start moving forward from the first LED
|
||||||
|
|
||||||
|
self.last_update = current_time
|
||||||
|
|
|
@ -23,10 +23,6 @@ class Settings(dict):
|
||||||
self["color_order"] = "rgb"
|
self["color_order"] = "rgb"
|
||||||
self["name"] = f"led-{ubinascii.hexlify(wifi.get_mac()).decode()}"
|
self["name"] = f"led-{ubinascii.hexlify(wifi.get_mac()).decode()}"
|
||||||
self["ap_password"] = ""
|
self["ap_password"] = ""
|
||||||
self["wifi_ssid"] = ""
|
|
||||||
self["wifi_password"] = ""
|
|
||||||
self["wifi_ip"] = ""
|
|
||||||
self["wifi_gateway"] = ""
|
|
||||||
self["id"] = 0
|
self["id"] = 0
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
@ -49,12 +45,17 @@ class Settings(dict):
|
||||||
self.set_defaults()
|
self.set_defaults()
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def set_settings(self, data, patterns):
|
def set_settings(self, data, patterns, save):
|
||||||
try:
|
try:
|
||||||
print(data)
|
print(data)
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
print(key, value)
|
print(key, value)
|
||||||
if key == "color1":
|
if key == "colors":
|
||||||
|
buff = []
|
||||||
|
for color in value:
|
||||||
|
buff.append(tuple(int(color[i:i+2], 16) for i in self.color_order))
|
||||||
|
patterns.set_colors(buff)
|
||||||
|
elif key == "color1":
|
||||||
patterns.set_color1(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
patterns.set_color1(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
||||||
elif key == "color2":
|
elif key == "color2":
|
||||||
patterns.set_color2(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
patterns.set_color2(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
||||||
|
@ -84,7 +85,9 @@ class Settings(dict):
|
||||||
else:
|
else:
|
||||||
return "Invalid key", 400
|
return "Invalid key", 400
|
||||||
self[key] = value
|
self[key] = value
|
||||||
|
#print(self)
|
||||||
patterns.sync()
|
patterns.sync()
|
||||||
|
if save:
|
||||||
self.save()
|
self.save()
|
||||||
return "OK", 200
|
return "OK", 200
|
||||||
except (KeyError, ValueError):
|
except (KeyError, ValueError):
|
||||||
|
|
|
@ -35,7 +35,7 @@ def web(settings, patterns):
|
||||||
if data:
|
if data:
|
||||||
|
|
||||||
# Process the received data
|
# Process the received data
|
||||||
_, status_code = settings.set_settings(json.loads(data), patterns)
|
_, status_code = settings.set_settings(json.loads(data), patterns, True)
|
||||||
#await ws.send(status_code)
|
#await ws.send(status_code)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in New Issue