Michael Wilkens / libTCS34725

Dependents:   MF_FUJIKO_BASE STEM_2019 STEM_2020

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCS34725.h Source File

TCS34725.h

00001 // TCS34725 RGB Color I2C Sensor breakout library for F030R8 Nucleo board
00002 #ifndef MBED_TCS34725_H
00003 #define MBED_TCS34725_H
00004 
00005 #include "mbed.h"
00006 
00007 //These pins should be the only piece specific to the F030R8
00008 #define SCL            PB_8
00009 #define SDA            PB_9
00010 
00011 //I2C Address
00012 
00013 #define SENSOR_ADDR (0x29<<1)
00014 #define COMMAND_BIT 0x80
00015 
00016 //Important Registers
00017 
00018 #define TCS34725_ENABLE                  (0x00) //for turning on the device
00019 #define TCS34725_ATIME                   (0x01)  //for integration time
00020 #define TCS34725_CONTROL                 (0x0F)  //for setting the gain
00021 #define TCS34725_ID                      (0x12)
00022 #define TCS34725_CDATAL                  (0x14)    /* Clear channel data */
00023 #define TCS34725_CDATAH                  (0x15)
00024 #define TCS34725_RDATAL                  (0x16)    /* Red channel data */
00025 #define TCS34725_RDATAH                  (0x17)
00026 #define TCS34725_GDATAL                  (0x18)    /* Green channel data */
00027 #define TCS34725_GDATAH                  (0x19)
00028 #define TCS34725_BDATAL                  (0x1A)    /* Blue channel data */
00029 #define TCS34725_BDATAH                  (0x1B)
00030 
00031 //Configuration Bits
00032 
00033 #define TCS34725_ENABLE_AEN              (0x02)    /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
00034 #define TCS34725_ENABLE_PON              (0x01) /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
00035 #define TCS34725_INTEGRATIONTIME_2_4MS    0xFF   /**<  2.4ms - 1 cycle    - Max Count: 1024  */
00036 #define TCS34725_INTEGRATIONTIME_24MS     0xF6   /**<  24ms  - 10 cycles  - Max Count: 10240 */
00037 #define TCS34725_INTEGRATIONTIME_50MS     0xEB   /**<  50ms  - 20 cycles  - Max Count: 20480 */
00038 #define TCS34725_INTEGRATIONTIME_101MS    0xD5   /**<  101ms - 42 cycles  - Max Count: 43008 */
00039 #define TCS34725_INTEGRATIONTIME_154MS    0xC0   /**<  154ms - 64 cycles  - Max Count: 65535 */
00040 #define TCS34725_INTEGRATIONTIME_700MS    0x00   /**<  700ms - 256 cycles - Max Count: 65535 */
00041 #define TCS34725_GAIN_1X                  0x00   /**<  No gain  */
00042 #define TCS34725_GAIN_4X                  0x01   /**<  4x gain  */
00043 #define TCS34725_GAIN_16X                 0x02   /**<  16x gain */
00044 #define TCS34725_GAIN_60X                 0x03   /**<  60x gain */
00045 
00046 /** TCS34725 control class.
00047  *
00048  * Example:
00049  * @code
00050  * //Send rgb data to the serial port
00051  * #include "TCS34725.h"
00052  * #include "mbed.h"
00053  *
00054  * TCS34725 colorSens(p9, p10); //I2C sda and scl
00055  * Serial pc(USBTX, USBRX); //USB serial
00056  *
00057  * int main() {
00058  *   uint16_t r,g,b,c;
00059  *       
00060  *   if(!colorSens.init(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_60X)){
00061  *       pc.printf("ERROR\n"); //check to see if i2c is responding
00062  *   }
00063  *
00064  *   while(1) {
00065         colorSens.getColor(r,g,b,c); //pass variables by reference...
00066  *      pc.printf("DATA: r%d g%d b%d c%d", r, g, b, c);
00067  *      wait(0.5);
00068  *  }
00069  * }
00070  * @endcode
00071  */
00072 
00073 class TCS34725 {
00074     private:
00075         I2C i2c;
00076         uint8_t t_intTime;
00077         uint8_t t_gain;
00078         void i2cWrite8(uint8_t addr, char reg, char data);
00079         uint8_t i2cRead8(uint8_t addr, char reg);
00080         uint16_t i2cRead16(uint8_t addr, char reg);
00081     public:
00082         /** Initialize object with default i2c pins */
00083         TCS34725();
00084         
00085         /** Initialize object with specific i2c pins
00086         * 
00087         * @param i2c_sda SDA pin
00088         * @param i2c_scl SCL pin
00089         */
00090         TCS34725(PinName i2c_sda, PinName i2c_scl);
00091         
00092         /** Boot up the sensor and checks if acking (see header for param defines)
00093         * 
00094         * @param intTime Integration time for reading (will delay accordingly)
00095         * @param i2c_scl Gain value
00096         */
00097         bool init(char intTime, char gain);
00098         
00099         /** Configure after initial boot (will restart sensor)
00100         * 
00101         * @param intTime Integration time for reading (will delay accordingly)
00102         * @param i2c_scl Gain value
00103         * @return 
00104         *   1 if failed
00105         *`  0 if successful
00106         */
00107         void config(char intTime, char gain);
00108         
00109         /** Returns rgbc reading from the sensor.
00110         * 
00111         * @param r Red value (passed by reference)
00112         * @param g Green value (passed by reference)
00113         * @param b Blue value (passed by reference)
00114         * @param c Clear value (all wavelengths - essentially shade) (passed by reference)
00115         */
00116         void getColor(uint16_t &r, uint16_t &g, uint16_t &b, uint16_t &c);
00117         
00118         /** Debug function... probably not useful unless youre debugging your i2c line
00119         * 
00120         * @param deb Serial object for debugging (passed by reference)
00121         */
00122         void DEBUG(Serial *deb);
00123 };
00124 
00125 #endif