digital light sensor using ardunio i2c pins
Dependents: LoRaWAN_singlechannel_endnode
Fork of TSL2561 by
TSL2561.cpp@3:0f0aa5c510e6, 2020-07-10 (annotated)
- Committer:
- Wayne Roberts
- Date:
- Fri Jul 10 11:12:34 2020 -0700
- Revision:
- 3:0f0aa5c510e6
- Parent:
- 2:1740d88b1edc
update to using mbed-6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anhnt2407 | 0:df9baf6459b7 | 1 | |
anhnt2407 | 0:df9baf6459b7 | 2 | #include "TSL2561.h" |
anhnt2407 | 1:ab906ac6e90b | 3 | #include "mbed.h" |
anhnt2407 | 1:ab906ac6e90b | 4 | |
dudmuck | 2:1740d88b1edc | 5 | /*Serial DEBUG(USBTX, USBRX); |
anhnt2407 | 0:df9baf6459b7 | 6 | |
anhnt2407 | 0:df9baf6459b7 | 7 | #define DEBUG_PRINTX(z,x) if(z==1) DEBUG.printf(x); |
anhnt2407 | 0:df9baf6459b7 | 8 | #define DEBUG_PRINTLNX(z,x) if(z==1) {DEBUG.printf(x); DEBUG.printf("\r\n");} |
anhnt2407 | 0:df9baf6459b7 | 9 | #define DEBUG_PRINTXY(z,x, y) if(z==1) DEBUG.printf(x, y); |
anhnt2407 | 0:df9baf6459b7 | 10 | #define DEBUG_PRINTLNXY(z,x, y) if(z==1) {DEBUG.printf(x, y); DEBUG.printf("\r\n");} |
dudmuck | 2:1740d88b1edc | 11 | */ |
anhnt2407 | 0:df9baf6459b7 | 12 | |
dudmuck | 2:1740d88b1edc | 13 | TSL2561::TSL2561():i2c(I2C_SDA, I2C_SCL){ |
anhnt2407 | 0:df9baf6459b7 | 14 | i2c.frequency (300); |
anhnt2407 | 1:ab906ac6e90b | 15 | _addr = TSL2561_ADDR_FLOAT; |
anhnt2407 | 0:df9baf6459b7 | 16 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 17 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 18 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 19 | } |
anhnt2407 | 0:df9baf6459b7 | 20 | |
dudmuck | 2:1740d88b1edc | 21 | TSL2561::TSL2561(uint8_t addr):i2c(I2C_SDA, I2C_SCL) { |
anhnt2407 | 0:df9baf6459b7 | 22 | |
anhnt2407 | 1:ab906ac6e90b | 23 | _addr = addr; |
anhnt2407 | 0:df9baf6459b7 | 24 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 25 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 26 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 27 | // we cant do wire initialization till later, because we havent loaded Wire yet |
anhnt2407 | 0:df9baf6459b7 | 28 | } |
anhnt2407 | 0:df9baf6459b7 | 29 | |
anhnt2407 | 0:df9baf6459b7 | 30 | TSL2561::TSL2561(PinName sda, PinName scl):i2c(sda, scl) { |
anhnt2407 | 0:df9baf6459b7 | 31 | |
anhnt2407 | 1:ab906ac6e90b | 32 | _addr = TSL2561_ADDR_FLOAT; |
anhnt2407 | 0:df9baf6459b7 | 33 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 34 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 35 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 36 | // we cant do wire initialization till later, because we havent loaded Wire yet |
anhnt2407 | 0:df9baf6459b7 | 37 | } |
anhnt2407 | 0:df9baf6459b7 | 38 | |
anhnt2407 | 0:df9baf6459b7 | 39 | TSL2561::TSL2561(PinName sda, PinName scl, uint8_t addr):i2c(sda, scl) { |
anhnt2407 | 0:df9baf6459b7 | 40 | |
anhnt2407 | 1:ab906ac6e90b | 41 | _addr = addr; |
anhnt2407 | 0:df9baf6459b7 | 42 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 43 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 44 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 45 | // we cant do wire initialization till later, because we havent loaded Wire yet |
anhnt2407 | 0:df9baf6459b7 | 46 | } |
anhnt2407 | 0:df9baf6459b7 | 47 | |
anhnt2407 | 0:df9baf6459b7 | 48 | uint16_t TSL2561::getLuminosity (uint8_t channel) { |
anhnt2407 | 0:df9baf6459b7 | 49 | |
anhnt2407 | 0:df9baf6459b7 | 50 | uint32_t x = getFullLuminosity(); |
anhnt2407 | 0:df9baf6459b7 | 51 | |
anhnt2407 | 0:df9baf6459b7 | 52 | if (channel == 0) { |
anhnt2407 | 0:df9baf6459b7 | 53 | // Reads two byte value from channel 0 (visible + infrared) |
anhnt2407 | 0:df9baf6459b7 | 54 | |
anhnt2407 | 0:df9baf6459b7 | 55 | return (x & 0xFFFF); |
anhnt2407 | 0:df9baf6459b7 | 56 | } else if (channel == 1) { |
anhnt2407 | 0:df9baf6459b7 | 57 | // Reads two byte value from channel 1 (infrared) |
anhnt2407 | 0:df9baf6459b7 | 58 | |
anhnt2407 | 0:df9baf6459b7 | 59 | return (x >> 16); |
anhnt2407 | 0:df9baf6459b7 | 60 | } else if (channel == 2) { |
anhnt2407 | 0:df9baf6459b7 | 61 | // Reads all and subtracts out just the visible! |
anhnt2407 | 0:df9baf6459b7 | 62 | |
anhnt2407 | 1:ab906ac6e90b | 63 | return ( (x & 0xFFFF) - (x >> 16) ); |
anhnt2407 | 0:df9baf6459b7 | 64 | } |
anhnt2407 | 0:df9baf6459b7 | 65 | |
anhnt2407 | 0:df9baf6459b7 | 66 | // unknown channel! |
anhnt2407 | 0:df9baf6459b7 | 67 | return 0; |
anhnt2407 | 0:df9baf6459b7 | 68 | } |
anhnt2407 | 0:df9baf6459b7 | 69 | |
anhnt2407 | 0:df9baf6459b7 | 70 | uint32_t TSL2561::getFullLuminosity (void) |
anhnt2407 | 0:df9baf6459b7 | 71 | { |
anhnt2407 | 0:df9baf6459b7 | 72 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 73 | |
anhnt2407 | 0:df9baf6459b7 | 74 | // Enable the device by setting the control bit to 0x03 |
anhnt2407 | 0:df9baf6459b7 | 75 | enable(); |
anhnt2407 | 0:df9baf6459b7 | 76 | |
anhnt2407 | 0:df9baf6459b7 | 77 | // Wait x ms for ADC to complete |
anhnt2407 | 0:df9baf6459b7 | 78 | switch (_integration) |
anhnt2407 | 0:df9baf6459b7 | 79 | { |
anhnt2407 | 0:df9baf6459b7 | 80 | case TSL2561_INTEGRATIONTIME_13MS: |
Wayne Roberts |
3:0f0aa5c510e6 | 81 | ThisThread::sleep_for(14);//wait_ms(14); |
anhnt2407 | 0:df9baf6459b7 | 82 | break; |
anhnt2407 | 0:df9baf6459b7 | 83 | case TSL2561_INTEGRATIONTIME_101MS: |
Wayne Roberts |
3:0f0aa5c510e6 | 84 | ThisThread::sleep_for(102);//wait_ms(102); |
anhnt2407 | 0:df9baf6459b7 | 85 | break; |
anhnt2407 | 0:df9baf6459b7 | 86 | default: |
Wayne Roberts |
3:0f0aa5c510e6 | 87 | ThisThread::sleep_for(403);//wait_ms(403); |
anhnt2407 | 0:df9baf6459b7 | 88 | break; |
anhnt2407 | 0:df9baf6459b7 | 89 | } |
anhnt2407 | 0:df9baf6459b7 | 90 | |
anhnt2407 | 1:ab906ac6e90b | 91 | //DEBUG_PRINTLNXY(0," Integration:= %d",_integration); |
anhnt2407 | 0:df9baf6459b7 | 92 | |
anhnt2407 | 0:df9baf6459b7 | 93 | uint32_t x; |
anhnt2407 | 0:df9baf6459b7 | 94 | x = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW); |
anhnt2407 | 0:df9baf6459b7 | 95 | |
anhnt2407 | 1:ab906ac6e90b | 96 | // DEBUG_PRINTLNXY(0," x:= %d",x); |
anhnt2407 | 0:df9baf6459b7 | 97 | |
anhnt2407 | 0:df9baf6459b7 | 98 | x <<= 16; |
anhnt2407 | 0:df9baf6459b7 | 99 | x |= read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW); |
anhnt2407 | 0:df9baf6459b7 | 100 | |
anhnt2407 | 1:ab906ac6e90b | 101 | //DEBUG_PRINTLNXY(0," x:= %d",x); |
anhnt2407 | 0:df9baf6459b7 | 102 | |
anhnt2407 | 1:ab906ac6e90b | 103 | //wait(3); |
anhnt2407 | 0:df9baf6459b7 | 104 | disable(); |
anhnt2407 | 0:df9baf6459b7 | 105 | |
anhnt2407 | 0:df9baf6459b7 | 106 | return x; |
anhnt2407 | 0:df9baf6459b7 | 107 | } |
anhnt2407 | 0:df9baf6459b7 | 108 | |
anhnt2407 | 0:df9baf6459b7 | 109 | |
anhnt2407 | 0:df9baf6459b7 | 110 | bool TSL2561::begin(void) { |
anhnt2407 | 0:df9baf6459b7 | 111 | |
anhnt2407 | 1:ab906ac6e90b | 112 | char reg[1]; |
anhnt2407 | 1:ab906ac6e90b | 113 | reg[0] = TSL2561_REGISTER_ID; |
anhnt2407 | 1:ab906ac6e90b | 114 | char receivedata[1]; |
anhnt2407 | 1:ab906ac6e90b | 115 | char read; |
anhnt2407 | 1:ab906ac6e90b | 116 | i2c.write(_addr<<1, reg, 1); |
anhnt2407 | 1:ab906ac6e90b | 117 | i2c.read(_addr<<1, receivedata, 1); |
anhnt2407 | 1:ab906ac6e90b | 118 | read=receivedata[0]; |
anhnt2407 | 1:ab906ac6e90b | 119 | |
anhnt2407 | 0:df9baf6459b7 | 120 | if (read & 0x0A ) { |
dudmuck | 2:1740d88b1edc | 121 | printf("Read 0x%x => Found TSL2561\r\n",read); |
anhnt2407 | 0:df9baf6459b7 | 122 | } else { |
dudmuck | 2:1740d88b1edc | 123 | printf("read:%x\r\n", read); |
anhnt2407 | 0:df9baf6459b7 | 124 | return false; |
anhnt2407 | 0:df9baf6459b7 | 125 | } |
anhnt2407 | 0:df9baf6459b7 | 126 | _initialized = true; |
anhnt2407 | 0:df9baf6459b7 | 127 | |
anhnt2407 | 0:df9baf6459b7 | 128 | // Set default integration time and gain |
anhnt2407 | 0:df9baf6459b7 | 129 | setTiming(_integration); |
anhnt2407 | 0:df9baf6459b7 | 130 | setGain(_gain); |
anhnt2407 | 0:df9baf6459b7 | 131 | |
anhnt2407 | 0:df9baf6459b7 | 132 | // Note: by default, the device is in power down mode on bootup |
anhnt2407 | 0:df9baf6459b7 | 133 | disable(); |
anhnt2407 | 0:df9baf6459b7 | 134 | |
anhnt2407 | 0:df9baf6459b7 | 135 | return true; |
anhnt2407 | 0:df9baf6459b7 | 136 | } |
anhnt2407 | 0:df9baf6459b7 | 137 | |
anhnt2407 | 0:df9baf6459b7 | 138 | uint16_t TSL2561::read16(uint8_t reg) |
anhnt2407 | 0:df9baf6459b7 | 139 | { |
anhnt2407 | 1:ab906ac6e90b | 140 | uint16_t x; |
anhnt2407 | 1:ab906ac6e90b | 141 | uint16_t t; |
anhnt2407 | 1:ab906ac6e90b | 142 | char _x; |
anhnt2407 | 1:ab906ac6e90b | 143 | char _t; |
anhnt2407 | 1:ab906ac6e90b | 144 | char r[1]; |
anhnt2407 | 0:df9baf6459b7 | 145 | r[0] = reg; |
anhnt2407 | 1:ab906ac6e90b | 146 | char receivedata[2]; |
anhnt2407 | 0:df9baf6459b7 | 147 | |
anhnt2407 | 1:ab906ac6e90b | 148 | i2c.write(_addr<<1, r, 1); |
anhnt2407 | 1:ab906ac6e90b | 149 | i2c.read(_addr<<1, receivedata, 2); |
anhnt2407 | 1:ab906ac6e90b | 150 | |
anhnt2407 | 1:ab906ac6e90b | 151 | _t=receivedata[0]; |
anhnt2407 | 1:ab906ac6e90b | 152 | _x=receivedata[1]; |
anhnt2407 | 0:df9baf6459b7 | 153 | |
dudmuck | 2:1740d88b1edc | 154 | /*printf("_t:=0x%x",_t); |
dudmuck | 2:1740d88b1edc | 155 | printf("_x:=0x%x",_x); */ |
anhnt2407 | 0:df9baf6459b7 | 156 | |
anhnt2407 | 1:ab906ac6e90b | 157 | t=(uint16_t)_t; |
anhnt2407 | 1:ab906ac6e90b | 158 | x=(uint16_t)_x; |
anhnt2407 | 0:df9baf6459b7 | 159 | x <<= 8; |
anhnt2407 | 0:df9baf6459b7 | 160 | x |= t; |
anhnt2407 | 1:ab906ac6e90b | 161 | |
dudmuck | 2:1740d88b1edc | 162 | //printf("x:= %d\r\n",x); |
anhnt2407 | 1:ab906ac6e90b | 163 | |
anhnt2407 | 0:df9baf6459b7 | 164 | return x; |
anhnt2407 | 0:df9baf6459b7 | 165 | } |
anhnt2407 | 0:df9baf6459b7 | 166 | |
anhnt2407 | 0:df9baf6459b7 | 167 | void TSL2561::write8 (uint8_t reg, uint8_t value) |
anhnt2407 | 0:df9baf6459b7 | 168 | { |
anhnt2407 | 0:df9baf6459b7 | 169 | i2c.start(); |
anhnt2407 | 1:ab906ac6e90b | 170 | i2c.write(_addr<<1); |
anhnt2407 | 0:df9baf6459b7 | 171 | i2c.write(reg); |
anhnt2407 | 0:df9baf6459b7 | 172 | i2c.write(value); |
anhnt2407 | 0:df9baf6459b7 | 173 | i2c.stop(); |
anhnt2407 | 0:df9baf6459b7 | 174 | } |
anhnt2407 | 0:df9baf6459b7 | 175 | |
anhnt2407 | 0:df9baf6459b7 | 176 | void TSL2561::setTiming(tsl2561IntegrationTime_t integration){ |
anhnt2407 | 0:df9baf6459b7 | 177 | |
dudmuck | 2:1740d88b1edc | 178 | if (!_initialized) |
dudmuck | 2:1740d88b1edc | 179 | begin(); |
dudmuck | 2:1740d88b1edc | 180 | /*else |
dudmuck | 2:1740d88b1edc | 181 | printf("--------------Set Timing---------\r\n");*/ |
anhnt2407 | 0:df9baf6459b7 | 182 | |
anhnt2407 | 0:df9baf6459b7 | 183 | enable(); |
anhnt2407 | 0:df9baf6459b7 | 184 | |
anhnt2407 | 0:df9baf6459b7 | 185 | _integration = integration; |
anhnt2407 | 0:df9baf6459b7 | 186 | |
dudmuck | 2:1740d88b1edc | 187 | /*printf("Integration: 0x%x ",_integration); |
dudmuck | 2:1740d88b1edc | 188 | printf("Gain: 0x%x ",_gain); |
dudmuck | 2:1740d88b1edc | 189 | printf("Integration | Gain: 0x%x\r\n",_integration | _gain);*/ |
anhnt2407 | 0:df9baf6459b7 | 190 | |
anhnt2407 | 0:df9baf6459b7 | 191 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); |
anhnt2407 | 0:df9baf6459b7 | 192 | |
anhnt2407 | 0:df9baf6459b7 | 193 | disable(); |
anhnt2407 | 0:df9baf6459b7 | 194 | |
dudmuck | 2:1740d88b1edc | 195 | //printf("--------------Complete Set Timing-------------\r\n"); |
anhnt2407 | 0:df9baf6459b7 | 196 | |
anhnt2407 | 1:ab906ac6e90b | 197 | //wait(1); |
anhnt2407 | 0:df9baf6459b7 | 198 | |
anhnt2407 | 0:df9baf6459b7 | 199 | } |
anhnt2407 | 0:df9baf6459b7 | 200 | |
anhnt2407 | 0:df9baf6459b7 | 201 | void TSL2561::setGain(tsl2561Gain_t gain) { |
anhnt2407 | 0:df9baf6459b7 | 202 | |
dudmuck | 2:1740d88b1edc | 203 | if (!_initialized) |
dudmuck | 2:1740d88b1edc | 204 | begin(); |
dudmuck | 2:1740d88b1edc | 205 | /*else |
dudmuck | 2:1740d88b1edc | 206 | printf("-------------Set Gain--------------\r\n");*/ |
anhnt2407 | 0:df9baf6459b7 | 207 | |
anhnt2407 | 0:df9baf6459b7 | 208 | |
anhnt2407 | 0:df9baf6459b7 | 209 | enable(); |
anhnt2407 | 0:df9baf6459b7 | 210 | |
dudmuck | 2:1740d88b1edc | 211 | /*printf("Intergration: 0x%x ",_integration); |
dudmuck | 2:1740d88b1edc | 212 | printf("Gain: 0x%x ",_gain); |
dudmuck | 2:1740d88b1edc | 213 | printf("Intergration | Gain: 0x%x\r\n",_integration | _gain);*/ |
anhnt2407 | 0:df9baf6459b7 | 214 | |
anhnt2407 | 0:df9baf6459b7 | 215 | _gain = gain; |
anhnt2407 | 0:df9baf6459b7 | 216 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); |
anhnt2407 | 0:df9baf6459b7 | 217 | //write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _gain); |
anhnt2407 | 0:df9baf6459b7 | 218 | disable(); |
anhnt2407 | 0:df9baf6459b7 | 219 | |
dudmuck | 2:1740d88b1edc | 220 | //printf("---------------Complete Set Gain----------------\r\n"); |
anhnt2407 | 1:ab906ac6e90b | 221 | //wait(1); |
anhnt2407 | 0:df9baf6459b7 | 222 | |
anhnt2407 | 0:df9baf6459b7 | 223 | } |
anhnt2407 | 0:df9baf6459b7 | 224 | |
anhnt2407 | 0:df9baf6459b7 | 225 | void TSL2561::enable(void) |
anhnt2407 | 0:df9baf6459b7 | 226 | { |
anhnt2407 | 0:df9baf6459b7 | 227 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 228 | |
anhnt2407 | 0:df9baf6459b7 | 229 | // Enable the device by setting the control bit to 0x03 |
dudmuck | 2:1740d88b1edc | 230 | //printf("Power On\r\n"); |
anhnt2407 | 0:df9baf6459b7 | 231 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); |
anhnt2407 | 0:df9baf6459b7 | 232 | } |
anhnt2407 | 0:df9baf6459b7 | 233 | |
anhnt2407 | 0:df9baf6459b7 | 234 | void TSL2561::disable(void) |
anhnt2407 | 0:df9baf6459b7 | 235 | { |
anhnt2407 | 0:df9baf6459b7 | 236 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 237 | |
anhnt2407 | 0:df9baf6459b7 | 238 | // Disable the device by setting the control bit to 0x03 |
dudmuck | 2:1740d88b1edc | 239 | //printf(" Power Off\r\n"); |
anhnt2407 | 0:df9baf6459b7 | 240 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); |
Wayne Roberts |
3:0f0aa5c510e6 | 241 | } |