-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel.py
129 lines (99 loc) · 2.93 KB
/
pixel.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
#!/usr/bin/env python2
# coding: utf-8
"""
LED light pattern like Google Home
"""
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[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.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):
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 _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)
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()