Procházet zdrojové kódy

Added files from AdaFruit-lib for TSL2561

Thomas Chef před 8 roky
rodič
revize
73161a98a7
6 změnil soubory, kde provedl 704 přidání a 44 odebrání
  1. 154 0
      Adafruit_Sensor.h
  2. 389 0
      Adafruit_TSL2591.cpp
  3. 152 0
      Adafruit_TSL2591.h
  4. 2 2
      README.md
  5. 3 2
      makefile
  6. 4 40
      wifi_lux_sensor.cpp

+ 154 - 0
Adafruit_Sensor.h

@@ -0,0 +1,154 @@
+/*
+* Copyright (C) 2008 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software< /span>
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+/* Update by K. Townsend (Adafruit Industries) for lighter typedefs, and
+ * extended sensor support to include color, voltage and current */
+ 
+#ifndef _ADAFRUIT_SENSOR_H
+#define _ADAFRUIT_SENSOR_H
+
+#if ARDUINO >= 100
+ #include "Arduino.h"
+ #include "Print.h"
+#else
+ #include "WProgram.h"
+#endif
+
+/* Intentionally modeled after sensors.h in the Android API:
+ * https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h */
+
+/* Constants */
+#define SENSORS_GRAVITY_EARTH             (9.80665F)              /**< Earth's gravity in m/s^2 */
+#define SENSORS_GRAVITY_MOON              (1.6F)                  /**< The moon's gravity in m/s^2 */
+#define SENSORS_GRAVITY_SUN               (275.0F)                /**< The sun's gravity in m/s^2 */
+#define SENSORS_GRAVITY_STANDARD          (SENSORS_GRAVITY_EARTH)
+#define SENSORS_MAGFIELD_EARTH_MAX        (60.0F)                 /**< Maximum magnetic field on Earth's surface */
+#define SENSORS_MAGFIELD_EARTH_MIN        (30.0F)                 /**< Minimum magnetic field on Earth's surface */
+#define SENSORS_PRESSURE_SEALEVELHPA      (1013.25F)              /**< Average sea level pressure is 1013.25 hPa */
+#define SENSORS_DPS_TO_RADS               (0.017453293F)          /**< Degrees/s to rad/s multiplier */
+#define SENSORS_GAUSS_TO_MICROTESLA       (100)                   /**< Gauss to micro-Tesla multiplier */
+
+/** Sensor types */
+typedef enum
+{
+  SENSOR_TYPE_ACCELEROMETER         = (1),   /**< Gravity + linear acceleration */
+  SENSOR_TYPE_MAGNETIC_FIELD        = (2),
+  SENSOR_TYPE_ORIENTATION           = (3),
+  SENSOR_TYPE_GYROSCOPE             = (4),
+  SENSOR_TYPE_LIGHT                 = (5),
+  SENSOR_TYPE_PRESSURE              = (6),
+  SENSOR_TYPE_PROXIMITY             = (8),
+  SENSOR_TYPE_GRAVITY               = (9),
+  SENSOR_TYPE_LINEAR_ACCELERATION   = (10),  /**< Acceleration not including gravity */
+  SENSOR_TYPE_ROTATION_VECTOR       = (11),
+  SENSOR_TYPE_RELATIVE_HUMIDITY     = (12),
+  SENSOR_TYPE_AMBIENT_TEMPERATURE   = (13),
+  SENSOR_TYPE_VOLTAGE               = (15),
+  SENSOR_TYPE_CURRENT               = (16),
+  SENSOR_TYPE_COLOR                 = (17)
+} sensors_type_t;
+
+/** struct sensors_vec_s is used to return a vector in a common format. */
+typedef struct {
+    union {
+        float v[3];
+        struct {
+            float x;
+            float y;
+            float z;
+        };
+        /* Orientation sensors */
+        struct {
+            float roll;    /**< Rotation around the longitudinal axis (the plane body, 'X axis'). Roll is positive and increasing when moving downward. -90°<=roll<=90° */
+            float pitch;   /**< Rotation around the lateral axis (the wing span, 'Y axis'). Pitch is positive and increasing when moving upwards. -180°<=pitch<=180°) */
+            float heading; /**< Angle between the longitudinal axis (the plane body) and magnetic north, measured clockwise when viewing from the top of the device. 0-359° */
+        };
+    };
+    int8_t status;
+    uint8_t reserved[3];
+} sensors_vec_t;
+
+/** struct sensors_color_s is used to return color data in a common format. */
+typedef struct {
+    union {
+        float c[3];
+        /* RGB color space */
+        struct {
+            float r;       /**< Red component */
+            float g;       /**< Green component */
+            float b;       /**< Blue component */
+        };
+    };
+    uint32_t rgba;         /**< 24-bit RGBA value */
+} sensors_color_t;
+
+/* Sensor event (36 bytes) */
+/** struct sensor_event_s is used to provide a single sensor event in a common format. */
+typedef struct
+{
+    int32_t version;                          /**< must be sizeof(struct sensors_event_t) */
+    int32_t sensor_id;                        /**< unique sensor identifier */
+    int32_t type;                             /**< sensor type */
+    int32_t reserved0;                        /**< reserved */
+    int32_t timestamp;                        /**< time is in milliseconds */
+    union
+    {
+        float           data[4];
+        sensors_vec_t   acceleration;         /**< acceleration values are in meter per second per second (m/s^2) */
+        sensors_vec_t   magnetic;             /**< magnetic vector values are in micro-Tesla (uT) */
+        sensors_vec_t   orientation;          /**< orientation values are in degrees */
+        sensors_vec_t   gyro;                 /**< gyroscope values are in rad/s */
+        float           temperature;          /**< temperature is in degrees centigrade (Celsius) */
+        float           distance;             /**< distance in centimeters */
+        float           light;                /**< light in SI lux units */
+        float           pressure;             /**< pressure in hectopascal (hPa) */
+        float           relative_humidity;    /**< relative humidity in percent */
+        float           current;              /**< current in milliamps (mA) */
+        float           voltage;              /**< voltage in volts (V) */
+        sensors_color_t color;                /**< color in RGB component values */
+    };
+} sensors_event_t;
+
+/* Sensor details (40 bytes) */
+/** struct sensor_s is used to describe basic information about a specific sensor. */
+typedef struct
+{
+    char     name[12];                        /**< sensor name */
+    int32_t  version;                         /**< version of the hardware + driver */
+    int32_t  sensor_id;                       /**< unique sensor identifier */
+    int32_t  type;                            /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */
+    float    max_value;                       /**< maximum value of this sensor's value in SI units */
+    float    min_value;                       /**< minimum value of this sensor's value in SI units */
+    float    resolution;                      /**< smallest difference between two values reported by this sensor */
+    int32_t  min_delay;                       /**< min delay in microseconds between events. zero = not a constant rate */
+} sensor_t;
+
+class Adafruit_Sensor {
+ public:
+  // Constructor(s)
+  Adafruit_Sensor() {}
+  virtual ~Adafruit_Sensor() {}
+
+  // These must be defined by the subclass
+  virtual void enableAutoRange(bool enabled) {};
+  virtual bool getEvent(sensors_event_t*) = 0;
+  virtual void getSensor(sensor_t*) = 0;
+  
+ private:
+  bool _autoRange;
+};
+
+#endif

