Class which provides functions to control a TAOS TCS3472 Color Light-to-Digital Converter with IR Filter via I2C.

Dependents:   Chipin_Main Test_Color LAB_10_control FINAL_PROJECT ... more

TCS3472_I2C.cpp

Committer:
karlmaxwell67
Date:
2014-03-19
Revision:
2:38d5187a4e7b
Parent:
1:70d7d9f1af01
Child:
3:6a89ac4a1979

File content as of revision 2:38d5187a4e7b:

#include "TCS3472_I2C.h"

TCS3472_I2C::TCS3472_I2C( PinName sda, PinName scl ) : i2c_( sda, scl ){   
    i2c_.frequency(400000);
    enableRGBC();
}

int TCS3472_I2C::writeSingleRegister( char address, char data ){
    char tx[2] = { address | 160, data }; //0d160 = 0b10100000
    int ack = i2c_.write( SLAVE_ADDRESS << 1 , tx, 2 );
    return ack;
}

char TCS3472_I2C::readSingleRegister( char address ){
    char output = 255;
    char command = address | 160; //0d160 = 0b10100000
    i2c_.write( SLAVE_ADDRESS << 1, &command, 1, true );
    i2c_.read( SLAVE_ADDRESS << 1, &output, 1 );
    return output;
}

int TCS3472_I2C::readMultipleRegisters( char address, char* output, int quantity ){
    char command = address | 160; //0d160 = 0b10100000
    i2c_.write( SLAVE_ADDRESS << 1, &command, 1, true );
    int ack = i2c_.read( SLAVE_ADDRESS << 1, output, quantity );
    return ack;
}

int TCS3472_I2C::enableRGBC(){
    int ack = writeSingleRegister( ENABLE_REGISTER, 3 );
    return ack;
}

char TCS3472_I2C::readEnableRegister(){
    return readSingleRegister( ENABLE_REGISTER );
}

int TCS3472_I2C::getAllColours( int* readings ){
    char buffer[8] = { 0 };

    readMultipleRegisters( CDATA, buffer, 8 );

    readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
    readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
    readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
    readings[3] = (int)buffer[7] << 8 | (int)buffer[6];
    
    return 0;
}

int TCS3472_I2C::getClearData(){
    char buffer[2] = { 0 };
    readMultipleRegisters( CDATA, buffer, 2 );
    int reading = (int)buffer[1] << 8 | (int)buffer[0];
    return reading;
}

int TCS3472_I2C::getRedData(){
    char buffer[2] = { 0 };
    readMultipleRegisters( RDATA, buffer, 2 );
    int reading = (int)buffer[1] << 8 | (int)buffer[0];
    return reading;
}

int TCS3472_I2C::getGreenData(){
    char buffer[2] = { 0 };
    readMultipleRegisters( GDATA, buffer, 2 );
    int reading = (int)buffer[1] << 8 | (int)buffer[0];
    return reading;
}

int TCS3472_I2C::getBlueData(){
    char buffer[2] = { 0 };
    readMultipleRegisters( BDATA, buffer, 2 );
    int reading = (int)buffer[1] << 8 | (int)buffer[0];
    return reading;
}

int TCS3472_I2C::setIntegrationTime( const float itime ){
    char atime = 256 - itime / 2.4;
    int ack = writeSingleRegister( ATIME, atime );
    return ack;
}

int TCS3472_I2C::enableWait(){
    char enable_old = readSingleRegister( ENABLE_REGISTER );
    char enable_new = enable_old | 8; // sets WEN (bit 4) to 1
    int ack = writeSingleRegister( ENABLE_REGISTER, enable_new );
    return ack;
}

int TCS3472_I2C::disableWait(){
    char enable_old = readSingleRegister( ENABLE_REGISTER );
    char enable_new = enable_old & 247; // sets WEN (bit 4) to 0
    int ack = writeSingleRegister( ENABLE_REGISTER, enable_new );
    return ack;
}

int TCS3472_I2C::enableInterrupt(){
    char enable_old = readSingleRegister( ENABLE_REGISTER );
    char enable_new = enable_old | 16; // sets AIEN (bit 5) to 1
    int ack = writeSingleRegister( ENABLE_REGISTER, enable_new );
    return ack;
}

int TCS3472_I2C::disableInterrupt(){
    char enable_old = readSingleRegister( ENABLE_REGISTER );
    char enable_new = enable_old & 239; // sets AIEN (bit 5) to 0
    int ack = writeSingleRegister( ENABLE_REGISTER, enable_new );
    return ack;
}

int TCS3472_I2C::setWaitTime( const float time ){
    int ack = 1;
    char wtime = 0;
    if ( time >= 2.4 && time <= 614.4 ){
        ack = writeSingleRegister( CONFIGURATION_REGISTER, 0 ); // sets WLONG to 0
        wtime = 256 - time / 2.4;
    }
    else if ( time > 614.4 && time <= 7400 ){
        ack = writeSingleRegister( CONFIGURATION_REGISTER, 2 ); // sets WLONG to 1
        wtime = 256 - ( time / 12 ) / 2.4;
    } 
    ack = ack || writeSingleRegister( WTIME, wtime );
    return ack;
}