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 Karl Maxwell

Files at this revision

API Documentation at this revision

Comitter:
karlmaxwell67
Date:
Wed Mar 19 17:23:11 2014 +0000
Parent:
0:453a43c8bf2b
Child:
2:38d5187a4e7b
Commit message:
Can successfully read data from sensor. No ATIME functionality present.

Changed in this revision

TCS3472_I2C.cpp Show annotated file Show diff for this revision Revisions of this file
TCS3472_I2C.h Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/TCS3472_I2C.h	Tue Mar 18 09:45:04 2014 +0000
+++ b/TCS3472_I2C.h	Wed Mar 19 17:23:11 2014 +0000
@@ -2,21 +2,33 @@
 #define TCS3472_I2C_H
 #include "mbed.h"
 
+//Defines 
+#define SLAVE_ADDRESS           0x29
+#define ENABLE_REGISTER         0x00
+#define CDATA                   0x14
+#define RDATA                   0x16
+#define GDATA                   0x18
+#define BDATA                   0x1A
+
 class TCS3472_I2C {
 public:
     TCS3472_I2C( PinName sda, PinName scl );
-
-
-
-
+    
+    int getAllColours( int* readings );
+    int getClearData();
+    int getRedData();
+    int getGreenData();
+    int getBlueData();
+    
+    char readEnableRegister();
 
 private:
     I2C i2c_;
-
-
-
-
-
+    
+    int enableRGBC();
+    int writeSingleRegister( char address, char data );
+    char readSingleRegister( char address );
+    int readMultipleRegisters( char address, char* output, int quantity );
 };
 
-#endif TCS3472_I2C_H
\ No newline at end of file
+#endif
\ No newline at end of file