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.
This commit is contained in:
Kumi 2024-03-19 20:21:47 +01:00
parent 588f1a28be
commit 800d003761
Signed by: kumi
GPG key ID: ECBCC9082395383F

17
main.py
View file

@ -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: