API of grovepi.read_i2c_block is unsafe and chashes grovepi.ultrasonicRead
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 494
- Forks
- 472
- PR merge metrics
- No merged PRs in 30d
Description
read_i2c_block normally returns a list but in case of an IOError it returns -1 (int). This is extremely difficult to handle as one would had to test the return value of read_i2c_block.
One would have to use something like this:
number = read_i2c_block(address)
if type(number) is list:
do something_usefull()
else:
handle_error()
which is the same thing as catching the exception.
ultrasonicRead and all the other function I saw in grovepi.py use the function in unsafe manor:
def ultrasonicRead(pin):
write_i2c_block(address, uRead_cmd + [pin, unused, unused])
time.sleep(.2)
read_i2c_byte(address)
number = read_i2c_block(address)
return (number[1] * 256 + number[2])
Which leads to an TypeError and the program to crash as one is not expecting an TypeError.
This also happens in the wild:
http://www.dexterindustries.com/forum/?topic=ultrasonic-ranger-1-0-broken/
It would be much clearer to just raise the IOError as this is what is happening as to convert it in an TypeError.
I have not checked all the other i2c routines maybe there are more problems like this.
Edit: Typo
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in grovepi.py with read_i2c_block and ultrasonicRead, then inspect the other I2C routines that call it. Reproduce the IOError path and verify that callers receive the intended IOError rather than failing later with a TypeError. Done means the affected routines handle the error consistently and the ultrasonicRead crash is prevented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100