-
Notifications
You must be signed in to change notification settings - Fork 6
_compute_lux can fail with an unset variable error #10
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
Comments
I did some testing with this sensor and I found that if I mishandle it, (touch the pins), it goes into it's "disabled" state. I can bring it back by entering when disabled, it returns (0,0) for the tsl.luminosity reading but in one case, I got it to return ( 1, 0) which would result in the error message reported since ch0 !=0 but ch1=0 so ch1/ch0 = 0 and fails all the tests. Perhaps the driver needs to check for tsl.enabled == False and if so, re-enable it or throw an error.
|
I am finding the sensor to be very sensitive to handling - it will go disabled sometimes just by handling the jumper wires. I may not have great connections but it is interesting that it goes into this "disabled" state. As before, it recovers if re-enabled. Time to look at the data sheet! |
Most likely, I just have a loose VCC connection so it power cycles the sensor and it comes up "disabled". but this does suggest that some checks would be helpful and that it is possible to get a reading with CH1=0 but CH0 non-zero thus triggering the reported error. |
according to the data sheet, it powers up with the "power" bits in the control register set to 0 (i.e. disabled) enabling it just turns on the power bits and it starts operating with default settings. If a user has reconfigured the settings, a power cycle will lose the settings, so I think the driver should just throw an error or return None if it detects the sensor disabled rather than automatically re-enabling it. |
aha! I just got it to return (1,0) whithout actually disabling so I think the disable question is another matter.
|
triggered the error by covering the sensor with a box after the first reading. Removed the box for the third reading.
|
Nice work. This PR fixes it: ratio < 0 is not possible, values are unsigned. |
See this forum post: https://forums.adafruit.com/viewtopic.php?f=60&t=128396
The chain of
if
statements in_compute_lux()
can fail to setlux
ifratio
<= 0 or possibly if somehow a value slips between the comparisons (might depend on floating point issues).The extra comparisons are redundant and could be rewritten as:
I don't know if
ratio
can end up <= 0, but that case should be checked for as well, and eitherNone
returned or maybe an error thrown. (I don't know about this sensor or have one to test.)The text was updated successfully, but these errors were encountered: