-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
3e64211
c681be6
2c0bfc2
af447e4
873ff8c
85c6655
e5bd457
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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() | ||
# 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. | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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, | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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: | ||
|
@@ -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"!", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 "!" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the original |
||
destination=packet[1], | ||
node=packet[0], | ||
identifier=packet[2], | ||
|
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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!