Skip to content

Commit

Permalink
Merge pull request #6 from joba-1/issue-5
Browse files Browse the repository at this point in the history
fix for handling saturated sensor
  • Loading branch information
joba-1 authored Jan 11, 2019
2 parents 326fff0 + 13215ec commit 43115a1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Joba Tsl2561 Library
version=2.0.7
version=2.0.8
author=joba-1
maintainer=joba-1 <[email protected]>
sentence=IoT library for using the Tsl2561 luminosity sensor
Expand Down
15 changes: 12 additions & 3 deletions src/Tsl2561Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -122,19 +125,25 @@ 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
}
gain = sensitivity[curr].gain;
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
}
}
Expand Down

0 comments on commit 43115a1

Please sign in to comment.