# aioespnow module for MicroPython on ESP32 and ESP8266
# MIT license; Copyright (c) 2022 Glenn Moloney @glenn20
# Vendored from micropython-lib/micropython/aioespnow

import asyncio
import espnow


class AIOESPNow(espnow.ESPNow):
    async def arecv(self):
        yield asyncio.core._io_queue.queue_read(self)
        return self.recv(0)

    async def airecv(self):
        yield asyncio.core._io_queue.queue_read(self)
        return self.irecv(0)

    async def asend(self, mac, msg=None, sync=None):
        if msg is None:
            msg, mac = mac, None
        yield asyncio.core._io_queue.queue_write(self)
        return self.send(mac, msg, sync)

    def __aiter__(self):
        return self

    async def __anext__(self):
        return await self.airecv()
