From 800d003761abd35a418134a28897633f07eeb695 Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 19 Mar 2024 20:21:47 +0100 Subject: [PATCH] feat: add buzzer feedback for RFID reads Introduced an optional buzzer parameter to `read_task` and `do_read` functions to provide audible feedback when an RFID/NFC card is successfully read. This enhancement is aimed at improving user interaction by providing immediate auditory confirmation of a successful card scan. The buzzer is triggered for a short duration upon successful card detection. This feature offers a more intuitive experience, especially in environments where visual confirmation might be challenging to notice. A GPIO pin is configured as output for the buzzer control, ensuring minimal impact on the code's structure and maintaining backward compatibility by keeping the buzzer parameter optional. --- main.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 6ff2c23..395ce7e 100644 --- a/main.py +++ b/main.py @@ -45,19 +45,19 @@ async def peripheral_task(): except BaseException as e: print("Exception in peripheral_task:", e) -async def read_task(rdr): +async def read_task(rdr, buzzer=None): # Start listening for a card time.sleep_ms(1000) print("Waiting for RFID/NFC card...") while True: try: - do_read(rdr) + do_read(rdr, buzzer) except Exception as e: print("Exception in read_task:", e) await asyncio.sleep_ms(100) -def do_read(rdr): +def do_read(rdr, buzzer=None): gc.collect() last_uid = None while True: @@ -74,6 +74,12 @@ def do_read(rdr): struid = "".join(['{:0>{w}}'.format(hex(i)[2:], w=2) for i in uid]) print("Found card with UID:", struid) rfid_characteristic.write(uid) + + if buzzer: + buzzer.value(1) + time.sleep_ms(300) + buzzer.value(0) + last_uid = uid except Exception as e: print("Something failed:", e) @@ -101,9 +107,12 @@ async def main(): gc.collect() + buzzer = Pin(27, Pin.OUT) + buzzer.value(0) + await asyncio.gather( peripheral_task(), - read_task(pn532) + read_task(pn532, buzzer) ) except KeyboardInterrupt: