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

Dependents:   HYBRYD2018_IZU_ROCKET Hybrid_IZU2019

TSL2561.cpp

Committer:
zebrin1422
Date:
2017-11-16
Revision:
0:40ff41e6257e
Child:
1:4e7b35c8d948

File content as of revision 0:40ff41e6257e:

#include "mbed.h"
#include "TSL2561.h"

Serial DEBUG(USBTX,USBRX);

myTSL2561::myTSL2561(I2C &i2cBus)
{
    i2c = &i2cBus;
    i2c->frequency(400000);
}


void myTSL2561::begin()
{
    power_up_cmd[0] = COMMAND | CONTROL_REG;
    power_up_cmd[1] = POWER_UP;
    
    i2c->write(SLV_WRITE, power_up_cmd, 2);
    
    read_cmd = COMMAND | CHECK_REG;
    
    i2c->write(SLV_WRITE, &read_cmd, 1);
    i2c->read(SLV_READ, &check_read, 1);
    
}

int myTSL2561::connect_check()
{
    check_cmd = COMMAND | CHECK_REG;
    
    i2c->write(SLV_WRITE, &check_cmd, 1);
    i2c->read(SLV_READ, &check_read, 1);
    
    DEBUG.printf("CHECK_REG read = %x\r\n",check_read);
    
    if(check_read == ID_NUMBER){ return 1;}
    else{ return 0;}
}

unsigned int myTSL2561::get_luminosity(unsigned int wait_time)
{
    read_cmd = COMMAND | RAW_DATA_LOW;
    
    i2c->write(SLV_WRITE, &read_cmd, 1);
    wait_ms(wait_time);
    i2c->read(SLV_READ, buff, 2);
    
    val[0] = (int)buff[0];    
    val[1] = (int)buff[1] << 8;   
    
    lux = (unsigned int)(val[1] | val[0]);
    
    return lux;
}

unsigned int myTSL2561::set_rate(int channel)
{
    timing_cmd[0] = COMMAND | TIMING_REG;
    
    if(channel == 0){
        time = 14;
        timing_cmd[1] = TIMING | 0x00;
        i2c->write(SLV_WRITE, timing_cmd, 2);
    }
    else if(channel == 1){
        time = 105;
        timing_cmd[1] = TIMING | 0x01;
        i2c->write(SLV_WRITE, timing_cmd, 2);
    }
    else if(channel == 2){
        time = 405;
        timing_cmd[1] = TIMING | 0x02;
        i2c->write(SLV_WRITE, timing_cmd, 2);
    }
    
    return time;
}