Basic library for the TCS34725, based off of several example programs and the Adafruit Arduino library

Dependents:   MF_FUJIKO_BASE STEM_2019 STEM_2020

Revision:
2:cc2c0831a763
Parent:
1:06c9bbbdb8b0
Child:
3:afb107db7994
--- a/TCS34725.cpp	Tue Jan 24 23:25:54 2017 +0000
+++ b/TCS34725.cpp	Wed Jan 25 19:25:28 2017 +0000
@@ -2,13 +2,17 @@
 
 I2C i2c(SDA, SCL);
 
+uint8_t t_intTime;
+uint8_t t_gain;
+
 void i2cWrite8(uint8_t addr, char reg, char data){
-    char packet[2] = {reg + 0x80,data}; // unclear why 0x80 needs to be added...
+    char packet[2] = {reg | COMMAND_BIT ,data & 0xFF}; 
     i2c.write(addr,packet,2,false);
+    wait(0.01);
 }
 
 uint8_t i2cRead8(uint8_t addr, char reg){
-    char packet[1] = {reg + 0x80};
+    char packet[1] = {reg | COMMAND_BIT};
     char data[1] = {0};
     i2c.write(addr,packet,1, true);
     i2c.read(addr,data,1,false);
@@ -16,7 +20,7 @@
 }
 
 uint16_t i2cRead16(uint8_t addr, char reg){
-    char packet[1] = {reg + 0x80};
+    char packet[1] = {reg | COMMAND_BIT};
     char data[2] = {0,0};
     i2c.write(addr,packet,1, true);
     i2c.read(addr,data,2, false);
@@ -30,7 +34,9 @@
     if(id != 0x44)return false;
     
     i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime);
+    t_intTime = intTime;
     i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain);
+    t_gain = gain;
     i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON);
     wait(0.003);
     i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
@@ -48,4 +54,32 @@
     *r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL);
     *g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL);
     *b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL);
+    switch(t_intTime){
+        case TCS34725_INTEGRATIONTIME_2_4MS:
+            wait(0.003);
+            break;
+        case TCS34725_INTEGRATIONTIME_24MS:
+            wait(0.024);
+            break;
+        case TCS34725_INTEGRATIONTIME_50MS:
+            wait(0.05);
+            break;
+        case TCS34725_INTEGRATIONTIME_101MS:
+            wait(0.101);
+            break;
+        case TCS34725_INTEGRATIONTIME_154MS:
+            wait(0.154);
+            break;
+        case TCS34725_INTEGRATIONTIME_700MS:
+            wait(0.7);
+            break;
+    }
+}
+
+void TCS34725_DEBUG(Serial * deb){
+    deb->printf("ATIME:%d ENABLE:%d CONTROL:%d ID:%d\n",
+    i2cRead8(SENSOR_ADDR, TCS34725_ATIME),
+    i2cRead8(SENSOR_ADDR, TCS34725_ENABLE),
+    i2cRead8(SENSOR_ADDR, TCS34725_CONTROL),
+    i2cRead16(SENSOR_ADDR, TCS34725_ID));
 }
\ No newline at end of file