diff --git a/README.rst b/README.rst index f3ec83d..66f2144 100644 --- a/README.rst +++ b/README.rst @@ -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 ============ @@ -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. \ No newline at end of file +locally verify it will pass. diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 30e64e1..812eb27 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -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. @@ -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. @@ -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 @@ -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') - 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 @@ -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 diff --git a/examples/rfm69_simpletest.py b/examples/rfm69_simpletest.py index 8bfa4f8..98ff961 100644 --- a/examples/rfm69_simpletest.py +++ b/examples/rfm69_simpletest.py @@ -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...')