Skip to content

Sensor Overflow Logic fix #6

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

Merged
merged 7 commits into from
Aug 13, 2018
15 changes: 13 additions & 2 deletions adafruit_tsl2591.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
_TSL2591_LUX_COEFB = 1.64
_TSL2591_LUX_COEFC = 0.59
_TSL2591_LUX_COEFD = 0.86
_TSL2591_MAX_COUNT_100MS = const(36863)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite this number as 0x8fff to match the one below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any aversion to writing the one below to 65535 to reflect what is in the datasheet. This way one can search for 36863 and find it in the datasheet as well as 65535, whereas the hex values are nowhere to be found in the datasheet.

See Control Register (0x01) in the TSL2591 datasheet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about writing one as a comment and one as a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the comments in for the hex representation as well

_TSL2591_MAX_COUNT = const(0xFFFF)

# User-facing constants:
GAIN_LOW = 0x00 # low gain (1x)
Expand Down Expand Up @@ -248,12 +250,21 @@ def lux(self):
and visible light channels.
"""
channel_0, channel_1 = self.raw_luminosity

# Compute the atime in milliseconds
atime = 100.0 * self._integration_time + 100.0

# Set the maximum sensor counts based on the atime setting
if atime == 100:
max_counts = _TSL2591_MAX_COUNT_100MS
else:
max_counts = _TSL2591_MAX_COUNT

# Handle overflow.
if channel_0 == 0xFFFF or channel_1 == 0xFFFF:
if channel_0 >= max_counts or channel_1 >= max_counts:
raise RuntimeError('Overflow reading light channels!')
# Calculate lux using same equation as Arduino library:
# https://github.com/adafruit/Adafruit_TSL2591_Library/blob/master/Adafruit_TSL2591.cpp
atime = 100.0 * self._integration_time + 100.0
again = 1.0
if self._gain == GAIN_MED:
again = 25.0
Expand Down