forked from j1nx/respeaker-4mic-hat-skill
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pixels.py
176 lines (142 loc) · 4.4 KB
/
pixels.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# All credits go to domcross (Github https://github.com/domcross)
"""
LED light pattern like Google Home
"""
#import apa102
from .apa102 import APA102
import time
import threading
try:
import queue as Queue
except ImportError:
import Queue as Queue
class Pixels:
PIXELS_N = 3
def __init__(self):
self.basis = [0] * 3 * self.PIXELS_N
#self.basis[2] = 3
#self.basis[5] = 6
#self.basis[8] = 3
self.basis[0] = 2
self.basis[3] = 1
self.basis[4] = 1
self.basis[7] = 2
self.colors = [0] * 3 * self.PIXELS_N
#self.dev = apa102.APA102(num_led=self.PIXELS_N)
self.dev = APA102(num_led=self.PIXELS_N)
self.next = threading.Event()
self.queue = Queue.Queue()
self.thread = threading.Thread(target=self._run)
self.thread.daemon = True
self.thread.start()
def wakeup(self, direction=0):
def f():
self._wakeup(direction)
self.next.set()
self.queue.put(f)
def listen(self):
self.next.set()
self.queue.put(self._listen)
def think(self):
self.next.set()
self.queue.put(self._think)
def speak(self):
self.next.set()
self.queue.put(self._speak)
def off(self):
self.next.set()
self.queue.put(self._off)
def _run(self):
while True:
func = self.queue.get()
func()
def _wakeup(self, direction=0):
#offset = int(((direction + 180 + 30) % 180) / 60)
#basis = self.basis[-offset*3:] + self.basis[:-offset*3]
for i in range(1, 25):
colors = [i * v for v in self.basis]
self.write(colors)
#time.sleep(0.01)
time.sleep(0.3)
self.colors = colors
def _listen(self):
for i in range(1, 25):
colors = [i * v for v in self.basis]
self.write(colors)
time.sleep(0.01)
self.colors = colors
def _think(self):
colors = self.colors
self.next.clear()
while not self.next.is_set():
colors = colors[3:] + colors[:3]
self.write(colors)
time.sleep(0.2)
t = 0.1
for i in range(0, 5):
colors = colors[3:] + colors[:3]
self.write([(v * (4 - i) / 4) for v in colors])
time.sleep(t)
t /= 2
# time.sleep(0.5)
self.colors = colors
def _speak(self):
colors = self.colors
gradient = -1
position = 24
self.next.clear()
while not self.next.is_set():
position += gradient
self.write([(v * position / 24) for v in colors])
if position == 24 or position == 4:
gradient = -gradient
time.sleep(0.2)
else:
time.sleep(0.01)
while position > 0:
position -= 1
self.write([(v * position / 24) for v in colors])
time.sleep(0.01)
# self._off()
def _off(self):
self.write([0] * 3 * self.PIXELS_N)
self.dev.clear_strip()
def write(self, colors):
for i in range(self.PIXELS_N):
self.dev.set_pixel(i, int(colors[3*i]), int(colors[3*i + 1]), int(colors[3*i + 2]))
self.dev.show()
pixels = Pixels()
if __name__ == '__main__':
while True:
try:
print('pixels wakeup')
pixels.wakeup()
time.sleep(3)
print('pixels listen')
pixels.listen()
time.sleep(3)
print('pixels think')
pixels.think()
time.sleep(3)
print('pixels speak')
pixels.speak()
time.sleep(3)
print('pixels off')
pixels.off()
time.sleep(3)
except KeyboardInterrupt:
break
pixels.off()
time.sleep(1)