diff --git a/lib/light.js b/lib/light.js index bd7bd9cb6..d483c545e 100644 --- a/lib/light.js +++ b/lib/light.js @@ -123,7 +123,7 @@ 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); @@ -131,43 +131,49 @@ var Controllers = { // 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: {