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
|
from mfrc522 import MFRC522
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import time
|
||||||
|
|
||||||
def uid_to_string(uid: list):
|
delay_sum = 0
|
||||||
return '0x' + ''.join(f'{i:02x}' for i in uid)
|
delay_count = 0
|
||||||
|
max_delay = 0
|
||||||
|
|
||||||
async def get_tag_uid(reader: MFRC522, poll_interval_ms: int = 50) -> list:
|
|
||||||
while True:
|
async def latency_test():
|
||||||
reader.init()
|
global delay_sum
|
||||||
|
global delay_count
|
||||||
# For now we omit the tag type
|
global max_delay
|
||||||
(stat, _) = reader.request(reader.REQIDL)
|
global min_delay
|
||||||
if stat == reader.OK:
|
min_delay = 0xffffffff
|
||||||
(stat, uid) = reader.SelectTagSN()
|
await asyncio.sleep_ms(1)
|
||||||
if stat == reader.OK:
|
while True:
|
||||||
return uid
|
for _ in range(2000):
|
||||||
|
before = time.ticks_us()
|
||||||
await asyncio.sleep_ms(poll_interval_ms)
|
await asyncio.sleep(0)
|
||||||
|
after = time.ticks_us()
|
||||||
|
delay = after - before
|
||||||
def main():
|
delay_sum += delay
|
||||||
reader = MFRC522(spi_id=1, sck=10, miso=12, mosi=11, cs=13, rst=9)
|
delay_count += 1
|
||||||
|
if delay > max_delay:
|
||||||
print("")
|
max_delay = delay
|
||||||
print("Please place card on reader")
|
if delay < min_delay:
|
||||||
print("")
|
min_delay = delay
|
||||||
|
await asyncio.sleep_ms(1)
|
||||||
uid = asyncio.run(get_tag_uid(reader))
|
print(f"delay (min / max / avg) [µs]: ({min_delay} / {max_delay} / {delay/delay_sum})")
|
||||||
print(f"Found tag with uid {uid_to_string(uid)}")
|
|
||||||
|
|
||||||
|
def uid_to_string(uid: list):
|
||||||
if __name__ == "__main__":
|
return '0x' + ''.join(f'{i:02x}' for i in uid)
|
||||||
main()
|
|
||||||
|
|
||||||
|
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