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

use supervisor.ticks_ms() instead of time.monotonic() if available #41

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Changes from 1 commit
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
70 changes: 54 additions & 16 deletions adafruit_rfm69.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@
https://github.com/adafruit/circuitpython/releases
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""
import time
import random

import time
import adafruit_bus_device.spi_device as spidev
from micropython import const

import adafruit_bus_device.spi_device as spidev
HAS_SUPERVISOR = False

try:
import supervisor

if hasattr(supervisor, "ticks_ms"):
HAS_SUPERVISOR = True
tannewt marked this conversation as resolved.
Show resolved Hide resolved
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM69.git"
Expand Down Expand Up @@ -116,6 +123,10 @@
FS_MODE = 0b010
TX_MODE = 0b011
RX_MODE = 0b100
# supervisor.ticks_ms() contants
_TICKS_PERIOD = const(1 << 29)
_TICKS_MAX = const(_TICKS_PERIOD - 1)
_TICKS_HALFPERIOD = const(_TICKS_PERIOD // 2)

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


def ticks_diff(ticks1, ticks2):
"""Compute the signed difference between two ticks values
assuming that they are within 2**28 ticks
"""
diff = (ticks1 - ticks2) & _TICKS_MAX
diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD
return diff


class RFM69:
"""Interface to a RFM69 series packet radio. Allows simple sending and
receiving of wireless data at supported frequencies of the radio
Expand Down Expand Up @@ -474,10 +494,16 @@ def operation_mode(self, val):
op_mode |= val << 2
self._write_u8(_REG_OP_MODE, op_mode)
# Wait for mode to change by polling interrupt bit.
start = time.monotonic()
while not self.mode_ready:
if (time.monotonic() - start) >= 1:
raise TimeoutError("Operation Mode failed to set.")
if HAS_SUPERVISOR:
start = supervisor.ticks_ms()
while not self.mode_ready:
if ticks_diff(supervisor.ticks_ms(), start) >= 1000:
raise TimeoutError("Operation Mode failed to set.")
else:
start = time.monotonic()
while not self.mode_ready:
if time.monotonic() - start >= 1:
raise TimeoutError("Operation Mode failed to set.")

@property
def sync_word(self):
Expand Down Expand Up @@ -693,6 +719,7 @@ def payload_ready(self):
"""Receive status"""
return (self._read_u8(_REG_IRQ_FLAGS2) & 0x4) >> 2

# pylint: disable=too-many-branches
def send(
self,
data,
Expand Down Expand Up @@ -751,11 +778,17 @@ def send(
self.transmit()
# Wait for packet sent interrupt with explicit polling (not ideal but
# best that can be done right now without interrupts).
start = time.monotonic()
timed_out = False
while not timed_out and not self.packet_sent():
if (time.monotonic() - start) >= self.xmit_timeout:
timed_out = True
if HAS_SUPERVISOR:
start = supervisor.ticks_ms()
while not timed_out and not self.packet_sent():
if ticks_diff(supervisor.ticks_ms(), start) >= self.xmit_timeout * 1000:
timed_out = True
else:
start = time.monotonic()
while not timed_out and not self.packet_sent():
if time.monotonic() - start >= self.xmit_timeout:
timed_out = True
tannewt marked this conversation as resolved.
Show resolved Hide resolved
# Listen again if requested.
if keep_listening:
self.listen()
Expand Down Expand Up @@ -800,7 +833,6 @@ def send_with_ack(self, data):
self.flags = 0 # clear flags
return got_ack

# pylint: disable=too-many-branches
def receive(
self, *, keep_listening=True, with_ack=False, timeout=None, with_header=False
):
Expand Down Expand Up @@ -828,11 +860,17 @@ def receive(
# interrupt supports.
# Make sure we are listening for packets.
self.listen()
start = time.monotonic()
timed_out = False
while not timed_out and not self.payload_ready():
if (time.monotonic() - start) >= timeout:
timed_out = True
if HAS_SUPERVISOR:
start = supervisor.ticks_ms()
while not timed_out and not self.payload_ready():
if ticks_diff(supervisor.ticks_ms(), start) >= timeout * 1000:
timed_out = True
else:
start = time.monotonic()
while not timed_out and not self.payload_ready():
if time.monotonic() - start >= timeout:
timed_out = True
# Payload ready is set, a packet is in the FIFO.
packet = None
# save last RSSI reading
Expand Down