for EVK odin mbed os 5.5
Dependents: Mbed-Connect-Cloud-Demo Mbed-Connect-Cloud-Demo HTTP-all-sensors mbed-cloud-connect-sensor-light ... more
Fork of TSL2561 by
TSL2561.cpp@4:8146a28a20cc, 2017-10-23 (annotated)
- Committer:
- andcor02
- Date:
- Mon Oct 23 19:34:05 2017 +0000
- Revision:
- 4:8146a28a20cc
- Parent:
- 3:8450950ca50b
removed UART;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anhnt2407 | 0:df9baf6459b7 | 1 | #include "TSL2561.h" |
anhnt2407 | 1:ab906ac6e90b | 2 | #include "mbed.h" |
anhnt2407 | 0:df9baf6459b7 | 3 | TSL2561::TSL2561():i2c(TSL2561_I2C_PINNAME_SDA,TSL2561_I2C_PINNAME_SCL){ |
anhnt2407 | 0:df9baf6459b7 | 4 | i2c.frequency (300); |
anhnt2407 | 1:ab906ac6e90b | 5 | _addr = TSL2561_ADDR_FLOAT; |
anhnt2407 | 0:df9baf6459b7 | 6 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 7 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
andcor02 | 4:8146a28a20cc | 8 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 9 | } |
anhnt2407 | 0:df9baf6459b7 | 10 | TSL2561::TSL2561(uint8_t addr):i2c(TSL2561_I2C_PINNAME_SDA,TSL2561_I2C_PINNAME_SCL) { |
anhnt2407 | 1:ab906ac6e90b | 11 | _addr = addr; |
anhnt2407 | 0:df9baf6459b7 | 12 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 13 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 14 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 15 | // we cant do wire initialization till later, because we havent loaded Wire yet |
anhnt2407 | 0:df9baf6459b7 | 16 | } |
anhnt2407 | 0:df9baf6459b7 | 17 | TSL2561::TSL2561(PinName sda, PinName scl):i2c(sda, scl) { |
anhnt2407 | 1:ab906ac6e90b | 18 | _addr = TSL2561_ADDR_FLOAT; |
anhnt2407 | 0:df9baf6459b7 | 19 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 20 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 21 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 22 | // we cant do wire initialization till later, because we havent loaded Wire yet |
anhnt2407 | 0:df9baf6459b7 | 23 | } |
anhnt2407 | 0:df9baf6459b7 | 24 | TSL2561::TSL2561(PinName sda, PinName scl, uint8_t addr):i2c(sda, scl) { |
anhnt2407 | 1:ab906ac6e90b | 25 | _addr = addr; |
anhnt2407 | 0:df9baf6459b7 | 26 | _initialized = false; |
anhnt2407 | 0:df9baf6459b7 | 27 | _integration = TSL2561_INTEGRATIONTIME_13MS; |
anhnt2407 | 0:df9baf6459b7 | 28 | _gain = TSL2561_GAIN_16X; |
anhnt2407 | 0:df9baf6459b7 | 29 | // we cant do wire initialization till later, because we havent loaded Wire yet |
anhnt2407 | 0:df9baf6459b7 | 30 | } |
anhnt2407 | 0:df9baf6459b7 | 31 | uint16_t TSL2561::getLuminosity (uint8_t channel) { |
anhnt2407 | 0:df9baf6459b7 | 32 | uint32_t x = getFullLuminosity(); |
anhnt2407 | 0:df9baf6459b7 | 33 | if (channel == 0) { |
anhnt2407 | 0:df9baf6459b7 | 34 | // Reads two byte value from channel 0 (visible + infrared) |
anhnt2407 | 0:df9baf6459b7 | 35 | return (x & 0xFFFF); |
anhnt2407 | 0:df9baf6459b7 | 36 | } else if (channel == 1) { |
anhnt2407 | 0:df9baf6459b7 | 37 | // Reads two byte value from channel 1 (infrared) |
anhnt2407 | 0:df9baf6459b7 | 38 | return (x >> 16); |
anhnt2407 | 0:df9baf6459b7 | 39 | } else if (channel == 2) { |
anhnt2407 | 0:df9baf6459b7 | 40 | // Reads all and subtracts out just the visible! |
anhnt2407 | 1:ab906ac6e90b | 41 | return ( (x & 0xFFFF) - (x >> 16) ); |
anhnt2407 | 0:df9baf6459b7 | 42 | } |
anhnt2407 | 0:df9baf6459b7 | 43 | // unknown channel! |
anhnt2407 | 0:df9baf6459b7 | 44 | return 0; |
anhnt2407 | 0:df9baf6459b7 | 45 | } |
anhnt2407 | 0:df9baf6459b7 | 46 | uint32_t TSL2561::getFullLuminosity (void) |
anhnt2407 | 0:df9baf6459b7 | 47 | { |
anhnt2407 | 0:df9baf6459b7 | 48 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 49 | // Enable the device by setting the control bit to 0x03 |
anhnt2407 | 0:df9baf6459b7 | 50 | enable(); |
anhnt2407 | 0:df9baf6459b7 | 51 | // Wait x ms for ADC to complete |
anhnt2407 | 0:df9baf6459b7 | 52 | switch (_integration) |
anhnt2407 | 0:df9baf6459b7 | 53 | { |
anhnt2407 | 0:df9baf6459b7 | 54 | case TSL2561_INTEGRATIONTIME_13MS: |
anhnt2407 | 0:df9baf6459b7 | 55 | wait_ms(14); |
anhnt2407 | 0:df9baf6459b7 | 56 | break; |
anhnt2407 | 0:df9baf6459b7 | 57 | case TSL2561_INTEGRATIONTIME_101MS: |
anhnt2407 | 0:df9baf6459b7 | 58 | wait_ms(102); |
anhnt2407 | 0:df9baf6459b7 | 59 | break; |
anhnt2407 | 0:df9baf6459b7 | 60 | default: |
andcor02 | 3:8450950ca50b | 61 | wait_ms(450); |
anhnt2407 | 0:df9baf6459b7 | 62 | break; |
anhnt2407 | 0:df9baf6459b7 | 63 | } |
anhnt2407 | 0:df9baf6459b7 | 64 | uint32_t x; |
anhnt2407 | 0:df9baf6459b7 | 65 | x = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW); |
anhnt2407 | 0:df9baf6459b7 | 66 | x <<= 16; |
anhnt2407 | 0:df9baf6459b7 | 67 | x |= read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW); |
anhnt2407 | 1:ab906ac6e90b | 68 | //wait(3); |
anhnt2407 | 0:df9baf6459b7 | 69 | disable(); |
anhnt2407 | 0:df9baf6459b7 | 70 | return x; |
anhnt2407 | 0:df9baf6459b7 | 71 | } |
anhnt2407 | 0:df9baf6459b7 | 72 | bool TSL2561::begin(void) { |
anhnt2407 | 1:ab906ac6e90b | 73 | char reg[1]; |
anhnt2407 | 1:ab906ac6e90b | 74 | reg[0] = TSL2561_REGISTER_ID; |
anhnt2407 | 1:ab906ac6e90b | 75 | char receivedata[1]; |
andcor02 | 4:8146a28a20cc | 76 | char read; |
anhnt2407 | 1:ab906ac6e90b | 77 | i2c.write(_addr<<1, reg, 1); |
anhnt2407 | 1:ab906ac6e90b | 78 | i2c.read(_addr<<1, receivedata, 1); |
anhnt2407 | 1:ab906ac6e90b | 79 | read=receivedata[0]; |
anhnt2407 | 0:df9baf6459b7 | 80 | if (read & 0x0A ) { |
anhnt2407 | 0:df9baf6459b7 | 81 | } else { |
anhnt2407 | 0:df9baf6459b7 | 82 | return false; |
andcor02 | 4:8146a28a20cc | 83 | } |
anhnt2407 | 0:df9baf6459b7 | 84 | _initialized = true; |
anhnt2407 | 0:df9baf6459b7 | 85 | // Set default integration time and gain |
anhnt2407 | 0:df9baf6459b7 | 86 | setTiming(_integration); |
anhnt2407 | 0:df9baf6459b7 | 87 | setGain(_gain); |
anhnt2407 | 0:df9baf6459b7 | 88 | // Note: by default, the device is in power down mode on bootup |
andcor02 | 4:8146a28a20cc | 89 | disable(); |
anhnt2407 | 0:df9baf6459b7 | 90 | return true; |
anhnt2407 | 0:df9baf6459b7 | 91 | } |
anhnt2407 | 0:df9baf6459b7 | 92 | uint16_t TSL2561::read16(uint8_t reg) |
anhnt2407 | 0:df9baf6459b7 | 93 | { |
andcor02 | 4:8146a28a20cc | 94 | uint16_t x; |
anhnt2407 | 1:ab906ac6e90b | 95 | uint16_t t; |
anhnt2407 | 1:ab906ac6e90b | 96 | char _x; |
anhnt2407 | 1:ab906ac6e90b | 97 | char _t; |
anhnt2407 | 1:ab906ac6e90b | 98 | char r[1]; |
anhnt2407 | 0:df9baf6459b7 | 99 | r[0] = reg; |
anhnt2407 | 1:ab906ac6e90b | 100 | char receivedata[2]; |
anhnt2407 | 1:ab906ac6e90b | 101 | i2c.write(_addr<<1, r, 1); |
andcor02 | 4:8146a28a20cc | 102 | i2c.read(_addr<<1, receivedata, 2); |
anhnt2407 | 1:ab906ac6e90b | 103 | _t=receivedata[0]; |
anhnt2407 | 1:ab906ac6e90b | 104 | _x=receivedata[1]; |
anhnt2407 | 1:ab906ac6e90b | 105 | t=(uint16_t)_t; |
anhnt2407 | 1:ab906ac6e90b | 106 | x=(uint16_t)_x; |
anhnt2407 | 0:df9baf6459b7 | 107 | x <<= 8; |
anhnt2407 | 0:df9baf6459b7 | 108 | x |= t; |
anhnt2407 | 0:df9baf6459b7 | 109 | return x; |
anhnt2407 | 0:df9baf6459b7 | 110 | } |
anhnt2407 | 0:df9baf6459b7 | 111 | void TSL2561::write8 (uint8_t reg, uint8_t value) |
andcor02 | 4:8146a28a20cc | 112 | { |
anhnt2407 | 0:df9baf6459b7 | 113 | i2c.start(); |
anhnt2407 | 1:ab906ac6e90b | 114 | i2c.write(_addr<<1); |
anhnt2407 | 0:df9baf6459b7 | 115 | i2c.write(reg); |
anhnt2407 | 0:df9baf6459b7 | 116 | i2c.write(value); |
andcor02 | 4:8146a28a20cc | 117 | i2c.stop(); |
anhnt2407 | 0:df9baf6459b7 | 118 | } |
anhnt2407 | 0:df9baf6459b7 | 119 | void TSL2561::setTiming(tsl2561IntegrationTime_t integration){ |
anhnt2407 | 0:df9baf6459b7 | 120 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 121 | enable(); |
anhnt2407 | 0:df9baf6459b7 | 122 | _integration = integration; |
andcor02 | 4:8146a28a20cc | 123 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); |
anhnt2407 | 0:df9baf6459b7 | 124 | disable(); |
anhnt2407 | 1:ab906ac6e90b | 125 | //wait(1); |
anhnt2407 | 0:df9baf6459b7 | 126 | } |
anhnt2407 | 0:df9baf6459b7 | 127 | void TSL2561::setGain(tsl2561Gain_t gain) { |
anhnt2407 | 0:df9baf6459b7 | 128 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 129 | enable(); |
anhnt2407 | 0:df9baf6459b7 | 130 | _gain = gain; |
andcor02 | 4:8146a28a20cc | 131 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); |
andcor02 | 4:8146a28a20cc | 132 | //write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _gain); |
anhnt2407 | 0:df9baf6459b7 | 133 | disable(); |
anhnt2407 | 1:ab906ac6e90b | 134 | //wait(1); |
anhnt2407 | 0:df9baf6459b7 | 135 | } |
anhnt2407 | 0:df9baf6459b7 | 136 | void TSL2561::enable(void) |
anhnt2407 | 0:df9baf6459b7 | 137 | { |
anhnt2407 | 0:df9baf6459b7 | 138 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 139 | // Enable the device by setting the control bit to 0x03 |
anhnt2407 | 0:df9baf6459b7 | 140 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); |
anhnt2407 | 0:df9baf6459b7 | 141 | } |
anhnt2407 | 0:df9baf6459b7 | 142 | void TSL2561::disable(void) |
anhnt2407 | 0:df9baf6459b7 | 143 | { |
anhnt2407 | 0:df9baf6459b7 | 144 | if (!_initialized) begin(); |
anhnt2407 | 0:df9baf6459b7 | 145 | // Disable the device by setting the control bit to 0x03 |
anhnt2407 | 0:df9baf6459b7 | 146 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); |
anhnt2407 | 0:df9baf6459b7 | 147 | } |