indication driver V2 with async indication
This commit is contained in:
parent
171c3f17cd
commit
ee300ca29f
@ -9,7 +9,7 @@ NEOPIXEL_BRIGHTNESS = 0.2
|
||||
TRASH_LOCATION = {
|
||||
"COMBUSTIBLE" : 5,
|
||||
"INCOMBUSTIBLE" : 15,
|
||||
"BOTTLES" : 25,
|
||||
"CANS" : 35,
|
||||
"BOTTLE" : 25,
|
||||
"CAN" : 35,
|
||||
"PAPER" : 45
|
||||
}
|
@ -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
|
||||
|
||||
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)
|
39
led_test.py
39
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()
|
||||
indication_driver = IndicationDriver()
|
||||
asyncio.run(__main__())
|
||||
sleep(2)
|
||||
indication_driver.indicate("CAN")
|
||||
sleep(2)
|
||||
indication_driver.clear()
|
Loading…
x
Reference in New Issue
Block a user