Merge pull request 'nfc-module' (#10) from nfc-module into main
All checks were successful
Check code formatting / Check-C-Format (push) Successful in 6s
Check code formatting / Check-Python-Flake8 (push) Successful in 9s
Check code formatting / Check-Bash-Shellcheck (push) Successful in 4s
Run unit tests on host / Run-Unit-Tests (push) Successful in 7s
Build RPi Pico firmware image / Build-Firmware (push) Successful in 2m58s
All checks were successful
Check code formatting / Check-C-Format (push) Successful in 6s
Check code formatting / Check-Python-Flake8 (push) Successful in 9s
Check code formatting / Check-Bash-Shellcheck (push) Successful in 4s
Run unit tests on host / Run-Unit-Tests (push) Successful in 7s
Build RPi Pico firmware image / Build-Firmware (push) Successful in 2m58s
Reviewed-on: #10 Reviewed-by: Matthias Blankertz <matthias@blankertz.org>
This commit was merged in pull request #10.
This commit is contained in:
@@ -9,7 +9,10 @@ module("rp2_neopixel.py", "../../src")
|
|||||||
require("sdcard")
|
require("sdcard")
|
||||||
require("aiorepl")
|
require("aiorepl")
|
||||||
|
|
||||||
|
# Third party modules
|
||||||
module("mfrc522.py", "../../lib/micropython-mfrc522/")
|
module("mfrc522.py", "../../lib/micropython-mfrc522/")
|
||||||
module("microdot.py", "../../lib/microdot/src/microdot/")
|
module("microdot.py", "../../lib/microdot/src/microdot/")
|
||||||
|
|
||||||
|
# TonberryPico modules
|
||||||
module("audiocore.py", "../../src/audiocore")
|
module("audiocore.py", "../../src/audiocore")
|
||||||
|
package("nfc", base_path="../../src/")
|
||||||
|
|||||||
7
software/src/nfc/__init__.py
Normal file
7
software/src/nfc/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
'''
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright (c) 2025 Stefan Kratochwil (Kratochwil-LA@gmx.de)
|
||||||
|
'''
|
||||||
|
from nfc.nfc import Nfc
|
||||||
|
|
||||||
|
__all__ = ['Nfc']
|
||||||
76
software/src/nfc/nfc.py
Normal file
76
software/src/nfc/nfc.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
'''
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright (c) 2025 Stefan Kratochwil (Kratochwil-LA@gmx.de)
|
||||||
|
'''
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
|
||||||
|
from mfrc522 import MFRC522
|
||||||
|
|
||||||
|
|
||||||
|
class Nfc:
|
||||||
|
'''
|
||||||
|
This class implements an asyncio task which continuously polls the mfrc522 nfc reader. If a new
|
||||||
|
nfc tag was detected, the uid of the tag is stored alongside with the current system time. This
|
||||||
|
information can be retrieved again.
|
||||||
|
|
||||||
|
Usage example:
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from nfc import Nfc
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
n = Nfc()
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep_ms(500)
|
||||||
|
print(f'{n.get_last_uid()}')
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
'''
|
||||||
|
def __init__(self, reader: MFRC522):
|
||||||
|
self.reader = reader
|
||||||
|
self.last_uid = None
|
||||||
|
self.last_uid_timestamp = None
|
||||||
|
self.task = asyncio.create_task(self._reader_poll_task())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def uid_to_string(uid: list):
|
||||||
|
'''
|
||||||
|
Helper function to convert a nfc tag uid to a readable string.
|
||||||
|
'''
|
||||||
|
return '0x' + ''.join(f'{i:02x}' for i in uid)
|
||||||
|
|
||||||
|
async def _reader_poll_task(self, poll_interval_ms: int = 50):
|
||||||
|
'''
|
||||||
|
Periodically polls the nfc reader. Stores tag uid and timestamp if a new tag was found.
|
||||||
|
'''
|
||||||
|
while True:
|
||||||
|
self.reader.init()
|
||||||
|
|
||||||
|
# For now we omit the tag type
|
||||||
|
(stat, _) = self.reader.request(self.reader.REQIDL)
|
||||||
|
if stat == self.reader.OK:
|
||||||
|
(stat, uid) = self.reader.SelectTagSN()
|
||||||
|
if stat == self.reader.OK:
|
||||||
|
self.last_uid = uid
|
||||||
|
self.last_uid_timestamp = time.ticks_us()
|
||||||
|
|
||||||
|
await asyncio.sleep_ms(poll_interval_ms)
|
||||||
|
|
||||||
|
def get_last_uid(self):
|
||||||
|
'''
|
||||||
|
Returns the last read nfc tag uid alongside with the timestamp it was stored at.
|
||||||
|
'''
|
||||||
|
return self.last_uid, self.last_uid_timestamp
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
async def main():
|
||||||
|
reader = MFRC522(spi_id=1, sck=10, miso=12, mosi=11, cs=13, rst=9, tocard_retries=20)
|
||||||
|
n = Nfc(reader=reader)
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep_ms(500)
|
||||||
|
print(f'{n.get_last_uid()}')
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
Reference in New Issue
Block a user