Skip to content

Commit af1cba8

Browse files
authored
Merge pull request #44 from jerryneedell/jerryn_size
implemented a few code size reductions
2 parents 261c917 + 743a1e9 commit af1cba8

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

adafruit_rfm69.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
# the warning to work around the error.
142142
# pylint: disable=too-many-instance-attributes
143143

144+
# disable another pylint nit-pick
145+
# pylint: disable=too-many-public-methods
146+
144147

145148
def ticks_diff(ticks1: int, ticks2: int) -> int:
146149
"""Compute the signed difference between two ticks values
@@ -306,9 +309,7 @@ def __init__( # pylint: disable=invalid-name
306309
# Check the version of the chip.
307310
version = self._read_u8(_REG_VERSION)
308311
if version != 0x24:
309-
raise RuntimeError(
310-
"Failed to find RFM69 with expected version, check wiring!"
311-
)
312+
raise RuntimeError("Invalid RFM69 version, check wiring!")
312313
self.idle() # Enter idle state.
313314
# Setup the chip in a similar way to the RadioHead RFM69 library.
314315
# Set FIFO TX condition to not empty and the default FIFO threshold to 15.
@@ -447,12 +448,16 @@ def reset(self) -> None:
447448
self._reset.value = False
448449
time.sleep(0.005) # 5 ms
449450

451+
def set_boost(self, setting: int) -> None:
452+
"""Set preamp boost if needed."""
453+
if self._tx_power >= 18:
454+
self._write_u8(_REG_TEST_PA1, setting)
455+
self._write_u8(_REG_TEST_PA2, setting)
456+
450457
def idle(self) -> None:
451458
"""Enter idle standby mode (switching off high power amplifiers if necessary)."""
452459
# Like RadioHead library, turn off high power boost if enabled.
453-
if self._tx_power >= 18:
454-
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
455-
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
460+
self.set_boost(_TEST_PA1_NORMAL)
456461
self.operation_mode = STANDBY_MODE
457462

458463
def sleep(self) -> None:
@@ -464,9 +469,7 @@ def listen(self) -> None:
464469
and retrieve packets as they're available.
465470
"""
466471
# Like RadioHead library, turn off high power boost if enabled.
467-
if self._tx_power >= 18:
468-
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
469-
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
472+
self.set_boost(_TEST_PA1_NORMAL)
470473
# Enable payload ready interrupt for D0 line.
471474
self.dio_0_mapping = 0b01
472475
# Enter RX mode (will clear FIFO!).
@@ -478,9 +481,7 @@ def transmit(self) -> None:
478481
:py:func:`send` instead.
479482
"""
480483
# Like RadioHead library, turn on high power boost if enabled.
481-
if self._tx_power >= 18:
482-
self._write_u8(_REG_TEST_PA1, _TEST_PA1_BOOST)
483-
self._write_u8(_REG_TEST_PA2, _TEST_PA2_BOOST)
484+
self.set_boost(_TEST_PA1_BOOST)
484485
# Enable packet sent interrupt for D0 line.
485486
self.dio_0_mapping = 0b00
486487
# Enter TX mode (will clear FIFO!).
@@ -645,42 +646,39 @@ def tx_power(self) -> int:
645646
pa0 = self.pa_0_on
646647
pa1 = self.pa_1_on
647648
pa2 = self.pa_2_on
649+
current_output_power = self.output_power
648650
if pa0 and not pa1 and not pa2:
649651
# -18 to 13 dBm range
650-
return -18 + self.output_power
652+
return -18 + current_output_power
651653
if not pa0 and pa1 and not pa2:
652654
# -2 to 13 dBm range
653-
return -18 + self.output_power
655+
return -18 + current_output_power
654656
if not pa0 and pa1 and pa2 and not self.high_power:
655657
# 2 to 17 dBm range
656-
return -14 + self.output_power
658+
return -14 + current_output_power
657659
if not pa0 and pa1 and pa2 and self.high_power:
658660
# 5 to 20 dBm range
659-
return -11 + self.output_power
660-
raise RuntimeError("Power amplifiers in unknown state!")
661+
return -11 + current_output_power
662+
raise RuntimeError("Power amps state unknown!")
661663

662664
@tx_power.setter
663665
def tx_power(self, val: float):
664666
val = int(val)
665667
# Determine power amplifier and output power values depending on
666668
# high power state and requested power.
667-
pa_0_on = 0
668-
pa_1_on = 0
669-
pa_2_on = 0
669+
pa_0_on = pa_1_on = pa_2_on = 0
670670
output_power = 0
671671
if self.high_power:
672672
# Handle high power mode.
673673
assert -2 <= val <= 20
674+
pa_1_on = 1
674675
if val <= 13:
675-
pa_1_on = 1
676676
output_power = val + 18
677677
elif 13 < val <= 17:
678-
pa_1_on = 1
679678
pa_2_on = 1
680679
output_power = val + 14
681680
else: # power >= 18 dBm
682681
# Note this also needs PA boost enabled separately!
683-
pa_1_on = 1
684682
pa_2_on = 1
685683
output_power = val + 11
686684
else:

0 commit comments

Comments
 (0)