Skip to content

Commit

Permalink
Merge pull request #44 from jerryneedell/jerryn_size
Browse files Browse the repository at this point in the history
implemented a few code size reductions
  • Loading branch information
dhalbert authored Feb 27, 2023
2 parents 261c917 + 743a1e9 commit af1cba8
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions adafruit_rfm69.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@
# the warning to work around the error.
# pylint: disable=too-many-instance-attributes

# disable another pylint nit-pick
# pylint: disable=too-many-public-methods


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

def set_boost(self, setting: int) -> None:
"""Set preamp boost if needed."""
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, setting)
self._write_u8(_REG_TEST_PA2, setting)

def idle(self) -> None:
"""Enter idle standby mode (switching off high power amplifiers if necessary)."""
# Like RadioHead library, turn off high power boost if enabled.
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
self.set_boost(_TEST_PA1_NORMAL)
self.operation_mode = STANDBY_MODE

def sleep(self) -> None:
Expand All @@ -464,9 +469,7 @@ def listen(self) -> None:
and retrieve packets as they're available.
"""
# Like RadioHead library, turn off high power boost if enabled.
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_NORMAL)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_NORMAL)
self.set_boost(_TEST_PA1_NORMAL)
# Enable payload ready interrupt for D0 line.
self.dio_0_mapping = 0b01
# Enter RX mode (will clear FIFO!).
Expand All @@ -478,9 +481,7 @@ def transmit(self) -> None:
:py:func:`send` instead.
"""
# Like RadioHead library, turn on high power boost if enabled.
if self._tx_power >= 18:
self._write_u8(_REG_TEST_PA1, _TEST_PA1_BOOST)
self._write_u8(_REG_TEST_PA2, _TEST_PA2_BOOST)
self.set_boost(_TEST_PA1_BOOST)
# Enable packet sent interrupt for D0 line.
self.dio_0_mapping = 0b00
# Enter TX mode (will clear FIFO!).
Expand Down Expand Up @@ -645,42 +646,39 @@ def tx_power(self) -> int:
pa0 = self.pa_0_on
pa1 = self.pa_1_on
pa2 = self.pa_2_on
current_output_power = self.output_power
if pa0 and not pa1 and not pa2:
# -18 to 13 dBm range
return -18 + self.output_power
return -18 + current_output_power
if not pa0 and pa1 and not pa2:
# -2 to 13 dBm range
return -18 + self.output_power
return -18 + current_output_power
if not pa0 and pa1 and pa2 and not self.high_power:
# 2 to 17 dBm range
return -14 + self.output_power
return -14 + current_output_power
if not pa0 and pa1 and pa2 and self.high_power:
# 5 to 20 dBm range
return -11 + self.output_power
raise RuntimeError("Power amplifiers in unknown state!")
return -11 + current_output_power
raise RuntimeError("Power amps state unknown!")

@tx_power.setter
def tx_power(self, val: float):
val = int(val)
# Determine power amplifier and output power values depending on
# high power state and requested power.
pa_0_on = 0
pa_1_on = 0
pa_2_on = 0
pa_0_on = pa_1_on = pa_2_on = 0
output_power = 0
if self.high_power:
# Handle high power mode.
assert -2 <= val <= 20
pa_1_on = 1
if val <= 13:
pa_1_on = 1
output_power = val + 18
elif 13 < val <= 17:
pa_1_on = 1
pa_2_on = 1
output_power = val + 14
else: # power >= 18 dBm
# Note this also needs PA boost enabled separately!
pa_1_on = 1
pa_2_on = 1
output_power = val + 11
else:
Expand Down

0 comments on commit af1cba8

Please sign in to comment.