Commit 141cf8ec authored by Ralf's avatar Ralf

animate in HSV space

parent ae479b63
......@@ -21,35 +21,68 @@ ugfx.string(140, 75, "Anyway", "Roboto_BlackItalic24", ugfx.WHITE)
ugfx.flush()
# LED fanciness
FRAMERATE = 1/30.0
R_PERIOD_SECONDS = 3.0
G_PERIOD_SECONDS = 3.1
B_PERIOD_SECONDS = 10.0
LED_STEP_SECONDS = 0.2
# HSV-to-LED color conversion
MAX_BRIGHTNESS = 40.0
MAX_BRIGHTNESS_INT = min(255, int(math.ceil(MAX_BRIGHTNESS)))
def f2i(f, minval=0.0, maxval=1.0):
'''Turn `f` into an integer color value, scaling from `[minval, maxval]`'''
# scale and round
i = int((f - minval)/(maxval - minval)*MAX_BRIGHTNESS + 0.5)
def f2i(f):
'''Turn `f` into an integer color value'''
# clamp
return max(0, min(i, MAX_BRIGHTNESS_INT))
f = max(0.0, min(f, 1.0))
# scale and round
return int(f*MAX_BRIGHTNESS + 0.5)
def hsv_rgb1(c, h1, x):
if 0 <= h1 <= 1:
return (c, x, 0)
if 1 <= h1 <= 2:
return (x, c, 0)
if 2 <= h1 <= 3:
return (0, c, x)
if 3 <= h1 <= 4:
return (0, x, c)
if 4 <= h1 <= 5:
return (x, 0, c)
if 5 <= h1 <= 6:
return (c, 0, x)
return (0, 0, 0)
def hsv_to_led(h, s, v):
c = v * s
h1 = h/60.0
x = c * (1.0 - math.fabs(h1 % 2.0 - 1.0))
(r, g, b) = hsv_rgb1(c, h1, x)
m = v - c
r += m
g += m
b += m
# now we got the RGB values in [0, 1] floating-point range. convert to LED range.
return [f2i(g), f2i(r), f2i(b), 0]
# LED fanciness
FRAMERATE = 1/60.0
HUE_BASE_PERIOD = 8.0/360.0 # seconds per degree
HUE_HIGH_PERIOD = 1.3 # seconds
HUE_HIGH_AMPLITUDE = 30.0 # degrees
VALUE_PERIOD = 5.2 # seconds
LED_STEP = 0.2 # seconds
def rescale(f, min_in, max_in, min_out, max_out):
# normalize into [0, 1]
f = (f - min_in)/(max_in - min_in)
# scale to out
return f*(max_out - min_out) + min_out
def get_color(seconds):
r = math.sin(seconds/R_PERIOD_SECONDS*2*math.pi)
g = math.sin(seconds/G_PERIOD_SECONDS*2*math.pi)
b = math.sin(seconds/B_PERIOD_SECONDS*2*math.pi)
return [f2i(g, -1.0, 1.0), f2i(r, -1.0, 1.0), f2i(b, -1.0, 1.0), 0]
h = seconds/HUE_BASE_PERIOD + math.cos(seconds/HUE_HIGH_PERIOD*2*math.pi)*HUE_HIGH_AMPLITUDE
v = math.sin(seconds/VALUE_PERIOD)
return hsv_to_led(h % 360.0, 1.0, rescale(v, -1.0, 1.0, 0.5, 1.0))
frame = 0
while True:
seconds = frame*FRAMERATE
leds = []
for led in range(0, 6):
leds += get_color(seconds + led*LED_STEP_SECONDS)
leds += get_color(seconds + led*LED_STEP)
badge.leds_send_data(bytes(leds), 24)
frame += 1
time.sleep(FRAMERATE)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment