From 35058766e5d6587fdd4160b4a97eda295b68c270 Mon Sep 17 00:00:00 2001 From: joba1 Date: Fri, 17 Aug 2018 10:40:03 +0200 Subject: [PATCH 1/2] add debug messages and some comments to autoGain for issue #5 --- src/Tsl2561Util.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Tsl2561Util.cpp b/src/Tsl2561Util.cpp index 025303c..b9d0553 100644 --- a/src/Tsl2561Util.cpp +++ b/src/Tsl2561Util.cpp @@ -93,6 +93,8 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t { true, Tsl2561::EXP_402 } // max }; + Serial.printf("autoGain start: gain=%u, expo=%u\n", gain, exposure); + // get current sensitivity if( !tsl.getSensitivity(gain, exposure) ) { return false; // I2C error @@ -110,9 +112,10 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t return false; // should not happen... } - // in a loop wait for next sample, get values and adjust sensitivity if needed + // sometimes sensor reports high brightness although it is darker. uint8_t retryOnSaturated = 10; + // in a loop wait for next sample, get values and adjust sensitivity if needed while( true ) { waitNext(exposure); @@ -122,11 +125,14 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t uint16_t limit = getLimit(exposure); if( full >= 1000 && full <= limit ) { - return true; // new value within limits + Serial.printf("autoGain normal full=%u, limits=1000-%u, curr=%u\n", full, limit, curr); + return true; // new value within limits of good accuracy } + // adjust sensitivity, if possible if( (full < 1000 && ++curr < sizeof(sensitivity)/sizeof(sensitivity[0])) || (full > limit && curr-- > 0) ) { + Serial.printf("autoGain adjust full=%u, limits=1000-%u, curr=%u\n", full, limit, curr); if( !tsl.setSensitivity(sensitivity[curr].gain, sensitivity[curr].exposure) ) { return false; // I2C error } @@ -134,7 +140,10 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t exposure = sensitivity[curr].exposure; } else { - if( ++curr > 0 && retryOnSaturated-- == 0 ) { + Serial.printf("autoGain limit full=%u, limits=1000-%u, curr=%u, retry=%u\n", full, limit, curr, retryOnSaturated); + // sensitivity already is at minimum or maximum + if( ++curr > 0 || retryOnSaturated-- == 0 ) { + // dark, or repeatedly confirmed high brightness return true; // saturated, but best we can do } } From d82a8d8b0b0b3ad9a273cebad88f3c6e6549dc91 Mon Sep 17 00:00:00 2001 From: Joba-1 Date: Fri, 11 Jan 2019 12:33:47 +0100 Subject: [PATCH 2/2] prepare for v2.0.8 (fix for handling saturated sensor) --- library.json | 2 +- library.properties | 2 +- src/Tsl2561Util.cpp | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library.json b/library.json index fbf3170..df05848 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Joba_Tsl2561", - "version": "2.0.6", + "version": "2.0.8", "keywords": "twowire, i2c, bus, sensor, luminosity, illuminance, lux", "description": "Arduino Library for ams (taos) luminance chip Tsl2561 with autogain", "repository": diff --git a/library.properties b/library.properties index e834984..3038b50 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Joba Tsl2561 Library -version=2.0.6 +version=2.0.8 author=joba-1 maintainer=joba-1 sentence=IoT library for using the Tsl2561 luminosity sensor diff --git a/src/Tsl2561Util.cpp b/src/Tsl2561Util.cpp index b9d0553..2486ccc 100644 --- a/src/Tsl2561Util.cpp +++ b/src/Tsl2561Util.cpp @@ -93,7 +93,7 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t { true, Tsl2561::EXP_402 } // max }; - Serial.printf("autoGain start: gain=%u, expo=%u\n", gain, exposure); + // Serial.printf("autoGain start: gain=%u, expo=%u\n", gain, exposure); // get current sensitivity if( !tsl.getSensitivity(gain, exposure) ) { @@ -125,14 +125,14 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t uint16_t limit = getLimit(exposure); if( full >= 1000 && full <= limit ) { - Serial.printf("autoGain normal full=%u, limits=1000-%u, curr=%u\n", full, limit, curr); + // Serial.printf("autoGain normal full=%u, limits=1000-%u, curr=%u\n", full, limit, curr); return true; // new value within limits of good accuracy } // adjust sensitivity, if possible if( (full < 1000 && ++curr < sizeof(sensitivity)/sizeof(sensitivity[0])) || (full > limit && curr-- > 0) ) { - Serial.printf("autoGain adjust full=%u, limits=1000-%u, curr=%u\n", full, limit, curr); + // Serial.printf("autoGain adjust full=%u, limits=1000-%u, curr=%u\n", full, limit, curr); if( !tsl.setSensitivity(sensitivity[curr].gain, sensitivity[curr].exposure) ) { return false; // I2C error } @@ -140,7 +140,7 @@ bool autoGain( Tsl2561 &tsl, bool &gain, Tsl2561::exposure_t &exposure, uint16_t exposure = sensitivity[curr].exposure; } else { - Serial.printf("autoGain limit full=%u, limits=1000-%u, curr=%u, retry=%u\n", full, limit, curr, retryOnSaturated); + // Serial.printf("autoGain limit full=%u, limits=1000-%u, curr=%u, retry=%u\n", full, limit, curr, retryOnSaturated); // sensitivity already is at minimum or maximum if( ++curr > 0 || retryOnSaturated-- == 0 ) { // dark, or repeatedly confirmed high brightness