Skip to content

Commit fc04167

Browse files
authored
Merge pull request #26 from terriko/error_improvement
adafruit_rfm9x.py: improve error messages (fixes #25 and #23)
2 parents 2dbb850 + 322fd74 commit fc04167

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

adafruit_rfm9x.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,17 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
362362
self.reset()
363363
# No device type check! Catch an error from the very first request and
364364
# throw a nicer message to indicate possible wiring problems.
365-
try:
366-
# Set sleep mode, wait 10s and confirm in sleep mode (basic device check).
367-
# Also set long range mode (LoRa mode) as it can only be done in sleep.
368-
self.sleep()
369-
time.sleep(0.01)
370-
self.long_range_mode = True
371-
if self.operation_mode != SLEEP_MODE or not self.long_range_mode:
372-
raise RuntimeError('Failed to configure radio for LoRa mode, check wiring!')
373-
except OSError:
374-
raise RuntimeError('Failed to communicate with radio, check wiring!')
365+
version = self._read_u8(_RH_RF95_REG_42_VERSION)
366+
if version != 18:
367+
raise RuntimeError('Failed to find rfm9x with expected version -- check wiring')
368+
369+
# Set sleep mode, wait 10s and confirm in sleep mode (basic device check).
370+
# Also set long range mode (LoRa mode) as it can only be done in sleep.
371+
self.sleep()
372+
time.sleep(0.01)
373+
self.long_range_mode = True
374+
if self.operation_mode != SLEEP_MODE or not self.long_range_mode:
375+
raise RuntimeError('Failed to configure radio for LoRa mode, check wiring!')
375376
# clear default setting for access to LF registers if frequency > 525MHz
376377
if frequency > 525:
377378
self.low_frequency_mode = 0
@@ -496,7 +497,8 @@ def frequency_mhz(self):
496497

497498
@frequency_mhz.setter
498499
def frequency_mhz(self, val):
499-
assert 240 <= val <= 960
500+
if val < 240 or val > 960:
501+
raise RuntimeError('frequency_mhz must be between 240 and 960')
500502
# Calculate FRF register 24-bit value.
501503
frf = int((val * 1000000.0) / _RH_RF95_FSTEP) & 0xFFFFFF
502504
# Extract byte values and update registers.
@@ -526,7 +528,8 @@ def tx_power(self):
526528
def tx_power(self, val):
527529
val = int(val)
528530
if self.high_power:
529-
assert 5 <= val <= 23
531+
if val < 5 or val > 23:
532+
raise RuntimeError('tx_power must be between 5 and 23')
530533
# Enable power amp DAC if power is above 20 dB.
531534
# Lower setting by 3db when PA_BOOST enabled - see Data Sheet Section 6.4
532535
if val > 20:

0 commit comments

Comments
 (0)