126
126
# is a complex chip which requires exposing many attributes and state. Disable
127
127
# the warning to work around the error.
128
128
# pylint: disable=too-many-instance-attributes
129
+ # pylint: disable=too-many-statements
129
130
130
131
131
132
class RFM9x :
@@ -145,7 +146,7 @@ class RFM9x:
145
146
is True for high power.
146
147
- baudrate: Baud rate of the SPI connection, default is 10mhz but you might
147
148
choose to lower to 1mhz if using long wires or a breadboard.
148
-
149
+ - agc: Boolean to Enable/Disable Automatic Gain Control - Default=False (AGC off)
149
150
Remember this library makes a best effort at receiving packets with pure
150
151
Python code. Trying to receive packets too quickly will result in lost data
151
152
so limit yourself to simple scenarios of sending and receiving single
@@ -225,6 +226,18 @@ def __set__(self, obj, val):
225
226
226
227
dio0_mapping = _RegisterBits (_RH_RF95_REG_40_DIO_MAPPING1 , offset = 6 , bits = 2 )
227
228
229
+ auto_agc = _RegisterBits (_RH_RF95_REG_26_MODEM_CONFIG3 , offset = 2 , bits = 1 )
230
+
231
+ low_datarate_optimize = _RegisterBits (
232
+ _RH_RF95_REG_26_MODEM_CONFIG3 , offset = 3 , bits = 1
233
+ )
234
+
235
+ lna_boost_hf = _RegisterBits (_RH_RF95_REG_0C_LNA , offset = 0 , bits = 2 )
236
+
237
+ auto_ifon = _RegisterBits (_RH_RF95_DETECTION_OPTIMIZE , offset = 7 , bits = 1 )
238
+
239
+ detection_optimize = _RegisterBits (_RH_RF95_DETECTION_OPTIMIZE , offset = 0 , bits = 3 )
240
+
228
241
bw_bins = (7800 , 10400 , 15600 , 20800 , 31250 , 41700 , 62500 , 125000 , 250000 )
229
242
230
243
def __init__ (
@@ -236,7 +249,8 @@ def __init__(
236
249
* ,
237
250
preamble_length = 8 ,
238
251
high_power = True ,
239
- baudrate = 5000000
252
+ baudrate = 5000000 ,
253
+ agc = False
240
254
):
241
255
self .high_power = high_power
242
256
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
@@ -281,14 +295,15 @@ def __init__(
281
295
self .spreading_factor = 7
282
296
# Default to disable CRC checking on incoming packets.
283
297
self .enable_crc = False
284
- # Note no sync word is set for LoRa mode either!
285
- self ._write_u8 (_RH_RF95_REG_26_MODEM_CONFIG3 , 0x00 ) # Preamble lsb?
298
+ # set AGC - Default = True
299
+ self .auto_agc = agc
300
+ """Automatic Gain Control state"""
286
301
# Set transmit power to 13 dBm, a safe value any module supports.
287
302
self .tx_power = 13
288
303
# initialize last RSSI reading
289
304
self .last_rssi = 0.0
290
305
"""The RSSI of the last received packet. Stored when the packet was received.
291
- This instantaneous RSSI value may not be accurate once the
306
+ The instantaneous RSSI value may not be accurate once the
292
307
operating mode has been changed.
293
308
"""
294
309
# initialize timeouts and delays delays
@@ -492,7 +507,12 @@ def rssi(self):
492
507
"""The received strength indicator (in dBm) of the last received message."""
493
508
# Read RSSI register and convert to value using formula in datasheet.
494
509
# Remember in LoRa mode the payload register changes function to RSSI!
495
- return self ._read_u8 (_RH_RF95_REG_1A_PKT_RSSI_VALUE ) - 137
510
+ raw_rssi = self ._read_u8 (_RH_RF95_REG_1A_PKT_RSSI_VALUE )
511
+ if self .low_frequency_mode :
512
+ raw_rssi -= 157
513
+ else :
514
+ raw_rssi -= 164
515
+ return raw_rssi
496
516
497
517
@property
498
518
def signal_bandwidth (self ):
@@ -519,6 +539,28 @@ def signal_bandwidth(self, val):
519
539
_RH_RF95_REG_1D_MODEM_CONFIG1 ,
520
540
(self ._read_u8 (_RH_RF95_REG_1D_MODEM_CONFIG1 ) & 0x0F ) | (bw_id << 4 ),
521
541
)
542
+ if val >= 500000 :
543
+ # see Semtech SX1276 errata note 2.3
544
+ self .auto_ifon = True
545
+ # see Semtech SX1276 errata note 2.1
546
+ if self .low_frequency_mode :
547
+ self ._write_u8 (0x36 , 0x02 )
548
+ self ._write_u8 (0x3A , 0x7F )
549
+ else :
550
+ self ._write_u8 (0x36 , 0x02 )
551
+ self ._write_u8 (0x3A , 0x64 )
552
+ else :
553
+ # see Semtech SX1276 errata note 2.3
554
+ self .auto_ifon = False
555
+ self ._write_u8 (0x36 , 0x03 )
556
+ if val == 7800 :
557
+ self ._write_u8 (0x2F , 0x48 )
558
+ elif val >= 62500 :
559
+ # see Semtech SX1276 errata note 2.3
560
+ self ._write_u8 (0x2F , 0x40 )
561
+ else :
562
+ self ._write_u8 (0x2F , 0x44 )
563
+ self ._write_u8 (0x30 , 0 )
522
564
523
565
@property
524
566
def coding_rate (self ):
@@ -553,7 +595,12 @@ def spreading_factor(self):
553
595
def spreading_factor (self , val ):
554
596
# Set spreading factor (set to 7 to match RadioHead Sf128).
555
597
val = min (max (val , 6 ), 12 )
556
- self ._write_u8 (_RH_RF95_DETECTION_OPTIMIZE , 0xC5 if val == 6 else 0xC3 )
598
+
599
+ if val == 6 :
600
+ self .detection_optimize = 0x5
601
+ else :
602
+ self .detection_optimize = 0x3
603
+
557
604
self ._write_u8 (_RH_RF95_DETECTION_THRESHOLD , 0x0C if val == 6 else 0x0A )
558
605
self ._write_u8 (
559
606
_RH_RF95_REG_1E_MODEM_CONFIG2 ,
0 commit comments