Skip to content

fix frequency setter and misc items #7

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

Merged
merged 9 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,20 @@ This is easily achieved by downloading

Usage Example
=============

See examples/rfm69_simpletest.py for a simple demo of the usage.
Note: the default baudrate for the SPI is 5000000 (5MHz).
The maximum setting is 10Mhz but
transmission errors have been observed expecially when using breakout boards.
For breakout boards or other configurations where the boards are separated,
it may be necessary to reduce the baudrate for reliable data transmission.
The baud rate may be specified as an keyword parameter when initializing the board.
To set it to 1000000 use :

.. code-block:: python

# Initialze RFM radio
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ,baudrate=1000000)


Contributing
============
Expand Down Expand Up @@ -94,4 +106,4 @@ Now, once you have the virtual environment activated:

This will output the documentation to ``docs/_build/html``. Open the index.html in your browser to
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
locally verify it will pass.
locally verify it will pass.
34 changes: 22 additions & 12 deletions adafruit_rfm69.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def __set__(self, obj, val):
payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2)

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

def send(self, data):
"""Send a string of data using the transmitter. You can only send 60 bytes at a time
(limited by chip's FIFO size and appended headers). Note this appends a 4 byte header to
be compatible with the RadioHead library.
def send(self, data, timeout=2.):
"""Send a string of data using the transmitter.
You can only send 60 bytes at a time
(limited by chip's FIFO size and appended headers).
Note this appends a 4 byte header to be compatible with the RadioHead library.
The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
"""
# Disable pylint warning to not use length as a check for zero.
# This is a puzzling warning as the below code is clearly the most
Expand Down Expand Up @@ -705,12 +707,17 @@ def send(self, data):
self.transmit()
# Wait for packet sent interrupt with explicit polling (not ideal but
# best that can be done right now without interrupts).
while not self.packet_sent:
pass
start = time.monotonic()
timed_out = False
while not timed_out and not self.packet_sent:
if (time.monotonic() - start) >= timeout:
timed_out = True
# Go back to idle mode after transmit.
self.idle()
if timed_out:
raise RuntimeError('Timeout during packet send')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


def receive(self, timeout_s=0.5, keep_listening=True):
def receive(self, timeout=0.5, keep_listening=True):
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
seconds for a packet to be received and decoded. If a packet is found the payload bytes
are returned, otherwise None is returned (which indicates the timeout elapsed with no
Expand All @@ -726,13 +733,16 @@ def receive(self, timeout_s=0.5, keep_listening=True):
# enough, however it's the best that can be done from Python without
# interrupt supports.
start = time.monotonic()
while not self.payload_ready:
if (time.monotonic() - start) >= timeout_s:
return None # Exceeded timeout.
timed_out = False
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
# Enter idle mode to stop receiving other packets.
self.idle()
if timed_out:
return None
# Read the data from the FIFO.
with self._device as device:
self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0
Expand Down
2 changes: 1 addition & 1 deletion examples/rfm69_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
while True:
packet = rfm69.receive()
# Optionally change the receive timeout from its default of 0.5 seconds:
#packet = rfm69.receive(timeout_s=5.0)
#packet = rfm69.receive(timeout=5.0)
# If no packet was received during the timeout then None is returned.
if packet is None:
print('Received nothing! Listening again...')
Expand Down