A lux sensor library
Diff: TSL2561.h
- Revision:
- 2:c45d34e9c3f0
- Parent:
- 1:52b855c13204
--- a/TSL2561.h Tue Aug 04 17:15:01 2015 +0000 +++ b/TSL2561.h Wed Aug 12 19:57:46 2015 +0000 @@ -7,16 +7,16 @@ #define _TSL2561_H_ #include "mbed.h" -//#define TSL2561_ADDRESS (0x39) +//#define TSL2561_ADDRESS (0x72) #define TSL2561_VISIBLE 2 // channel 0 - channel 1 #define TSL2561_INFRARED 1 // channel 1 #define TSL2561_FULLSPECTRUM 0 // channel 0 // 3 i2c address options! -//#define TSL2561_ADDR_LOW 0x29 -#define TSL2561_ADDR_FLOAT (0x39) -//#define TSL2561_ADDR_HIGH 0x49 +//#define TSL2561_ADDR_LOW 0x52 +#define TSL2561_ADDR_FLOAT (0x72) +//#define TSL2561_ADDR_HIGH 0x92 // Lux calculations differ slightly for CS package //#define TSL2561_PACKAGE_CS @@ -32,7 +32,7 @@ #define TSL2561_CONTROL_POWERON (0x03) #define TSL2561_CONTROL_POWEROFF (0x00) -//ID +//ID #define I_AM_TSL2561 0x50 #define REG_NO_MASK 0x0F @@ -176,36 +176,32 @@ #define M8C 0x0000 // 0.000 * 2^LUX_SCALE //Enum of all the register addresses as not to put hex numbers in .cpp - -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 +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 +typedef enum { + TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms + TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms + TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms } tsl2561IntegrationTime_t; -typedef enum -{ - TSL2561_GAIN_0X = 0x00, // No gain - TSL2561_GAIN_16X = 0x10, // 16x gain +typedef enum { + TSL2561_GAIN_0X = 0x00, // No gain + TSL2561_GAIN_16X = 0x10, // 16x gain } tsl2561Gain_t; @@ -213,76 +209,55 @@ class TSL2561 { - public: +public: //create instances TSL2561(PinName sda, PinName scl, char slave_address = TSL2561_ADDR_FLOAT); - TSL2561(I2C &i2c_obj, char slave_adr = TSL2561_ADDR_FLOAT); - + //destructor - //virtual ~TSL2561() - + virtual ~TSL2561(); + /** Begin Initialization SI1145 sensor * Configure sensor setting and read parameters for calibration */ - //check id register before proceeding - bool begin(void); - - //Enable sensor using control register + bool initialize(void); + + //Enable sensor using control register void enable(void); - //Disable sensor using control register void disable(void); + + //set the integration time and gain using command, timing, and gain registers + void setTimingGain(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain); - //set the integration time using command and timing registers - void setTiming(tsl2561IntegrationTime_t integration); + //read channal 0 and 1, adjust the gain if needed, read again(repeat), and then calculate lux. + void read_lux(void); - //set the gain using command and timing registers - void setGain(tsl2561Gain_t gain); + uint32_t lux; //Final data + uint16_t channal0; //Raw data of vis and ir + uint16_t channal1; //Raw data of just ir + + + //Read data registers than convert to a lux value uint32_t calculateLux(uint16_t ch0, uint16_t ch1); - + //get luminosity from data registers uint32_t getFullLuminosity (void); uint16_t getLuminosity (uint8_t channel); - - - //function to read data - uint16_t get_lux(void); - - private: + + + + +private: I2C *i2c_p; I2C &i2c; char address; - tsl2561IntegrationTime_t _integration; - tsl2561Gain_t _gain; - bool _initialized; - int32_t t_fine; - - + tsl2561IntegrationTime_t _integration; + tsl2561Gain_t _gain; + + }; -#endif -/*class TSL2561 { - public: - TSL2561(uint8_t addr); - boolean begin(void); - void enable(void); - void disable(void); - void write8(uint8_t r, uint8_t v); - uint16_t read16(uint8_t reg); - - uint32_t calculateLux(uint16_t ch0, uint16_t ch1); - void setTiming(tsl2561IntegrationTime_t integration); - void setGain(tsl2561Gain_t gain); - uint16_t getLuminosity (uint8_t channel); - uint32_t getFullLuminosity (); - - private: - int8_t _addr; - tsl2561IntegrationTime_t _integration; - tsl2561Gain_t _gain; - - boolean _initialized; -};*/ +#endif