Skip to content

Commit 3e1c9cc

Browse files
committed
uset supervisor.ticks_ms() instead of time.monotonic() if available
1 parent dd65bdf commit 3e1c9cc

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

adafruit_rfm69.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,20 @@
4747
https://github.com/adafruit/circuitpython/releases
4848
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4949
"""
50-
import time
5150
import random
52-
51+
import time
52+
import adafruit_bus_device.spi_device as spidev
5353
from micropython import const
5454

55-
import adafruit_bus_device.spi_device as spidev
55+
HAS_SUPERVISOR = False
5656

57+
try:
58+
import supervisor
59+
60+
if hasattr(supervisor, "ticks_ms"):
61+
HAS_SUPERVISOR = True
62+
except ImportError:
63+
pass
5764

5865
__version__ = "0.0.0-auto.0"
5966
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM69.git"
@@ -116,6 +123,10 @@
116123
FS_MODE = 0b010
117124
TX_MODE = 0b011
118125
RX_MODE = 0b100
126+
# supervisor.ticks_ms() contants
127+
_TICKS_PERIOD = const(1 << 29)
128+
_TICKS_MAX = const(_TICKS_PERIOD - 1)
129+
_TICKS_HALFPERIOD = const(_TICKS_PERIOD // 2)
119130

120131
# Disable the silly too many instance members warning. Pylint has no knowledge
121132
# of the context and is merely guessing at the proper amount of members. This
@@ -124,6 +135,15 @@
124135
# pylint: disable=too-many-instance-attributes
125136

126137

138+
def ticks_diff(ticks1, ticks2):
139+
"""Compute the signed difference between two ticks values
140+
assuming that they are within 2**28 ticks
141+
"""
142+
diff = (ticks1 - ticks2) & _TICKS_MAX
143+
diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD
144+
return diff
145+
146+
127147
class RFM69:
128148
"""Interface to a RFM69 series packet radio. Allows simple sending and
129149
receiving of wireless data at supported frequencies of the radio
@@ -474,10 +494,16 @@ def operation_mode(self, val):
474494
op_mode |= val << 2
475495
self._write_u8(_REG_OP_MODE, op_mode)
476496
# Wait for mode to change by polling interrupt bit.
477-
start = time.monotonic()
478-
while not self.mode_ready:
479-
if (time.monotonic() - start) >= 1:
480-
raise TimeoutError("Operation Mode failed to set.")
497+
if HAS_SUPERVISOR:
498+
start = supervisor.ticks_ms()
499+
while not self.mode_ready:
500+
if ticks_diff(supervisor.ticks_ms(), start) >= 1000:
501+
raise TimeoutError("Operation Mode failed to set.")
502+
else:
503+
start = time.monotonic()
504+
while not self.mode_ready:
505+
if time.monotonic() - start >= 1:
506+
raise TimeoutError("Operation Mode failed to set.")
481507

482508
@property
483509
def sync_word(self):
@@ -693,6 +719,7 @@ def payload_ready(self):
693719
"""Receive status"""
694720
return (self._read_u8(_REG_IRQ_FLAGS2) & 0x4) >> 2
695721

722+
# pylint: disable=too-many-branches
696723
def send(
697724
self,
698725
data,
@@ -751,11 +778,17 @@ def send(
751778
self.transmit()
752779
# Wait for packet sent interrupt with explicit polling (not ideal but
753780
# best that can be done right now without interrupts).
754-
start = time.monotonic()
755781
timed_out = False
756-
while not timed_out and not self.packet_sent():
757-
if (time.monotonic() - start) >= self.xmit_timeout:
758-
timed_out = True
782+
if HAS_SUPERVISOR:
783+
start = supervisor.ticks_ms()
784+
while not timed_out and not self.packet_sent():
785+
if ticks_diff(supervisor.ticks_ms(), start) >= self.xmit_timeout * 1000:
786+
timed_out = True
787+
else:
788+
start = time.monotonic()
789+
while not timed_out and not self.packet_sent():
790+
if time.monotonic() - start >= self.xmit_timeout:
791+
timed_out = True
759792
# Listen again if requested.
760793
if keep_listening:
761794
self.listen()
@@ -800,7 +833,6 @@ def send_with_ack(self, data):
800833
self.flags = 0 # clear flags
801834
return got_ack
802835

803-
# pylint: disable=too-many-branches
804836
def receive(
805837
self, *, keep_listening=True, with_ack=False, timeout=None, with_header=False
806838
):
@@ -828,11 +860,17 @@ def receive(
828860
# interrupt supports.
829861
# Make sure we are listening for packets.
830862
self.listen()
831-
start = time.monotonic()
832863
timed_out = False
833-
while not timed_out and not self.payload_ready():
834-
if (time.monotonic() - start) >= timeout:
835-
timed_out = True
864+
if HAS_SUPERVISOR:
865+
start = supervisor.ticks_ms()
866+
while not timed_out and not self.payload_ready():
867+
if ticks_diff(supervisor.ticks_ms(), start) >= timeout * 1000:
868+
timed_out = True
869+
else:
870+
start = time.monotonic()
871+
while not timed_out and not self.payload_ready():
872+
if time.monotonic() - start >= timeout:
873+
timed_out = True
836874
# Payload ready is set, a packet is in the FIFO.
837875
packet = None
838876
# save last RSSI reading

0 commit comments

Comments
 (0)