Skip to content

Commit 7592e30

Browse files
committed
ise subervisor.ticks_ms when available instead of time.monotonic()
1 parent 86b2ba9 commit 7592e30

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

adafruit_rfm9x.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
"""
1515
import random
1616
import time
17-
1817
import adafruit_bus_device.spi_device as spidev
1918
from micropython import const
2019

20+
try:
21+
import supervisor
22+
23+
HAS_SUPERVISOR = True
24+
except ImportError:
25+
HAS_SUPERVISOR = False
26+
2127
__version__ = "0.0.0-auto.0"
2228
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM9x.git"
2329

@@ -99,7 +105,10 @@
99105
TX_MODE = 0b011
100106
FS_RX_MODE = 0b100
101107
RX_MODE = 0b101
102-
108+
# supervisor.ticks_ms() contants
109+
_TICKS_PERIOD = const(1 << 29)
110+
_TICKS_MAX = const(_TICKS_PERIOD - 1)
111+
_TICKS_HALFPERIOD = const(_TICKS_PERIOD // 2)
103112

104113
# Disable the too many instance members warning. Pylint has no knowledge
105114
# of the context and is merely guessing at the proper amount of members. This
@@ -109,6 +118,15 @@
109118
# pylint: disable=too-many-statements
110119

111120

121+
def ticks_diff(ticks1, ticks2):
122+
"""Compute the signed difference between two ticks values
123+
assuming that they are within 2**28 ticks
124+
"""
125+
diff = (ticks1 - ticks2) & _TICKS_MAX
126+
diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD
127+
return diff
128+
129+
112130
class RFM9x:
113131
"""Interface to a RFM95/6/7/8 LoRa radio module. Allows sending and
114132
receivng bytes of data in long range LoRa mode at a support board frequency
@@ -641,6 +659,7 @@ def crc_error(self):
641659
"""crc status"""
642660
return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x20) >> 5
643661

662+
# pylint: disable=too-many-branches
644663
def send(
645664
self,
646665
data,
@@ -701,11 +720,17 @@ def send(
701720
self.transmit()
702721
# Wait for tx done interrupt with explicit polling (not ideal but
703722
# best that can be done right now without interrupts).
704-
start = time.monotonic()
705723
timed_out = False
706-
while not timed_out and not self.tx_done():
707-
if (time.monotonic() - start) >= self.xmit_timeout:
708-
timed_out = True
724+
if HAS_SUPERVISOR:
725+
start = supervisor.ticks_ms()
726+
while not timed_out and not self.tx_done():
727+
if ticks_diff(supervisor.ticks_ms(), start) >= self.xmit_timeout * 1000:
728+
timed_out = True
729+
else:
730+
start = time.monotonic()
731+
while not timed_out and not self.tx_done():
732+
if time.monotonic() - start >= self.xmit_timeout:
733+
timed_out = True
709734
# Listen again if necessary and return the result packet.
710735
if keep_listening:
711736
self.listen()
@@ -753,7 +778,6 @@ def send_with_ack(self, data):
753778
self.flags = 0 # clear flags
754779
return got_ack
755780

756-
# pylint: disable=too-many-branches
757781
def receive(
758782
self, *, keep_listening=True, with_header=False, with_ack=False, timeout=None
759783
):
@@ -781,11 +805,17 @@ def receive(
781805
# interrupt supports.
782806
# Make sure we are listening for packets.
783807
self.listen()
784-
start = time.monotonic()
785808
timed_out = False
786-
while not timed_out and not self.rx_done():
787-
if (time.monotonic() - start) >= timeout:
788-
timed_out = True
809+
if HAS_SUPERVISOR:
810+
start = supervisor.ticks_ms()
811+
while not timed_out and not self.rx_done():
812+
if ticks_diff(supervisor.ticks_ms(), start) >= timeout * 1000:
813+
timed_out = True
814+
else:
815+
start = time.monotonic()
816+
while not timed_out and not self.rx_done():
817+
if time.monotonic() - start >= timeout:
818+
timed_out = True
789819
# Payload ready is set, a packet is in the FIFO.
790820
packet = None
791821
# save last RSSI reading

0 commit comments

Comments
 (0)