Usable mfrc522 code.
Not as async as possible, and definately needs a future revisit. But works for now, with acceptable delays, if the newly introduced tocard_retries parameter of class MFRC522 is used and set to a reasonable low value.
This commit is contained in:
Submodule software/lib/micropython-mfrc522 updated: c206c319ed...3c038feb18
@@ -1,35 +1,65 @@
|
||||
from mfrc522 import MFRC522
|
||||
import asyncio
|
||||
|
||||
|
||||
def uid_to_string(uid: list):
|
||||
return '0x' + ''.join(f'{i:02x}' for i in uid)
|
||||
|
||||
|
||||
async def get_tag_uid(reader: MFRC522, poll_interval_ms: int = 50) -> list:
|
||||
while True:
|
||||
reader.init()
|
||||
|
||||
# For now we omit the tag type
|
||||
(stat, _) = reader.request(reader.REQIDL)
|
||||
if stat == reader.OK:
|
||||
(stat, uid) = reader.SelectTagSN()
|
||||
if stat == reader.OK:
|
||||
return uid
|
||||
|
||||
await asyncio.sleep_ms(poll_interval_ms)
|
||||
|
||||
|
||||
def main():
|
||||
reader = MFRC522(spi_id=1, sck=10, miso=12, mosi=11, cs=13, rst=9)
|
||||
|
||||
print("")
|
||||
print("Please place card on reader")
|
||||
print("")
|
||||
|
||||
uid = asyncio.run(get_tag_uid(reader))
|
||||
print(f"Found tag with uid {uid_to_string(uid)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
from mfrc522 import MFRC522
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
delay_sum = 0
|
||||
delay_count = 0
|
||||
max_delay = 0
|
||||
|
||||
|
||||
async def latency_test():
|
||||
global delay_sum
|
||||
global delay_count
|
||||
global max_delay
|
||||
global min_delay
|
||||
min_delay = 0xffffffff
|
||||
await asyncio.sleep_ms(1)
|
||||
while True:
|
||||
for _ in range(2000):
|
||||
before = time.ticks_us()
|
||||
await asyncio.sleep(0)
|
||||
after = time.ticks_us()
|
||||
delay = after - before
|
||||
delay_sum += delay
|
||||
delay_count += 1
|
||||
if delay > max_delay:
|
||||
max_delay = delay
|
||||
if delay < min_delay:
|
||||
min_delay = delay
|
||||
await asyncio.sleep_ms(1)
|
||||
print(f"delay (min / max / avg) [µs]: ({min_delay} / {max_delay} / {delay/delay_sum})")
|
||||
|
||||
|
||||
def uid_to_string(uid: list):
|
||||
return '0x' + ''.join(f'{i:02x}' for i in uid)
|
||||
|
||||
|
||||
async def get_tag_uid(reader: MFRC522, poll_interval_ms: int = 50) -> list:
|
||||
while True:
|
||||
reader.init()
|
||||
|
||||
# For now we omit the tag type
|
||||
(stat, _) = reader.request(reader.REQIDL)
|
||||
if stat == reader.OK:
|
||||
(stat, uid) = reader.SelectTagSN()
|
||||
if stat == reader.OK:
|
||||
print(f"uid={uid_to_string(uid)}")
|
||||
|
||||
await asyncio.sleep_ms(poll_interval_ms)
|
||||
|
||||
|
||||
def main():
|
||||
reader = MFRC522(spi_id=1, sck=10, miso=12, mosi=11, cs=13, rst=9, tocard_retries=5)
|
||||
|
||||
print("")
|
||||
print("Please place card on reader")
|
||||
print("")
|
||||
|
||||
asyncio.create_task(get_tag_uid(reader))
|
||||
asyncio.create_task(latency_test())
|
||||
|
||||
asyncio.get_event_loop().run_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user