From e427332971436c5b71872b195bf2d4c37cfd64ee Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Thu, 9 Aug 2018 17:14:32 -0400 Subject: [PATCH 1/7] Modified for Issue #5 for the maxCounts to determine if the sensor overflowed --- adafruit_tsl2591.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index aba569d..187eac6 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -248,8 +248,15 @@ def lux(self): and visible light channels. """ channel_0, channel_1 = self.raw_luminosity + + # Set the maximum sensor counts based on the atime setting + if 100 == atime: + maxCounts = 36863 + else: + maxCounts = 0xFFFF + # Handle overflow. - if channel_0 == 0xFFFF or channel_1 == 0xFFFF: + if channel_0 == maxCounts or channel_1 == maxCounts: 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 From d9702069fdd050c0ec7a867e3d842764af788e50 Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Thu, 9 Aug 2018 17:21:47 -0400 Subject: [PATCH 2/7] Using consts to define the max counts and moved where atime gets calculated --- adafruit_tsl2591.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index 187eac6..d86edcf 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -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) +_TSL2591_MAX_COUNT = const(0xFFFF) # User-facing constants: GAIN_LOW = 0x00 # low gain (1x) @@ -249,18 +251,20 @@ def lux(self): """ 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 100 == atime: - maxCounts = 36863 + maxCounts = _TSL2591_MAX_COUNT_100MS else: - maxCounts = 0xFFFF + maxCounts = _TSL2591_MAX_COUNT # Handle overflow. if channel_0 == maxCounts or channel_1 == maxCounts: 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 From 9032c5fc8f124f43b7314e6cb700ed643d6a60ea Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Thu, 9 Aug 2018 17:22:39 -0400 Subject: [PATCH 3/7] Changed logic to >= to capture the case where the sensor reports slightly higher values when in the 100 ms atime setting --- adafruit_tsl2591.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index d86edcf..be9bc65 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -261,7 +261,7 @@ def lux(self): maxCounts = _TSL2591_MAX_COUNT # Handle overflow. - if channel_0 == maxCounts or channel_1 == maxCounts: + if channel_0 >= maxCounts or channel_1 >= maxCounts: 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 From 5e31d3b942d5ee311a290d028acddd118faf899d Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Thu, 9 Aug 2018 17:27:57 -0400 Subject: [PATCH 4/7] Changes to fix linting --- adafruit_tsl2591.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index be9bc65..5e6f290 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -250,18 +250,18 @@ 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 100 == atime: - maxCounts = _TSL2591_MAX_COUNT_100MS + if atime == 100: + max_counts = _TSL2591_MAX_COUNT_100MS else: - maxCounts = _TSL2591_MAX_COUNT - + max_counts = _TSL2591_MAX_COUNT + # Handle overflow. - if channel_0 >= maxCounts or channel_1 >= maxCounts: + 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 From 1566ffdf7360bbfad3790f6ebc9af550e6a7d64e Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Fri, 10 Aug 2018 16:16:44 -0400 Subject: [PATCH 5/7] Moving constants to the same represenation to reflect the datasheet and allow for "copy/search" within it. --- adafruit_tsl2591.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index 5e6f290..e245418 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -70,7 +70,7 @@ _TSL2591_LUX_COEFC = 0.59 _TSL2591_LUX_COEFD = 0.86 _TSL2591_MAX_COUNT_100MS = const(36863) -_TSL2591_MAX_COUNT = const(0xFFFF) +_TSL2591_MAX_COUNT = const(65535) # User-facing constants: GAIN_LOW = 0x00 # low gain (1x) From 22fe497810a6239753be6095b508333194540c06 Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Fri, 10 Aug 2018 16:18:18 -0400 Subject: [PATCH 6/7] Added hex comment for reference --- adafruit_tsl2591.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index e245418..2e51362 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -69,8 +69,8 @@ _TSL2591_LUX_COEFB = 1.64 _TSL2591_LUX_COEFC = 0.59 _TSL2591_LUX_COEFD = 0.86 -_TSL2591_MAX_COUNT_100MS = const(36863) -_TSL2591_MAX_COUNT = const(65535) +_TSL2591_MAX_COUNT_100MS = const(36863) # 0x8FFF +_TSL2591_MAX_COUNT = const(65535) # 0xFFFF # User-facing constants: GAIN_LOW = 0x00 # low gain (1x) From 584a24f3bc914618d15683a45fd1fe17373fbaab Mon Sep 17 00:00:00 2001 From: Rob Vice Date: Fri, 10 Aug 2018 17:16:53 -0400 Subject: [PATCH 7/7] Switch to using the integration time defines --- adafruit_tsl2591.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_tsl2591.py b/adafruit_tsl2591.py index 2e51362..25b1b17 100644 --- a/adafruit_tsl2591.py +++ b/adafruit_tsl2591.py @@ -254,8 +254,8 @@ def lux(self): # 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: + # Set the maximum sensor counts based on the integration time (atime) setting + if self._integration_time == INTEGRATIONTIME_100MS: max_counts = _TSL2591_MAX_COUNT_100MS else: max_counts = _TSL2591_MAX_COUNT