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

Dependents:   HYBRYD2018_IZU_ROCKET

Files at this revision

API Documentation at this revision

Comitter:
Sigma884
Date:
Sat Jan 12 17:05:34 2019 +0000
Parent:
2:276a177de00b
Commit message:
The correct illumination could be measured now.

Changed in this revision

TSL2561.cpp Show annotated file Show diff for this revision Revisions of this file
TSL2561.h Show annotated file Show diff for this revision Revisions of this file
--- a/TSL2561.cpp	Wed Nov 14 09:13:55 2018 +0000
+++ b/TSL2561.cpp	Sat Jan 12 17:05:34 2019 +0000
@@ -1,85 +1,158 @@
 #include "mbed.h"
+#include "math.h"
 #include "TSL2561.h"
 
-//Serial DEBUG(USBTX,USBRX);
-
-myTSL2561::myTSL2561(I2C &i2cBus,AD0 celect)
-{
+myTSL2561::myTSL2561(I2C &i2cBus, AD0 celect){
     i2c = &i2cBus;
-    if(celect == AD0_LOW) _addr = SLV_ADDR_LOW;
-    else if(celect == AD0_HIGH) _addr = SLV_ADDR_HIGH;
-    else _addr = SLV_ADDR_OPEN;
+    if(celect == AD0_LOW){
+        _addr = SLV_ADDR_LOW;
+    }
+    else if(celect == AD0_HIGH){
+        _addr = SLV_ADDR_HIGH;
+    }
+    else{
+        _addr = SLV_ADDR_OPEN;
+    }
     SLV_WRITE_TSL = _addr;
     SLV_READ_TSL  = _addr | 1;
-    i2c->frequency(400000);
+    i2c -> frequency(400000);
 }
 
 
-void myTSL2561::begin()
-{
-    power_up_cmd[0] = COMMAND | CONTROL_REG;
-    power_up_cmd[1] = POWER_UP;
+void myTSL2561::begin(){
+    //power_up_cmd[0] = COMMAND | CONTROL_REG;
+    //power_up_cmd[1] = POWER_UP;
+    cmd[0] = COMMAND | CONTROL_REG;
+    cmd[1] = POWER_UP;
     
-    i2c->write(SLV_WRITE_TSL, power_up_cmd, 2);
-    
-    read_cmd = COMMAND | CHECK_REG;
+    //i2c -> write(SLV_WRITE_TSL, power_up_cmd, 2);
+    i2c -> write(SLV_WRITE_TSL, cmd, 2);
     
-    i2c->write(SLV_WRITE_TSL, &read_cmd, 1);
-    i2c->read(SLV_READ_TSL, &check_read, 1);
+    //read_cmd = COMMAND | CHECK_REG;
+    cmd[0] = COMMAND | CHECK_REG;
     
+    //i2c->write(SLV_WRITE_TSL, &read_cmd, 1);
+    //i2c->read(SLV_READ_TSL, &check_read, 1);
+    i2c -> write(SLV_WRITE_TSL, cmd, 1);
+    i2c -> read(SLV_READ_TSL, buff, 1);
 }
 
-int myTSL2561::connect_check()
-{
-    check_cmd = COMMAND | CHECK_REG;
+int myTSL2561::connectCheck(){
+    //check_cmd = COMMAND | CHECK_REG;
+    cmd[0] = COMMAND | CHECK_REG;
     
-    i2c->write(SLV_WRITE_TSL, &check_cmd, 1);
-    i2c->read(SLV_READ_TSL, &check_read, 1);
+    //i2c->write(SLV_WRITE_TSL, &check_cmd, 1);
+    //i2c->read(SLV_READ_TSL, &check_read, 1);
+    i2c -> write(SLV_WRITE_TSL, cmd, 1);
+    i2c -> read(SLV_READ_TSL, buff, 1);
     
     //DEBUG.printf("CHECK_REG read = %x\r\n",check_read);
     
-    if(check_read == ID_NUMBER){ return 1;}
-    else{ return 0;}
+    //if(check_read == ID_NUMBER){ return 1;}
+    if(buff[0] == ID_NUMBER){
+        return 1;
+    }
+    else{
+        return 0;
+    }
 }
 
-float myTSL2561::get_luminosity(unsigned int wait_time)
-{
-    read_cmd = COMMAND | RAW_DATA_LOW;
+//float myTSL2561::getLuminous0(unsigned int wait_time){
+//int myTSL2561::getLuminous0(unsigned int wait_time){
+int myTSL2561::getLuminous0(){
+    cmd[0] = COMMAND | RAW_DATA_LOW;
     
-    i2c->write(SLV_WRITE_TSL, &read_cmd, 1);
-    //wait_ms(wait_time);
-    i2c->read(SLV_READ_TSL, buff, 2);
+    //i2c->write(SLV_WRITE_TSL, &read_cmd, 1);
+    i2c -> write(SLV_WRITE_TSL, cmd, 1);
+    //i2c->read(SLV_READ_TSL, buff, 2);
+    i2c -> read(SLV_READ_TSL, buff, 2);
     
     val[0] = (int)buff[0];    
     val[1] = (int)buff[1] << 8;   
     
-    lux = (float)(val[1] | val[0]);
+    //lux = (float)(val[1] | val[0]);
+    //lux0 = (int)(val[1] | val[0]);
+    lux0 = (float)(val[1] | val[0]) / scale;
+    
+    //return lux;
+    return (int)lux0;
+}
+
+int myTSL2561::getLuminous1(){
+    cmd[0] = COMMAND | RAW_DATA_R_LOW;
     
-    return lux;
+    i2c -> write(SLV_WRITE_TSL, cmd, 1);
+    i2c -> read(SLV_READ_TSL, buff, 2);
+    
+    val[0] = (int)buff[0];    
+    val[1] = (int)buff[1] << 8;   
+    
+    //lux = (int)(val[1] | val[0]);
+    //lux1 = (int)(val[1] | val[0]);
+    lux1 = (float)(val[1] | val[0]) / scale;
+    
+    //return lux;
+    return (int)lux1;
 }
 
-unsigned int myTSL2561::set_rate(int channel)
-{
-    timing_cmd[0] = COMMAND | TIMING_REG;
+int myTSL2561::getLuminous(){
+    lux0 = getLuminous0();
+    lux1 = getLuminous1();
+    
+    ration = lux1 / lux0;
+    if(ration <= 0.50){
+        //lux = (int)(0.0304 * (float)lux0 - 0.062 * (float)lux0 * powf(ration, 1.4));
+        lux = 0.0304 * lux0 - 0.062 * lux0 * powf(ration, 1.4);
+    }
+    else if(ration <= 0.61){
+        //lux = (int)(0.0224 * (float)lux0 - 0.031 * (float)lux1);
+        lux = 0.0224 * lux0 - 0.031 * lux1;
+    }
+    else if(ration <= 0.80){
+        //lux = (int)(0.0128 * (float)lux0 - 0.0153 * (float)lux1);
+        lux = 0.0128 * lux0 - 0.0153 * lux1;
+    }
+    else if(ration <= 1.30){
+        //lux = (int)(0.00146 * (float)lux0 - 0.00112 * (float)lux1);
+        lux = 0.00146 * lux0 - 0.00112 * lux1;
+    }
+    else{
+        lux = 0;
+    }
+    
+    return (int)lux;
+}
+
+//unsigned int myTSL2561::setRate(int channel){
+void myTSL2561::setRate(int channel){
+    //timing_cmd[0] = COMMAND | TIMING_REG;
+    cmd[0] = COMMAND | TIMING_REG;
     
     if(channel == 0){
-        time = 14;
-        timing_cmd[1] = TIMING | 0x00;
-        i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
+        //time = 14;
+        //timing_cmd[1] = TIMING | 0x00;
+        cmd[1] = TIMING | 0x00;
+        scale = 0.034;
+        //i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
+        //i2c -> write(SLV_WRITE_TSL, cmd, 2);
     }
     else if(channel == 1){
-        time = 105;
-        timing_cmd[1] = TIMING | 0x01;
-        i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
+        //time = 105;
+        //timing_cmd[1] = TIMING | 0x01;
+        cmd[1] = TIMING | 0x01;
+        scale = 0.252;
+        //i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
+        //i2c -> write(SLV_WRITE_TSL, cmd, 2);
     }
     else if(channel == 2){
-        time = 405;
-        timing_cmd[1] = TIMING | 0x02;
-        i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
+        //time = 405;
+        //timing_cmd[1] = TIMING | 0x02;
+        cmd[1] = TIMING | 0x02;
+        scale = 1;
+        //i2c->write(SLV_WRITE_TSL, timing_cmd, 2);
+        //i2c -> write(SLV_WRITE_TSL, cmd, 2);
     }
+    i2c -> write(SLV_WRITE_TSL, cmd, 2);
     
-    return time;
+    //return time;
 }
-    
-    
-    
\ No newline at end of file
--- a/TSL2561.h	Wed Nov 14 09:13:55 2018 +0000
+++ b/TSL2561.h	Sat Jan 12 17:05:34 2019 +0000
@@ -13,6 +13,8 @@
 #define POWER_DOWN      0x00
 #define RAW_DATA_LOW    0x0C
 #define RAW_DATA_HIGH   0x0D
+#define RAW_DATA_R_LOW  0x0E
+#define RAW_DATA_R_HIGH 0x0F
 #define CHECK_REG       0x0A
 #define TIMING_REG      0x01;
 
@@ -23,67 +25,75 @@
 {
 public:
 
-            typedef enum AD0{
-                AD0_LOW  = 0,
-                AD0_HIGH = 1,
-                AD0_OPEN = 2
-            } AD0;
+    typedef enum AD0{
+        AD0_LOW  = 0,
+        AD0_HIGH = 1,
+        AD0_OPEN = 2
+    } AD0;
 
-            myTSL2561(I2C &i2cBus,AD0 celect = AD0_OPEN);
-            
-            void begin();
-            /*******************************************************************
-            TSL2561を起動させる関数
-            データをとる前に起動させておく
-            *******************************************************************/            
-            int connect_check();
-            /*******************************************************************
-            0が返ってきたらError
-            1が返ってきたらOK
-            *******************************************************************/            
-            float get_luminosity(unsigned int wait_time);
-            /*******************************************************************
-            照度(Lux)を返してくれる関数
-            引数のwait_timeには下のset_rateが返してくる値を入れてください
-            @example
-            
-            int time;
-            time = myTSL2561.set_rate(0);
-            pc.printf("Lux = %d\r\n",myTSL2561.get_luminosity(time));
-            *******************************************************************/
-            
-            unsigned int set_rate(int channel);
-            /*******************************************************************
-            channelに入れる値によってデータの変換時間が変わる
-            0:  変換時間13.7[ms]
-            1: 変換時間101[ms]
-            2: 変換時間402[ms]
-            この時間が返ってくるので、それをget_luminosityの引数に入れる(@exampleを参照)
-            *******************************************************************/
-            
-            
-            
+    myTSL2561(I2C &i2cBus,AD0 celect = AD0_OPEN);
+    
+    /*******************************************************************
+    TSL2561を起動させる関数
+    データをとる前に起動させておく
+    *******************************************************************/
+    void begin();
+    
+    /*******************************************************************
+    0が返ってきたらError
+    1が返ってきたらOK
+    *******************************************************************/   
+    int connectCheck();         
+    
+    /*******************************************************************
+    照度(Lux)を返してくれる関数
+    引数のwait_timeには下のset_rateが返してくる値を入れてください
+    @example
+    
+    int time;
+    time = myTSL2561.set_rate(0);
+    pc.printf("Lux = %d\r\n",myTSL2561.get_luminosity(time));
+    *******************************************************************/
+    //float getLuminous0(unsigned int wait_time);
+    //int getLuminous0(unsigned int wait_time);
+    int getLuminous0();
+    int getLuminous1();
+    int getLuminous();
+    
+    /*******************************************************************
+    channelに入れる値によってデータの変換時間が変わる
+    0:  変換時間13.7[ms]
+    1: 変換時間101[ms]
+    2: 変換時間402[ms]
+    この時間が返ってくるので、それをget_luminosityの引数に入れる(@exampleを参照)
+    *******************************************************************/
+    //unsigned int setRate(int channel);
+    void setRate(int channel);
+    
+    
 private:
 
-            I2C *i2c;
-            char _addr;
-            char SLV_WRITE_TSL;
-            char SLV_READ_TSL;
-            char power_up_cmd[2];
-            char power_down_cmd[2];
-            char check_cmd;
-            char read_cmd;
-            char timing_cmd[2];
+    I2C *i2c;
+    char _addr;
+    char SLV_WRITE_TSL;
+    char SLV_READ_TSL;
+    
+    /*char power_up_cmd[2];
+    char power_down_cmd[2];
+    char check_cmd;
+    char read_cmd;
+    char timing_cmd[2];
+    */
+    char cmd[2], buff[2];
 
-            char check_read,buff[2];
-            int val[2];
-            unsigned int sekigai,time;
-            float lux;
-            
-            
+    //char check_read, buff[2];
+    int val[2];
+    //unsigned int sekigai, time;
+    //float lux;
+    //int lux0, lux1, lux;
+    float lux0, lux1, lux;
+    float ration, scale;
+    
 };
 
 #endif
-            
-        
-        
\ No newline at end of file