ソースを参照

Changed to the correct TSL2561-driver. And it works.

Thomas Chef 8 年 前
コミット
e64fac8e26
7 ファイル変更788 行追加557 行削除
  1. 515 0
      Adafruit_TSL2561_U.cpp
  2. 204 0
      Adafruit_TSL2561_U.h
  3. 0 389
      Adafruit_TSL2591.cpp
  4. 0 152
      Adafruit_TSL2591.h
  5. 2 1
      README.md
  6. 1 1
      makefile
  7. 66 14
      wifi_lux_sensor.cpp

+ 515 - 0
Adafruit_TSL2561_U.cpp

@@ -0,0 +1,515 @@
+/**************************************************************************/
+/*!
+    @file     Adafruit_TSL2561.cpp
+    @author   K.Townsend (Adafruit Industries)
+    @license  BSD (see license.txt)
+    Driver for the TSL2561 digital luminosity (light) sensors.
+    Pick one up at http://www.adafruit.com/products/439
+    Adafruit invests time and resources providing this open source code,
+    please support Adafruit and open-source hardware by purchasing
+    products from Adafruit!
+    @section  HISTORY
+    v2.0 - Rewrote driver for Adafruit_Sensor and Auto-Gain support, and
+           added lux clipping check (returns 0 lux on sensor saturation)
+    v1.0 - First release (previously TSL2561)
+*/
+/**************************************************************************/
+#if defined(__AVR__)
+#include <avr/pgmspace.h>
+#include <util/delay.h>
+#else
+#include "pgmspace.h"
+#endif
+#include <stdlib.h>
+
+#include "Adafruit_TSL2561_U.h"
+
+#define TSL2561_DELAY_INTTIME_13MS    (15)
+#define TSL2561_DELAY_INTTIME_101MS   (120)
+#define TSL2561_DELAY_INTTIME_402MS   (450)
+
+/*========================================================================*/
+/*                          PRIVATE FUNCTIONS                             */
+/*========================================================================*/
+
+/**************************************************************************/
+/*!
+    @brief  Writes a register and an 8 bit value over I2C
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::write8 (uint8_t reg, uint32_t value)
+{
+  Wire.beginTransmission(_addr);
+  Wire.write(reg);
+  Wire.write(value & 0xFF);
+  Wire.endTransmission();
+}
+
+/**************************************************************************/
+/*!
+    @brief  Reads an 8 bit value over I2C
+*/
+/**************************************************************************/
+uint8_t Adafruit_TSL2561_Unified::read8(uint8_t reg)
+{
+  Wire.beginTransmission(_addr);
+  #if ARDUINO >= 100
+  Wire.write(reg);
+  #else
+  Wire.send(reg);
+  #endif
+  Wire.endTransmission();
+
+  Wire.requestFrom(_addr, 1);
+  #if ARDUINO >= 100
+  return Wire.read();
+  #else
+  return Wire.receive();
+  #endif
+}
+
+/**************************************************************************/
+/*!
+    @brief  Reads a 16 bit values over I2C
+*/
+/**************************************************************************/
+uint16_t Adafruit_TSL2561_Unified::read16(uint8_t reg)
+{
+  uint16_t x; uint16_t t;
+
+  Wire.beginTransmission(_addr);
+  #if ARDUINO >= 100
+  Wire.write(reg);
+  #else
+  Wire.send(reg);
+  #endif
+  Wire.endTransmission();
+
+  Wire.requestFrom(_addr, 2);
+  #if ARDUINO >= 100
+  t = Wire.read();
+  x = Wire.read();
+  #else
+  t = Wire.receive();
+  x = Wire.receive();
+  #endif
+  x <<= 8;
+  x |= t;
+  return x;
+}
+
+/**************************************************************************/
+/*!
+    Enables the device
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::enable(void)
+{
+  /* Enable the device by setting the control bit to 0x03 */
+  write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);
+}
+
+/**************************************************************************/
+/*!
+    Disables the device (putting it in lower power sleep mode)
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::disable(void)
+{
+  /* Turn the device off to save power */
+  write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF);
+}
+
+/**************************************************************************/
+/*!
+    Private function to read luminosity on both channels
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::getData (uint16_t *broadband, uint16_t *ir)
+{
+  /* Enable the device by setting the control bit to 0x03 */
+  enable();
+
+  /* Wait x ms for ADC to complete */
+  switch (_tsl2561IntegrationTime)
+  {
+    case TSL2561_INTEGRATIONTIME_13MS:
+      delay(TSL2561_DELAY_INTTIME_13MS);  // KTOWN: Was 14ms
+      break;
+    case TSL2561_INTEGRATIONTIME_101MS:
+      delay(TSL2561_DELAY_INTTIME_101MS); // KTOWN: Was 102ms
+      break;
+    default:
+      delay(TSL2561_DELAY_INTTIME_402MS); // KTOWN: Was 403ms
+      break;
+  }
+
+  /* Reads a two byte value from channel 0 (visible + infrared) */
+  *broadband = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
+
+  /* Reads a two byte value from channel 1 (infrared) */
+  *ir = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
+
+  /* Turn the device off to save power */
+  disable();
+}
+
+/*========================================================================*/
+/*                            CONSTRUCTORS                                */
+/*========================================================================*/
+
+/**************************************************************************/
+/*!
+    Constructor
+*/
+/**************************************************************************/
+Adafruit_TSL2561_Unified::Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID) 
+{
+  _addr = addr;
+  _tsl2561Initialised = false;
+  _tsl2561AutoGain = false;
+  _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_13MS;
+  _tsl2561Gain = TSL2561_GAIN_1X;
+  _tsl2561SensorID = sensorID;
+}
+
+/*========================================================================*/
+/*                           PUBLIC FUNCTIONS                             */
+/*========================================================================*/
+
+/**************************************************************************/
+/*!
+    Initializes I2C and configures the sensor (call this function before
+    doing anything else)
+*/
+/**************************************************************************/
+boolean Adafruit_TSL2561_Unified::begin(void) 
+{
+  Wire.begin();
+
+  /* Make sure we're actually connected */
+  uint8_t x = read8(TSL2561_REGISTER_ID);
+  if (!(x & 0x0A))
+  {
+    return false;
+  }
+  _tsl2561Initialised = true;
+
+  /* Set default integration time and gain */
+  setIntegrationTime(_tsl2561IntegrationTime);
+  setGain(_tsl2561Gain);
+
+  /* Note: by default, the device is in power down mode on bootup */
+  disable();
+
+  return true;
+}
+  
+/**************************************************************************/
+/*!
+    @brief  Enables or disables the auto-gain settings when reading
+            data from the sensor
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::enableAutoRange(bool enable)
+{
+   _tsl2561AutoGain = enable ? true : false;
+}
+
+/**************************************************************************/
+/*!
+    Sets the integration time for the TSL2561
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::setIntegrationTime(tsl2561IntegrationTime_t time)
+{
+  if (!_tsl2561Initialised) begin();
+
+  /* Enable the device by setting the control bit to 0x03 */
+  enable();
+
+  /* Update the timing register */
+  write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, time | _tsl2561Gain);
+
+  /* Update value placeholders */
+  _tsl2561IntegrationTime = time;
+
+  /* Turn the device off to save power */
+  disable();
+}
+
+/**************************************************************************/
+/*!
+    Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::setGain(tsl2561Gain_t gain)
+{
+  if (!_tsl2561Initialised) begin();
+
+  /* Enable the device by setting the control bit to 0x03 */
+  enable();
+
+  /* Update the timing register */
+  write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _tsl2561IntegrationTime | gain);
+
+  /* Update value placeholders */
+  _tsl2561Gain = gain;
+
+  /* Turn the device off to save power */
+  disable();
+}
+
+/**************************************************************************/
+/*!
+    @brief  Gets the broadband (mixed lighting) and IR only values from
+            the TSL2561, adjusting gain if auto-gain is enabled
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::getLuminosity (uint16_t *broadband, uint16_t *ir)
+{
+  bool valid = false;
+
+  if (!_tsl2561Initialised) begin();
+
+  /* If Auto gain disabled get a single reading and continue */
+  if(!_tsl2561AutoGain)
+  {
+    getData (broadband, ir);
+    return;
+  }
+
+  /* Read data until we find a valid range */
+  bool _agcCheck = false;
+  do
+  {
+    uint16_t _b, _ir;
+    uint16_t _hi, _lo;
+    tsl2561IntegrationTime_t _it = _tsl2561IntegrationTime;
+
+    /* Get the hi/low threshold for the current integration time */
+    switch(_it)
+    {
+      case TSL2561_INTEGRATIONTIME_13MS:
+        _hi = TSL2561_AGC_THI_13MS;
+        _lo = TSL2561_AGC_TLO_13MS;
+        break;
+      case TSL2561_INTEGRATIONTIME_101MS:
+        _hi = TSL2561_AGC_THI_101MS;
+        _lo = TSL2561_AGC_TLO_101MS;
+        break;
+      default:
+        _hi = TSL2561_AGC_THI_402MS;
+        _lo = TSL2561_AGC_TLO_402MS;
+        break;
+    }
+
+    getData(&_b, &_ir);
+
+    /* Run an auto-gain check if we haven't already done so ... */
+    if (!_agcCheck)
+    {
+      if ((_b < _lo) && (_tsl2561Gain == TSL2561_GAIN_1X))
+      {
+        /* Increase the gain and try again */
+        setGain(TSL2561_GAIN_16X);
+        /* Drop the previous conversion results */
+        getData(&_b, &_ir);
+        /* Set a flag to indicate we've adjusted the gain */
+        _agcCheck = true;
+      }
+      else if ((_b > _hi) && (_tsl2561Gain == TSL2561_GAIN_16X))
+      {
+        /* Drop gain to 1x and try again */
+        setGain(TSL2561_GAIN_1X);
+        /* Drop the previous conversion results */
+        getData(&_b, &_ir);
+        /* Set a flag to indicate we've adjusted the gain */
+        _agcCheck = true;
+      }
+      else
+      {
+        /* Nothing to look at here, keep moving ....
+           Reading is either valid, or we're already at the chips limits */
+        *broadband = _b;
+        *ir = _ir;
+        valid = true;
+      }
+    }
+    else
+    {
+      /* If we've already adjusted the gain once, just return the new results.
+         This avoids endless loops where a value is at one extreme pre-gain,
+         and the the other extreme post-gain */
+      *broadband = _b;
+      *ir = _ir;
+      valid = true;
+    }
+  } while (!valid);
+}
+
+/**************************************************************************/
+/*!
+    Converts the raw sensor values to the standard SI lux equivalent.
+    Returns 0 if the sensor is saturated and the values are unreliable.
+*/
+/**************************************************************************/
+uint32_t Adafruit_TSL2561_Unified::calculateLux(uint16_t broadband, uint16_t ir)
+{
+  unsigned long chScale;
+  unsigned long channel1;
+  unsigned long channel0;  
+  
+  /* Make sure the sensor isn't saturated! */
+  uint16_t clipThreshold;
+  switch (_tsl2561IntegrationTime)
+  {
+    case TSL2561_INTEGRATIONTIME_13MS:
+      clipThreshold = TSL2561_CLIPPING_13MS;
+      break;
+    case TSL2561_INTEGRATIONTIME_101MS:
+      clipThreshold = TSL2561_CLIPPING_101MS;
+      break;
+    default:
+      clipThreshold = TSL2561_CLIPPING_402MS;
+      break;
+  }
+
+  /* Return 65536 lux if the sensor is saturated */
+  if ((broadband > clipThreshold) || (ir > clipThreshold))
+  {
+    return 65536;
+  }
+
+  /* Get the correct scale depending on the intergration time */
+  switch (_tsl2561IntegrationTime)
+  {
+    case TSL2561_INTEGRATIONTIME_13MS:
+      chScale = TSL2561_LUX_CHSCALE_TINT0;
+      break;
+    case TSL2561_INTEGRATIONTIME_101MS:
+      chScale = TSL2561_LUX_CHSCALE_TINT1;
+      break;
+    default: /* No scaling ... integration time = 402ms */
+      chScale = (1 << TSL2561_LUX_CHSCALE);
+      break;
+  }
+
+  /* Scale for gain (1x or 16x) */
+  if (!_tsl2561Gain) chScale = chScale << 4;
+
+  /* Scale the channel values */
+  channel0 = (broadband * chScale) >> TSL2561_LUX_CHSCALE;
+  channel1 = (ir * chScale) >> TSL2561_LUX_CHSCALE;
+
+  /* Find the ratio of the channel values (Channel1/Channel0) */
+  unsigned long ratio1 = 0;
+  if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0;
+
+  /* round the ratio value */
+  unsigned long ratio = (ratio1 + 1) >> 1;
+
+  unsigned int b, m;
+
+#ifdef TSL2561_PACKAGE_CS
+  if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C))
+    {b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;}
+  else if (ratio <= TSL2561_LUX_K2C)
+    {b=TSL2561_LUX_B2C; m=TSL2561_LUX_M2C;}
+  else if (ratio <= TSL2561_LUX_K3C)
+    {b=TSL2561_LUX_B3C; m=TSL2561_LUX_M3C;}
+  else if (ratio <= TSL2561_LUX_K4C)
+    {b=TSL2561_LUX_B4C; m=TSL2561_LUX_M4C;}
+  else if (ratio <= TSL2561_LUX_K5C)
+    {b=TSL2561_LUX_B5C; m=TSL2561_LUX_M5C;}
+  else if (ratio <= TSL2561_LUX_K6C)
+    {b=TSL2561_LUX_B6C; m=TSL2561_LUX_M6C;}
+  else if (ratio <= TSL2561_LUX_K7C)
+    {b=TSL2561_LUX_B7C; m=TSL2561_LUX_M7C;}
+  else if (ratio > TSL2561_LUX_K8C)
+    {b=TSL2561_LUX_B8C; m=TSL2561_LUX_M8C;}
+#else
+  if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T))
+    {b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;}
+  else if (ratio <= TSL2561_LUX_K2T)
+    {b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;}
+  else if (ratio <= TSL2561_LUX_K3T)
+    {b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;}
+  else if (ratio <= TSL2561_LUX_K4T)
+    {b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;}
+  else if (ratio <= TSL2561_LUX_K5T)
+    {b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;}
+  else if (ratio <= TSL2561_LUX_K6T)
+    {b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;}
+  else if (ratio <= TSL2561_LUX_K7T)
+    {b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;}
+  else if (ratio > TSL2561_LUX_K8T)
+    {b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;}
+#endif
+
+  unsigned long temp;
+  temp = ((channel0 * b) - (channel1 * m));
+
+  /* Do not allow negative lux value */
+  if (temp < 0) temp = 0;
+
+  /* Round lsb (2^(LUX_SCALE-1)) */
+  temp += (1 << (TSL2561_LUX_LUXSCALE-1));
+
+  /* Strip off fractional portion */
+  uint32_t lux = temp >> TSL2561_LUX_LUXSCALE;
+
+  /* Signal I2C had no errors */
+  return lux;
+}
+
+/**************************************************************************/
+/*!
+    @brief  Gets the most recent sensor event
+    returns true if sensor reading is between 0 and 65535 lux
+    returns false if sensor is saturated
+*/
+/**************************************************************************/
+bool Adafruit_TSL2561_Unified::getEvent(sensors_event_t *event)
+{
+  uint16_t broadband, ir;
+  
+  /* Clear the event */
+  memset(event, 0, sizeof(sensors_event_t));
+  
+  event->version   = sizeof(sensors_event_t);
+  event->sensor_id = _tsl2561SensorID;
+  event->type      = SENSOR_TYPE_LIGHT;
+  event->timestamp = millis();
+
+  /* Calculate the actual lux value */
+  getLuminosity(&broadband, &ir);
+  event->light = calculateLux(broadband, ir);
+  
+  if (event->light == 65536) {
+    return false; 
+  }
+  return true;
+}
+
+/**************************************************************************/
+/*!
+    @brief  Gets the sensor_t data
+*/
+/**************************************************************************/
+void Adafruit_TSL2561_Unified::getSensor(sensor_t *sensor)
+{
+  /* Clear the sensor_t object */
+  memset(sensor, 0, sizeof(sensor_t));
+
+  /* Insert the sensor name in the fixed length char array */
+  strncpy (sensor->name, "TSL2561", sizeof(sensor->name) - 1);
+  sensor->name[sizeof(sensor->name)- 1] = 0;
+  sensor->version     = 1;
+  sensor->sensor_id   = _tsl2561SensorID;
+  sensor->type        = SENSOR_TYPE_LIGHT;
+  sensor->min_delay   = 0;
+  sensor->max_value   = 17000.0;  /* Based on trial and error ... confirm! */
+  sensor->min_value   = 0.0;
+  sensor->resolution  = 1.0;
+}

