|
47 | 47 | https://github.com/adafruit/circuitpython/releases
|
48 | 48 | * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
|
49 | 49 | """
|
50 |
| -import time |
51 | 50 | import random
|
52 |
| - |
| 51 | +import time |
| 52 | +import adafruit_bus_device.spi_device as spidev |
53 | 53 | from micropython import const
|
54 | 54 |
|
55 |
| -import adafruit_bus_device.spi_device as spidev |
| 55 | +HAS_SUPERVISOR = False |
56 | 56 |
|
| 57 | +try: |
| 58 | + import supervisor |
| 59 | + |
| 60 | + if hasattr(supervisor, "ticks_ms"): |
| 61 | + HAS_SUPERVISOR = True |
| 62 | +except ImportError: |
| 63 | + pass |
57 | 64 |
|
58 | 65 | __version__ = "0.0.0-auto.0"
|
59 | 66 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM69.git"
|
|
116 | 123 | FS_MODE = 0b010
|
117 | 124 | TX_MODE = 0b011
|
118 | 125 | 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) |
119 | 130 |
|
120 | 131 | # Disable the silly too many instance members warning. Pylint has no knowledge
|
121 | 132 | # of the context and is merely guessing at the proper amount of members. This
|
|
124 | 135 | # pylint: disable=too-many-instance-attributes
|
125 | 136 |
|
126 | 137 |
|
| 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 | + |
127 | 147 | class RFM69:
|
128 | 148 | """Interface to a RFM69 series packet radio. Allows simple sending and
|
129 | 149 | receiving of wireless data at supported frequencies of the radio
|
@@ -474,10 +494,16 @@ def operation_mode(self, val):
|
474 | 494 | op_mode |= val << 2
|
475 | 495 | self._write_u8(_REG_OP_MODE, op_mode)
|
476 | 496 | # 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.") |
481 | 507 |
|
482 | 508 | @property
|
483 | 509 | def sync_word(self):
|
@@ -693,6 +719,7 @@ def payload_ready(self):
|
693 | 719 | """Receive status"""
|
694 | 720 | return (self._read_u8(_REG_IRQ_FLAGS2) & 0x4) >> 2
|
695 | 721 |
|
| 722 | + # pylint: disable=too-many-branches |
696 | 723 | def send(
|
697 | 724 | self,
|
698 | 725 | data,
|
@@ -751,11 +778,17 @@ def send(
|
751 | 778 | self.transmit()
|
752 | 779 | # Wait for packet sent interrupt with explicit polling (not ideal but
|
753 | 780 | # best that can be done right now without interrupts).
|
754 |
| - start = time.monotonic() |
755 | 781 | 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 |
759 | 792 | # Listen again if requested.
|
760 | 793 | if keep_listening:
|
761 | 794 | self.listen()
|
@@ -800,7 +833,6 @@ def send_with_ack(self, data):
|
800 | 833 | self.flags = 0 # clear flags
|
801 | 834 | return got_ack
|
802 | 835 |
|
803 |
| - # pylint: disable=too-many-branches |
804 | 836 | def receive(
|
805 | 837 | self, *, keep_listening=True, with_ack=False, timeout=None, with_header=False
|
806 | 838 | ):
|
@@ -828,11 +860,17 @@ def receive(
|
828 | 860 | # interrupt supports.
|
829 | 861 | # Make sure we are listening for packets.
|
830 | 862 | self.listen()
|
831 |
| - start = time.monotonic() |
832 | 863 | 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 |
836 | 874 | # Payload ready is set, a packet is in the FIFO.
|
837 | 875 | packet = None
|
838 | 876 | # save last RSSI reading
|
|
0 commit comments