robot.drivers.pixels 源代码

from . 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] = 1 self.basis[4] = 1 self.basis[8] = 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 self.next.clear() while not self.next.is_set(): for i in range(5, 25): colors = [(v * i / 24) for v in colors] self.write(colors) time.sleep(0.01) time.sleep(0.3) for i in range(24, 4, -1): colors = [(v * i / 24) for v in colors] self.write(colors) time.sleep(0.01) time.sleep(0.3) 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()
pixels = Pixels() if __name__ == '__main__': while True: try: pixels.wakeup() time.sleep(3) pixels.think() time.sleep(3) pixels.speak() time.sleep(3) pixels.off() time.sleep(3) except KeyboardInterrupt: break pixels.off() time.sleep(1)