From ee300ca29f2d3e859489a345f3503d84137bc6a3 Mon Sep 17 00:00:00 2001 From: ploedige Date: Tue, 17 Jan 2023 10:29:17 +0000 Subject: [PATCH] indication driver V2 with async indication --- config.py | 4 ++-- indication_driver.py | 56 +++++++++++++++++++++++++++++++++----------- led_test.py | 39 ++++++++++++------------------ 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/config.py b/config.py index d13097a..499cb21 100644 --- a/config.py +++ b/config.py @@ -9,7 +9,7 @@ NEOPIXEL_BRIGHTNESS = 0.2 TRASH_LOCATION = { "COMBUSTIBLE" : 5, "INCOMBUSTIBLE" : 15, - "BOTTLES" : 25, - "CANS" : 35, + "BOTTLE" : 25, + "CAN" : 35, "PAPER" : 45 } \ No newline at end of file diff --git a/indication_driver.py b/indication_driver.py index e2ced8a..48cd027 100644 --- a/indication_driver.py +++ b/indication_driver.py @@ -1,28 +1,56 @@ import neopixel -from config import PIXEL_PIN, NUM_PIXELS, NEOPIXEL_BRIGHTNESS, TRASH_LOCATION from time import sleep +import asyncio +from typing import List + +import sys +sys.path.append('.') +import config class IndicationDriver: def __init__(self): self.pixels = neopixel.NeoPixel( - PIXEL_PIN, - NUM_PIXELS, - brightness= NEOPIXEL_BRIGHTNESS, + config.PIXEL_PIN, + config.NUM_PIXELS, + brightness= config.NEOPIXEL_BRIGHTNESS, auto_write=False, pixel_order=neopixel.GRB ) - self.stop() + self.clear() + self.indication_stop_event = asyncio.Event() - def stop(self): + + def stop_loop(self): + self.indication_stop_event.set() + + def clear(self): self.pixels.fill((0,0,0)) self.pixels.show() - def indicate(self, trash_type: str): - self.stop() - if trash_type in TRASH_LOCATION.keys(): - location = TRASH_LOCATION[trash_type] - for idx in range(location-2, location+3): - self.pixels[idx] = (0, 255, 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 \ No newline at end of file + raise ValueError + + def indicate(self, trash_type: str): + self.clear() + for idx in self._get_trash_location(trash_type): + self.pixels[idx] = (0, 255, 0) + self.pixels.show() + + async def indicate_async(self, trash_type: str): + self.clear() + self.indication_stop_event.clear() + self.indicate(trash_type) + location = self._get_trash_location(trash_type) + while not self.indication_stop_event.is_set(): + 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() + await asyncio.sleep(.1) \ No newline at end of file diff --git a/led_test.py b/led_test.py index 5c64f68..57eee28 100644 --- a/led_test.py +++ b/led_test.py @@ -1,26 +1,17 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries -# SPDX-License-Identifier: MIT +from indication_driver import IndicationDriver +from time import sleep +import asyncio -# Simple test for NeoPixels on Raspberry Pi -import time -import board -import neopixel +async def __main__(): + task = asyncio.create_task(indication_driver.indicate_async("BOTTLE")) + await asyncio.sleep(5) + indication_driver.stop_loop() + await task + indication_driver.clear() - -# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18 -# NeoPixels must be connected to D10, D12, D18 or D21 to work. -pixel_pin = board.D18 - -# The number of NeoPixels -num_pixels = 5 - -# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! -# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. -ORDER = neopixel.GRB - -pixels = neopixel.NeoPixel( - pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER -) - -pixels.fill((0,255,0)) -pixels.show() \ No newline at end of file +indication_driver = IndicationDriver() +asyncio.run(__main__()) +sleep(2) +indication_driver.indicate("CAN") +sleep(2) +indication_driver.clear() \ No newline at end of file