TSL2561(照度センサ)のライブラリです
Dependents: HYBRYD2018_IZU_ROCKET Hybrid_IZU2019
TSL2561.cpp@2:276a177de00b, 2018-11-14 (annotated)
- Committer:
- Sigma884
- Date:
- Wed Nov 14 09:13:55 2018 +0000
- Revision:
- 2:276a177de00b
- Parent:
- 1:4e7b35c8d948
- Child:
- 3:f62d7ea19cbb
?????TSL2561??????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zebrin1422 | 0:40ff41e6257e | 1 | #include "mbed.h" |
zebrin1422 | 0:40ff41e6257e | 2 | #include "TSL2561.h" |
zebrin1422 | 0:40ff41e6257e | 3 | |
Sigma884 | 2:276a177de00b | 4 | //Serial DEBUG(USBTX,USBRX); |
zebrin1422 | 0:40ff41e6257e | 5 | |
zebrin1422 | 1:4e7b35c8d948 | 6 | myTSL2561::myTSL2561(I2C &i2cBus,AD0 celect) |
zebrin1422 | 0:40ff41e6257e | 7 | { |
zebrin1422 | 0:40ff41e6257e | 8 | i2c = &i2cBus; |
zebrin1422 | 1:4e7b35c8d948 | 9 | if(celect == AD0_LOW) _addr = SLV_ADDR_LOW; |
zebrin1422 | 1:4e7b35c8d948 | 10 | else if(celect == AD0_HIGH) _addr = SLV_ADDR_HIGH; |
zebrin1422 | 1:4e7b35c8d948 | 11 | else _addr = SLV_ADDR_OPEN; |
zebrin1422 | 1:4e7b35c8d948 | 12 | SLV_WRITE_TSL = _addr; |
zebrin1422 | 1:4e7b35c8d948 | 13 | SLV_READ_TSL = _addr | 1; |
zebrin1422 | 0:40ff41e6257e | 14 | i2c->frequency(400000); |
zebrin1422 | 0:40ff41e6257e | 15 | } |
zebrin1422 | 0:40ff41e6257e | 16 | |
zebrin1422 | 0:40ff41e6257e | 17 | |
zebrin1422 | 0:40ff41e6257e | 18 | void myTSL2561::begin() |
zebrin1422 | 0:40ff41e6257e | 19 | { |
zebrin1422 | 0:40ff41e6257e | 20 | power_up_cmd[0] = COMMAND | CONTROL_REG; |
zebrin1422 | 0:40ff41e6257e | 21 | power_up_cmd[1] = POWER_UP; |
zebrin1422 | 0:40ff41e6257e | 22 | |
zebrin1422 | 1:4e7b35c8d948 | 23 | i2c->write(SLV_WRITE_TSL, power_up_cmd, 2); |
zebrin1422 | 0:40ff41e6257e | 24 | |
zebrin1422 | 0:40ff41e6257e | 25 | read_cmd = COMMAND | CHECK_REG; |
zebrin1422 | 0:40ff41e6257e | 26 | |
zebrin1422 | 1:4e7b35c8d948 | 27 | i2c->write(SLV_WRITE_TSL, &read_cmd, 1); |
zebrin1422 | 1:4e7b35c8d948 | 28 | i2c->read(SLV_READ_TSL, &check_read, 1); |
zebrin1422 | 0:40ff41e6257e | 29 | |
zebrin1422 | 0:40ff41e6257e | 30 | } |
zebrin1422 | 0:40ff41e6257e | 31 | |
zebrin1422 | 0:40ff41e6257e | 32 | int myTSL2561::connect_check() |
zebrin1422 | 0:40ff41e6257e | 33 | { |
zebrin1422 | 0:40ff41e6257e | 34 | check_cmd = COMMAND | CHECK_REG; |
zebrin1422 | 0:40ff41e6257e | 35 | |
zebrin1422 | 1:4e7b35c8d948 | 36 | i2c->write(SLV_WRITE_TSL, &check_cmd, 1); |
zebrin1422 | 1:4e7b35c8d948 | 37 | i2c->read(SLV_READ_TSL, &check_read, 1); |
zebrin1422 | 0:40ff41e6257e | 38 | |
Sigma884 | 2:276a177de00b | 39 | //DEBUG.printf("CHECK_REG read = %x\r\n",check_read); |
zebrin1422 | 0:40ff41e6257e | 40 | |
zebrin1422 | 0:40ff41e6257e | 41 | if(check_read == ID_NUMBER){ return 1;} |
zebrin1422 | 0:40ff41e6257e | 42 | else{ return 0;} |
zebrin1422 | 0:40ff41e6257e | 43 | } |
zebrin1422 | 0:40ff41e6257e | 44 | |
zebrin1422 | 1:4e7b35c8d948 | 45 | float myTSL2561::get_luminosity(unsigned int wait_time) |
zebrin1422 | 0:40ff41e6257e | 46 | { |
zebrin1422 | 0:40ff41e6257e | 47 | read_cmd = COMMAND | RAW_DATA_LOW; |
zebrin1422 | 0:40ff41e6257e | 48 | |
zebrin1422 | 1:4e7b35c8d948 | 49 | i2c->write(SLV_WRITE_TSL, &read_cmd, 1); |
Sigma884 | 2:276a177de00b | 50 | //wait_ms(wait_time); |
zebrin1422 | 1:4e7b35c8d948 | 51 | i2c->read(SLV_READ_TSL, buff, 2); |
zebrin1422 | 0:40ff41e6257e | 52 | |
zebrin1422 | 0:40ff41e6257e | 53 | val[0] = (int)buff[0]; |
zebrin1422 | 0:40ff41e6257e | 54 | val[1] = (int)buff[1] << 8; |
zebrin1422 | 0:40ff41e6257e | 55 | |
zebrin1422 | 1:4e7b35c8d948 | 56 | lux = (float)(val[1] | val[0]); |
zebrin1422 | 0:40ff41e6257e | 57 | |
zebrin1422 | 0:40ff41e6257e | 58 | return lux; |
zebrin1422 | 0:40ff41e6257e | 59 | } |
zebrin1422 | 0:40ff41e6257e | 60 | |
zebrin1422 | 0:40ff41e6257e | 61 | unsigned int myTSL2561::set_rate(int channel) |
zebrin1422 | 0:40ff41e6257e | 62 | { |
zebrin1422 | 0:40ff41e6257e | 63 | timing_cmd[0] = COMMAND | TIMING_REG; |
zebrin1422 | 0:40ff41e6257e | 64 | |
zebrin1422 | 0:40ff41e6257e | 65 | if(channel == 0){ |
zebrin1422 | 0:40ff41e6257e | 66 | time = 14; |
zebrin1422 | 0:40ff41e6257e | 67 | timing_cmd[1] = TIMING | 0x00; |
zebrin1422 | 1:4e7b35c8d948 | 68 | i2c->write(SLV_WRITE_TSL, timing_cmd, 2); |
zebrin1422 | 0:40ff41e6257e | 69 | } |
zebrin1422 | 0:40ff41e6257e | 70 | else if(channel == 1){ |
zebrin1422 | 0:40ff41e6257e | 71 | time = 105; |
zebrin1422 | 0:40ff41e6257e | 72 | timing_cmd[1] = TIMING | 0x01; |
zebrin1422 | 1:4e7b35c8d948 | 73 | i2c->write(SLV_WRITE_TSL, timing_cmd, 2); |
zebrin1422 | 0:40ff41e6257e | 74 | } |
zebrin1422 | 0:40ff41e6257e | 75 | else if(channel == 2){ |
zebrin1422 | 0:40ff41e6257e | 76 | time = 405; |
zebrin1422 | 0:40ff41e6257e | 77 | timing_cmd[1] = TIMING | 0x02; |
zebrin1422 | 1:4e7b35c8d948 | 78 | i2c->write(SLV_WRITE_TSL, timing_cmd, 2); |
zebrin1422 | 0:40ff41e6257e | 79 | } |
zebrin1422 | 0:40ff41e6257e | 80 | |
zebrin1422 | 0:40ff41e6257e | 81 | return time; |
zebrin1422 | 0:40ff41e6257e | 82 | } |
zebrin1422 | 0:40ff41e6257e | 83 | |
zebrin1422 | 0:40ff41e6257e | 84 | |
zebrin1422 | 0:40ff41e6257e | 85 |