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:
Fri Mar 21 17:38:11 2014 +0000
Parent:
3:6a89ac4a1979
Child:
5:d4cf0fa1a182
Commit message:
Added isPowerEnabled, isRGBCEnabled etc. and roundTowardsZero which is used in setWaitTime and setIntegrationTime

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	Thu Mar 20 16:56:56 2014 +0000
+++ b/TCS3472_I2C.cpp	Fri Mar 21 17:38:11 2014 +0000
@@ -91,6 +91,13 @@
     return ack;
 }
 
+bool TCS3472_I2C::isPowerEnabled(){
+    char enable = readSingleRegister( ENABLE );
+    char pon = enable << 7;
+    pon = pon >> 7; // gets PON (bit 0) from ENABLE register byte
+    return (bool)pon;
+}
+
 int TCS3472_I2C::enableRGBC(){
     char enable_old = readSingleRegister( ENABLE );
     char enable_new = enable_old | 2; // sets AEN (bit 1) to 1
@@ -105,6 +112,13 @@
     return ack;
 }
 
+bool TCS3472_I2C::isRGBCEnabled(){
+    char enable = readSingleRegister( ENABLE );
+    char aen = enable << 6;
+    aen = aen >> 7; // gets AEN (bit 1) from ENABLE register byte
+    return (bool)aen;
+}
+
 int TCS3472_I2C::enablePowerAndRGBC(){
     char enable_old = readSingleRegister( ENABLE );
     char enable_new = enable_old | 3; // sets PON (bit 0) and AEN (bit 1) to 1
@@ -114,60 +128,95 @@
 
 int TCS3472_I2C::disablePowerAndRGBC(){
     char enable_old = readSingleRegister( ENABLE );
-    char enable_new = enable_old | 252; // sets PON (bit 0) and AEN (bit 1) to 0
+    char enable_new = enable_old & 252; // sets PON (bit 0) and AEN (bit 1) to 0
     int ack = writeSingleRegister( ENABLE, enable_new );
     return ack;
 }
 
 int TCS3472_I2C::enableWait(){
     char enable_old = readSingleRegister( ENABLE );
-    char enable_new = enable_old | 8; // sets WEN (bit 4) to 1
+    char enable_new = enable_old | 8; // sets WEN (bit 3) to 1
     int ack = writeSingleRegister( ENABLE, enable_new );
     return ack;
 }
 
 int TCS3472_I2C::disableWait(){
     char enable_old = readSingleRegister( ENABLE );
-    char enable_new = enable_old & 247; // sets WEN (bit 4) to 0
+    char enable_new = enable_old & 247; // sets WEN (bit 3) to 0
     int ack = writeSingleRegister( ENABLE, enable_new );
     return ack;
 }
 
+bool TCS3472_I2C::isWaitEnabled(){ 
+    char enable = readSingleRegister( ENABLE );
+    char wen = enable << 4;
+    wen = wen >> 7; // gets WEN (bit 3) from ENABLE register byte
+    return (bool)wen;
+}
+
 int TCS3472_I2C::enableInterrupt(){
     char enable_old = readSingleRegister( ENABLE );
-    char enable_new = enable_old | 16; // sets AIEN (bit 5) to 1
+    char enable_new = enable_old | 16; // sets AIEN (bit 4) to 1
     int ack = writeSingleRegister( ENABLE, enable_new );
     return ack;
 }
 
 int TCS3472_I2C::disableInterrupt(){
     char enable_old = readSingleRegister( ENABLE );
-    char enable_new = enable_old & 239; // sets AIEN (bit 5) to 0
+    char enable_new = enable_old & 239; // sets AIEN (bit 4) to 0
     int ack = writeSingleRegister( ENABLE, enable_new );
     return ack;
 }
 
+bool TCS3472_I2C::isInterruptEnabled(){
+    char enable = readSingleRegister( ENABLE );
+    char aien = enable << 3;
+    aien = aien >> 7; // gets AIEN (bit 4) from ENABLE register byte
+    return (bool)aien;
+}
+
 int TCS3472_I2C::setIntegrationTime( const float itime ){
-    char atime = 256 - itime / 2.4;
+    char atime = 256 - roundTowardsZero( itime / 2.4 ); // rounding ensures nearest value of atime is used
     int ack = writeSingleRegister( ATIME, atime );
     return ack;
 }
 
+float TCS3472_I2C::readIntegrationTime(){
+    float itime = 0;
+    char atime = readSingleRegister( ATIME );
+    itime = 2.4 * ( 256 - atime );
+    return itime;
+}
+
 int TCS3472_I2C::setWaitTime( const float time ){
     int ack = 1;
     char wtime = 0;
-    if ( time >= 2.4 && time <= 614.4 ){
+    if ( time >= 2.39 && time <= 614.4 ){ // 2.39 instead of 2.4 to allow for float accuracy errors
         ack = writeSingleRegister( CONFIG, 0 ); // sets WLONG to 0
-        wtime = 256 - time / 2.4;
+        wtime = 256 - roundTowardsZero( time / 2.4 );
     }
-    else if ( time > 614.4 && time <= 7400 ){
+    else if ( time > 614.4 && time <= 7400.1 ){ // 7400.1 instead of 7400 to allow for float accuracy errors
         ack = writeSingleRegister( CONFIG, 2 ); // sets WLONG to 1
-        wtime = 256 - ( time / 12 ) / 2.4;
+        wtime = 256 - roundTowardsZero( time / 28.8 );
     } 
     ack = ack || writeSingleRegister( WTIME, wtime );
     return ack;
 }
 
+float TCS3472_I2C::readWaitTime(){
+    float time = 0;
+    char wtime = readSingleRegister( WTIME );
+    char config = readSingleRegister( CONFIG );
+    int wlong = ( config << 6 ) >> 7; // gets WLONG (bit 1) from CONFIG register byte
+    if ( wlong == 0 ){
+        time = 2.4 * ( 256 - wtime );
+    }
+    else if ( wlong == 1 ){
+        time = 28.8 * ( 256 - wtime ); // 28.8 = 2.4 * 12
+    }
+    return time;
+}
+
 char TCS3472_I2C::readEnableRegister(){
     return readSingleRegister( ENABLE );
 }
@@ -385,4 +434,15 @@
 
 char TCS3472_I2C::readStatusRegister(){
     return readSingleRegister( STATUS );
+}
+
+float TCS3472_I2C::roundTowardsZero( const float value ){
+    float result = 0;
+    if ( ( value >= 0 && ( value - (int)value ) < 0.5 ) || ( value < 0 && ( abs(value) - (int)abs(value) ) >= 0.5 ) ){
+        result = floor(value);
+    }
+    else{
+        result = ceil(value);
+    }
+    return result;
 }
\ No newline at end of file
--- a/TCS3472_I2C.h	Thu Mar 20 16:56:56 2014 +0000
+++ b/TCS3472_I2C.h	Fri Mar 21 17:38:11 2014 +0000
@@ -32,16 +32,23 @@
    
     int enablePower();
     int disablePower();
+    bool isPowerEnabled();
     int enableRGBC();
     int disableRGBC();
+    bool isRGBCEnabled();
     int enablePowerAndRGBC();
     int disablePowerAndRGBC();
     int enableWait();
     int disableWait();
+    bool isWaitEnabled();
     int enableInterrupt();
     int disableInterrupt();
+    bool isInterruptEnabled();
+    
     int setWaitTime( const float wtime ); // wtime (in ms) should be in the range 2.4 - 7400ms.
+    float readWaitTime();
     int setIntegrationTime( const float itime ); // itime (in ms) should be in the range 2.4 - 614.4ms.
+    float readIntegrationTime();
     
     char readEnableRegister();
     int readLowInterruptThreshold();
@@ -63,6 +70,8 @@
     int writeMultipleRegisters( char address, char* data, int quantity );
     char readSingleRegister( char address );
     int readMultipleRegisters( char address, char* output, int quantity );
+    
+    float roundTowardsZero( const float value );
 };
 
 #endif
\ No newline at end of file