Skip to content

Commit 0df4521

Browse files
authored
Merge pull request #68 from jerryneedell/jerryn_ticks
use supervisor.ticks_ms when available instead of time.monotonic()
2 parents 86b2ba9 + 775399c commit 0df4521

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

adafruit_rfm9x.py

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

20+
HAS_SUPERVISOR = False
21+
22+
try:
23+
import supervisor
24+
25+
if hasattr(supervisor, "ticks_ms"):
26+
HAS_SUPERVISOR = True
27+
except ImportError:
28+
pass
2129
__version__ = "0.0.0-auto.0"
2230
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM9x.git"
2331

@@ -99,7 +107,10 @@
99107
TX_MODE = 0b011
100108
FS_RX_MODE = 0b100
101109
RX_MODE = 0b101
102-
110+
# supervisor.ticks_ms() contants
111+
_TICKS_PERIOD = const(1 << 29)
112+
_TICKS_MAX = const(_TICKS_PERIOD - 1)
113+
_TICKS_HALFPERIOD = const(_TICKS_PERIOD // 2)
103114

104115
# Disable the too many instance members warning. Pylint has no knowledge
105116
# of the context and is merely guessing at the proper amount of members. This
@@ -109,6 +120,15 @@
109120
# pylint: disable=too-many-statements
110121

111122

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

664+
# pylint: disable=too-many-branches
644665
def send(
645666
self,
646667
data,
@@ -701,11 +722,17 @@ def send(
701722
self.transmit()
702723
# Wait for tx done interrupt with explicit polling (not ideal but
703724
# best that can be done right now without interrupts).
704-
start = time.monotonic()
705725
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
726+
if HAS_SUPERVISOR:
727+
start = supervisor.ticks_ms()
728+
while not timed_out and not self.tx_done():
729+
if ticks_diff(supervisor.ticks_ms(), start) >= self.xmit_timeout * 1000:
730+
timed_out = True
731+
else:
732+
start = time.monotonic()
733+
while not timed_out and not self.tx_done():
734+
if time.monotonic() - start >= self.xmit_timeout:
735+
timed_out = True
709736
# Listen again if necessary and return the result packet.
710737
if keep_listening:
711738
self.listen()
@@ -753,7 +780,6 @@ def send_with_ack(self, data):
753780
self.flags = 0 # clear flags
754781
return got_ack
755782

756-
# pylint: disable=too-many-branches
757783
def receive(
758784
self, *, keep_listening=True, with_header=False, with_ack=False, timeout=None
759785
):
@@ -781,11 +807,17 @@ def receive(
781807
# interrupt supports.
782808
# Make sure we are listening for packets.
783809
self.listen()
784-
start = time.monotonic()
785810
timed_out = False
786-
while not timed_out and not self.rx_done():
787-
if (time.monotonic() - start) >= timeout:
788-
timed_out = True
811+
if HAS_SUPERVISOR:
812+
start = supervisor.ticks_ms()
813+
while not timed_out and not self.rx_done():
814+
if ticks_diff(supervisor.ticks_ms(), start) >= timeout * 1000:
815+
timed_out = True
816+
else:
817+
start = time.monotonic()
818+
while not timed_out and not self.rx_done():
819+
if time.monotonic() - start >= timeout:
820+
timed_out = True
789821
# Payload ready is set, a packet is in the FIFO.
790822
packet = None
791823
# save last RSSI reading

0 commit comments

Comments
 (0)