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 from mfrc522 import MFRC522
import utime import asyncio
def uid_to_string(uid): def uid_to_string(uid: list):
mystring = "0x" uid_string = "0x"
# The mfrc522 lib returns all tested uids in inverse order for i in uid:
for i in reversed(uid): uid_string += f'{i:02x}'
mystring += "%02X" % i return uid_string
return mystring
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(): def main():
reader = MFRC522(spi_id=1,sck=10,miso=12,mosi=11,cs=13,rst=9) 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("Please place card on reader")
print("") print("")
previous_uid = [0] uid = asyncio.run(get_tag_uid(reader))
print(f"Found tag with uid {uid_to_string(uid)}")
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
if __name__ == "__main__": if __name__ == "__main__":
main() main()