-
Notifications
You must be signed in to change notification settings - Fork 0
/
seismo_master.py
126 lines (105 loc) · 4.1 KB
/
seismo_master.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
from Adafruit_BBIO import SPI, GPIO
from math import ceil, floor
import random
import time
# Get a colors library so we don't need to do so much RGB arrays?
class LedBase:
def __init__(self, length, base_color):
''' init the interface '''
self.length = length
self.base_color = base_color
# funcs
def write(self, index, color):
''' Write one color in one place '''
pass
def write_all(self, color_array, from=0, to=None):
''' Write a list of colors '''
pass
def reset(self):
''' Reset all colors to default '''
pass
class SeismoEngine (LedBase):
def __init__(self, length, iface_addr=(0,1), default_background=(0,0,0)):
self.background = default_background
self.interface = SPI.SPI(*iface_addr)
self.length = length
def write(self, data=None):
# clear bytes
self.interface.writebytes([0,0,0]*(self.length/32+1))
if data:
self.interface.writebytes(data)
else:
for value in self.buffer:
self.interface.writebytes(value)
#self.interface.writebytes([128,128,128])
def clear(self):
self.buffer = [self.background] * self.length
self.write()
def make_gradient(self, length=None, color_start=[], color_end=[]):
if not length:
length = self.length
diff = map(lambda x,y: (y - x) / (float(length) - 1), color_start, color_end)
result = []
for i in range(length):
result.append(map(lambda x,y: int(ceil(x + y * i)), color_start, diff))
return result
def gradient(self, length=None, color_start=[], color_end=[]):
if not length:
length = self.length
self.buffer = self.make_gradient(length, color_start, color_end)
self.write()
def moving_gradient(self, length=None, colors=[], timestep=0.1, shiftsteps=20):
if not length:
length = self.length
self.gradient(length, colors[0], colors[1])
time.sleep(timestep)
for i in range(2, len(colors), 2):
start_gradient = self.make_gradient(shiftsteps, colors[i-2], colors[i])
for value in start_gradient:
self.gradient(length, value, colors[i-1])
time.sleep(timestep)
end_gradient = self.make_gradient(shiftsteps, colors[i-1], colors[i+1])
for value in end_gradient:
self.gradient(length, colors[i], value)
time.sleep(timestep)
def whole_moving_gradient(self, length=None, colors=[], timestep=0.1, shiftsteps=20):
if not length:
length = self.length
for i in range(0, len(colors), 2):
grad = self.make_gradient(shiftsteps, colors[i], colors[i + 1])
for index in range(0, shiftsteps):
self.buffer = [grad[index]] * length
self.write()
time.sleep(timestep)
def whole_pattern_shift(self, pattern, timestep=0.1, shiftsteps=20):
final_gradient = []
gradient_steps = []
for i in range(0, len(self.buffer)):
gradient_steps += [self.make_gradient(length=shiftsteps, color_start=self.buffer[i], color_end=pattern[i])]
for i in range(0, shiftsteps):
self.buffer = []
for j in range(0, len(gradient_steps)):
self.buffer += [gradient_steps[j][i]]
self.write()
time.sleep(timestep)
def set_slow_clear(self, shiftsteps=20):
gradient_steps = []
self.slow_clear_gradient = []
for i in range(0, len(self.buffer)):
gradient_steps += [self.make_gradient(length=shiftsteps, color_start=self.buffer[i], color_end=self.background)]
for i in range(0, shiftsteps):
subgrad = []
for j in range(0, len(gradient_steps)):
subgrad +=[gradient_steps[j][i]]
self.slow_clear_gradient += [subgrad]
def cancel_slow_clear(self):
self.slow_clear_gradient = None
def slow_clear(self):
if self.slow_clear_gradient:
self.buffer = self.slow_clear_gradient.pop(0)#] * self.length
self.write()
if not self.slow_clear_gradient:
return True
#else:
# self.buffer = []