Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TSL2591.cpp
00001 #include "TSL2591.h" 00002 00003 TSL2591::TSL2591 (I2C& tsl2591_i2c, uint8_t tsl2591_addr): 00004 _i2c(tsl2591_i2c), _addr(tsl2591_addr<<1) 00005 { 00006 _init = false; 00007 _integ = TSL2591_INTT_100MS; 00008 _gain = TSL2591_GAIN_LOW; 00009 } 00010 /* 00011 * Initialize TSL2591 00012 * Checks ID and sets gain and integration time 00013 */ 00014 bool TSL2591::init(void) 00015 { 00016 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ID)}; 00017 if(_i2c.write(_addr, write, 1, 0) == 0) { 00018 char read[1]; 00019 _i2c.read(_addr, read, 1, 0); 00020 if(read[0] == TSL2591_ID) { 00021 _init = true; 00022 setGain(TSL2591_GAIN_LOW); 00023 setTime(TSL2591_INTT_100MS); 00024 disable(); 00025 return true; 00026 } 00027 } 00028 return false; 00029 } 00030 /* 00031 * Power On TSL2591 00032 */ 00033 void TSL2591::enable(void) 00034 { 00035 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_PON|TSL2591_EN_AEN|TSL2591_EN_AIEN|TSL2591_EN_NPIEN)}; 00036 _i2c.write(_addr, write, 2, 0); 00037 } 00038 /* 00039 * Power Off TSL2591 00040 */ 00041 void TSL2591::disable(void) 00042 { 00043 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_POFF)}; 00044 _i2c.write(_addr, write, 2, 0); 00045 } 00046 /* 00047 * Set Gain and Write 00048 * Set gain and write time and gain 00049 */ 00050 void TSL2591::setGain(tsl2591Gain_t gain) 00051 { 00052 enable(); 00053 _gain = gain; 00054 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_CONTROL), (_integ|_gain)}; 00055 _i2c.write(_addr, write, 2, 0); 00056 disable(); 00057 } 00058 /* 00059 * Set Integration Time and Write 00060 * Set gain and write time and gain 00061 */ 00062 void TSL2591::setTime(tsl2591IntegrationTime_t integ) 00063 { 00064 enable(); 00065 _integ = integ; 00066 char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_CONTROL), (_integ|_gain)}; 00067 _i2c.write(_addr, write, 2, 0); 00068 disable(); 00069 } 00070 /* 00071 * Read ALS 00072 * Read full spectrum, infrared, and visible 00073 */ 00074 void TSL2591::getALS(void) 00075 { 00076 enable(); 00077 for(uint8_t t=0; t<=_integ+1; t++) { 00078 wait(0.12); 00079 } 00080 char write1[] = {(TSL2591_CMD_BIT|TSL2591_REG_CHAN1_L)}; 00081 _i2c.write(_addr, write1, 1, 0); 00082 char read1[2]; 00083 _i2c.read(_addr, read1, 2, 0); 00084 char write2[] = {(TSL2591_CMD_BIT|TSL2591_REG_CHAN0_L)}; 00085 _i2c.write(_addr, write2, 1, 0); 00086 char read2[2]; 00087 _i2c.read(_addr, read2, 2, 0); 00088 rawALS = (((read1[1]<<8)|read1[0])<<16)|((read2[1]<<8)|read2[0]); 00089 disable(); 00090 full = rawALS & 0xFFFF; 00091 ir = rawALS >> 16; 00092 visible = full - ir; 00093 } 00094 /* 00095 * Calculate Lux 00096 */ 00097 void TSL2591::calcLux(void) 00098 { 00099 float atime, again, cpl, lux1, lux2, lux3; 00100 if((full == 0xFFFF)|(ir == 0xFFFF)) { 00101 lux3 = 0; 00102 return; 00103 } 00104 switch(_integ) { 00105 case TSL2591_INTT_100MS: 00106 atime = 100.0F; 00107 break; 00108 case TSL2591_INTT_200MS: 00109 atime = 200.0F; 00110 break; 00111 case TSL2591_INTT_300MS: 00112 atime = 300.0F; 00113 break; 00114 case TSL2591_INTT_400MS: 00115 atime = 400.0F; 00116 break; 00117 case TSL2591_INTT_500MS: 00118 atime = 500.0F; 00119 break; 00120 case TSL2591_INTT_600MS: 00121 atime = 600.0F; 00122 break; 00123 default: 00124 atime = 100.0F; 00125 break; 00126 } 00127 switch(_gain) { 00128 case TSL2591_GAIN_LOW: 00129 again = 1.0F; 00130 break; 00131 case TSL2591_GAIN_MED: 00132 again = 25.0F; 00133 break; 00134 case TSL2591_GAIN_HIGH: 00135 again = 428.0F; 00136 break; 00137 case TSL2591_GAIN_MAX: 00138 again = 9876.0F; 00139 break; 00140 default: 00141 again = 1.0F; 00142 break; 00143 } 00144 cpl = (atime * again) / TSL2591_LUX_DF; 00145 lux1 = ((float)full - (TSL2591_LUX_COEFB * (float)ir)) / cpl; 00146 lux2 = (( TSL2591_LUX_COEFC * (float)full ) - ( TSL2591_LUX_COEFD * (float)ir)) / cpl; 00147 lux3 = lux1 > lux2 ? lux1 : lux2; 00148 lux = (uint32_t)lux3; 00149 }
Generated on Sun Jul 17 2022 18:52:39 by
1.7.2