+ 389 - 0
Adafruit_TSL2591.cpp

@@ -0,0 +1,389 @@
+/**************************************************************************/
+/*! 
+    @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;
+}

+ 152 - 0
Adafruit_TSL2591.h

@@ -0,0 +1,152 @@
+/**************************************************************************/
+/*! 
+    @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 - 2
README.md

@@ -10,12 +10,12 @@ WiFi ssid and password are stored in a file called config.h, which is not commit
 #define WIFI_PASSWORD "password"
 ```
 
-Cable-connection to sensor:
+Cable-connection to sensor:  
 Red: +3.3V  
 Black: GND  
 Yellow: SDA -> GPIO4  
 Blue: SCL -> GPIO5
 
-Useful links:
+Useful links:  
 <http://blog.abarbanell.de/arduino-esp8266/iot/i2c/>  
 <https://github.com/esp8266/Arduino#documentation>

+ 3 - 2
makefile

@@ -18,7 +18,8 @@
 #====================================================================================
 
 #=== Project specific definitions: sketch and list of needed libraries
-SKETCH ?= $(HOME)/makeEspArduino/WiFi_Temp_Logger/wifi_lux_sensor.ino
+SKETCH ?= $(HOME)/makeEspArduino/WiFi_Temp_Logger/wifi_lux_sensor.cpp
+ADDITIONAL_FILES ?= Adafruit_TSL2591.cpp
 LIBS ?= \
 		$(ESP_LIBS)/ESP8266WiFi \
         $(ESP_LIBS)/Wire \
@@ -106,7 +107,7 @@ CORE_OBJ = $(patsubst %,$(OBJ_DIR)/%$(OBJ_EXT),$(notdir $(CORE_SRC)))
 CORE_LIB = $(OBJ_DIR)/core.ar
 
 # User defined compilation units
-USER_SRC = $(SKETCH) $(shell find $(LIBS) -name "*.S" -o -name "*.c" -o -name "*.cpp")
+USER_SRC = $(SKETCH) $(ADDITIONAL_FILES) $(shell find $(LIBS) -name "*.S" -o -name "*.c" -o -name "*.cpp")
 # Object file suffix seems to be significant for the linker...
 USER_OBJ = $(subst .ino,.cpp,$(patsubst %,$(OBJ_DIR)/%$(OBJ_EXT),$(notdir $(USER_SRC))))
 USER_DIRS = $(sort $(dir $(USER_SRC)))

+ 4 - 40
wifi_lux_sensor.cpp

@@ -2,17 +2,16 @@
 #include <Wire.h> 
 
 #include "config.h"
-#include "lux_sensor.h"
+#include "Adafruit_TSL2591.h"
 
 // ----------- DEFINES ---------------
 #define LUX_ID 0x39     // I2C-Id of the lux-sensor
 
 // ----------- GLOBALS ---------------
-tsl2591Gain_t _gain;
-tsl2591IntegrationTime_t _integration;
-
 WiFiClient espClient;
 
+Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
+
 void connectWifi() {
   WiFi.disconnect(false);
   Serial.printf("Wi-Fi mode set to WIFI_STA: %s\n", WiFi.mode(WIFI_STA) ? "Ok" : "Failed!");
@@ -34,36 +33,6 @@ void connectWifi() {
 }
 
 
-
-void write8 (uint8_t reg, uint8_t value)
-{
-  Wire.beginTransmission(LUX_ID);
-  Wire.write(reg);
-  Wire.write(value);
-  Wire.endTransmission();
-}
-
-void enable(void)
-{
-  // 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 disable(void)
-{
-  // Disable the device by setting the control bit to 0x00
-  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
-}
-
-void setConfiguration(tsl2591Gain_t gain, tsl2591IntegrationTime_t integration) 
-{
-  enable();
-  _gain = gain;
-  _integration = integration;
-  write8( TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
-  disable();
-}
-
 void setup() {
   Serial.begin(38400); //Opens USB-Serial connection for terminal
   delay(1000);
@@ -71,12 +40,7 @@ void setup() {
   connectWifi();
   Wire.begin(4, 5); // sda on pin D2, scl on pin D1
 
-  // Config lux-sensor
-  _integration = TSL2591_INTEGRATIONTIME_100MS;
-  _gain        = TSL2591_GAIN_MED;
-
-  // Set default integration time and gain
-  setConfiguration(_gain, _integration);
+  tsl.begin();
 }
 
 void loop() {