Light sensor library

Fork of TSL2561 by Tuan Anh Nguyen

Committer:
anhnt2407
Date:
Tue Sep 10 15:13:49 2013 +0000
Revision:
0:df9baf6459b7
Child:
2:9a0788c18d78
20130911-TSL2561_Townsend

Who changed what in which revision?

UserRevisionLine numberNew contents of line
anhnt2407 0:df9baf6459b7 1
anhnt2407 0:df9baf6459b7 2 #ifndef _TSL2561_H_
anhnt2407 0:df9baf6459b7 3 #define _TSL2561_H_
anhnt2407 0:df9baf6459b7 4
anhnt2407 0:df9baf6459b7 5 #include "mbed.h"
anhnt2407 0:df9baf6459b7 6
anhnt2407 0:df9baf6459b7 7 #define TSL2561_I2C_PINNAME_SDA p28
anhnt2407 0:df9baf6459b7 8 #define TSL2561_I2C_PINNAME_SCL p27
anhnt2407 0:df9baf6459b7 9
anhnt2407 0:df9baf6459b7 10 #define TSL2561_VISIBLE 2 // channel 0 - channel 1
anhnt2407 0:df9baf6459b7 11 #define TSL2561_INFRARED 1 // channel 1
anhnt2407 0:df9baf6459b7 12 #define TSL2561_FULLSPECTRUM 0 // channel 0
anhnt2407 0:df9baf6459b7 13
anhnt2407 0:df9baf6459b7 14 // 3 i2c address options!
anhnt2407 0:df9baf6459b7 15 #define TSL2561_ADDR_LOW 0x29
anhnt2407 0:df9baf6459b7 16 #define TSL2561_ADDR_FLOAT 0x39
anhnt2407 0:df9baf6459b7 17 #define TSL2561_ADDR_HIGH 0x49
anhnt2407 0:df9baf6459b7 18
anhnt2407 0:df9baf6459b7 19 // Lux calculations differ slightly for CS package
anhnt2407 0:df9baf6459b7 20 //#define TSL2561_PACKAGE_CS
anhnt2407 0:df9baf6459b7 21 #define TSL2561_PACKAGE_T_FN_CL
anhnt2407 0:df9baf6459b7 22
anhnt2407 0:df9baf6459b7 23 #define TSL2561_READBIT (0x01)
anhnt2407 0:df9baf6459b7 24
anhnt2407 0:df9baf6459b7 25 #define TSL2561_COMMAND_BIT (0x80) // Must be 1
anhnt2407 0:df9baf6459b7 26 #define TSL2561_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear)
anhnt2407 0:df9baf6459b7 27 #define TSL2561_WORD_BIT (0x20) // 1 = read/write word (rather than byte)
anhnt2407 0:df9baf6459b7 28 #define TSL2561_BLOCK_BIT (0x10) // 1 = using block read/write
anhnt2407 0:df9baf6459b7 29
anhnt2407 0:df9baf6459b7 30 #define TSL2561_CONTROL_POWERON (0x03)
anhnt2407 0:df9baf6459b7 31 #define TSL2561_CONTROL_POWEROFF (0x00)
anhnt2407 0:df9baf6459b7 32
anhnt2407 0:df9baf6459b7 33 #define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14
anhnt2407 0:df9baf6459b7 34 #define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9
anhnt2407 0:df9baf6459b7 35 #define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10
anhnt2407 0:df9baf6459b7 36 #define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE
anhnt2407 0:df9baf6459b7 37 #define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE
anhnt2407 0:df9baf6459b7 38
anhnt2407 0:df9baf6459b7 39 // T, FN and CL package values
anhnt2407 0:df9baf6459b7 40 #define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 41 #define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 42 #define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 43 #define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 44 #define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 45 #define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 46 #define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 47 #define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 48 #define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 49 #define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 50 #define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 51 #define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 52 #define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 53 #define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 54 #define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 55 #define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 56 #define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 57 #define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 58 #define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 59 #define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 60 #define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 61 #define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 62 #define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 63 #define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 64
anhnt2407 0:df9baf6459b7 65 // CS package values
anhnt2407 0:df9baf6459b7 66 #define TSL2561_LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 67 #define TSL2561_LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 68 #define TSL2561_LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 69 #define TSL2561_LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 70 #define TSL2561_LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 71 #define TSL2561_LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 72 #define TSL2561_LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 73 #define TSL2561_LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 74 #define TSL2561_LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 75 #define TSL2561_LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 76 #define TSL2561_LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 77 #define TSL2561_LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 78 #define TSL2561_LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 79 #define TSL2561_LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 80 #define TSL2561_LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 81 #define TSL2561_LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 82 #define TSL2561_LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 83 #define TSL2561_LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 84 #define TSL2561_LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 85 #define TSL2561_LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 86 #define TSL2561_LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 87 #define TSL2561_LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE
anhnt2407 0:df9baf6459b7 88 #define TSL2561_LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 89 #define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE
anhnt2407 0:df9baf6459b7 90
anhnt2407 0:df9baf6459b7 91 enum
anhnt2407 0:df9baf6459b7 92 {
anhnt2407 0:df9baf6459b7 93 TSL2561_REGISTER_CONTROL = 0x00,
anhnt2407 0:df9baf6459b7 94 TSL2561_REGISTER_TIMING = 0x01,
anhnt2407 0:df9baf6459b7 95 TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02,
anhnt2407 0:df9baf6459b7 96 TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03,
anhnt2407 0:df9baf6459b7 97 TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04,
anhnt2407 0:df9baf6459b7 98 TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05,
anhnt2407 0:df9baf6459b7 99 TSL2561_REGISTER_INTERRUPT = 0x06,
anhnt2407 0:df9baf6459b7 100 TSL2561_REGISTER_CRC = 0x08,
anhnt2407 0:df9baf6459b7 101 TSL2561_REGISTER_ID = 0x0A,
anhnt2407 0:df9baf6459b7 102 TSL2561_REGISTER_CHAN0_LOW = 0x0C,
anhnt2407 0:df9baf6459b7 103 TSL2561_REGISTER_CHAN0_HIGH = 0x0D,
anhnt2407 0:df9baf6459b7 104 TSL2561_REGISTER_CHAN1_LOW = 0x0E,
anhnt2407 0:df9baf6459b7 105 TSL2561_REGISTER_CHAN1_HIGH = 0x0F
anhnt2407 0:df9baf6459b7 106 };
anhnt2407 0:df9baf6459b7 107
anhnt2407 0:df9baf6459b7 108 typedef enum
anhnt2407 0:df9baf6459b7 109 {
anhnt2407 0:df9baf6459b7 110 TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms
anhnt2407 0:df9baf6459b7 111 TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms
anhnt2407 0:df9baf6459b7 112 TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms
anhnt2407 0:df9baf6459b7 113 }
anhnt2407 0:df9baf6459b7 114 tsl2561IntegrationTime_t;
anhnt2407 0:df9baf6459b7 115
anhnt2407 0:df9baf6459b7 116 typedef enum
anhnt2407 0:df9baf6459b7 117 {
anhnt2407 0:df9baf6459b7 118 TSL2561_GAIN_0X = 0x00, // No gain
anhnt2407 0:df9baf6459b7 119 TSL2561_GAIN_16X = 0x10, // 16x gain
anhnt2407 0:df9baf6459b7 120 }
anhnt2407 0:df9baf6459b7 121 tsl2561Gain_t;
anhnt2407 0:df9baf6459b7 122
anhnt2407 0:df9baf6459b7 123 class TSL2561 {
anhnt2407 0:df9baf6459b7 124
anhnt2407 0:df9baf6459b7 125 public:
anhnt2407 0:df9baf6459b7 126 //---CLASS CONSTRUCTOR---//
anhnt2407 0:df9baf6459b7 127 TSL2561();
anhnt2407 0:df9baf6459b7 128 TSL2561(uint8_t addr);
anhnt2407 0:df9baf6459b7 129 TSL2561(PinName sda, PinName scl);
anhnt2407 0:df9baf6459b7 130 TSL2561(PinName sda, PinName scl, uint8_t addr);
anhnt2407 0:df9baf6459b7 131
anhnt2407 0:df9baf6459b7 132 bool begin(void);
anhnt2407 0:df9baf6459b7 133 void enable(void);
anhnt2407 0:df9baf6459b7 134 void disable(void);
anhnt2407 0:df9baf6459b7 135 void write8(uint8_t r, uint8_t v);
anhnt2407 0:df9baf6459b7 136 uint16_t read16(uint8_t reg);
anhnt2407 0:df9baf6459b7 137
anhnt2407 0:df9baf6459b7 138 uint32_t calculateLux(uint16_t ch0, uint16_t ch1);
anhnt2407 0:df9baf6459b7 139 void setTiming(tsl2561IntegrationTime_t integration);
anhnt2407 0:df9baf6459b7 140 void setGain(tsl2561Gain_t gain);
anhnt2407 0:df9baf6459b7 141 uint16_t getLuminosity (uint8_t channel);
anhnt2407 0:df9baf6459b7 142 uint32_t getFullLuminosity ();
anhnt2407 0:df9baf6459b7 143
anhnt2407 0:df9baf6459b7 144 private:
anhnt2407 0:df9baf6459b7 145 I2C i2c;
anhnt2407 0:df9baf6459b7 146 int8_t _addr;
anhnt2407 0:df9baf6459b7 147 tsl2561IntegrationTime_t _integration;
anhnt2407 0:df9baf6459b7 148 tsl2561Gain_t _gain;
anhnt2407 0:df9baf6459b7 149
anhnt2407 0:df9baf6459b7 150 bool _initialized;
anhnt2407 0:df9baf6459b7 151 };
anhnt2407 0:df9baf6459b7 152 #endif