Skip to content

Commit 28dd218

Browse files
authored
Merge pull request #7 from jerryneedell/jerryn_fix_freq
fix frequency setter and misc items
2 parents 5407b92 + bb6d069 commit 28dd218

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

README.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@ This is easily achieved by downloading
3939

4040
Usage Example
4141
=============
42-
4342
See examples/rfm69_simpletest.py for a simple demo of the usage.
43+
Note: the default baudrate for the SPI is 5000000 (5MHz).
44+
The maximum setting is 10Mhz but
45+
transmission errors have been observed expecially when using breakout boards.
46+
For breakout boards or other configurations where the boards are separated,
47+
it may be necessary to reduce the baudrate for reliable data transmission.
48+
The baud rate may be specified as an keyword parameter when initializing the board.
49+
To set it to 1000000 use :
50+
51+
.. code-block:: python
52+
53+
# Initialze RFM radio
54+
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000)
55+
4456
4557
Contributing
4658
============
@@ -94,4 +106,4 @@ Now, once you have the virtual environment activated:
94106
95107
This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
96108
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
97-
locally verify it will pass.
109+
locally verify it will pass.

adafruit_rfm69.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def __set__(self, obj, val):
289289
payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2)
290290

291291
def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
292-
preamble_length=4, encryption_key=None, high_power=True, baudrate=10000000):
292+
preamble_length=4, encryption_key=None, high_power=True, baudrate=5000000):
293293
self._tx_power = 13
294294
self.high_power = high_power
295295
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
@@ -340,7 +340,7 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
340340
# Set the preamble length.
341341
self.preamble_length = preamble_length
342342
# Set frequency.
343-
self.frequency = frequency
343+
self.frequency_mhz = frequency
344344
# Set encryption key.
345345
self.encryption_key = encryption_key
346346
# Set transmit power to 13 dBm, a safe value any module supports.
@@ -673,10 +673,12 @@ def frequency_deviation(self, val):
673673
self._write_u8(_REG_FDEV_MSB, fdev >> 8)
674674
self._write_u8(_REG_FDEV_LSB, fdev & 0xFF)
675675

676-
def send(self, data):
677-
"""Send a string of data using the transmitter. You can only send 60 bytes at a time
678-
(limited by chip's FIFO size and appended headers). Note this appends a 4 byte header to
679-
be compatible with the RadioHead library.
676+
def send(self, data, timeout=2.):
677+
"""Send a string of data using the transmitter.
678+
You can only send 60 bytes at a time
679+
(limited by chip's FIFO size and appended headers).
680+
Note this appends a 4 byte header to be compatible with the RadioHead library.
681+
The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
680682
"""
681683
# Disable pylint warning to not use length as a check for zero.
682684
# This is a puzzling warning as the below code is clearly the most
@@ -705,12 +707,17 @@ def send(self, data):
705707
self.transmit()
706708
# Wait for packet sent interrupt with explicit polling (not ideal but
707709
# best that can be done right now without interrupts).
708-
while not self.packet_sent:
709-
pass
710+
start = time.monotonic()
711+
timed_out = False
712+
while not timed_out and not self.packet_sent:
713+
if (time.monotonic() - start) >= timeout:
714+
timed_out = True
710715
# Go back to idle mode after transmit.
711716
self.idle()
717+
if timed_out:
718+
raise RuntimeError('Timeout during packet send')
712719

713-
def receive(self, timeout_s=0.5, keep_listening=True):
720+
def receive(self, timeout=0.5, keep_listening=True):
714721
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
715722
seconds for a packet to be received and decoded. If a packet is found the payload bytes
716723
are returned, otherwise None is returned (which indicates the timeout elapsed with no
@@ -726,13 +733,16 @@ def receive(self, timeout_s=0.5, keep_listening=True):
726733
# enough, however it's the best that can be done from Python without
727734
# interrupt supports.
728735
start = time.monotonic()
729-
while not self.payload_ready:
730-
if (time.monotonic() - start) >= timeout_s:
731-
return None # Exceeded timeout.
736+
timed_out = False
737+
while not timed_out and not self.payload_ready:
738+
if (time.monotonic() - start) >= timeout:
739+
timed_out = True
732740
# Payload ready is set, a packet is in the FIFO.
733741
packet = None
734742
# Enter idle mode to stop receiving other packets.
735743
self.idle()
744+
if timed_out:
745+
return None
736746
# Read the data from the FIFO.
737747
with self._device as device:
738748
self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0

examples/rfm69_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
while True:
5252
packet = rfm69.receive()
5353
# Optionally change the receive timeout from its default of 0.5 seconds:
54-
#packet = rfm69.receive(timeout_s=5.0)
54+
#packet = rfm69.receive(timeout=5.0)
5555
# If no packet was received during the timeout then None is returned.
5656
if packet is None:
5757
print('Received nothing! Listening again...')

0 commit comments

Comments
 (0)