From d90ac6b12fde025fd056e9282c967c72d81d08fb Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 19 Mar 2024 20:42:32 +0100 Subject: [PATCH] refactor: optimize sleep durations for RFID reading Reduced initial sleep time before card detection and adjusted sleep intervals within read loops to enhance responsiveness and resource efficiency in RFID/NFC reading routines. This adjustment aims to minimize unnecessary delays, improving the user experience by making card detection more prompt without compromising system stability. Moved consistent sleeping to a `finally` block in `do_read` to ensure regular timing intervals, regardless of read success or exceptions, contributing to a more predictable and reliable operation. --- main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 395ce7e..f3409c9 100644 --- a/main.py +++ b/main.py @@ -47,7 +47,6 @@ async def peripheral_task(): async def read_task(rdr, buzzer=None): # Start listening for a card - time.sleep_ms(1000) print("Waiting for RFID/NFC card...") while True: @@ -61,7 +60,6 @@ def do_read(rdr, buzzer=None): gc.collect() last_uid = None while True: - time.sleep_ms(100) try: uid = rdr.read_passive_target(timeout=0.2) if last_uid == uid: @@ -81,10 +79,14 @@ def do_read(rdr, buzzer=None): buzzer.value(0) last_uid = uid + except Exception as e: print("Something failed:", e) pass + finally: + time.sleep_ms(100) + async def main(): try: i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0)) @@ -103,7 +105,7 @@ async def main(): print("Found PN532 with firmware version: {0}.{1}".format(ver, rev)) pn532.SAM_configuration() - time.sleep_ms(1000) + time.sleep_ms(100) gc.collect()