Adafruit TSL2591 sensor

Dependencies:   mbed

Committer:
12104404
Date:
Mon Apr 04 09:09:12 2016 +0000
Revision:
4:66ce66d4c07c
Parent:
3:fecb1929cbef
<<1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
12104404 0:815555c72774 1 #include "TSL2591.h"
12104404 0:815555c72774 2
12104404 0:815555c72774 3 TSL2591::TSL2591 (I2C& tsl2591_i2c, uint8_t tsl2591_addr):
12104404 4:66ce66d4c07c 4 _i2c(tsl2591_i2c), _addr(tsl2591_addr<<1)
12104404 0:815555c72774 5 {
12104404 0:815555c72774 6 _init = false;
12104404 0:815555c72774 7 _integ = TSL2591_INTT_100MS;
12104404 2:dd10c541a3dc 8 _gain = TSL2591_GAIN_LOW;
12104404 0:815555c72774 9 }
12104404 0:815555c72774 10 /*
12104404 3:fecb1929cbef 11 * Initialize TSL2591
12104404 3:fecb1929cbef 12 * Checks ID and sets gain and integration time
12104404 0:815555c72774 13 */
12104404 0:815555c72774 14 bool TSL2591::init(void)
12104404 0:815555c72774 15 {
12104404 0:815555c72774 16 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ID)};
12104404 4:66ce66d4c07c 17 if(_i2c.write(_addr, write, 1, 0) == 0) {
12104404 0:815555c72774 18 char read[1];
12104404 4:66ce66d4c07c 19 _i2c.read(_addr, read, 1, 0);
12104404 0:815555c72774 20 if(read[0] == TSL2591_ID) {
12104404 0:815555c72774 21 _init = true;
12104404 2:dd10c541a3dc 22 setGain(TSL2591_GAIN_LOW);
12104404 1:308cc5302475 23 setTime(TSL2591_INTT_100MS);
12104404 0:815555c72774 24 disable();
12104404 0:815555c72774 25 return true;
12104404 0:815555c72774 26 }
12104404 0:815555c72774 27 }
12104404 0:815555c72774 28 return false;
12104404 0:815555c72774 29 }
12104404 3:fecb1929cbef 30 /*
12104404 3:fecb1929cbef 31 * Power On TSL2591
12104404 3:fecb1929cbef 32 */
12104404 0:815555c72774 33 void TSL2591::enable(void)
12104404 0:815555c72774 34 {
12104404 0:815555c72774 35 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_PON|TSL2591_EN_AEN|TSL2591_EN_AIEN|TSL2591_EN_NPIEN)};
12104404 4:66ce66d4c07c 36 _i2c.write(_addr, write, 2, 0);
12104404 0:815555c72774 37 }
12104404 3:fecb1929cbef 38 /*
12104404 3:fecb1929cbef 39 * Power Off TSL2591
12104404 3:fecb1929cbef 40 */
12104404 0:815555c72774 41 void TSL2591::disable(void)
12104404 0:815555c72774 42 {
12104404 0:815555c72774 43 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_POFF)};
12104404 4:66ce66d4c07c 44 _i2c.write(_addr, write, 2, 0);
12104404 0:815555c72774 45 }
12104404 3:fecb1929cbef 46 /*
12104404 3:fecb1929cbef 47 * Set Gain and Write
12104404 3:fecb1929cbef 48 * Set gain and write time and gain
12104404 3:fecb1929cbef 49 */
12104404 0:815555c72774 50 void TSL2591::setGain(tsl2591Gain_t gain)
12104404 0:815555c72774 51 {
12104404 0:815555c72774 52 enable();
12104404 0:815555c72774 53 _gain = gain;
12104404 0:815555c72774 54 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_CONTROL), (_integ|_gain)};
12104404 4:66ce66d4c07c 55 _i2c.write(_addr, write, 2, 0);
12104404 0:815555c72774 56 disable();
12104404 0:815555c72774 57 }
12104404 3:fecb1929cbef 58 /*
12104404 3:fecb1929cbef 59 * Set Integration Time and Write
12104404 3:fecb1929cbef 60 * Set gain and write time and gain
12104404 3:fecb1929cbef 61 */
12104404 1:308cc5302475 62 void TSL2591::setTime(tsl2591IntegrationTime_t integ)
12104404 0:815555c72774 63 {
12104404 0:815555c72774 64 enable();
12104404 0:815555c72774 65 _integ = integ;
12104404 0:815555c72774 66 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_CONTROL), (_integ|_gain)};
12104404 4:66ce66d4c07c 67 _i2c.write(_addr, write, 2, 0);
12104404 0:815555c72774 68 disable();
12104404 0:815555c72774 69 }
12104404 3:fecb1929cbef 70 /*
12104404 3:fecb1929cbef 71 * Read ALS
12104404 3:fecb1929cbef 72 * Read full spectrum, infrared, and visible
12104404 3:fecb1929cbef 73 */
12104404 0:815555c72774 74 void TSL2591::getALS(void)
12104404 0:815555c72774 75 {
12104404 0:815555c72774 76 enable();
12104404 1:308cc5302475 77 for(uint8_t t=0; t<=_integ+1; t++) {
12104404 1:308cc5302475 78 wait(0.12);
12104404 0:815555c72774 79 }
12104404 1:308cc5302475 80 char write1[] = {(TSL2591_CMD_BIT|TSL2591_REG_CHAN1_L)};
12104404 4:66ce66d4c07c 81 _i2c.write(_addr, write1, 1, 0);
12104404 0:815555c72774 82 char read1[2];
12104404 4:66ce66d4c07c 83 _i2c.read(_addr, read1, 2, 0);
12104404 1:308cc5302475 84 char write2[] = {(TSL2591_CMD_BIT|TSL2591_REG_CHAN0_L)};
12104404 4:66ce66d4c07c 85 _i2c.write(_addr, write2, 1, 0);
12104404 0:815555c72774 86 char read2[2];
12104404 4:66ce66d4c07c 87 _i2c.read(_addr, read2, 2, 0);
12104404 3:fecb1929cbef 88 rawALS = (((read1[1]<<8)|read1[0])<<16)|((read2[1]<<8)|read2[0]);
12104404 0:815555c72774 89 disable();
12104404 3:fecb1929cbef 90 full = rawALS & 0xFFFF;
12104404 3:fecb1929cbef 91 ir = rawALS >> 16;
12104404 2:dd10c541a3dc 92 visible = full - ir;
12104404 2:dd10c541a3dc 93 }
12104404 3:fecb1929cbef 94 /*
12104404 3:fecb1929cbef 95 * Calculate Lux
12104404 3:fecb1929cbef 96 */
12104404 2:dd10c541a3dc 97 void TSL2591::calcLux(void)
12104404 2:dd10c541a3dc 98 {
12104404 2:dd10c541a3dc 99 float atime, again, cpl, lux1, lux2, lux3;
12104404 2:dd10c541a3dc 100 if((full == 0xFFFF)|(ir == 0xFFFF)) {
12104404 2:dd10c541a3dc 101 lux3 = 0;
12104404 2:dd10c541a3dc 102 return;
12104404 2:dd10c541a3dc 103 }
12104404 2:dd10c541a3dc 104 switch(_integ) {
12104404 2:dd10c541a3dc 105 case TSL2591_INTT_100MS:
12104404 2:dd10c541a3dc 106 atime = 100.0F;
12104404 2:dd10c541a3dc 107 break;
12104404 2:dd10c541a3dc 108 case TSL2591_INTT_200MS:
12104404 2:dd10c541a3dc 109 atime = 200.0F;
12104404 2:dd10c541a3dc 110 break;
12104404 2:dd10c541a3dc 111 case TSL2591_INTT_300MS:
12104404 2:dd10c541a3dc 112 atime = 300.0F;
12104404 2:dd10c541a3dc 113 break;
12104404 2:dd10c541a3dc 114 case TSL2591_INTT_400MS:
12104404 2:dd10c541a3dc 115 atime = 400.0F;
12104404 2:dd10c541a3dc 116 break;
12104404 2:dd10c541a3dc 117 case TSL2591_INTT_500MS:
12104404 2:dd10c541a3dc 118 atime = 500.0F;
12104404 2:dd10c541a3dc 119 break;
12104404 2:dd10c541a3dc 120 case TSL2591_INTT_600MS:
12104404 2:dd10c541a3dc 121 atime = 600.0F;
12104404 2:dd10c541a3dc 122 break;
12104404 2:dd10c541a3dc 123 default:
12104404 2:dd10c541a3dc 124 atime = 100.0F;
12104404 2:dd10c541a3dc 125 break;
12104404 2:dd10c541a3dc 126 }
12104404 2:dd10c541a3dc 127 switch(_gain) {
12104404 2:dd10c541a3dc 128 case TSL2591_GAIN_LOW:
12104404 2:dd10c541a3dc 129 again = 1.0F;
12104404 2:dd10c541a3dc 130 break;
12104404 2:dd10c541a3dc 131 case TSL2591_GAIN_MED:
12104404 2:dd10c541a3dc 132 again = 25.0F;
12104404 2:dd10c541a3dc 133 break;
12104404 2:dd10c541a3dc 134 case TSL2591_GAIN_HIGH:
12104404 2:dd10c541a3dc 135 again = 428.0F;
12104404 2:dd10c541a3dc 136 break;
12104404 2:dd10c541a3dc 137 case TSL2591_GAIN_MAX:
12104404 2:dd10c541a3dc 138 again = 9876.0F;
12104404 2:dd10c541a3dc 139 break;
12104404 2:dd10c541a3dc 140 default:
12104404 2:dd10c541a3dc 141 again = 1.0F;
12104404 2:dd10c541a3dc 142 break;
12104404 2:dd10c541a3dc 143 }
12104404 2:dd10c541a3dc 144 cpl = (atime * again) / TSL2591_LUX_DF;
12104404 2:dd10c541a3dc 145 lux1 = ((float)full - (TSL2591_LUX_COEFB * (float)ir)) / cpl;
12104404 2:dd10c541a3dc 146 lux2 = (( TSL2591_LUX_COEFC * (float)full ) - ( TSL2591_LUX_COEFD * (float)ir)) / cpl;
12104404 2:dd10c541a3dc 147 lux3 = lux1 > lux2 ? lux1 : lux2;
12104404 2:dd10c541a3dc 148 lux = (uint32_t)lux3;
12104404 3:fecb1929cbef 149 }