TSL2561(照度センサ)のライブラリです

Dependents:   HYBRYD2018_IZU_ROCKET Hybrid_IZU2019

Committer:
Sigma884
Date:
Sat Jan 12 17:05:34 2019 +0000
Revision:
3:f62d7ea19cbb
Parent:
2:276a177de00b
Child:
4:c1e82279b4bb
The correct illumination could be measured now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zebrin1422 0:40ff41e6257e 1 #include "mbed.h"
Sigma884 3:f62d7ea19cbb 2 #include "math.h"
zebrin1422 0:40ff41e6257e 3 #include "TSL2561.h"
zebrin1422 0:40ff41e6257e 4
Sigma884 3:f62d7ea19cbb 5 myTSL2561::myTSL2561(I2C &i2cBus, AD0 celect){
zebrin1422 0:40ff41e6257e 6 i2c = &i2cBus;
Sigma884 3:f62d7ea19cbb 7 if(celect == AD0_LOW){
Sigma884 3:f62d7ea19cbb 8 _addr = SLV_ADDR_LOW;
Sigma884 3:f62d7ea19cbb 9 }
Sigma884 3:f62d7ea19cbb 10 else if(celect == AD0_HIGH){
Sigma884 3:f62d7ea19cbb 11 _addr = SLV_ADDR_HIGH;
Sigma884 3:f62d7ea19cbb 12 }
Sigma884 3:f62d7ea19cbb 13 else{
Sigma884 3:f62d7ea19cbb 14 _addr = SLV_ADDR_OPEN;
Sigma884 3:f62d7ea19cbb 15 }
zebrin1422 1:4e7b35c8d948 16 SLV_WRITE_TSL = _addr;
zebrin1422 1:4e7b35c8d948 17 SLV_READ_TSL = _addr | 1;
Sigma884 3:f62d7ea19cbb 18 i2c -> frequency(400000);
zebrin1422 0:40ff41e6257e 19 }
zebrin1422 0:40ff41e6257e 20
zebrin1422 0:40ff41e6257e 21
Sigma884 3:f62d7ea19cbb 22 void myTSL2561::begin(){
Sigma884 3:f62d7ea19cbb 23 //power_up_cmd[0] = COMMAND | CONTROL_REG;
Sigma884 3:f62d7ea19cbb 24 //power_up_cmd[1] = POWER_UP;
Sigma884 3:f62d7ea19cbb 25 cmd[0] = COMMAND | CONTROL_REG;
Sigma884 3:f62d7ea19cbb 26 cmd[1] = POWER_UP;
zebrin1422 0:40ff41e6257e 27
Sigma884 3:f62d7ea19cbb 28 //i2c -> write(SLV_WRITE_TSL, power_up_cmd, 2);
Sigma884 3:f62d7ea19cbb 29 i2c -> write(SLV_WRITE_TSL, cmd, 2);
zebrin1422 0:40ff41e6257e 30
Sigma884 3:f62d7ea19cbb 31 //read_cmd = COMMAND | CHECK_REG;
Sigma884 3:f62d7ea19cbb 32 cmd[0] = COMMAND | CHECK_REG;
zebrin1422 0:40ff41e6257e 33
Sigma884 3:f62d7ea19cbb 34 //i2c->write(SLV_WRITE_TSL, &read_cmd, 1);
Sigma884 3:f62d7ea19cbb 35 //i2c->read(SLV_READ_TSL, &check_read, 1);
Sigma884 3:f62d7ea19cbb 36 i2c -> write(SLV_WRITE_TSL, cmd, 1);
Sigma884 3:f62d7ea19cbb 37 i2c -> read(SLV_READ_TSL, buff, 1);
zebrin1422 0:40ff41e6257e 38 }
zebrin1422 0:40ff41e6257e 39
Sigma884 3:f62d7ea19cbb 40 int myTSL2561::connectCheck(){
Sigma884 3:f62d7ea19cbb 41 //check_cmd = COMMAND | CHECK_REG;
Sigma884 3:f62d7ea19cbb 42 cmd[0] = COMMAND | CHECK_REG;
zebrin1422 0:40ff41e6257e 43
Sigma884 3:f62d7ea19cbb 44 //i2c->write(SLV_WRITE_TSL, &check_cmd, 1);
Sigma884 3:f62d7ea19cbb 45 //i2c->read(SLV_READ_TSL, &check_read, 1);
Sigma884 3:f62d7ea19cbb 46 i2c -> write(SLV_WRITE_TSL, cmd, 1);
Sigma884 3:f62d7ea19cbb 47 i2c -> read(SLV_READ_TSL, buff, 1);
zebrin1422 0:40ff41e6257e 48
Sigma884 2:276a177de00b 49 //DEBUG.printf("CHECK_REG read = %x\r\n",check_read);
zebrin1422 0:40ff41e6257e 50
Sigma884 3:f62d7ea19cbb 51 //if(check_read == ID_NUMBER){ return 1;}
Sigma884 3:f62d7ea19cbb 52 if(buff[0] == ID_NUMBER){
Sigma884 3:f62d7ea19cbb 53 return 1;
Sigma884 3:f62d7ea19cbb 54 }
Sigma884 3:f62d7ea19cbb 55 else{
Sigma884 3:f62d7ea19cbb 56 return 0;
Sigma884 3:f62d7ea19cbb 57 }
zebrin1422 0:40ff41e6257e 58 }
zebrin1422 0:40ff41e6257e 59
Sigma884 3:f62d7ea19cbb 60 //float myTSL2561::getLuminous0(unsigned int wait_time){
Sigma884 3:f62d7ea19cbb 61 //int myTSL2561::getLuminous0(unsigned int wait_time){
Sigma884 3:f62d7ea19cbb 62 int myTSL2561::getLuminous0(){
Sigma884 3:f62d7ea19cbb 63 cmd[0] = COMMAND | RAW_DATA_LOW;
zebrin1422 0:40ff41e6257e 64
Sigma884 3:f62d7ea19cbb 65 //i2c->write(SLV_WRITE_TSL, &read_cmd, 1);
Sigma884 3:f62d7ea19cbb 66 i2c -> write(SLV_WRITE_TSL, cmd, 1);
Sigma884 3:f62d7ea19cbb 67 //i2c->read(SLV_READ_TSL, buff, 2);
Sigma884 3:f62d7ea19cbb 68 i2c -> read(SLV_READ_TSL, buff, 2);
zebrin1422 0:40ff41e6257e 69
zebrin1422 0:40ff41e6257e 70 val[0] = (int)buff[0];
zebrin1422 0:40ff41e6257e 71 val[1] = (int)buff[1] << 8;
zebrin1422 0:40ff41e6257e 72
Sigma884 3:f62d7ea19cbb 73 //lux = (float)(val[1] | val[0]);
Sigma884 3:f62d7ea19cbb 74 //lux0 = (int)(val[1] | val[0]);
Sigma884 3:f62d7ea19cbb 75 lux0 = (float)(val[1] | val[0]) / scale;
Sigma884 3:f62d7ea19cbb 76
Sigma884 3:f62d7ea19cbb 77 //return lux;
Sigma884 3:f62d7ea19cbb 78 return (int)lux0;
Sigma884 3:f62d7ea19cbb 79 }
Sigma884 3:f62d7ea19cbb 80
Sigma884 3:f62d7ea19cbb 81 int myTSL2561::getLuminous1(){
Sigma884 3:f62d7ea19cbb 82 cmd[0] = COMMAND | RAW_DATA_R_LOW;
zebrin1422 0:40ff41e6257e 83
Sigma884 3:f62d7ea19cbb 84 i2c -> write(SLV_WRITE_TSL, cmd, 1);
Sigma884 3:f62d7ea19cbb 85 i2c -> read(SLV_READ_TSL, buff, 2);
Sigma884 3:f62d7ea19cbb 86
Sigma884 3:f62d7ea19cbb 87 val[0] = (int)buff[0];
Sigma884 3:f62d7ea19cbb 88 val[1] = (int)buff[1] << 8;
Sigma884 3:f62d7ea19cbb 89
Sigma884 3:f62d7ea19cbb 90 //lux = (int)(val[1] | val[0]);
Sigma884 3:f62d7ea19cbb 91 //lux1 = (int)(val[1] | val[0]);
Sigma884 3:f62d7ea19cbb 92 lux1 = (float)(val[1] | val[0]) / scale;
Sigma884 3:f62d7ea19cbb 93
Sigma884 3:f62d7ea19cbb 94 //return lux;
Sigma884 3:f62d7ea19cbb 95 return (int)lux1;
zebrin1422 0:40ff41e6257e 96 }
zebrin1422 0:40ff41e6257e 97
Sigma884 3:f62d7ea19cbb 98 int myTSL2561::getLuminous(){
Sigma884 3:f62d7ea19cbb 99 lux0 = getLuminous0();
Sigma884 3:f62d7ea19cbb 100 lux1 = getLuminous1();
Sigma884 3:f62d7ea19cbb 101
Sigma884 3:f62d7ea19cbb 102 ration = lux1 / lux0;
Sigma884 3:f62d7ea19cbb 103 if(ration <= 0.50){
Sigma884 3:f62d7ea19cbb 104 //lux = (int)(0.0304 * (float)lux0 - 0.062 * (float)lux0 * powf(ration, 1.4));
Sigma884 3:f62d7ea19cbb 105 lux = 0.0304 * lux0 - 0.062 * lux0 * powf(ration, 1.4);
Sigma884 3:f62d7ea19cbb 106 }
Sigma884 3:f62d7ea19cbb 107 else if(ration <= 0.61){
Sigma884 3:f62d7ea19cbb 108 //lux = (int)(0.0224 * (float)lux0 - 0.031 * (float)lux1);
Sigma884 3:f62d7ea19cbb 109 lux = 0.0224 * lux0 - 0.031 * lux1;
Sigma884 3:f62d7ea19cbb 110 }
Sigma884 3:f62d7ea19cbb 111 else if(ration <= 0.80){
Sigma884 3:f62d7ea19cbb 112 //lux = (int)(0.0128 * (float)lux0 - 0.0153 * (float)lux1);
Sigma884 3:f62d7ea19cbb 113 lux = 0.0128 * lux0 - 0.0153 * lux1;
Sigma884 3:f62d7ea19cbb 114 }
Sigma884 3:f62d7ea19cbb 115 else if(ration <= 1.30){
Sigma884 3:f62d7ea19cbb 116 //lux = (int)(0.00146 * (float)lux0 - 0.00112 * (float)lux1);
Sigma884 3:f62d7ea19cbb 117 lux = 0.00146 * lux0 - 0.00112 * lux1;
Sigma884 3:f62d7ea19cbb 118 }
Sigma884 3:f62d7ea19cbb 119 else{
Sigma884 3:f62d7ea19cbb 120 lux = 0;
Sigma884 3:f62d7ea19cbb 121 }
Sigma884 3:f62d7ea19cbb 122
Sigma884 3:f62d7ea19cbb 123 return (int)lux;
Sigma884 3:f62d7ea19cbb 124 }
Sigma884 3:f62d7ea19cbb 125
Sigma884 3:f62d7ea19cbb 126 //unsigned int myTSL2561::setRate(int channel){
Sigma884 3:f62d7ea19cbb 127 void myTSL2561::setRate(int channel){
Sigma884 3:f62d7ea19cbb 128 //timing_cmd[0] = COMMAND | TIMING_REG;
Sigma884 3:f62d7ea19cbb 129 cmd[0] = COMMAND | TIMING_REG;
zebrin1422 0:40ff41e6257e 130
zebrin1422 0:40ff41e6257e 131 if(channel == 0){
Sigma884 3:f62d7ea19cbb 132 //time = 14;
Sigma884 3:f62d7ea19cbb 133 //timing_cmd[1] = TIMING | 0x00;
Sigma884 3:f62d7ea19cbb 134 cmd[1] = TIMING | 0x00;
Sigma884 3:f62d7ea19cbb 135 scale = 0.034;
Sigma884 3:f62d7ea19cbb 136 //i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
Sigma884 3:f62d7ea19cbb 137 //i2c -> write(SLV_WRITE_TSL, cmd, 2);
zebrin1422 0:40ff41e6257e 138 }
zebrin1422 0:40ff41e6257e 139 else if(channel == 1){
Sigma884 3:f62d7ea19cbb 140 //time = 105;
Sigma884 3:f62d7ea19cbb 141 //timing_cmd[1] = TIMING | 0x01;
Sigma884 3:f62d7ea19cbb 142 cmd[1] = TIMING | 0x01;
Sigma884 3:f62d7ea19cbb 143 scale = 0.252;
Sigma884 3:f62d7ea19cbb 144 //i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
Sigma884 3:f62d7ea19cbb 145 //i2c -> write(SLV_WRITE_TSL, cmd, 2);
zebrin1422 0:40ff41e6257e 146 }
zebrin1422 0:40ff41e6257e 147 else if(channel == 2){
Sigma884 3:f62d7ea19cbb 148 //time = 405;
Sigma884 3:f62d7ea19cbb 149 //timing_cmd[1] = TIMING | 0x02;
Sigma884 3:f62d7ea19cbb 150 cmd[1] = TIMING | 0x02;
Sigma884 3:f62d7ea19cbb 151 scale = 1;
Sigma884 3:f62d7ea19cbb 152 //i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
Sigma884 3:f62d7ea19cbb 153 //i2c -> write(SLV_WRITE_TSL, cmd, 2);
zebrin1422 0:40ff41e6257e 154 }
Sigma884 3:f62d7ea19cbb 155 i2c -> write(SLV_WRITE_TSL, cmd, 2);
zebrin1422 0:40ff41e6257e 156
Sigma884 3:f62d7ea19cbb 157 //return time;
zebrin1422 0:40ff41e6257e 158 }