Fix: Improve Pixy2 I2C robustness to prevent hangs on communication errors #32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem:
When using the Pixy2 Arduino library over I2C, particularly on platforms like Teensy with potentially long I2C cables or noisy environments, intermittent communication errors could cause the library's receiving functions to enter an infinite loop. This resulted in the entire microcontroller program hanging indefinitely, making the system unresponsive. This issue was observed when Wire.requestFrom returned 0 (indicating an I2C NACK, timeout, or other bus error) while the library was still expecting to receive more bytes.
Solution:
Robust Link2I2C::recv Method (Pixy2I2C.h):
Improved Error Checking in TPixy2.h:
Flexible I2C Bus Selection (Pixy2I2C.h, TPixy2.h):
Impact:
Testing:
This fix was developed in response to observed hangs in a real-world application (NXP Cup race car) using long I2C cables where communication errors are more likely. Testing in the application showed that these changes successfully prevent the system hang and allow the application to continue operating, logging the I2C errors as they occur. Further testing across different platforms and I2C scenarios is welcome.