+ 204 - 0
Adafruit_TSL2561_U.h

@@ -0,0 +1,204 @@
+/**************************************************************************/
+/*! 
+    @file     Adafruit_TSL2561.h
+    @author   K. Townsend (Adafruit Industries)
+    @section LICENSE
+    Software License Agreement (BSD License)
+    Copyright (c) 2013, Adafruit Industries
+    All rights reserved.
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+    1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+    3. Neither the name of the copyright holders nor the
+    names of its contributors may be used to endorse or promote products
+    derived from this software without specific prior written permission.
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
+    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/**************************************************************************/
+#ifndef _TSL2561_H_
+#define _TSL2561_H_
+
+#if ARDUINO >= 100
+ #include <Arduino.h>
+#else
+ #include <WProgram.h>
+#endif
+#include <Adafruit_Sensor.h>
+
+#ifdef __AVR_ATtiny85__
+  #include "TinyWireM.h"
+  #define Wire TinyWireM
+#else
+  #include <Wire.h>
+#endif
+
+
+#define TSL2561_VISIBLE 2                   // channel 0 - channel 1
+#define TSL2561_INFRARED 1                  // channel 1
+#define TSL2561_FULLSPECTRUM 0              // channel 0
+
+// I2C address options
+#define TSL2561_ADDR_LOW          (0x29)
+#define TSL2561_ADDR_FLOAT        (0x39)    // Default address (pin left floating)
+#define TSL2561_ADDR_HIGH         (0x49)
+
+// Lux calculations differ slightly for CS package
+//#define TSL2561_PACKAGE_CS
+#define TSL2561_PACKAGE_T_FN_CL
+
+#define TSL2561_COMMAND_BIT       (0x80)    // Must be 1
+#define TSL2561_CLEAR_BIT         (0x40)    // Clears any pending interrupt (write 1 to clear)
+#define TSL2561_WORD_BIT          (0x20)    // 1 = read/write word (rather than byte)
+#define TSL2561_BLOCK_BIT         (0x10)    // 1 = using block read/write
+
+#define TSL2561_CONTROL_POWERON   (0x03)
+#define TSL2561_CONTROL_POWEROFF  (0x00)
+
+#define TSL2561_LUX_LUXSCALE      (14)      // Scale by 2^14
+#define TSL2561_LUX_RATIOSCALE    (9)       // Scale ratio by 2^9
+#define TSL2561_LUX_CHSCALE       (10)      // Scale channel values by 2^10
+#define TSL2561_LUX_CHSCALE_TINT0 (0x7517)  // 322/11 * 2^TSL2561_LUX_CHSCALE
+#define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7)  // 322/81 * 2^TSL2561_LUX_CHSCALE
+
+// T, FN and CL package values
+#define TSL2561_LUX_K1T           (0x0040)  // 0.125 * 2^RATIO_SCALE
+#define TSL2561_LUX_B1T           (0x01f2)  // 0.0304 * 2^LUX_SCALE
+#define TSL2561_LUX_M1T           (0x01be)  // 0.0272 * 2^LUX_SCALE
+#define TSL2561_LUX_K2T           (0x0080)  // 0.250 * 2^RATIO_SCALE
+#define TSL2561_LUX_B2T           (0x0214)  // 0.0325 * 2^LUX_SCALE
+#define TSL2561_LUX_M2T           (0x02d1)  // 0.0440 * 2^LUX_SCALE
+#define TSL2561_LUX_K3T           (0x00c0)  // 0.375 * 2^RATIO_SCALE
+#define TSL2561_LUX_B3T           (0x023f)  // 0.0351 * 2^LUX_SCALE
+#define TSL2561_LUX_M3T           (0x037b)  // 0.0544 * 2^LUX_SCALE
+#define TSL2561_LUX_K4T           (0x0100)  // 0.50 * 2^RATIO_SCALE
+#define TSL2561_LUX_B4T           (0x0270)  // 0.0381 * 2^LUX_SCALE
+#define TSL2561_LUX_M4T           (0x03fe)  // 0.0624 * 2^LUX_SCALE
+#define TSL2561_LUX_K5T           (0x0138)  // 0.61 * 2^RATIO_SCALE
+#define TSL2561_LUX_B5T           (0x016f)  // 0.0224 * 2^LUX_SCALE
+#define TSL2561_LUX_M5T           (0x01fc)  // 0.0310 * 2^LUX_SCALE
+#define TSL2561_LUX_K6T           (0x019a)  // 0.80 * 2^RATIO_SCALE
+#define TSL2561_LUX_B6T           (0x00d2)  // 0.0128 * 2^LUX_SCALE
+#define TSL2561_LUX_M6T           (0x00fb)  // 0.0153 * 2^LUX_SCALE
+#define TSL2561_LUX_K7T           (0x029a)  // 1.3 * 2^RATIO_SCALE
+#define TSL2561_LUX_B7T           (0x0018)  // 0.00146 * 2^LUX_SCALE
+#define TSL2561_LUX_M7T           (0x0012)  // 0.00112 * 2^LUX_SCALE
+#define TSL2561_LUX_K8T           (0x029a)  // 1.3 * 2^RATIO_SCALE
+#define TSL2561_LUX_B8T           (0x0000)  // 0.000 * 2^LUX_SCALE
+#define TSL2561_LUX_M8T           (0x0000)  // 0.000 * 2^LUX_SCALE
+
+// CS package values
+#define TSL2561_LUX_K1C           (0x0043)  // 0.130 * 2^RATIO_SCALE
+#define TSL2561_LUX_B1C           (0x0204)  // 0.0315 * 2^LUX_SCALE
+#define TSL2561_LUX_M1C           (0x01ad)  // 0.0262 * 2^LUX_SCALE
+#define TSL2561_LUX_K2C           (0x0085)  // 0.260 * 2^RATIO_SCALE
+#define TSL2561_LUX_B2C           (0x0228)  // 0.0337 * 2^LUX_SCALE
+#define TSL2561_LUX_M2C           (0x02c1)  // 0.0430 * 2^LUX_SCALE
+#define TSL2561_LUX_K3C           (0x00c8)  // 0.390 * 2^RATIO_SCALE
+#define TSL2561_LUX_B3C           (0x0253)  // 0.0363 * 2^LUX_SCALE
+#define TSL2561_LUX_M3C           (0x0363)  // 0.0529 * 2^LUX_SCALE
+#define TSL2561_LUX_K4C           (0x010a)  // 0.520 * 2^RATIO_SCALE
+#define TSL2561_LUX_B4C           (0x0282)  // 0.0392 * 2^LUX_SCALE
+#define TSL2561_LUX_M4C           (0x03df)  // 0.0605 * 2^LUX_SCALE
+#define TSL2561_LUX_K5C           (0x014d)  // 0.65 * 2^RATIO_SCALE
+#define TSL2561_LUX_B5C           (0x0177)  // 0.0229 * 2^LUX_SCALE
+#define TSL2561_LUX_M5C           (0x01dd)  // 0.0291 * 2^LUX_SCALE
+#define TSL2561_LUX_K6C           (0x019a)  // 0.80 * 2^RATIO_SCALE
+#define TSL2561_LUX_B6C           (0x0101)  // 0.0157 * 2^LUX_SCALE
+#define TSL2561_LUX_M6C           (0x0127)  // 0.0180 * 2^LUX_SCALE
+#define TSL2561_LUX_K7C           (0x029a)  // 1.3 * 2^RATIO_SCALE
+#define TSL2561_LUX_B7C           (0x0037)  // 0.00338 * 2^LUX_SCALE
+#define TSL2561_LUX_M7C           (0x002b)  // 0.00260 * 2^LUX_SCALE
+#define TSL2561_LUX_K8C           (0x029a)  // 1.3 * 2^RATIO_SCALE
+#define TSL2561_LUX_B8C           (0x0000)  // 0.000 * 2^LUX_SCALE
+#define TSL2561_LUX_M8C           (0x0000)  // 0.000 * 2^LUX_SCALE
+
+// Auto-gain thresholds
+#define TSL2561_AGC_THI_13MS      (4850)    // Max value at Ti 13ms = 5047
+#define TSL2561_AGC_TLO_13MS      (100)
+#define TSL2561_AGC_THI_101MS     (36000)   // Max value at Ti 101ms = 37177
+#define TSL2561_AGC_TLO_101MS     (200)
+#define TSL2561_AGC_THI_402MS     (63000)   // Max value at Ti 402ms = 65535
+#define TSL2561_AGC_TLO_402MS     (500)
+
+// Clipping thresholds
+#define TSL2561_CLIPPING_13MS     (4900)
+#define TSL2561_CLIPPING_101MS    (37000)
+#define TSL2561_CLIPPING_402MS    (65000)
+
+enum
+{
+  TSL2561_REGISTER_CONTROL          = 0x00,
+  TSL2561_REGISTER_TIMING           = 0x01,
+  TSL2561_REGISTER_THRESHHOLDL_LOW  = 0x02,
+  TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03,
+  TSL2561_REGISTER_THRESHHOLDH_LOW  = 0x04,
+  TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05,
+  TSL2561_REGISTER_INTERRUPT        = 0x06,
+  TSL2561_REGISTER_CRC              = 0x08,
+  TSL2561_REGISTER_ID               = 0x0A,
+  TSL2561_REGISTER_CHAN0_LOW        = 0x0C,
+  TSL2561_REGISTER_CHAN0_HIGH       = 0x0D,
+  TSL2561_REGISTER_CHAN1_LOW        = 0x0E,
+  TSL2561_REGISTER_CHAN1_HIGH       = 0x0F
+};
+
+typedef enum
+{
+  TSL2561_INTEGRATIONTIME_13MS      = 0x00,    // 13.7ms
+  TSL2561_INTEGRATIONTIME_101MS     = 0x01,    // 101ms
+  TSL2561_INTEGRATIONTIME_402MS     = 0x02     // 402ms
+}
+tsl2561IntegrationTime_t;
+
+typedef enum
+{
+  TSL2561_GAIN_1X                   = 0x00,    // No gain
+  TSL2561_GAIN_16X                  = 0x10,    // 16x gain
+}
+tsl2561Gain_t;
+
+class Adafruit_TSL2561_Unified : public Adafruit_Sensor {
+ public:
+  Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1);
+  boolean begin(void);
+  
+  /* TSL2561 Functions */
+  void enableAutoRange(bool enable);
+  void setIntegrationTime(tsl2561IntegrationTime_t time);
+  void setGain(tsl2561Gain_t gain);
+  void getLuminosity (uint16_t *broadband, uint16_t *ir);
+  uint32_t calculateLux(uint16_t broadband, uint16_t ir);
+  
+  /* Unified Sensor API Functions */  
+  bool getEvent(sensors_event_t*);
+  void getSensor(sensor_t*);
+
+ private:
+  int8_t _addr;
+  boolean _tsl2561Initialised;
+  boolean _tsl2561AutoGain;
+  tsl2561IntegrationTime_t _tsl2561IntegrationTime;
+  tsl2561Gain_t _tsl2561Gain;
+  int32_t _tsl2561SensorID;
+  
+  void     enable (void);
+  void     disable (void);
+  void     write8 (uint8_t reg, uint32_t value);
+  uint8_t  read8 (uint8_t reg);
+  uint16_t read16 (uint8_t reg);
+  void     getData (uint16_t *broadband, uint16_t *ir);
+};
+#endif

