Adafruit_CircuitPython_PCA9685
Adafruit_CircuitPython_PCA9685 copied to clipboard
Friendly error message to catch and avoid ZeroDivisionError
This library can end up throwing a ZeroDivisionError in https://github.com/adafruit/Adafruit_CircuitPython_PCA9685/blob/65f0e2c96ab97a8723a10d3a08c773682371cbc2/adafruit_pca9685.py#L142-L145 - this is probably occuring around issues with i2c communications but I'm not really sure. This is a confusing error especially for programmers not used to reading stack traces who may think it's a division problem in their code.
Users who have bumped into this (and reported it):
- Adafruit Forums: RaspberryPi Stepper HAT - example code not working
- Adafruit Forums: Servo on Adafruit 16-channel PWM/Servo Hat ZeroDivisionError
- StackOverflow: Raspberry Pi - ZeroDivisionError: float division by zero - adafruit pca9685
I'm finding some of the code in the PCA9685 class a bit puzzling but I'll put another issue in for that.
Hello.
I want to try this issue. I have a draft of the solution here but I need a few clarifications:
- How can I test my solution is working.
- Should I print the error instead of returning it? The return value supposed to be float but in my case it will return a string. Will that cause any issue?
Thanks.
@razinc Do you have any hardware you can test with? Ideally we'd raise the exception that indicates an i2c error. It may be an issue in the lower levels of Blinka.
Hello @tannewt.
Sorry. I don't have any hardware I can test it on. Is there any alternative?
Thanks.
@makermelissa does this sound like a Blinka issue to you? Seems like I2C should raise an exception rather than returning zero.
@tannewt I think disallowing a zero value from any register at a Blinka level would likely produce unexpected results because it might be a valid value. I think adding it to this library is the correct approach.
Hi @tannewt, @makermelissa,
Please let me know if my code needs modifications.
Thanks.
@makermelissa I wasn't trying to suggest erroring on zero value. I was thinking this could be a problem where Blinka should detect and i2c error and raise an exception rather than returning zero.