From 655fdaefe8cf6cefffd431f3c0e7dff207eb08e1 Mon Sep 17 00:00:00 2001 From: mvn23 Date: Tue, 30 Apr 2019 20:48:52 +0200 Subject: [PATCH] Fix negative lux value check. Rewrite negative lux value check to avoid comparing unsigned long < 0. --- Adafruit_TSL2561_U.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Adafruit_TSL2561_U.cpp b/Adafruit_TSL2561_U.cpp index 231af5f..400fb05 100644 --- a/Adafruit_TSL2561_U.cpp +++ b/Adafruit_TSL2561_U.cpp @@ -443,10 +443,12 @@ uint32_t Adafruit_TSL2561_Unified::calculateLux(uint16_t broadband, uint16_t ir) #endif unsigned long temp; - temp = ((channel0 * b) - (channel1 * m)); - + channel0 = channel0 * b; + channel1 = channel1 * m; + + temp = 0; /* Do not allow negative lux value */ - if (temp < 0) temp = 0; + if (channel0 > channel1) temp = channel0 - channel1; /* Round lsb (2^(LUX_SCALE-1)) */ temp += (1 << (TSL2561_LUX_LUXSCALE-1));