diff --git a/indication_driver.py b/indication_driver.py index 48cd027..aa718d1 100644 --- a/indication_driver.py +++ b/indication_driver.py @@ -53,4 +53,5 @@ class IndicationDriver: else: self.pixels[idx] = (0, 0, 0) self.pixels.show() - await asyncio.sleep(.1) \ No newline at end of file + await asyncio.sleep(.1) + self.clear() \ No newline at end of file diff --git a/rfid_driver.py b/rfid_driver.py new file mode 100644 index 0000000..c0429c4 --- /dev/null +++ b/rfid_driver.py @@ -0,0 +1,17 @@ +import RPi._GPIO as GPIO +from mfrc522 import SimpleMFRC522 +import asyncio + +class RFIDDriver: + def __init__(self): + self.reader = SimpleMFRC522() + + def __del__(self): + GPIO.cleanup() + + async def get_next_id(self) -> int: + id = self.reader.read_id_no_block() + while not id: + id = self.reader.read_id_no_block() + await asyncio.sleep(0.01) + return id \ No newline at end of file diff --git a/test_scripts/rfid_test.py b/test_scripts/rfid_test.py index 5624caf..e835e1c 100644 --- a/test_scripts/rfid_test.py +++ b/test_scripts/rfid_test.py @@ -1,23 +1,41 @@ from time import sleep import RPi.GPIO as GPIO from mfrc522 import SimpleMFRC522 +import asyncio import sys sys.path.append('..') import TREx.config as config from TREx.indication_driver import IndicationDriver +from TREx.rfid_driver import RFIDDriver -reader = SimpleMFRC522() -indication_driver = IndicationDriver() +async def __main__(): + rfid_driver = RFIDDriver() + indication_driver = IndicationDriver() + indication_task = None + while True: + id = await rfid_driver.get_next_id() + trash_category = None + try: + if indication_task is not None: + indication_driver.stop_loop() + await indication_task + trash_category = config.RFID_LOOKUP_TABLE[id] + indication_task = asyncio.create_task(indication_driver.indicate_async(trash_category)) + await asyncio.sleep(1) + except KeyError: + indication_driver.stop_loop() -while True: - id, _ = reader.read() - if id in config.RFID_LOOKUP_TABLE.keys(): - trash_category = config.RFID_LOOKUP_TABLE[id] - print(f"{id}: {trash_category}") - indication_driver.indicate(trash_category) - else: - print(id) - sleep(1) -GPIO.cleanup() \ No newline at end of file +asyncio.run(__main__()) + +# while True: +# id, _ = reader.read() +# if id in config.RFID_LOOKUP_TABLE.keys(): +# trash_category = config.RFID_LOOKUP_TABLE[id] +# print(f"{id}: {trash_category}") +# indication_driver.indicate(trash_category) +# else: +# print(id) +# sleep(1) +# GPIO.cleanup() \ No newline at end of file