diff --git a/library.json b/library.json index 94585c4..df05848 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Joba_Tsl2561", - "version": "2.0.7", + "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 ba18407..3038b50 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Joba Tsl2561 Library -version=2.0.7 +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 ae811f7..f431462 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 } }