Skip to content

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

Merged
merged 3 commits into from
Dec 13, 2024

Conversation

jerryneedell
Copy link
Collaborator

fixes #5
fixes #4

I was not able to reproduce the OP issue for #5 but the fix appears to have worked for the OP and is better coding practice.
Tested with rfm69 and rfm9x bonnets on Raspberry Pi.

@jerryneedell jerryneedell requested a review from a team December 5, 2024 17:26
Copy link
Member

@tekktrik tekktrik left a comment

Choose a reason for hiding this comment

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

Thanks! A single comment/question below (though it applies for all three locations changed).

@@ -521,6 +521,7 @@ def read_fifo(self) -> 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
Copy link
Member

Choose a reason for hiding this comment

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

If this can return None, then the type annotation should specify Optional[bytearray]. Is it better to return an empty bytearray? Not entirely sure what the utility of each case is and which would be better.

Copy link

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If this can return None, then the type annotation should specify Optional[bytearray]. Is it better to return an empty bytearray? Not entirely sure what the utility of each case is and which would be better.

Good catch... I want to think about this a bit -- the calling code has a confusing check:

                    if len(packet) < 5:
                        # reject the packet if it is too small to contain the RadioHead Header
                        packet = None

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 packet is not None before checking len(packet). This way read_fifo will be consistent with receive in returning the Optional[bytearray]. Does that make sense?

Copy link
Collaborator Author

@jerryneedell jerryneedell Dec 6, 2024

Choose a reason for hiding this comment

The 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.

@@ -567,6 +567,7 @@ def read_fifo(self) -> 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
Copy link
Member

Choose a reason for hiding this comment

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

See my previous comment re: returning None.

@@ -643,6 +643,7 @@ def read_fifo(self) -> bytearray:
"""Read the packet from the FIFO."""
# Read the length of the FIFO.
fifo_length = self.read_u8(_RF69_REG_00_FIFO)
packet = None # return None if FIFO empty
Copy link
Member

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.

@jerryneedell jerryneedell requested a review from tekktrik December 6, 2024 19:26
@jerryneedell
Copy link
Collaborator Author

@tekktrik When you have a chance would you take a look at my attempt to resolve this. Let me know if it looks OK.

@jerryneedell jerryneedell requested a review from a team December 12, 2024 17:51
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

This looks good to me. I tested simpletest and transmit examples successfully with lora featherwings and a feather RP2040 + a pygamer.

@FoamyGuy FoamyGuy merged commit 903e665 into adafruit:main Dec 13, 2024
1 check passed
@jerryneedell jerryneedell deleted the jerryn_read_fifo branch December 13, 2024 19:06
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Dec 14, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM to 1.0.3 from 1.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM#6 from jerryneedell/jerryn_read_fifo
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM#3 from FoamyGuy/docs_container_version_fix

Updating https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation to 2.10.0 from 2.9.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#121 from FoamyGuy/rainbow_period_property

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

local variable reference before assignment in rfm69.py Incorrect Documentation of Baudrate
4 participants