TSL2561

Dependents:   Hexi_TSL2561 HexiHeart_Main

Fork of TSL2561 by Kenji Arai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TSL2561.h Source File

TSL2561.h

00001 /*
00002  * mbed library program
00003  *  Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output)
00004  *  TSL2561 by Texas Advanced Optoelectronic Solutions Inc.
00005  *
00006  * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
00007  *  http://www.page.sannet.ne.jp/kenjia/index.html
00008  *  http://mbed.org/users/kenjiArai/
00009  *      Created: Feburary   21st, 2015
00010  *      Revised: August     23rd, 2017
00011  */
00012 /*
00013  *---------------- REFERENCE ----------------------------------------------------------------------
00014  *  https://docs.google.com/viewer?url=http%3A%2F%2Fwww.adafruit.com%2Fdatasheets%2FTSL256x.pdf
00015  *  https://learn.adafruit.com/tsl2561?view=all
00016  *  http://www.adafruit.com/products/439
00017  *  http://akizukidenshi.com/catalog/g/gM-08219/
00018  */
00019 
00020 #ifndef TSL2561_H
00021 #define TSL2561_H
00022 
00023 #include "mbed.h"
00024 
00025 // Luminosity sensor, TSL2561
00026 // Address b7=0,b6=1,b5=1,b4=1,b3=0,b2=0,b1=1, b0=R/W
00027 #define TSL2561_ADDRESS_GND         (0x29 << 1)
00028 #define TSL2561_ADDRESS_FLOAT       (0x39 << 1)
00029 #define TSL2561_ADDRESS_VDD         (0x49 << 1)
00030 
00031 ////////////// Registers //////////////////////////////////
00032 // Register definition
00033 #define TSL2561_CONTROL             0x00
00034 #define TSL2561_TIMING              0x01
00035 #define TSL2561_THRESHLOWLOW        0x02
00036 #define TSL2561_THRESHHIGHLOW       0x04
00037 #define TSL2561_INTERRUPT           0x06
00038 #define TSL2561_CRC                 0x08
00039 #define TSL2561_ID                  0x0A
00040 #define TSL2561_DATA0LOW            0x0C
00041 #define TSL2561_DATA0HIGH           0x0D
00042 #define TSL2561_DATA1LOW            0x0E
00043 #define TSL2561_DATA1HIGH           0x0F
00044 
00045 ////////////// TIMING PARAMETER ///////////////////////////
00046 #define TIMING_GAIN_1               (0UL << 4)
00047 #define TIMING_GAIN_16              (1UL << 4)
00048 #define TIMING_TIME_13R7            (0x0)
00049 #define TIMING_TIME_101             (0x1)
00050 #define TIMING_TIME_402             (0x2)
00051 #define TIMING_TIME_MANU            (0x3)
00052 #define TIMING_DEFAULT              (TIMING_GAIN_1 + TIMING_TIME_402)
00053 
00054 ////////////// ID /////////////////////////////////////////
00055 #define I_AM_TSL2561                0x50
00056 #define REG_NO_MASK                 0x0F
00057 
00058 ////////////// COMMAND ////////////////////////////////////
00059 #define CMD_CMDMODE                 (1UL << 7)
00060 #define CMD_CLEAR                   (1UL << 6)
00061 #define CMD_WORD                    (1UL << 5)
00062 #define CMD_BLOCK                   (1UL << 4)
00063 #define CMD_SINGLE                  (CMD_CMDMODE)
00064 #define CMD_MULTI                   (CMD_CMDMODE + CMD_WORD)
00065 
00066 /** Interface for Luminosity sensor, TSL2561
00067  * @code
00068  * #include "mbed.h"
00069  * #include "TSL2561.h"
00070  *
00071  * // I2C Communication
00072  *  TSL2561      lum(dp5,dp27);    // TSL2561 SDA, SCL
00073  * // If you connected I2C line not only this device but also other devices,
00074  * //     you need to declare following method.
00075  *  I2C          i2c(dp5,dp27);    // SDA, SCL
00076  *  TSL2561      lum(i2c);         // TSL2561 SDA, SCL (Data available every 400mSec)
00077  *
00078  * int main() {;
00079  *   while(true){
00080  *      printf("Illuminance: %+7.2f [Lux]\r\n", lum.lux());
00081  *      wait(1.0);
00082  *   }
00083  * }
00084  * @endcode
00085  */
00086 
00087 class TSL2561
00088 {
00089 public:
00090     /** Configure data pin
00091       * @param data SDA and SCL pins
00092       */
00093     TSL2561(PinName p_sda, PinName p_scl);
00094     TSL2561(PinName p_sda, PinName p_scl, uint8_t addr);
00095 
00096     /** Configure data pin (with other devices on I2C line)
00097       * @param I2C previous definition
00098       */
00099     TSL2561(I2C& p_i2c);
00100     TSL2561(I2C& p_i2c, uint8_t addr);
00101 
00102     /** Get approximates the human eye response
00103       *  in the commonly used Illuminance unit of Lux
00104       * @param none
00105       * @return Lux
00106       */
00107     float lux(void);
00108 
00109     /** Set timing register
00110       * @param timing parameter
00111       * @return timing read data
00112       */
00113     uint8_t set_timing_reg(uint8_t parameter);
00114 
00115     /** Read timing register
00116       * @param timing parameter
00117       * @return timing read data
00118       */
00119     uint8_t read_timing_reg(void);
00120 
00121     /** Set I2C clock frequency
00122       * @param freq.
00123       * @return none
00124       */
00125     void frequency(int hz);
00126 
00127     /** check Device ID number
00128       * @param none
00129       * @return TSL2561 = 1, others  0
00130       */
00131     uint8_t who_am_i(void);
00132 
00133     /** Read ID and Revision Number
00134       * @param none
00135       * @return ID + REVNO
00136       */
00137     uint16_t read_ID(void);
00138 
00139     /** Power Up/Down
00140       * @param none
00141       * @return none
00142       */
00143     void power_up(void);
00144     void power_down(void);
00145 
00146 protected:
00147     I2C *_i2c_p;
00148     I2C &_i2c;
00149 
00150     void init(void);
00151 
00152 private:
00153     uint8_t  TSL2561_addr;
00154     uint8_t  dt[4];
00155     uint32_t ch0;
00156     uint32_t ch1;
00157     int8_t   gain;
00158     uint16_t  id_number;
00159     double   integ_time;
00160 };
00161 
00162 #endif      // TSL2561_H