+ 0 - 389
Adafruit_TSL2591.cpp

@@ -1,389 +0,0 @@
-/**************************************************************************/
-/*! 
-    @file     Adafruit_TSL2591.cpp
-    @author   KT0WN (adafruit.com)
-
-    This is a library for the Adafruit TSL2591 breakout board
-    This library works with the Adafruit TSL2591 breakout 
-    ----> https://www.adafruit.com/products/1980
-	
-    Check out the links above for our tutorials and wiring diagrams 
-    These chips use I2C to communicate
-	
-    Adafruit invests time and resources providing this open source code, 
-    please support Adafruit and open-source hardware by purchasing 
-    products from Adafruit!
-
-    @section LICENSE
-
-    Software License Agreement (BSD License)
-
-    Copyright (c) 2014 Adafruit Industries
-    All rights reserved.
-
-    Redistribution and use in source and binary forms, with or without
-    modification, are permitted provided that the following conditions are met:
-    1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
-    2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
-    3. Neither the name of the copyright holders nor the
-    names of its contributors may be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
-    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
-    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
-    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-/**************************************************************************/
-#if defined(ESP8266)
-#include <pgmspace.h>
-#else
-#include <avr/pgmspace.h>
-#endif
-#if defined(__AVR__)
-#include <util/delay.h>
-#endif
-#include <stdlib.h>
-
-#include "Adafruit_TSL2591.h"
-
-Adafruit_TSL2591::Adafruit_TSL2591(int32_t sensorID) 
-{
-  _initialized = false;
-  _integration = TSL2591_INTEGRATIONTIME_100MS;
-  _gain        = TSL2591_GAIN_MED;
-  _sensorID    = sensorID;
-
-  // we cant do wire initialization till later, because we havent loaded Wire yet
-}
-
-boolean Adafruit_TSL2591::begin(void) 
-{
-  uint8_t id = read8(0x12);
-  if (id == 0x50 ) 
-  {
-    Serial.println("Found Adafruit_TSL2591");
-  } 
-  else 
-  {
-    return false;
-  }
-  
-  _initialized = true;
-
-  // Set default integration time and gain
-  setTiming(_integration);
-  setGain(_gain);
-  
-  // Note: by default, the device is in power down mode on bootup
-  disable();
-
-  return true;
-}
-
-void Adafruit_TSL2591::enable(void)
-{
-  if (!_initialized)
-  {
-    if (!begin())
-    {
-      return;
-    }
-  }
-
-  // Enable the device by setting the control bit to 0x01
-  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
-}
-
-void Adafruit_TSL2591::disable(void)
-{
-  if (!_initialized)
-  {
-    if (!begin())
-    {
-      return;
-    }
-  }
-
-  // Disable the device by setting the control bit to 0x00
-  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
-}
-
-void Adafruit_TSL2591::setGain(tsl2591Gain_t gain) 
-{
-  if (!_initialized)
-  {
-    if (!begin())
-    {
-      return;
-    }
-  }
-
-  enable();
-  _gain = gain;
-  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
-  disable();
-}
-
-tsl2591Gain_t Adafruit_TSL2591::getGain()
-{
-  return _gain;
-}
-
-void Adafruit_TSL2591::setTiming(tsl2591IntegrationTime_t integration)
-{
-  if (!_initialized)
-  {
-    if (!begin())
-    {
-      return;
-    }
-  }
-
-  enable();
-  _integration = integration;
-  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
-  disable();
-}
-
-tsl2591IntegrationTime_t Adafruit_TSL2591::getTiming()
-{
-  return _integration;
-}
-
-uint32_t Adafruit_TSL2591::calculateLux(uint16_t ch0, uint16_t ch1)
-{
-  uint16_t atime, again;
-  float    cpl, lux1, lux2, lux;
-  uint32_t chan0, chan1;
-
-  // Check for overflow conditions first
-  if ((ch0 == 0xFFFF) | (ch1 == 0xFFFF))
-  {
-    // Signal an overflow
-    return 0;
-  }
-
-  // Note: This algorithm is based on preliminary coefficients
-  // provided by AMS and may need to be updated in the future
-  
-  switch (_integration)
-  {
-    case TSL2591_INTEGRATIONTIME_100MS :
-      atime = 100.0F;
-      break;
-    case TSL2591_INTEGRATIONTIME_200MS :
-      atime = 200.0F;
-      break;
-    case TSL2591_INTEGRATIONTIME_300MS :
-      atime = 300.0F;
-      break;
-    case TSL2591_INTEGRATIONTIME_400MS :
-      atime = 400.0F;
-      break;
-    case TSL2591_INTEGRATIONTIME_500MS :
-      atime = 500.0F;
-      break;
-    case TSL2591_INTEGRATIONTIME_600MS :
-      atime = 600.0F;
-      break;
-    default: // 100ms
-      atime = 100.0F;
-      break;
-  }
-  
-  switch (_gain)
-  {
-    case TSL2591_GAIN_LOW :
-      again = 1.0F;
-      break;
-    case TSL2591_GAIN_MED :
-      again = 25.0F;
-      break;
-    case TSL2591_GAIN_HIGH :
-      again = 428.0F;
-      break;
-    case TSL2591_GAIN_MAX :
-      again = 9876.0F;
-      break;
-    default:
-      again = 1.0F;
-      break;
-  }
-
-  // cpl = (ATIME * AGAIN) / DF
-  cpl = (atime * again) / TSL2591_LUX_DF;
-  
-  lux1 = ( (float)ch0 - (TSL2591_LUX_COEFB * (float)ch1) ) / cpl;
-  lux2 = ( ( TSL2591_LUX_COEFC * (float)ch0 ) - ( TSL2591_LUX_COEFD * (float)ch1 ) ) / cpl;
-  
-  // The highest value is the approximate lux equivalent
-  lux = lux1 > lux2 ? lux1 : lux2;
-
-  // Signal I2C had no errors
-  return (uint32_t)lux;
-}
-
-uint32_t Adafruit_TSL2591::getFullLuminosity (void)
-{
-  if (!_initialized)
-  {
-    if (!begin())
-    {
-      return 0;
-    }
-  }
-
-  // Enable the device
-  enable();
-
-  // Wait x ms for ADC to complete
-  for (uint8_t d=0; d<=_integration; d++) 
-  {
-    delay(120);
-  }
-
-  uint32_t x;
-  x = read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW);
-  x <<= 16;
-  x |= read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
-
-  disable();
-
-  return x;
-}
-
-uint16_t Adafruit_TSL2591::getLuminosity (uint8_t channel) 
-{
-  uint32_t x = getFullLuminosity();
-
-  if (channel == TSL2591_FULLSPECTRUM) 
-  {
-    // Reads two byte value from channel 0 (visible + infrared)
-    return (x & 0xFFFF);
-  } 
-  else if (channel == TSL2591_INFRARED) 
-  {
-    // Reads two byte value from channel 1 (infrared)
-    return (x >> 16);
-  } 
-  else if (channel == TSL2591_VISIBLE) 
-  {
-    // Reads all and subtracts out just the visible!
-    return ( (x & 0xFFFF) - (x >> 16));
-  }
-  
-  // unknown channel!
-  return 0;
-}
-
-uint8_t Adafruit_TSL2591::read8(uint8_t reg)
-{
-  Wire.beginTransmission(TSL2591_ADDR);
-  Wire.write(0x80 | 0x20 | reg); // command bit, normal mode
-  Wire.endTransmission();
-
-  Wire.requestFrom(TSL2591_ADDR, 1);
-  while (! Wire.available());
-  return Wire.read();
-}
-
-uint16_t Adafruit_TSL2591::read16(uint8_t reg)
-{
-  uint16_t x; 
-  uint16_t t;
-
-  Wire.beginTransmission(TSL2591_ADDR);
-#if ARDUINO >= 100
-  Wire.write(reg);
-#else
-  Wire.send(reg);
-#endif
-  Wire.endTransmission();
-
-  Wire.requestFrom(TSL2591_ADDR, 2);
-#if ARDUINO >= 100
-  t = Wire.read();
-  x = Wire.read();
-#else
-  t = Wire.receive();
-  x = Wire.receive();
-#endif
-  x <<= 8;
-  x |= t;
-  return x;
-}
-
-void Adafruit_TSL2591::write8 (uint8_t reg, uint8_t value)
-{
-  Wire.beginTransmission(TSL2591_ADDR);
-#if ARDUINO >= 100
-  Wire.write(reg);
-  Wire.write(value);
-#else
-  Wire.send(reg);
-  Wire.send(value);
-#endif
-  Wire.endTransmission();
-}
-
-/**************************************************************************/
-/*!
-    @brief  Gets the most recent sensor event
-*/
-/**************************************************************************/
-bool Adafruit_TSL2591::getEvent(sensors_event_t *event)
-{
-  uint16_t ir, full;
-  uint32_t lum = getFullLuminosity();
-  /* Early silicon seems to have issues when there is a sudden jump in */
-  /* light levels. :( To work around this for now sample the sensor 2x */
-  lum = getFullLuminosity();
-  ir = lum >> 16;
-  full = lum & 0xFFFF;  
-  
-  /* Clear the event */
-  memset(event, 0, sizeof(sensors_event_t));
-  
-  event->version   = sizeof(sensors_event_t);
-  event->sensor_id = _sensorID;
-  event->type      = SENSOR_TYPE_LIGHT;
-  event->timestamp = millis();
-
-  /* Calculate the actual lux value */
-  /* 0 = sensor overflow (too much light) */
-  event->light = calculateLux(full, ir);
-  
-  return true;
-}
-
-/**************************************************************************/
-/*!
-    @brief  Gets the sensor_t data
-*/
-/**************************************************************************/
-void Adafruit_TSL2591::getSensor(sensor_t *sensor)
-{
-  /* Clear the sensor_t object */
-  memset(sensor, 0, sizeof(sensor_t));
-
-  /* Insert the sensor name in the fixed length char array */
-  strncpy (sensor->name, "TSL2591", sizeof(sensor->name) - 1);
-  sensor->name[sizeof(sensor->name)- 1] = 0;
-  sensor->version     = 1;
-  sensor->sensor_id   = _sensorID;
-  sensor->type        = SENSOR_TYPE_LIGHT;
-  sensor->min_delay   = 0;
-  sensor->max_value   = 88000.0;
-  sensor->min_value   = 0.0;
-  sensor->resolution  = 1.0;
-}

