-
Notifications
You must be signed in to change notification settings - Fork 6
fix read_fifo to always define packet before return, Correct typo in dosctring #6
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
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
from circuitpython_typing import ReadableBuffer | ||
|
||
try: | ||
from typing import Literal | ||
from typing import Literal, Optional | ||
except ImportError: | ||
from typing_extensions import Literal | ||
|
||
|
@@ -131,7 +131,7 @@ class RFM9x(RFMSPI): | |
- preamble_length: The length in bytes of the packet preamble (default 8). | ||
- high_power: Boolean to indicate a high power board (RFM95, etc.). Default | ||
is True for high power. | ||
- baudrate: Baud rate of the SPI connection, default is 10mhz but you might | ||
- baudrate: Baud rate of the SPI connection, default is 5mhz but you might | ||
choose to lower to 1mhz if using long wires or a breadboard. | ||
- agc: Boolean to Enable/Disable Automatic Gain Control - Default=False (AGC off) | ||
- crc: Boolean to Enable/Disable Cyclic Redundancy Check - Default=True (CRC Enabled) | ||
|
@@ -517,10 +517,11 @@ def fill_fifo(self, payload: ReadableBuffer) -> None: | |
# Write payload and header length. | ||
self.write_u8(_RF95_REG_22_PAYLOAD_LENGTH, len(payload)) | ||
|
||
def read_fifo(self) -> bytearray: | ||
def read_fifo(self) -> Optional[bytearray]: | ||
"""Read the data from the FIFO.""" | ||
# Read the length of the FIFO. | ||
fifo_length = self.read_u8(_RF95_REG_13_RX_NB_BYTES) | ||
packet = None # return None if FIFO empty | ||
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. If this can return 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. This might be important since the function that calls read_fifo checks len(packet). Please confirm. But this will probably not work on None. In that case bytearray(0) is probably the correct solution. I will change my test code to set packet = bytearray(0). Unless I report back assume it's working that way. 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. And we're still left with the mystery about how read_fifo could be called with nothing in the FIFO - which was happening to me the other day. I can't tell if that's happening anymore with my rig. Nonetheless, I think this change and those in the other two files is best practice. 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.
Good catch... I want to think about this a bit -- the calling code has a confusing check:
which will clearly now fail if read_fifo returns None..... I think it best to use your original suggestion to make the return type annotation Optional[bytearray] and refactor the code to always check for 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. I changed it to Optional[bytearray] in the 3 files and in rfm_common.py changed code to avoid testing len(packet) if packet is None. Let me know what you think. |
||
if fifo_length > 0: # read and clear the FIFO if anything in it | ||
packet = bytearray(fifo_length) | ||
current_addr = self.read_u8(_RF95_REG_10_FIFO_RX_CURRENT_ADDR) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -563,10 +563,11 @@ def fill_fifo(self, payload: ReadableBuffer) -> None: | |
# Write payload to transmit fifo | ||
self.write_from(_RF95_REG_00_FIFO, complete_payload) | ||
|
||
def read_fifo(self) -> bytearray: | ||
def read_fifo(self) -> Optional[bytearray]: | ||
"""Read the data from the FIFO.""" | ||
# Read the length of the FIFO. | ||
fifo_length = self.read_u8(_RF95_REG_00_FIFO) | ||
packet = None # return None if FIFO empty | ||
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. See my previous comment re: returning |
||
if fifo_length > 0: # read and clear the FIFO if anything in it | ||
packet = bytearray(fifo_length) | ||
# read the packet | ||
|
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.
See my other comment re: returning None.