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