Skip to content

Commit

Permalink
Implement timing delay that adheres to the timing set to the chip at …
Browse files Browse the repository at this point in the history
…initialization. Fixes gh-1196

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Aug 24, 2016
1 parent fa7318f commit 34542db
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions lib/light.js
Expand Up @@ -123,51 +123,57 @@ var Controllers = {

opts.address = address;

this.io.i2cConfig();
this.io.i2cConfig(opts);

// Write the "power on" byte to the control register
this.io.i2cWriteReg(address, this.REGISTER.CONTROL, 0x03);

// Configure the timing and gain
this.io.i2cWriteReg(address, this.REGISTER.TIMING, timing);

this.io.i2cRead(address, this.REGISTER.READ, 4, function(data) {
var ch0 = int16(data[1], data[0]);
var ch1 = int16(data[3], data[2]);
var value = 0;
var ratio = 0;
var read = function() {
this.io.i2cReadOnce(address, this.REGISTER.READ, 4, function(data) {
var ch0 = int16(data[1], data[0]);
var ch1 = int16(data[3], data[2]);
var value = 0;
var ratio = 0;

// http://www.adafruit.com/datasheets/TSL2561.pdf
// Page 23
if (ch0 === 0xFFFF || ch1 === 0xFFFF) {
value = 0;
} else {

ratio = ch1 / ch0;

ch0 *= 402 / iMs;
ch1 *= 402 / iMs;
// http://www.adafruit.com/datasheets/TSL2561.pdf
// Page 23
if (ch0 === 0xFFFF || ch1 === 0xFFFF) {
value = 0;
} else {

if (!gain) {
ch0 *= 16;
ch1 *= 16;
ratio = ch1 / ch0;

ch0 *= 402 / iMs;
ch1 *= 402 / iMs;

if (!gain) {
ch0 *= 16;
ch1 *= 16;
}

if (ratio < 0.5) {
value = 0.0304 * ch0 - 0.062 * ch0 * Math.pow(ratio, 1.4);
} else if (ratio < 0.61) {
value = 0.0224 * ch0 - 0.031 * ch1;
} else if (ratio < 0.80) {
value = 0.0128 * ch0 - 0.0153 * ch1;
} else if (ratio < 1.30) {
value = 0.00146 * ch0 - 0.00112 * ch1;
} else {
value = 0;
}
}

if (ratio < 0.5) {
value = 0.0304 * ch0 - 0.062 * ch0 * Math.pow(ratio, 1.4);
} else if (ratio < 0.61) {
value = 0.0224 * ch0 - 0.031 * ch1;
} else if (ratio < 0.80) {
value = 0.0128 * ch0 - 0.0153 * ch1;
} else if (ratio < 1.30) {
value = 0.00146 * ch0 - 0.00112 * ch1;
} else {
value = 0;
}
}
dataHandler(value);

dataHandler(value);
});
setTimeout(read, iMs);
});
}.bind(this);

read();
}
},
toIntensityLevel: {
Expand Down

0 comments on commit 34542db

Please sign in to comment.