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 = { | TRASH_LOCATION = { | ||||||
|     "COMBUSTIBLE" : 5, |     "COMBUSTIBLE" : 5, | ||||||
|     "INCOMBUSTIBLE" : 15, |     "INCOMBUSTIBLE" : 15, | ||||||
|     "BOTTLES" : 25, |     "BOTTLE" : 25, | ||||||
|     "CANS" : 35, |     "CAN" : 35, | ||||||
|     "PAPER" : 45 |     "PAPER" : 45 | ||||||
| } | } | ||||||
| @ -1,28 +1,56 @@ | |||||||
| import neopixel | import neopixel | ||||||
| from config import PIXEL_PIN, NUM_PIXELS, NEOPIXEL_BRIGHTNESS, TRASH_LOCATION |  | ||||||
| from time import sleep | from time import sleep | ||||||
|  | import asyncio | ||||||
|  | from typing import List | ||||||
|  | 
 | ||||||
|  | import sys | ||||||
|  | sys.path.append('.') | ||||||
|  | import config | ||||||
| 
 | 
 | ||||||
| class IndicationDriver: | class IndicationDriver: | ||||||
|     def __init__(self): |     def __init__(self): | ||||||
|         self.pixels = neopixel.NeoPixel( |         self.pixels = neopixel.NeoPixel( | ||||||
|             PIXEL_PIN,  |             config.PIXEL_PIN,  | ||||||
|             NUM_PIXELS,  |             config.NUM_PIXELS,  | ||||||
|             brightness= NEOPIXEL_BRIGHTNESS,  |             brightness= config.NEOPIXEL_BRIGHTNESS,  | ||||||
|             auto_write=False,  |             auto_write=False,  | ||||||
|             pixel_order=neopixel.GRB |             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.fill((0,0,0)) | ||||||
|         self.pixels.show() |         self.pixels.show() | ||||||
| 
 | 
 | ||||||
|     def indicate(self, trash_type: str): |     def _get_trash_location(self, trash_type: str) -> List[int]: | ||||||
|         self.stop() |         if trash_type in config.TRASH_LOCATION: | ||||||
|         if trash_type in TRASH_LOCATION.keys(): |             center_location = config.TRASH_LOCATION[trash_type] | ||||||
|             location = TRASH_LOCATION[trash_type] |             return range(center_location-2, center_location+3) | ||||||
|             for idx in range(location-2, location+3): |  | ||||||
|                 self.pixels[idx] = (0, 255, 0) |  | ||||||
|             self.pixels.show() |  | ||||||
|         else: |         else: | ||||||
|             raise ValueError |             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 | from indication_driver import IndicationDriver | ||||||
| # SPDX-License-Identifier: MIT | from time import sleep | ||||||
|  | import asyncio | ||||||
| 
 | 
 | ||||||
| # Simple test for NeoPixels on Raspberry Pi | async def __main__(): | ||||||
| import time |     task = asyncio.create_task(indication_driver.indicate_async("BOTTLE")) | ||||||
| import board |     await asyncio.sleep(5) | ||||||
| import neopixel |     indication_driver.stop_loop() | ||||||
|  |     await task | ||||||
|  |     indication_driver.clear() | ||||||
| 
 | 
 | ||||||
| 
 | indication_driver = IndicationDriver() | ||||||
| # Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18 | asyncio.run(__main__()) | ||||||
| # NeoPixels must be connected to D10, D12, D18 or D21 to work. | sleep(2) | ||||||
| pixel_pin = board.D18 | indication_driver.indicate("CAN") | ||||||
| 
 | sleep(2) | ||||||
| # The number of NeoPixels | indication_driver.clear() | ||||||
| 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() |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user