diff --git a/adafruit_rfm9x.py b/adafruit_rfm9x.py index 276e4e8..bc9e20b 100644 --- a/adafruit_rfm9x.py +++ b/adafruit_rfm9x.py @@ -16,6 +16,7 @@ import time import adafruit_bus_device.spi_device as spidev from micropython import const +import tasko HAS_SUPERVISOR = False @@ -42,7 +43,7 @@ pass __version__ = "0.0.0-auto.0" -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM9x.git" +__repo__ = "https://github.com/PyCubed-Mini/RFM9x_asyncio.git" # Internal constants: # Register names (FSK Mode even though we use LoRa instead, from table 85) @@ -682,7 +683,7 @@ def crc_error(self) -> bool: return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x20) >> 5 # pylint: disable=too-many-branches - def send( + async def send( self, data: ReadableBuffer, *, @@ -748,11 +749,15 @@ def send( while not timed_out and not self.tx_done(): if ticks_diff(supervisor.ticks_ms(), start) >= self.xmit_timeout * 1000: timed_out = True + else: + await tasko.sleep(0) else: start = time.monotonic() while not timed_out and not self.tx_done(): if time.monotonic() - start >= self.xmit_timeout: timed_out = True + else: + await tasko.sleep(0) # Listen again if necessary and return the result packet. if keep_listening: self.listen() @@ -763,7 +768,7 @@ def send( self._write_u8(_RH_RF95_REG_12_IRQ_FLAGS, 0xFF) return not timed_out - def send_with_ack(self, data: ReadableBuffer) -> bool: + async def send_with_ack(self, data: ReadableBuffer) -> bool: """Reliable Datagram mode: Send a packet with data and wait for an ACK response. The packet header is automatically generated. @@ -777,13 +782,13 @@ def send_with_ack(self, data: ReadableBuffer) -> bool: self.sequence_number = (self.sequence_number + 1) & 0xFF while not got_ack and retries_remaining: self.identifier = self.sequence_number - self.send(data, keep_listening=True) + await self.send(data, keep_listening=True) # Don't look for ACK from Broadcast message if self.destination == _RH_BROADCAST_ADDRESS: got_ack = True else: # wait for a packet from our destination - ack_packet = self.receive(timeout=self.ack_wait, with_header=True) + ack_packet = await self.receive(timeout=self.ack_wait, with_header=True) if ack_packet is not None: if ack_packet[3] & _RH_FLAGS_ACK: # check the ID @@ -793,14 +798,14 @@ def send_with_ack(self, data: ReadableBuffer) -> bool: # pause before next retry -- random delay if not got_ack: # delay by random amount before next try - time.sleep(self.ack_wait + self.ack_wait * random.random()) + await tasko.sleep(self.ack_wait + self.ack_wait * random.random()) retries_remaining = retries_remaining - 1 # set retry flag in packet header self.flags |= _RH_FLAGS_RETRY self.flags = 0 # clear flags return got_ack - def receive( + async def receive( self, *, keep_listening: bool = True, @@ -838,11 +843,15 @@ def receive( while not timed_out and not self.rx_done(): if ticks_diff(supervisor.ticks_ms(), start) >= timeout * 1000: timed_out = True + else: + await tasko.sleep(0) else: start = time.monotonic() while not timed_out and not self.rx_done(): if time.monotonic() - start >= timeout: timed_out = True + else: + await tasko.sleep(0) # Payload ready is set, a packet is in the FIFO. packet = None # save last RSSI reading @@ -889,7 +898,7 @@ def receive( if self.ack_delay is not None: time.sleep(self.ack_delay) # send ACK packet to sender (data is b'!') - self.send( + await self.send( b"!", destination=packet[1], node=packet[0],