Skip to content

Commit 4d25d76

Browse files
committed
Refactor RFM9x __init__ parameters into properties.
1 parent ce0a8f0 commit 4d25d76

File tree

2 files changed

+106
-53
lines changed

2 files changed

+106
-53
lines changed

README.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ To set it to 1000000 use :
6464
6565
Optional controls exist to alter the signal bandwidth, coding rate, and spreading factor
6666
settings used by the radio to achieve better performance in different environments.
67-
By default, settings compatible with RadioHead Bw125Cr45Sf128 mode are used, matching
68-
the following example:
67+
By default, settings compatible with RadioHead Bw125Cr45Sf128 mode are used, which can
68+
be altered in the following manner (continued from the above example):
6969

7070
.. code-block:: python
7171
72-
# Initialze RFM radio with conservative baudrate and default modem config
73-
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ, baudrate=1000000,
74-
signal_bandwidth=125000, coding_rate=5, spreading_factor=7,
75-
enable_crc=False)
72+
# Apply new modem config settings to the radio to improve its effective range
73+
rfm9x.signal_bandwidth = 62500
74+
rfm9x.coding_rate = 6
75+
rfm9x.spreading_factor = 8
76+
rfm9x.enable_crc = True
7677
7778
See examples/rfm9x_simpletest.py for an expanded demo of the usage.
7879

adafruit_rfm9x.py

Lines changed: 99 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ def __set__(self, obj, val):
347347
bw_bins = (7800, 10400, 15600, 20800, 31250, 41700, 62500, 125000, 250000)
348348

349349
def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
350-
high_power=True, baudrate=5000000, signal_bandwidth=125000,
351-
coding_rate=5, spreading_factor=7, enable_crc=False):
350+
high_power=True, baudrate=5000000):
352351
self.high_power = high_power
353352
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
354353
# Set Default Baudrate to 5MHz to avoid problems
@@ -382,51 +381,11 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
382381
# Set mode idle
383382
self.idle()
384383
# Defaults set modem config to RadioHead compatible Bw125Cr45Sf128 mode.
385-
# Set signal bandwidth (set to 125000 to match RadioHead Bw125).
386-
for bwid, cutoff in enumerate(self.bw_bins):
387-
if signal_bandwidth <= cutoff:
388-
break
389-
else:
390-
bwid = 9
391-
self._write_u8(
392-
_RH_RF95_REG_1D_MODEM_CONFIG1,
393-
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0x0f) | (bwid << 4)
394-
)
395-
# Set coding rate (set to 5 to match RadioHead Cr45).
396-
self._write_u8(
397-
_RH_RF95_REG_1D_MODEM_CONFIG1,
398-
(
399-
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0xf1) |
400-
((min(max(coding_rate, 5), 8) - 4) << 1)
401-
)
402-
)
403-
# Set spreading factor (set to 7 to match RadioHead Sf128).
404-
spreading_factor = min(max(spreading_factor, 6), 12)
405-
self._write_u8(
406-
_RH_RF95_DETECTION_OPTIMIZE, 0xc5 if spreading_factor == 6 else 0xc3
407-
)
408-
self._write_u8(
409-
_RH_RF95_DETECTION_THRESHOLD, 0x0c if spreading_factor == 6 else 0x0a
410-
)
411-
self._write_u8(
412-
_RH_RF95_REG_1E_MODEM_CONFIG2,
413-
(
414-
(self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0x0f) |
415-
((spreading_factor << 4) & 0xf0)
416-
)
417-
)
418-
# Optionally enable CRC checking on incoming packets.
419-
if enable_crc:
420-
self._write_u8(
421-
_RH_RF95_REG_1E_MODEM_CONFIG2,
422-
self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) | 0x04
423-
)
424-
else:
425-
self._write_u8(
426-
_RH_RF95_REG_1E_MODEM_CONFIG2,
427-
self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0xfb
428-
)
429-
self.enable_crc = enable_crc
384+
self.signal_bandwidth = 125000
385+
self.coding_rate = 5
386+
self.spreading_factor = 7
387+
# Default to disable CRC checking on incoming packets.
388+
self.enable_crc = False
430389
# Note no sync word is set for LoRa mode either!
431390
self._write_u8(_RH_RF95_REG_26_MODEM_CONFIG3, 0x00) # Preamble lsb?
432391
# Set preamble length (default 8 bytes to match radiohead).
@@ -590,6 +549,99 @@ def rssi(self):
590549
# Remember in LoRa mode the payload register changes function to RSSI!
591550
return self._read_u8(_RH_RF95_REG_1A_PKT_RSSI_VALUE) - 137
592551

