Class which provides functions to control a TAOS TCS3472 Color Light-to-Digital Converter with IR Filter via I2C. (Tidied up)
Dependents: openwear-lifelogger-example
Fork of TCS3472_I2C by
Diff: TCS3472_I2C.cpp
- Revision:
- 1:70d7d9f1af01
- Parent:
- 0:453a43c8bf2b
- Child:
- 2:38d5187a4e7b
diff -r 453a43c8bf2b -r 70d7d9f1af01 TCS3472_I2C.cpp --- a/TCS3472_I2C.cpp Tue Mar 18 09:45:04 2014 +0000 +++ b/TCS3472_I2C.cpp Wed Mar 19 17:23:11 2014 +0000 @@ -1,5 +1,77 @@ -#include TCS3472_I2C.h +#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 ); +} -TCS3472_I2C::TCS3472_I2C( PinName sda, PinName scl ) : i2c_( sda, scl ){ +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; } \ No newline at end of file