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

Revision:
4:5d1f8d7d81ff
Parent:
3:6a89ac4a1979
Child:
5:d4cf0fa1a182
--- 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