|
14 | 14 | """
|
15 | 15 | import random
|
16 | 16 | import time
|
17 |
| - |
18 | 17 | import adafruit_bus_device.spi_device as spidev
|
19 | 18 | from micropython import const
|
20 | 19 |
|
| 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 |
21 | 29 | __version__ = "0.0.0-auto.0"
|
22 | 30 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM9x.git"
|
23 | 31 |
|
|
99 | 107 | TX_MODE = 0b011
|
100 | 108 | FS_RX_MODE = 0b100
|
101 | 109 | 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) |
103 | 114 |
|
104 | 115 | # Disable the too many instance members warning. Pylint has no knowledge
|
105 | 116 | # of the context and is merely guessing at the proper amount of members. This
|
|
109 | 120 | # pylint: disable=too-many-statements
|
110 | 121 |
|
111 | 122 |
|
| 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 | + |
112 | 132 | class RFM9x:
|
113 | 133 | """Interface to a RFM95/6/7/8 LoRa radio module. Allows sending and
|
114 | 134 | receivng bytes of data in long range LoRa mode at a support board frequency
|
@@ -641,6 +661,7 @@ def crc_error(self):
|
641 | 661 | """crc status"""
|
642 | 662 | return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x20) >> 5
|
643 | 663 |
|
| 664 | + # pylint: disable=too-many-branches |
644 | 665 | def send(
|
645 | 666 | self,
|
646 | 667 | data,
|
@@ -701,11 +722,17 @@ def send(
|
701 | 722 | self.transmit()
|
702 | 723 | # Wait for tx done interrupt with explicit polling (not ideal but
|
703 | 724 | # best that can be done right now without interrupts).
|
704 |
| - start = time.monotonic() |
705 | 725 | 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 |
709 | 736 | # Listen again if necessary and return the result packet.
|
710 | 737 | if keep_listening:
|
711 | 738 | self.listen()
|
@@ -753,7 +780,6 @@ def send_with_ack(self, data):
|
753 | 780 | self.flags = 0 # clear flags
|
754 | 781 | return got_ack
|
755 | 782 |
|
756 |
| - # pylint: disable=too-many-branches |
757 | 783 | def receive(
|
758 | 784 | self, *, keep_listening=True, with_header=False, with_ack=False, timeout=None
|
759 | 785 | ):
|
@@ -781,11 +807,17 @@ def receive(
|
781 | 807 | # interrupt supports.
|
782 | 808 | # Make sure we are listening for packets.
|
783 | 809 | self.listen()
|
784 |
| - start = time.monotonic() |
785 | 810 | 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 |
789 | 821 | # Payload ready is set, a packet is in the FIFO.
|
790 | 822 | packet = None
|
791 | 823 | # save last RSSI reading
|
|
0 commit comments