Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it async #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions adafruit_rfm9x.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import time
import adafruit_bus_device.spi_device as spidev
from micropython import const
import tasko

HAS_SUPERVISOR = False

Expand All @@ -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)
Expand Down Expand Up @@ -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,
*,
Expand Down Expand Up @@ -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()
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down