import neopixel from time import sleep from typing import List from threading import Thread import sys sys.path.append('.') import config class IndicationDriver: def __init__(self): self.pixels = neopixel.NeoPixel( config.PIXEL_PIN, config.NUM_PIXELS, brightness= config.NEOPIXEL_BRIGHTNESS, auto_write=False, pixel_order=neopixel.GRB ) self._run_loop = True self._indication_loop = None self.stop() def stop(self): if self._indication_loop: self._run_loop = False self._indication_loop.join() self.pixels.fill((0,0,0)) self.pixels.show() def _get_trash_location(self, trash_type: str) -> List[int]: if trash_type in config.TRASH_LOCATION: center_location = config.TRASH_LOCATION[trash_type] return range(center_location-2, center_location+3) else: raise ValueError def indicate(self, trash_type: str): self.stop() location = self._get_trash_location(trash_type) def _indication_loop(self, location: List[int]): for idx in location: self.pixels[idx] = (0, 255, 0) self.pixels.show() self._run_loop = True while self._run_loop: for step in range (4): for idx in range(location[0]): if idx % 4 == step: self.pixels[idx] = (0, 0, 255) else: self.pixels[idx] = (0, 0, 0) self.pixels.show() sleep(.1) self._indication_loop = Thread(target = _indication_loop, args = (self, location, )) self._indication_loop.start() sleep(1)