+ 0 - 152
Adafruit_TSL2591.h

@@ -1,152 +0,0 @@
-/**************************************************************************/
-/*! 
-    @file     Adafruit_TSL2591.h
-    @author   KT0WN (adafruit.com)
-
-    This is a library for the Adafruit TSL2591 breakout board
-    This library works with the Adafruit TSL2591 breakout 
-    ----> https://www.adafruit.com/products/1980
-	
-    Check out the links above for our tutorials and wiring diagrams 
-    These chips use I2C to communicate
- 
-    Adafruit invests time and resources providing this open source code, 
-    please support Adafruit and open-source hardware by purchasing 
-    products from Adafruit!
-
-    @section LICENSE
-
-    Software License Agreement (BSD License)
-
-    Copyright (c) 2014 Adafruit Industries
-    All rights reserved.
-
-    Redistribution and use in source and binary forms, with or without
-    modification, are permitted provided that the following conditions are met:
-    1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
-    2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
-    3. Neither the name of the copyright holders nor the
-    names of its contributors may be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
-    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
-    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
-    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
-    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-/**************************************************************************/
-
-#ifndef _TSL2591_H_
-#define _TSL2591_H_
-
-#if ARDUINO >= 100
- #include <Arduino.h>
-#else
- #include <WProgram.h>
-#endif
-#include "Adafruit_Sensor.h"
-#include <Wire.h>
-
-#define TSL2591_VISIBLE           (2)       // channel 0 - channel 1
-#define TSL2591_INFRARED          (1)       // channel 1
-#define TSL2591_FULLSPECTRUM      (0)       // channel 0
-
-#define TSL2591_ADDR              (0x29)
-#define TSL2591_READBIT           (0x01)
-
-#define TSL2591_COMMAND_BIT       (0xA0)    // bits 7 and 5 for 'command normal'
-#define TSL2591_CLEAR_BIT         (0x40)    // Clears any pending interrupt (write 1 to clear)
-#define TSL2591_WORD_BIT          (0x20)    // 1 = read/write word (rather than byte)
-#define TSL2591_BLOCK_BIT         (0x10)    // 1 = using block read/write
-
-#define TSL2591_ENABLE_POWERON    (0x01)
-#define TSL2591_ENABLE_POWEROFF   (0x00)
-#define TSL2591_ENABLE_AEN        (0x02)
-#define TSL2591_ENABLE_AIEN       (0x10)
-
-#define TSL2591_CONTROL_RESET     (0x80)
-
-#define TSL2591_LUX_DF            (408.0F)
-#define TSL2591_LUX_COEFB         (1.64F)  // CH0 coefficient 
-#define TSL2591_LUX_COEFC         (0.59F)  // CH1 coefficient A
-#define TSL2591_LUX_COEFD         (0.86F)  // CH2 coefficient B
-
-enum
-{
-  TSL2591_REGISTER_ENABLE           = 0x00,
-  TSL2591_REGISTER_CONTROL          = 0x01,
-  TSL2591_REGISTER_THRESHHOLDL_LOW  = 0x02,
-  TSL2591_REGISTER_THRESHHOLDL_HIGH = 0x03,
-  TSL2591_REGISTER_THRESHHOLDH_LOW  = 0x04,
-  TSL2591_REGISTER_THRESHHOLDH_HIGH = 0x05,
-  TSL2591_REGISTER_INTERRUPT        = 0x06,
-  TSL2591_REGISTER_CRC              = 0x08,
-  TSL2591_REGISTER_ID               = 0x0A,
-  TSL2591_REGISTER_CHAN0_LOW        = 0x14,
-  TSL2591_REGISTER_CHAN0_HIGH       = 0x15,
-  TSL2591_REGISTER_CHAN1_LOW        = 0x16,
-  TSL2591_REGISTER_CHAN1_HIGH       = 0x17
-};
-
-typedef enum
-{
-  TSL2591_INTEGRATIONTIME_100MS     = 0x00,
-  TSL2591_INTEGRATIONTIME_200MS     = 0x01,
-  TSL2591_INTEGRATIONTIME_300MS     = 0x02,
-  TSL2591_INTEGRATIONTIME_400MS     = 0x03,
-  TSL2591_INTEGRATIONTIME_500MS     = 0x04,
-  TSL2591_INTEGRATIONTIME_600MS     = 0x05,
-}
-tsl2591IntegrationTime_t;
-
-typedef enum
-{
-  TSL2591_GAIN_LOW                  = 0x00,    // low gain (1x)
-  TSL2591_GAIN_MED                  = 0x10,    // medium gain (25x)
-  TSL2591_GAIN_HIGH                 = 0x20,    // medium gain (428x)
-  TSL2591_GAIN_MAX                  = 0x30,    // max gain (9876x)
-}
-tsl2591Gain_t;
-
-class Adafruit_TSL2591 : public Adafruit_Sensor
-{
- public:
-  Adafruit_TSL2591(int32_t sensorID = -1);
-  
-  boolean   begin   ( void );
-  void      enable  ( void );
-  void      disable ( void );
-  void      write8  ( uint8_t r, uint8_t v );
-  uint16_t  read16  ( uint8_t reg );
-  uint8_t   read8   ( uint8_t reg );
-
-  uint32_t  calculateLux  ( uint16_t ch0, uint16_t ch1 );
-  void      setGain       ( tsl2591Gain_t gain );
-  void      setTiming     ( tsl2591IntegrationTime_t integration );
-  uint16_t  getLuminosity (uint8_t channel );
-  uint32_t  getFullLuminosity ( );
-
-  tsl2591IntegrationTime_t getTiming();
-  tsl2591Gain_t            getGain();
-  
-  /* Unified Sensor API Functions */  
-  bool getEvent  ( sensors_event_t* );
-  void getSensor ( sensor_t* );
-
- private:
-  tsl2591IntegrationTime_t _integration;
-  tsl2591Gain_t _gain;
-  int32_t _sensorID;
-
-  boolean _initialized;
-};
-#endif

