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:
1:06c9bbbdb8b0
Parent:
0:4796574af790
Child:
2:cc2c0831a763
--- a/TCS34725.cpp	Thu Jan 19 18:53:40 2017 +0000
+++ b/TCS34725.cpp	Tue Jan 24 23:25:54 2017 +0000
@@ -1,25 +1,51 @@
 #include "TCS34725.h"
 
+I2C i2c(SDA, SCL);
+
 void i2cWrite8(uint8_t addr, char reg, char data){
-    
+    char packet[2] = {reg + 0x80,data}; // unclear why 0x80 needs to be added...
+    i2c.write(addr,packet,2,false);
 }
 
 uint8_t i2cRead8(uint8_t addr, char reg){
-    
+    char packet[1] = {reg + 0x80};
+    char data[1] = {0};
+    i2c.write(addr,packet,1, true);
+    i2c.read(addr,data,1,false);
+    return (uint8_t)data[0];
 }
 
 uint16_t i2cRead16(uint8_t addr, char reg){
-    
-}
-
-TCS34725_init(char intTime, char gain){
-    
+    char packet[1] = {reg + 0x80};
+    char data[2] = {0,0};
+    i2c.write(addr,packet,1, true);
+    i2c.read(addr,data,2, false);
+    return ((uint16_t)data[1] << 8) | (uint16_t)data[0];
 }
 
-TCS34725_config(char intTime, char gain){
+bool TCS34725_init(char intTime, char gain){
+    i2c.frequency(400000);
+    
+    uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID);
+    if(id != 0x44)return false;
     
+    i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime);
+    i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain);
+    i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON);
+    wait(0.003);
+    i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
+    return true;
 }
 
-TCS34725_getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){
-    
+void TCS34725_config(char intTime, char gain){
+    uint8_t reg = i2cRead8(SENSOR_ADDR, TCS34725_ENABLE);
+    i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, reg | ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
+    TCS34725_init(intTime,gain);
+}
+
+void TCS34725_getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){
+    *c = i2cRead16(SENSOR_ADDR, TCS34725_CDATAL);
+    *r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL);
+    *g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL);
+    *b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL);
 }
\ No newline at end of file