-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainbow_dot.py
75 lines (64 loc) · 1.97 KB
/
rainbow_dot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# See: https://raw.githubusercontent.com/pimoroni/unicorn-hat/master/examples/rainbow_blinky.py
# or: https://github.com/pimoroni/unicorn-hat/blob/master/examples/rainbow_blinky.py
# Which was used as base and changed to fit my needs
import colorsys
import time
import random
import math
import numpy
import unicornhathd as unicorn
width,height=unicorn.get_shape()
if height==width:
delta=0
else:
delta=2
def make_gaussian(fwhm,x0,y0):
x = numpy.arange(0, 16, 1, float)
y = x[:, numpy.newaxis]
fwhm = fwhm
gauss = numpy.exp(-4 * numpy.log(2) * ((x - x0) ** 2 + (y - y0) ** 2) / fwhm ** 2)
return gauss
def constrain(var, lower, upper):
if var < lower:
return lower
if var > upper:
return upper
return var
def main(run, running):
print("called")
running(True)
x0 = 7.5
y0 = 7.5
while run():
unicorn.off()
time.sleep(0.03)
angle = random.uniform(0, math.pi * 2)
x0 += math.sin(angle)
y0 += math.cos(angle)
x0 = constrain(x0, 3.5, 12.5)
y0 = constrain(y0, 3.5, 12.5)
for z in list(range(1, 10)[::-1]) + list(range(1, 10)):
fwhm = 5.0/z
gauss = make_gaussian(fwhm, x0,y0)
start = time.time()
for y in range(height):
for x in range(width):
h = 1.0/(x + y + delta + 1)
s = 0.8
if height<=width:
v = gauss[x,y+delta]
else:
v = gauss[x+delta,y]
rgb = colorsys.hsv_to_rgb(h, s, v)
r = int(rgb[0]*255.0)
g = int(rgb[1]*255.0)
b = int(rgb[2]*255.0)
unicorn.set_pixel(x, y, r, g, b)
unicorn.show()
end = time.time()
t = end - start
if t < 0.11:
time.sleep(0.11 - t)
unicorn.off()
running(False)