Made tag uid retrieval awaitable.

This commit is contained in:
2024-11-26 21:37:27 +01:00
parent 553bbe95b2
commit 7188722cc8

View File

@@ -1,13 +1,27 @@
from mfrc522 import MFRC522
import utime
import asyncio
def uid_to_string(uid):
mystring = "0x"
# The mfrc522 lib returns all tested uids in inverse order
for i in reversed(uid):
mystring += "%02X" % i
return mystring
def uid_to_string(uid: list):
uid_string = "0x"
for i in uid:
uid_string += f'{i:02x}'
return uid_string
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)
@@ -16,30 +30,8 @@ def main():
print("Please place card on reader")
print("")
previous_uid = [0]
try:
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 uid == previous_uid:
continue
if stat == reader.OK:
print(uid_to_string(uid))
previous_uid = uid
else:
pass
else:
previous_uid=[0]
utime.sleep_ms(50)
except KeyboardInterrupt:
pass
uid = asyncio.run(get_tag_uid(reader))
print(f"Found tag with uid {uid_to_string(uid)}")
if __name__ == "__main__":
main()