+ 2 - 1
README.md

@@ -18,4 +18,5 @@ Blue: SCL -> GPIO5
 
 Useful links:  
 <http://blog.abarbanell.de/arduino-esp8266/iot/i2c/>  
-<https://github.com/esp8266/Arduino#documentation>
+<https://github.com/esp8266/Arduino#documentation>  
+<https://github.com/adafruit/Adafruit_TSL2561>  

+ 1 - 1
makefile

@@ -19,7 +19,7 @@
 
 #=== Project specific definitions: sketch and list of needed libraries
 SKETCH ?= $(HOME)/makeEspArduino/WiFi_Temp_Logger/wifi_lux_sensor.cpp
-ADDITIONAL_FILES ?= Adafruit_TSL2591.cpp
+ADDITIONAL_FILES ?= Adafruit_TSL2561_U.cpp
 LIBS ?= \
 		$(ESP_LIBS)/ESP8266WiFi \
         $(ESP_LIBS)/Wire \

+ 66 - 14
wifi_lux_sensor.cpp

@@ -2,7 +2,7 @@
 #include <Wire.h> 
 
 #include "config.h"
-#include "Adafruit_TSL2591.h"
+#include "Adafruit_TSL2561_U.h"
 
 // ----------- DEFINES ---------------
 #define LUX_ID 0x39     // I2C-Id of the lux-sensor
