Edwin Cho
/
TSL2591
Adafruit TSL2591 sensor
TSL2591.cpp@0:815555c72774, 2016-03-13 (annotated)
- Committer:
- 12104404
- Date:
- Sun Mar 13 03:10:20 2016 +0000
- Revision:
- 0:815555c72774
- Child:
- 1:308cc5302475
values read incorrect
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:815555c72774 | 4 | _i2c(tsl2591_i2c), _addr(tsl2591_addr) |
12104404 | 0:815555c72774 | 5 | { |
12104404 | 0:815555c72774 | 6 | _init = false; |
12104404 | 0:815555c72774 | 7 | _integ = TSL2591_INTT_100MS; |
12104404 | 0:815555c72774 | 8 | _gain = TSL2591_GAIN_MED; |
12104404 | 0:815555c72774 | 9 | } |
12104404 | 0:815555c72774 | 10 | /* |
12104404 | 0:815555c72774 | 11 | * Initialize TSL2591 |
12104404 | 0:815555c72774 | 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 | 0:815555c72774 | 17 | if(_i2c.write(_addr<<1, write, 1, 0) == 0) { |
12104404 | 0:815555c72774 | 18 | char read[1]; |
12104404 | 0:815555c72774 | 19 | _i2c.read(_addr<<1, read, 1, 0); |
12104404 | 0:815555c72774 | 20 | if(read[0] == TSL2591_ID) { |
12104404 | 0:815555c72774 | 21 | _init = true; |
12104404 | 0:815555c72774 | 22 | setGain(TSL2591_GAIN_HIGH); |
12104404 | 0:815555c72774 | 23 | setIntegration(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 | 0:815555c72774 | 30 | |
12104404 | 0:815555c72774 | 31 | void TSL2591::enable(void) |
12104404 | 0:815555c72774 | 32 | { |
12104404 | 0:815555c72774 | 33 | char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_PON|TSL2591_EN_AEN|TSL2591_EN_AIEN|TSL2591_EN_NPIEN)}; |
12104404 | 0:815555c72774 | 34 | _i2c.write(_addr<<1, write, 2, 0); |
12104404 | 0:815555c72774 | 35 | } |
12104404 | 0:815555c72774 | 36 | |
12104404 | 0:815555c72774 | 37 | void TSL2591::disable(void) |
12104404 | 0:815555c72774 | 38 | { |
12104404 | 0:815555c72774 | 39 | char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_ENABLE), (TSL2591_EN_POFF)}; |
12104404 | 0:815555c72774 | 40 | _i2c.write(_addr<<1, write, 2, 0); |
12104404 | 0:815555c72774 | 41 | } |
12104404 | 0:815555c72774 | 42 | |
12104404 | 0:815555c72774 | 43 | void TSL2591::setGain(tsl2591Gain_t gain) |
12104404 | 0:815555c72774 | 44 | { |
12104404 | 0:815555c72774 | 45 | enable(); |
12104404 | 0:815555c72774 | 46 | _gain = gain; |
12104404 | 0:815555c72774 | 47 | char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_CONTROL), (_integ|_gain)}; |
12104404 | 0:815555c72774 | 48 | _i2c.write(_addr<<1, write, 2, 0); |
12104404 | 0:815555c72774 | 49 | disable(); |
12104404 | 0:815555c72774 | 50 | } |
12104404 | 0:815555c72774 | 51 | |
12104404 | 0:815555c72774 | 52 | void TSL2591::setIntegration(tsl2591IntegrationTime_t integ) |
12104404 | 0:815555c72774 | 53 | { |
12104404 | 0:815555c72774 | 54 | enable(); |
12104404 | 0:815555c72774 | 55 | _integ = integ; |
12104404 | 0:815555c72774 | 56 | char write[] = {(TSL2591_CMD_BIT|TSL2591_REG_CONTROL), (_integ|_gain)}; |
12104404 | 0:815555c72774 | 57 | _i2c.write(_addr<<1, write, 2, 0); |
12104404 | 0:815555c72774 | 58 | disable(); |
12104404 | 0:815555c72774 | 59 | } |
12104404 | 0:815555c72774 | 60 | |
12104404 | 0:815555c72774 | 61 | void TSL2591::getALS(void) |
12104404 | 0:815555c72774 | 62 | { |
12104404 | 0:815555c72774 | 63 | enable(); |
12104404 | 0:815555c72774 | 64 | for(uint8_t t=0; t<=_integ; t++) { |
12104404 | 0:815555c72774 | 65 | wait(.12); |
12104404 | 0:815555c72774 | 66 | } |
12104404 | 0:815555c72774 | 67 | char write1[] = {(TSL2591_CMD_BIT|TSL2591_REG_CHAN1_H)}; |
12104404 | 0:815555c72774 | 68 | _i2c.write(_addr<<1, write1, 1, 0); |
12104404 | 0:815555c72774 | 69 | char read1[2]; |
12104404 | 0:815555c72774 | 70 | _i2c.read(_addr<<1, read1, 2, 0); |
12104404 | 0:815555c72774 | 71 | char write2[] = {(TSL2591_CMD_BIT|TSL2591_REG_CHAN0_H)}; |
12104404 | 0:815555c72774 | 72 | _i2c.write(_addr<<1, write2, 1, 0); |
12104404 | 0:815555c72774 | 73 | char read2[2]; |
12104404 | 0:815555c72774 | 74 | _i2c.read(_addr<<1, read2, 2, 0); |
12104404 | 0:815555c72774 | 75 | printf("%d\n",(((read1[0]<<8)|read1[1])<<16)|((read2[0]<<8)|read2[1])); |
12104404 | 0:815555c72774 | 76 | disable(); |
12104404 | 0:815555c72774 | 77 | } |
12104404 | 0:815555c72774 | 78 | |
12104404 | 0:815555c72774 | 79 | /* |
12104404 | 0:815555c72774 | 80 | uint16_t TSL2591::getLumin(uint8_t channel) |
12104404 | 0:815555c72774 | 81 | { |
12104404 | 0:815555c72774 | 82 | uint32_t x = |
12104404 | 0:815555c72774 | 83 | } |
12104404 | 0:815555c72774 | 84 | */ |
12104404 | 0:815555c72774 | 85 | //uint32_t TSL2591::calcLux() |