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
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ To install in a virtual environment in your current project:
Usage Example
=============
See examples/rfm69_simpletest.py for a simple demo of the usage.
Note: the default baudrate for the SPI is 5000000 (5MHz).
Note: the default baudrate for the SPI is 2000000 (2MHz).
The maximum setting is 10Mhz but
transmission errors have been observed expecially when using breakout boards.
For breakout boards or other configurations where the boards are separated,
Expand Down
105 changes: 46 additions & 59 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 All @@ -270,7 +267,7 @@ def __init__(
preamble_length=4,
encryption_key=None,
high_power=True,
baudrate=5000000
baudrate=2000000
):
self._tx_power = 13
self.high_power = high_power
Expand Down Expand Up @@ -300,8 +297,24 @@ 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.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 +367,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 @@ -409,19 +400,6 @@ def _write_from(self, address, buf, length=None):
device.write(self._BUFFER, end=1)
device.write(buf, end=length) # send data

def _write_fifo_from(self, buf, length=None):
# Write a number of bytes to the transmit FIFO and taken from the
# provided buffer. If no length is specified (the default) the entire
# buffer is written.
if length is None:
length = len(buf)
with self._device as device:
self._BUFFER[0] = (_REG_FIFO | 0x80) & 0xFF # Set top bit to 1 to
# indicate a write.
self._BUFFER[1] = length & 0xFF # Set packt length
device.write(self._BUFFER, end=2) # send address and lenght)
device.write(buf, end=length) # send data

def _write_u8(self, address, val):
# Write a byte register to the chip. Specify the 7-bit address and the
# 8-bit value to write to that address.
Expand Down Expand Up @@ -722,6 +700,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 @@ -755,33 +741,34 @@ def send(
self.idle() # Stop receiving to clear FIFO and keep it clear.
# Fill the FIFO with a packet to send.
# Combine header and data to form payload
payload = bytearray(4)
payload = bytearray(5)
payload[0] = 4 + len(data)
if destination is None: # use attribute
payload[0] = self.destination
payload[1] = self.destination
else: # use kwarg
payload[0] = destination
payload[1] = destination
if node is None: # use attribute
payload[1] = self.node
payload[2] = self.node
else: # use kwarg
payload[1] = node
payload[2] = node
if identifier is None: # use attribute
payload[2] = self.identifier
payload[3] = self.identifier
else: # use kwarg
payload[2] = identifier
payload[3] = identifier
if flags is None: # use attribute
payload[3] = self.flags
payload[4] = self.flags
else: # use kwarg
payload[3] = flags
payload[4] = flags
payload = payload + data
# Write payload to transmit fifo
self._write_fifo_from(payload)
self._write_from(_REG_FIFO, payload)
# Turn on transmit mode to send out the packet.
self.transmit()
# Wait for packet sent interrupt with explicit polling (not ideal but
# 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 +845,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 All @@ -874,7 +861,8 @@ def receive(
# RadioHead header and at least one byte of data --reject this packet and ignore it.
if fifo_length > 0: # read and clear the FIFO if anything in it
packet = bytearray(fifo_length)
self._read_into(_REG_FIFO, packet)
self._read_into(_REG_FIFO, packet, fifo_length)

if fifo_length < 5:
packet = None
else:
Expand All @@ -893,10 +881,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