Add sine brightness pattern
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
import utime
|
||||
import random
|
||||
import math
|
||||
from patterns_base import PatternBase # Import PatternBase
|
||||
import _thread
|
||||
from machine import WDT
|
||||
@@ -24,6 +25,7 @@ class Patterns(PatternBase): # Inherit from PatternBase
|
||||
"on": self.on,
|
||||
"bl": self.blink,
|
||||
"cl": self.circle_loading,
|
||||
"sb": self.sine_brightness,
|
||||
}
|
||||
self.step = 0
|
||||
self.run = True
|
||||
@@ -138,6 +140,51 @@ class Patterns(PatternBase): # Inherit from PatternBase
|
||||
self.run = False
|
||||
self.running = False
|
||||
|
||||
def sine_brightness(self):
|
||||
"""Sine wave brightness pattern - n1=min brightness, brightness=max brightness, wavelength=delay"""
|
||||
self.run = True
|
||||
|
||||
# Calculate sine wave parameters
|
||||
min_brightness = max(0, int(self.n1)) # n1 = minimum brightness
|
||||
max_brightness = self.brightness # brightness = maximum brightness
|
||||
amplitude = max_brightness - min_brightness # Range between min and max
|
||||
wavelength = max(1, self.delay) # Wavelength = delay in ms
|
||||
|
||||
# Convert wavelength to frequency (cycles per second)
|
||||
frequency = 1000.0 / wavelength # Hz
|
||||
|
||||
start_time = utime.ticks_ms()
|
||||
|
||||
while self.run:
|
||||
self.wdt.feed()
|
||||
current_time = utime.ticks_ms()
|
||||
|
||||
# Calculate time elapsed in seconds
|
||||
elapsed_ms = utime.ticks_diff(current_time, start_time)
|
||||
elapsed_seconds = elapsed_ms / 1000.0
|
||||
|
||||
# Calculate sine wave value (-1 to 1)
|
||||
sine_value = math.sin(2 * math.pi * frequency * elapsed_seconds)
|
||||
|
||||
# Convert to brightness (min_brightness to max_brightness)
|
||||
current_brightness = int(min_brightness + (sine_value + 1) * amplitude / 2)
|
||||
current_brightness = max(0, min(255, current_brightness))
|
||||
|
||||
# Apply brightness to all LEDs
|
||||
color = self.apply_brightness(self.colors[0])
|
||||
# Override brightness with calculated value
|
||||
adjusted_color = (
|
||||
int(color[0] * current_brightness / 255),
|
||||
int(color[1] * current_brightness / 255),
|
||||
int(color[2] * current_brightness / 255)
|
||||
)
|
||||
|
||||
self.fill(adjusted_color)
|
||||
self.n.write()
|
||||
|
||||
self.run = False
|
||||
self.running = False
|
||||
|
||||
# def flicker(self):
|
||||
# current_time = utime.ticks_ms()
|
||||
# base_color = self.colors[0]
|
||||
|
||||
Reference in New Issue
Block a user