Permalink
Browse files

Implemented a 2d soothing rainbow

  • Loading branch information...
1 parent 3bb22c7 commit 33b66f588e3946dc8d056f9e881ad389bf9d60af Ledbar Ledbar committed Dec 10, 2012
Showing with 60 additions and 0 deletions.
  1. +60 −0 host_python/rainbow2d.py
View
@@ -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)

0 comments on commit 33b66f5

Please sign in to comment.