Skip to content

implement a few size reduction changes #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 9, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions adafruit_rfm69.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,9 @@ def __set__(self, obj, val):
crc_auto_clear_off = _RegisterBits(_REG_PACKET_CONFIG1, offset=3, bits=1)
address_filter = _RegisterBits(_REG_PACKET_CONFIG1, offset=1, bits=2)
mode_ready = _RegisterBits(_REG_IRQ_FLAGS1, offset=7)
rx_ready = _RegisterBits(_REG_IRQ_FLAGS1, offset=6)
tx_ready = _RegisterBits(_REG_IRQ_FLAGS1, offset=5)
dio_0_mapping = _RegisterBits(_REG_DIO_MAPPING1, offset=6, bits=2)
packet_sent = _RegisterBits(_REG_IRQ_FLAGS2, offset=3)
payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2)

# pylint: disable=too-many-statements
def __init__(
self,
spi,
Expand Down Expand Up @@ -300,8 +297,26 @@ def __init__(
self.preamble_length = preamble_length # Set the preamble length.
self.frequency_mhz = frequency # Set frequency.
self.encryption_key = encryption_key # Set encryption key.
# set radio configuration parameters
self._configure_radio()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason you removed configure_radio?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

putting the code inline rather than as a function call saved 80 bytes!

# Configure modulation for RadioHead library GFSK_Rb250Fd250 mode
# by default. Users with advanced knowledge can manually reconfigure
# for any other mode (consulting the datasheet is absolutely
# necessary!).
self.data_mode = 0b00 # Packet mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed a few register initializations that were setting the registers to their default values.

This got me thinking - are any of these values default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data_mode and modulation_type are the defaults -- I'll remove them
saved 12 bytes!

self.modulation_type = 0b00 # FSK modulation
self.modulation_shaping = 0b01 # Gaussian filter, BT=1.0
self.bitrate = 250000 # 250kbs
self.frequency_deviation = 250000 # 250khz
self.rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
self.rx_bw_mantissa = 0b00
self.rx_bw_exponent = 0b000
self.afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
self.afc_bw_mantissa = 0b00
self.afc_bw_exponent = 0b000
self.packet_format = 1 # Variable length.
self.dc_free = 0b10 # Whitening
# Set transmit power to 13 dBm, a safe value any module supports.
self.tx_power = 13

# initialize last RSSI reading
self.last_rssi = 0.0
"""The RSSI of the last received packet. Stored when the packet was received.
Expand Down Expand Up @@ -354,29 +369,7 @@ def __init__(
Fourth byte of the RadioHead header.
"""

def _configure_radio(self):
# Configure modulation for RadioHead library GFSK_Rb250Fd250 mode
# by default. Users with advanced knowledge can manually reconfigure
# for any other mode (consulting the datasheet is absolutely
# necessary!).
self.data_mode = 0b00 # Packet mode
self.modulation_type = 0b00 # FSK modulation
self.modulation_shaping = 0b01 # Gaussian filter, BT=1.0
self.bitrate = 250000 # 250kbs
self.frequency_deviation = 250000 # 250khz
self.rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
self.rx_bw_mantissa = 0b00
self.rx_bw_exponent = 0b000
self.afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
self.afc_bw_mantissa = 0b00
self.afc_bw_exponent = 0b000
self.packet_format = 1 # Variable length.
self.dc_free = 0b10 # Whitening
self.crc_on = 1 # CRC enabled
self.crc_auto_clear = 0 # Clear FIFO on CRC fail
self.address_filtering = 0b00 # No address filtering
# Set transmit power to 13 dBm, a safe value any module supports.
self.tx_power = 13
# pylint: enable=too-many-statements

# pylint: disable=no-member
# Reconsider this disable when it can be tested.
Expand Down Expand Up @@ -722,6 +715,14 @@ def frequency_deviation(self, val):
self._write_u8(_REG_FDEV_MSB, fdev >> 8)
self._write_u8(_REG_FDEV_LSB, fdev & 0xFF)

def packet_sent(self):
"""Transmit status"""
return (self._read_u8(_REG_IRQ_FLAGS2) & 0x8) >> 3

def payload_ready(self):
"""Receive status"""
return (self._read_u8(_REG_IRQ_FLAGS2) & 0x4) >> 2

def send(
self,
data,
Expand Down Expand Up @@ -781,7 +782,7 @@ def send(
# best that can be done right now without interrupts).
start = time.monotonic()
timed_out = False
while not timed_out and not self.packet_sent:
while not timed_out and not self.packet_sent():
if (time.monotonic() - start) >= self.xmit_timeout:
timed_out = True
# Listen again if requested.
Expand Down Expand Up @@ -858,7 +859,7 @@ def receive(
self.listen()
start = time.monotonic()
timed_out = False
while not timed_out and not self.payload_ready:
while not timed_out and not self.payload_ready():
if (time.monotonic() - start) >= timeout:
timed_out = True
# Payload ready is set, a packet is in the FIFO.
Expand Down Expand Up @@ -893,10 +894,9 @@ def receive(
# delay before sending Ack to give receiver a chance to get ready
if self.ack_delay is not None:
time.sleep(self.ack_delay)
# send ACK packet to sender
data = bytes("!", "UTF-8")
# send ACK packet to sender (data is b'!')
self.send(
data,
b"!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is b"!", being sent instead of the data?

Copy link
Contributor Author

@jerryneedell jerryneedell Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just seemed simpler to do it this way. The ACK packet is always just "!"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the original bytes("!", "UTF-8") stores two strings plus the byte codes to convert them to a bytes object. data = b"!"will save those two strings and only cost the assign byte code assuming the namedata` is used elsewhere.

destination=packet[1],
node=packet[0],
identifier=packet[2],
Expand Down