Adafruit_CircuitPython_PCA9685 icon indicating copy to clipboard operation
Adafruit_CircuitPython_PCA9685 copied to clipboard

Friendly error message to catch and avoid ZeroDivisionError

Open kevinjwalters opened this issue 5 years ago • 7 comments

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):

I'm finding some of the code in the PCA9685 class a bit puzzling but I'll put another issue in for that.

kevinjwalters avatar Jun 14 '20 12:06 kevinjwalters

Hello.

I want to try this issue. I have a draft of the solution here but I need a few clarifications:

  1. How can I test my solution is working.
  2. 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 avatar Oct 02 '20 13:10 razinc

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

tannewt avatar Oct 02 '20 22:10 tannewt

Hello @tannewt.

Sorry. I don't have any hardware I can test it on. Is there any alternative?

Thanks.

razinc avatar Oct 03 '20 00:10 razinc

@makermelissa does this sound like a Blinka issue to you? Seems like I2C should raise an exception rather than returning zero.

tannewt avatar Oct 05 '20 19:10 tannewt

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

makermelissa avatar Oct 05 '20 20:10 makermelissa

Hi @tannewt, @makermelissa,

Please let me know if my code needs modifications.

Thanks.

razinc avatar Oct 06 '20 00:10 razinc

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

tannewt avatar Oct 06 '20 23:10 tannewt