Kenji Arai / TSL2561

Dependents:   MusicBoxForFathersDay FTHR_SensorHub Affich_Lum_Moist Projetv0 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TSL2561.h Source File

TSL2561.h

00001 /*!
00002  * @file Adafruit_TSL2561_U.h
00003  *
00004  * This is part of Adafruit's FXOS8700 driver for the Arduino platform.  It is
00005  * designed specifically to work with the Adafruit FXOS8700 breakout:
00006  * https://www.adafruit.com/products/3463
00007  *
00008  * These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
00009  * to interface with the breakout.
00010  *
00011  * Adafruit invests time and resources providing this open source code,
00012  * please support Adafruit and open-source hardware by purchasing
00013  * products from Adafruit!
00014  *
00015  * Written by Kevin "KTOWN" Townsend for Adafruit Industries.
00016  *
00017  * BSD license, all text here must be included in any redistribution.
00018  *
00019  */
00020 
00021 //------- Modified by ----------------------------------------------------------
00022 //  Kenji Arai / JH1PJL
00023 //  http://www.page.sannet.ne.jp/kenjia/index.html
00024 //  http://mbed.org/users/kenjiArai/
00025 //      Created: Feburary   21st, 2015
00026 //      Revised: August     23rd, 2017
00027 //      Revised: Feburary   20th, 2018   bug fix -> read_ID() & who_am_i()
00028 //                                       Thanks PARK JAICHANG
00029 //      Revised: March      31st, 2018   Added "Auto Range mode"
00030 //                                       Use Adafruit library
00031 //
00032 // Original information     https://www.adafruit.com/product/439
00033 // Original source files    https://github.com/adafruit/TSL2561-Arduino-Library
00034 //                          https://github.com/adafruit/Adafruit_TSL2561
00035 // Change for Mbed platform
00036 //    modified -> all related files
00037 //------------------------------------------------------------------------------
00038 
00039 #ifndef TSL2561_H_
00040 #define TSL2561_H_
00041 
00042 #include "mbed.h"
00043 
00044 // Luminosity sensor, TSL2561
00045 // Address b7=0,b6=1,b5=1,b4=1,b3=0,b2=0,b1=1, b0=R/W
00046 #define TSL2561_ADDRESS_GND       (0x29 << 1)
00047 #define TSL2561_ADDR_LOW          (0x29 << 1)
00048 #define TSL2561_ADDRESS_FLOAT     (0x39 << 1)
00049 #define TSL2561_ADDR_FLOAT        (0x39 << 1)
00050 #define TSL2561_ADDRESS_VDD       (0x49 << 1)
00051 #define TSL2561_ADDR_HIGH         (0x49 << 1)
00052 
00053 // Lux calculations differ slightly for CS package
00054 //#define TSL2561_PACKAGE_CS                ///< Chip scale package
00055 #define TSL2561_PACKAGE_T_FN_CL             ///< Dual Flat No-Lead package
00056 
00057 // ID
00058 #define I_AM_TSL2561CS             0x01
00059 #define I_AM_TSL2561T_FN_CL        0x05
00060 
00061 // COMMAND register bit definition
00062 #define TSL2561_COMMAND_BIT       (0x80)    ///< Must be 1
00063 ///< Clears any pending interrupt (write 1 to clear)
00064 #define TSL2561_CLEAR_BIT         (0x40)
00065 ///< 1 = read/write word (rather than byte)
00066 #define TSL2561_WORD_BIT          (0x20)
00067 ///< 1 = using block read/write
00068 #define TSL2561_BLOCK_BIT         (0x10)
00069 
00070 // Control register setting to turn on/off
00071 #define TSL2561_CONTROL_POWERON   (0x03)
00072 #define TSL2561_CONTROL_POWEROFF  (0x00)
00073 
00074 #define TSL2561_LUX_LUXSCALE      (14)      ///< Scale by 2^14
00075 #define TSL2561_LUX_RATIOSCALE    (9)       ///< Scale ratio by 2^9
00076 #define TSL2561_LUX_CHSCALE       (10)      ///< Scale channel values by 2^10
00077 #define TSL2561_LUX_CHSCALE_TINT0 (0x7517)  ///< 322/11 * 2^TSL2561_LUX_CHSCALE
00078 #define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7)  ///< 322/81 * 2^TSL2561_LUX_CHSCALE
00079 
00080 /** TSL2561 I2C Registers */
00081 enum {
00082     TSL2561_REGISTER_CONTROL          = 0x00, // Control/power register
00083     TSL2561_REGISTER_TIMING           = 0x01, // Set integration time register
00084     TSL2561_REGISTER_THRESHHOLDL_LOW  = 0x02, // Interrupt low threshold low-byte
00085     TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03, // Interrupt low threshold high-byte
00086     TSL2561_REGISTER_THRESHHOLDH_LOW  = 0x04, // Interrupt high threshold low-byte
00087     TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05, // Interrupt high threshold high-byte
00088     TSL2561_REGISTER_INTERRUPT        = 0x06, // Interrupt settings
00089     TSL2561_REGISTER_CRC              = 0x08, // Factory use only
00090     TSL2561_REGISTER_ID               = 0x0A, // TSL2561 identification setting
00091     TSL2561_REGISTER_CHAN0_LOW        = 0x0C, // Light data channel 0, low byte
00092     TSL2561_REGISTER_CHAN0_HIGH       = 0x0D, // Light data channel 0, high byte
00093     TSL2561_REGISTER_CHAN1_LOW        = 0x0E, // Light data channel 1, low byte
00094     TSL2561_REGISTER_CHAN1_HIGH       = 0x0F  // Light data channel 1, high byte
00095 };
00096 
00097 /** Three options for how long to integrate readings for */
00098 typedef enum {
00099     TSL2561_INTEGRATIONTIME_13MS  = 0x00,    // 13.7ms
00100     TSL2561_INTEGRATIONTIME_101MS = 0x01,    // 101ms
00101     TSL2561_INTEGRATIONTIME_402MS = 0x02     // 402ms
00102 }
00103 TSL2561IntegrationTime_t;
00104 
00105 /** TSL2561 offers 2 gain settings */
00106 typedef enum {
00107     TSL2561_GAIN_1X               = 0x00,    // No gain
00108     TSL2561_GAIN_16X              = 0x10,    // 16x gain
00109 }
00110 TSL2561Gain_t;
00111 
00112 /** struct sensors_color_s is used to return color data in a common format. */
00113 typedef struct {
00114     union {
00115         float c[3];
00116         /* RGB color space */
00117         struct {
00118             float r;       /**< Red component */
00119             float g;       /**< Green component */
00120             float b;       /**< Blue component */
00121         };
00122     };
00123     uint32_t rgba;         /**< 24-bit RGBA value */
00124 } sensors_color_t;
00125 
00126 #define SENSOR_TYPE_LIGHT       5
00127 
00128 /* Sensor details */
00129 /** struct sensor_s is used to describe basic information
00130         about a specific sensor. */
00131 typedef struct {
00132     float    max_value; /**< maximum value of this sensor's value in SI units */
00133     float    min_value; /**< minimum value of this sensor's value in SI units */
00134     /**< smallest difference between two values reported by this sensor */
00135     float    resolution;
00136     /**< min delay in microseconds between events. zero = not a constant rate */
00137     int32_t  min_delay;
00138 } sensor_t;
00139 
00140 /** Interface for Luminosity sensor, TSL2561
00141  * @code
00142  * #include "mbed.h"
00143  * #include "TSL2561.h"
00144  *
00145  * // I2C Communication
00146  *  TSL2561      lum(dp5,dp27);    // TSL2561 SDA, SCL
00147  * // If you connected I2C line not only this device but also other devices,
00148  * //     you need to declare following method.
00149  *  I2C          i2c(dp5,dp27); // SDA, SCL
00150  *  TSL2561      lum(i2c); // TSL2561 SDA, SCL (Data available every 400mSec)
00151  *
00152  * int main() {;
00153  *   while(true){
00154  *      printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux());
00155  *      wait(1.0);
00156  *   }
00157  * }
00158  * @endcode
00159  */
00160 
00161 /**************************************************************************/
00162 /*!
00163     @brief  Class that stores state and functions
00164                  for interacting with TSL2561 Light Sensor
00165 */
00166 /**************************************************************************/
00167 class TSL2561
00168 {
00169 public:
00170     /** Configure data pin
00171       * @param data SDA and SCL pins
00172       */
00173     TSL2561(PinName p_sda, PinName p_scl);
00174     TSL2561(PinName p_sda, PinName p_scl, uint8_t addr);
00175 
00176     /** Configure data pin (with other devices on I2C line)
00177       * @param I2C previous definition
00178       */
00179     TSL2561(I2C& p_i2c);
00180     TSL2561(I2C& p_i2c, uint8_t addr);
00181 
00182     /** Get approximates the human eye response
00183       *  in the commonly used Illuminance unit of Lux
00184       * @param none
00185       * @return Lux
00186       */
00187     float lux(void);
00188 
00189     /** Get approximates the human eye response with "Auto Range"
00190       *  in the commonly used Illuminance unit of Lux
00191       * @param none
00192       * @return Lux
00193       */
00194     float lux_auto(void);
00195 
00196     /** Set timing register
00197       * @param timing parameter
00198       * @return timing read data
00199       */
00200     uint8_t set_timing_reg(uint8_t parameter);
00201 
00202     /** Read timing register
00203       * @param timing parameter
00204       * @return timing read data
00205       */
00206     uint8_t read_timing_reg(void);
00207 
00208     /** Set I2C clock frequency
00209       * @param freq.
00210       * @return none
00211       */
00212     void frequency(int hz);
00213 
00214     /** check Device ID number
00215       * @param none
00216       * @return TSL2561 = 1, others  0
00217       */
00218     uint8_t who_am_i(void);
00219 
00220     /** Read ID and Revision Number
00221       * @param none
00222       * @return ID + REVNO
00223       */
00224     uint8_t read_ID(void);
00225 
00226     /** Power Up/Down
00227       * @param none
00228       * @return none
00229       */
00230     void power_up(void);
00231     void power_down(void);
00232 
00233     //---------- Adafruit_TSL2561_U.h Original functions part ------------------
00234     bool init();
00235 
00236     /* TSL2561 Functions */
00237     void enableAutoRange(bool enable);
00238     void setIntegrationTime(TSL2561IntegrationTime_t time);
00239     void setGain(TSL2561Gain_t gain);
00240     void getLuminosity (uint16_t *broadband, uint16_t *ir);
00241     uint32_t calculateLux(uint16_t broadband, uint16_t ir);
00242 
00243     /* Unified Sensor API Functions */
00244     bool getEvent(uint32_t*);
00245     void getSensor(sensor_t*);
00246 
00247 private:
00248     I2C *_i2c_p;
00249     I2C &_i2c;
00250     Timer   t;
00251 
00252     int8_t  _addr;
00253     bool    _TSL2561Initialised;
00254     bool    _TSL2561AutoGain;
00255     TSL2561IntegrationTime_t _TSL2561IntegrationTime;
00256     TSL2561Gain_t _TSL2561Gain;
00257     uint32_t ch0;
00258     uint32_t ch1;
00259     int8_t   gain;
00260     uint8_t  id_number;
00261     float    integ_time;
00262 
00263     uint8_t  dt[4];
00264 
00265     void     enable (void);
00266     void     disable (void);
00267     void     write8 (uint8_t reg, uint8_t value);
00268     uint8_t  read8 (uint8_t reg);
00269     uint16_t read16 (uint8_t reg);
00270     void     getData (uint16_t *broadband, uint16_t *ir);
00271 };
00272 
00273 #endif // TSL2561_H