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

Dependents:   MF_FUJIKO_BASE STEM_2019 STEM_2020

Files at this revision

API Documentation at this revision

Comitter:
mwilkens241
Date:
Wed Jan 25 20:17:57 2017 +0000
Parent:
2:cc2c0831a763
Child:
4:41f1acad69e5
Commit message:
class-ified library and added docs

Changed in this revision

TCS34725.cpp Show annotated file Show diff for this revision Revisions of this file
TCS34725.h Show annotated file Show diff for this revision Revisions of this file
--- a/TCS34725.cpp	Wed Jan 25 19:25:28 2017 +0000
+++ b/TCS34725.cpp	Wed Jan 25 20:17:57 2017 +0000
@@ -1,17 +1,15 @@
 #include "TCS34725.h"
 
-I2C i2c(SDA, SCL);
+TCS34725::TCS34725() : i2c(SDA,SCL) {}
+TCS34725::TCS34725(PinName i2c_sda, PinName i2c_scl) : i2c(i2c_sda,i2c_scl) {}
 
-uint8_t t_intTime;
-uint8_t t_gain;
-
-void i2cWrite8(uint8_t addr, char reg, char data){
+void TCS34725::i2cWrite8(uint8_t addr, char reg, char data){
     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){
+uint8_t TCS34725::i2cRead8(uint8_t addr, char reg){
     char packet[1] = {reg | COMMAND_BIT};
     char data[1] = {0};
     i2c.write(addr,packet,1, true);
@@ -19,7 +17,7 @@
     return (uint8_t)data[0];
 }
 
-uint16_t i2cRead16(uint8_t addr, char reg){
+uint16_t TCS34725::i2cRead16(uint8_t addr, char reg){
     char packet[1] = {reg | COMMAND_BIT};
     char data[2] = {0,0};
     i2c.write(addr,packet,1, true);
@@ -27,7 +25,7 @@
     return ((uint16_t)data[1] << 8) | (uint16_t)data[0];
 }
 
-bool TCS34725_init(char intTime, char gain){
+bool TCS34725::init(char intTime, char gain){
     i2c.frequency(400000);
     
     uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID);
@@ -43,13 +41,13 @@
     return true;
 }
 
-void TCS34725_config(char intTime, char gain){
+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);
+    init(intTime,gain);
 }
 
-void TCS34725_getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){
+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);
@@ -76,7 +74,7 @@
     }
 }
 
-void TCS34725_DEBUG(Serial * deb){
+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),
--- a/TCS34725.h	Wed Jan 25 19:25:28 2017 +0000
+++ b/TCS34725.h	Wed Jan 25 20:17:57 2017 +0000
@@ -1,4 +1,7 @@
 // TCS34725 RGB Color I2C Sensor breakout library for F030R8 Nucleo board
+#ifndef MBED_TCS34725_H
+#define MBED_TCS34725_H
+
 #include "mbed.h"
 
 //These pins should be the only piece specific to the F030R8
@@ -40,12 +43,83 @@
 #define TCS34725_GAIN_16X                 0x02   /**<  16x gain */
 #define TCS34725_GAIN_60X                 0x03   /**<  60x gain */
 
-void i2cWrite8(uint8_t addr, char reg, char data);
-uint8_t i2cRead8(uint8_t addr, char reg);
-uint16_t i2cRead16(uint8_t addr, char reg);
+/** TCS34725 control class.
+ *
+ * Example:
+ * @code
+ * //Send rgb data to the serial port
+ * #include "TCS34725.h"
+ * #include "mbed.h"
+ *
+ * TCS34725 colorSens(p9, p10); //I2C sda and scl
+ * Serial pc(USBTX, USBRX); //USB serial
+ *
+ * int main() {
+ *   uint16_t r,g,b,c;
+ *       
+ *   if(!colorSens.init(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_60X)){
+ *       pc.printf("ERROR\n"); //check to see if i2c is responding
+ *   }
+ *
+ *   while(1) {
+        colorSens.getColor(&r,&g,&b,&c); //pass variables by reference...
+ *      pc.printf("DATA: r%d g%d b%d c%d", r, g, b, c);
+ *      wait(0.5);
+ *  }
+ * }
+ * @endcode
+ */
 
-bool TCS34725_init(char intTime, char gain);
-void TCS34725_config(char intTime, char gain);
-void TCS34725_getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
+class TCS34725 {
+    private:
+        I2C i2c;
+        uint8_t t_intTime;
+        uint8_t t_gain;
+        void i2cWrite8(uint8_t addr, char reg, char data);
+        uint8_t i2cRead8(uint8_t addr, char reg);
+        uint16_t i2cRead16(uint8_t addr, char reg);
+    public:
+        /** Initialize object with default i2c pins */
+        TCS34725();
+        
+        /** Initialize object with specific i2c pins
+        * 
+        * @param i2c_sda SDA pin
+        * @param i2c_scl SCL pin
+        */
+        TCS34725(PinName i2c_sda, PinName i2c_scl);
+        
+        /** Boot up the sensor and checks if acking (see header for param defines)
+        * 
+        * @param intTime Integration time for reading (will delay accordingly)
+        * @param i2c_scl Gain value
+        */
+        bool init(char intTime, char gain);
+        
+        /** Configure after initial boot (will restart sensor)
+        * 
+        * @param intTime Integration time for reading (will delay accordingly)
+        * @param i2c_scl Gain value
+        * @return 
+        *   1 if failed
+        *`  0 if successful
+        */
+        void config(char intTime, char gain);
+        
+        /** Returns rgbc reading from the sensor.
+        * 
+        * @param r Red value (passed by reference)
+        * @param g Green value (passed by reference)
+        * @param b Blue value (passed by reference)
+        * @param c Clear value (all wavelengths - essentially shade) (passed by reference)
+        */
+        void getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
+        
+        /** Debug function... probably not useful unless youre debugging your i2c line
+        * 
+        * @param deb Serial object for debugging (passed by reference)
+        */
+        void DEBUG(Serial *deb);
+};
 
-void TCS34725_DEBUG(Serial *deb);
\ No newline at end of file
+#endif
\ No newline at end of file