2023-04-07 12:32:16 +00:00
|
|
|
# aioespnow module for MicroPython on ESP32 and ESP8266
|
|
|
|
# MIT license; Copyright (c) 2022 Glenn Moloney @glenn20
|
|
|
|
|
2024-06-12 03:44:20 +00:00
|
|
|
import asyncio
|
2023-04-07 12:32:16 +00:00
|
|
|
import espnow
|
|
|
|
|
|
|
|
|
2024-06-12 03:44:20 +00:00
|
|
|
# Modelled on the asyncio.Stream class (extmod/asyncio/stream.py)
|
|
|
|
# NOTE: Relies on internal implementation of asyncio.core (_io_queue)
|
2023-04-07 12:32:16 +00:00
|
|
|
class AIOESPNow(espnow.ESPNow):
|
|
|
|
# Read one ESPNow message
|
|
|
|
async def arecv(self):
|
|
|
|
yield asyncio.core._io_queue.queue_read(self)
|
2023-05-02 22:39:46 +00:00
|
|
|
return self.recv(0) # type: ignore[misc]
|
2023-04-07 12:32:16 +00:00
|
|
|
|
|
|
|
async def airecv(self):
|
|
|
|
yield asyncio.core._io_queue.queue_read(self)
|
2023-05-02 22:39:46 +00:00
|
|
|
return self.irecv(0) # type: ignore[misc]
|
2023-04-07 12:32:16 +00:00
|
|
|
|
|
|
|
async def asend(self, mac, msg=None, sync=None):
|
|
|
|
if msg is None:
|
|
|
|
msg, mac = mac, None # If msg is None: swap mac and msg
|
|
|
|
yield asyncio.core._io_queue.queue_write(self)
|
2023-05-02 22:39:46 +00:00
|
|
|
return self.send(mac, msg, sync) # type: ignore[misc]
|
2023-04-07 12:32:16 +00:00
|
|
|
|
|
|
|
# "async for" support
|
|
|
|
def __aiter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def __anext__(self):
|
|
|
|
return await self.airecv()
|