From b4a35c452f5963fb77f1b1c55b76c33be741f790 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 28 Jan 2026 16:33:31 +0000 Subject: [PATCH] =?UTF-8?q?ledpixels.py=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ledpixels.py | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 ledpixels.py diff --git a/ledpixels.py b/ledpixels.py new file mode 100644 index 0000000..2e41661 --- /dev/null +++ b/ledpixels.py @@ -0,0 +1,150 @@ +""" +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() + + +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) \ No newline at end of file