@@ -10,7 +10,7 @@
 // ----------- GLOBALS ---------------
 WiFiClient espClient;
 
-Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
+Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(0x39, 12345);
 
 void connectWifi() {
   WiFi.disconnect(false);
@@ -37,31 +37,83 @@ void setup() {
   Serial.begin(38400); //Opens USB-Serial connection for terminal
   delay(1000);
   Serial.println("Init of WiFi-Lux-sensor project");
-  connectWifi();
+  //connectWifi();
   Wire.begin(4, 5); // sda on pin D2, scl on pin D1
 
   tsl.begin();
+
+  Serial.println("Set gain.");
+  // You can change the gain on the fly, to adapt to brighter/dimmer light situations
+  tsl.setGain(TSL2561_GAIN_1X);    // 1x gain (bright light)
+  //tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
+  //tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain
+  
+  // Changing the integration time gives you a longer time over which to sense light
+  // longer timelines are slower, but are good in very low light situtations!
+  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)
+  //tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
+  //tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
+  //tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
+  //tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
+  //tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)
+
+  tsl.enableAutoRange(true);
 }
 
+uint32_t readLuxSensor(void)
+{
+  uint16_t broadband;
+  uint16_t ir;
+  
+  tsl.getLuminosity(&broadband, &ir);
+  uint32_t lux = tsl.calculateLux(broadband, ir);
+  Serial.printf("Lux:%u  ", lux);
+
+  Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
+  Serial.print("IR: "); Serial.print(ir);  Serial.print("  ");
+  Serial.print("broadband: "); Serial.print(broadband); Serial.print("  ");
+  Serial.print("Visible: "); Serial.print(broadband - ir); Serial.println("  ");
+  
+  
+  
+  if ( (lux > 4294966000.0) ||
+       (lux <-4294966000.0) )
+  {
+    return 0;
+  }
+  else {
+    return lux;
+  }
+}
+
+unsigned int readI2CRegister16bit(int addr, int reg) {
+  Wire.beginTransmission(addr);
+  Wire.write(reg);
+  Wire.endTransmission();
+  delay(200);
+  Wire.requestFrom(addr, 2);
+  unsigned int t = Wire.read() << 8;
+  t = t | Wire.read();
+  return t;
+}
+
+
 void loop() {
   // put your main code here, to run repeatedly:
   byte error, address;
+
+
+  //unsigned int value = readI2CRegister16bit(0x39, 0xAC);
+
+  //Serial.printf("Value:%u\n", value);
   
+  /*
   Serial.print("WiFi heartbeat - ms since boot: ");
   Serial.print(millis());
   Serial.println();
+  */
 
-  Wire.beginTransmission(LUX_ID);
-  error = Wire.endTransmission();
-
-  if (error == 0) {
-    Serial.println("I2C-Lux device found");
-  }
-  else if (error==4) {
-    Serial.println("--Unknown error.");
-  }    
-
-  Serial.println("done\n");
+  readLuxSensor();
  
   delay(1000);