mirror of
https://github.com/brmlab/ledbar.git
synced 2025-06-09 21:34:00 +02:00
Implemented a 2d soothing rainbow
This commit is contained in:
parent
3bb22c73db
commit
33b66f588e
1 changed files with 60 additions and 0 deletions
60
host_python/rainbow2d.py
Normal file
60
host_python/rainbow2d.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
from ledbar import Ledbar
|
||||
import math, time
|
||||
|
||||
WIDTH = 4
|
||||
HEIGHT = 5
|
||||
PIXELS = 20
|
||||
|
||||
def lint(x, points, values):
|
||||
assert(len(points) == len(values))
|
||||
|
||||
for i in range(len(points)-1):
|
||||
if x > points[i+1]:
|
||||
continue
|
||||
width = points[i+1] - points[i]
|
||||
assert(width > 0)
|
||||
|
||||
t = (x - points[i]) / width
|
||||
t_1 = 1.0 - t
|
||||
return values[i]*t_1 + values[i+1]*t
|
||||
|
||||
return values[len(values)-1]
|
||||
|
||||
# Correction values
|
||||
corr_r_p = [0.0, 0.5, 0.7, 0.9, 1.0]
|
||||
corr_r_v = [0.0, 0.05, 0.15, 0.45, 1.0]
|
||||
corr_g_p = [0.0, 0.55, 0.9, 1.0]
|
||||
corr_g_v = [0.0, 0.08, 0.2, 1.0]
|
||||
corr_b_p = [0.0, 0.6, 0.75, 1.0]
|
||||
corr_b_v = [0.0, 0.13, 0.18, 1.0]
|
||||
|
||||
def set_pixel_2d(bar, x, y, r, g, b):
|
||||
bar.set_pixel(WIDTH - x - 1 + y*WIDTH, r, g, b)
|
||||
|
||||
def cdist(center, point, scale):
|
||||
dist = math.sqrt(reduce(
|
||||
lambda a,b: a+b,
|
||||
map(lambda (x,y): (x-y)**2, zip(center,point))
|
||||
))
|
||||
dist = dist*scale
|
||||
return dist
|
||||
|
||||
l = Ledbar(PIXELS)
|
||||
|
||||
M = max(WIDTH, HEIGHT)
|
||||
center_b = [0,0]
|
||||
work = True
|
||||
t = 0
|
||||
while work:
|
||||
center_r = [math.sin(t)*(M/2)+WIDTH/2-1, math.cos(t)*(M/2)+HEIGHT/2-1]
|
||||
center_g = [math.sin(t*0.53234)*(M/2)+WIDTH/2-1, HEIGHT/2-1]
|
||||
for x in range(WIDTH):
|
||||
for y in range(HEIGHT):
|
||||
r = cdist(center_r, [x,y], 0.2)
|
||||
r = lint(r, corr_r_p, corr_r_v)
|
||||
g = cdist(center_g, [x,y], 0.2)
|
||||
g = lint(g, corr_g_p, corr_g_v)
|
||||
set_pixel_2d(l, x, y, r, g, 0)
|
||||
work = l.update()
|
||||
t += 0.03
|
||||
time.sleep(0.025)
|
Loading…
Add table
Add a link
Reference in a new issue