WIP: First working example for an async background task
This commit is contained in:
@@ -9,7 +9,10 @@ module("rp2_neopixel.py", "../../src")
|
||||
require("sdcard")
|
||||
require("aiorepl")
|
||||
|
||||
# Third party modules
|
||||
module("mfrc522.py", "../../lib/micropython-mfrc522/")
|
||||
module("microdot.py", "../../lib/microdot/src/microdot/")
|
||||
|
||||
# TonberryPico modules
|
||||
module("audiocore.py", "../../src/audiocore")
|
||||
package("nfc", base_path="../../src/")
|
||||
|
||||
1
software/src/nfc/__init__.py
Normal file
1
software/src/nfc/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from nfc.nfc import Nfc
|
||||
12
software/src/nfc/main.py
Normal file
12
software/src/nfc/main.py
Normal file
@@ -0,0 +1,12 @@
|
||||
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())
|
||||
37
software/src/nfc/nfc.py
Normal file
37
software/src/nfc/nfc.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from mfrc522 import MFRC522
|
||||
|
||||
class Nfc:
|
||||
def __init__(self):
|
||||
self.reader = MFRC522(spi_id=1, sck=10, miso=12, mosi=11, cs=13, rst=9, tocard_retries=10)
|
||||
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):
|
||||
return '0x' + ''.join(f'{i:02x}' for i in uid)
|
||||
|
||||
|
||||
async def _reader_poll_task(self, poll_interval_ms: int = 50) -> list:
|
||||
print('reader_poll_task alive')
|
||||
|
||||
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):
|
||||
return self.last_uid, self.last_uid_timestamp
|
||||
Reference in New Issue
Block a user