552+
@property
553+
def signal_bandwidth(self):
554+
"""The signal bandwidth used by the radio (try setting to a higher
555+
value to increase throughput or to a lower value to increase the
556+
likelihood of successfully received payloads). Valid values are
557+
listed in RFM9x.bw_bins."""
558+
bw_id = (self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0xf0) >> 4
559+
if bw_id >= len(self.bw_bins):
560+
return 500000
561+
else:
562+
return self.bw_bins[bw_id]
563+
564+
@signal_bandwidth.setter
565+
def signal_bandwidth(self, val):
566+
# Set signal bandwidth (set to 125000 to match RadioHead Bw125).
567+
for bw_id, cutoff in enumerate(self.bw_bins):
568+
if val <= cutoff:
569+
break
570+
else:
571+
bw_id = 9
572+
self._write_u8(
573+
_RH_RF95_REG_1D_MODEM_CONFIG1,
574+
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0x0f) | (bw_id << 4)
575+
)
576+
577+
@property
578+
def coding_rate(self):
579+
"""The coding rate used by the radio to control forward error
580+
correction (try setting to a higher value to increase tolerance of
581+
short bursts of interference or to a lower value to increase bit
582+
rate). Valid values are limited to 5, 6, 7, or 8."""
583+
cr_id = (self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0x0e) >> 1
584+
denominator = cr_id + 4
585+
return denominator
586+
587+
@coding_rate.setter
588+
def coding_rate(self, val):
589+
# Set coding rate (set to 5 to match RadioHead Cr45).
590+
denominator = min(max(val, 5), 8)
591+
cr_id = denominator - 4
592+
self._write_u8(
593+
_RH_RF95_REG_1D_MODEM_CONFIG1,
594+
(self._read_u8(_RH_RF95_REG_1D_MODEM_CONFIG1) & 0xf1) | (cr_id << 1)
595+
)
596+
597+
@property
598+
def spreading_factor(self):
599+
"""The spreading factor used by the radio (try setting to a higher
600+
value to increase the receiver's ability to distinguish signal from
601+
noise or to a lower value to increase the data transmission rate).
602+
Valid values are limited to 6, 7, 8, 9, 10, 11, or 12."""
603+
sf_id = (self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0xf0) >> 4
604+
return sf_id
605+
606+
@spreading_factor.setter
607+
def spreading_factor(self, val):
608+
# Set spreading factor (set to 7 to match RadioHead Sf128).
609+
val = min(max(val, 6), 12)
610+
self._write_u8(
611+
_RH_RF95_DETECTION_OPTIMIZE, 0xc5 if val == 6 else 0xc3
612+
)
613+
self._write_u8(
614+
_RH_RF95_DETECTION_THRESHOLD, 0x0c if val == 6 else 0x0a
615+
)
616+
self._write_u8(
617+
_RH_RF95_REG_1E_MODEM_CONFIG2,
618+
(
619+
(self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0x0f) |
620+
((val << 4) & 0xf0)
621+
)
622+
)
623+
624+
@property
625+
def enable_crc(self):
626+
"""Set to True to enable hardware CRC checking of incoming packets.
627+
Incoming packets that fail the CRC check are not processed. Set to
628+
False to disable CRC checking and process all incoming packets."""
629+
return (self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0x04) == 0x04
630+
631+
@enable_crc.setter
632+
def enable_crc(self, val):
633+
# Optionally enable CRC checking on incoming packets.
634+
if val:
635+
self._write_u8(
636+
_RH_RF95_REG_1E_MODEM_CONFIG2,
637+
self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) | 0x04
638+
)
639+
else:
640+
self._write_u8(
641+
_RH_RF95_REG_1E_MODEM_CONFIG2,
642+
self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0xfb
643+
)
644+
593645
def send(self, data, timeout=2.,
594646
tx_header=(_RH_BROADCAST_ADDRESS, _RH_BROADCAST_ADDRESS, 0, 0)):
595647
"""Send a string of data using the transmitter.

0 commit comments

Comments
 (0)