Basic library for the TCS34725, based off of several example programs and the Adafruit Arduino library
Dependents: MF_FUJIKO_BASE STEM_2019 STEM_2020
TCS34725.h@4:41f1acad69e5, 2017-03-22 (annotated)
- Committer:
- mwilkens241
- Date:
- Wed Mar 22 20:54:37 2017 +0000
- Revision:
- 4:41f1acad69e5
- Parent:
- 3:afb107db7994
color sensor testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mwilkens241 | 0:4796574af790 | 1 | // TCS34725 RGB Color I2C Sensor breakout library for F030R8 Nucleo board |
mwilkens241 | 3:afb107db7994 | 2 | #ifndef MBED_TCS34725_H |
mwilkens241 | 3:afb107db7994 | 3 | #define MBED_TCS34725_H |
mwilkens241 | 3:afb107db7994 | 4 | |
mwilkens241 | 1:06c9bbbdb8b0 | 5 | #include "mbed.h" |
mwilkens241 | 0:4796574af790 | 6 | |
mwilkens241 | 0:4796574af790 | 7 | //These pins should be the only piece specific to the F030R8 |
mwilkens241 | 0:4796574af790 | 8 | #define SCL PB_8 |
mwilkens241 | 0:4796574af790 | 9 | #define SDA PB_9 |
mwilkens241 | 0:4796574af790 | 10 | |
mwilkens241 | 0:4796574af790 | 11 | //I2C Address |
mwilkens241 | 0:4796574af790 | 12 | |
mwilkens241 | 2:cc2c0831a763 | 13 | #define SENSOR_ADDR (0x29<<1) |
mwilkens241 | 2:cc2c0831a763 | 14 | #define COMMAND_BIT 0x80 |
mwilkens241 | 0:4796574af790 | 15 | |
mwilkens241 | 0:4796574af790 | 16 | //Important Registers |
mwilkens241 | 0:4796574af790 | 17 | |
mwilkens241 | 0:4796574af790 | 18 | #define TCS34725_ENABLE (0x00) //for turning on the device |
mwilkens241 | 0:4796574af790 | 19 | #define TCS34725_ATIME (0x01) //for integration time |
mwilkens241 | 0:4796574af790 | 20 | #define TCS34725_CONTROL (0x0F) //for setting the gain |
mwilkens241 | 1:06c9bbbdb8b0 | 21 | #define TCS34725_ID (0x12) |
mwilkens241 | 0:4796574af790 | 22 | #define TCS34725_CDATAL (0x14) /* Clear channel data */ |
mwilkens241 | 0:4796574af790 | 23 | #define TCS34725_CDATAH (0x15) |
mwilkens241 | 0:4796574af790 | 24 | #define TCS34725_RDATAL (0x16) /* Red channel data */ |
mwilkens241 | 0:4796574af790 | 25 | #define TCS34725_RDATAH (0x17) |
mwilkens241 | 0:4796574af790 | 26 | #define TCS34725_GDATAL (0x18) /* Green channel data */ |
mwilkens241 | 0:4796574af790 | 27 | #define TCS34725_GDATAH (0x19) |
mwilkens241 | 0:4796574af790 | 28 | #define TCS34725_BDATAL (0x1A) /* Blue channel data */ |
mwilkens241 | 0:4796574af790 | 29 | #define TCS34725_BDATAH (0x1B) |
mwilkens241 | 0:4796574af790 | 30 | |
mwilkens241 | 0:4796574af790 | 31 | //Configuration Bits |
mwilkens241 | 0:4796574af790 | 32 | |
mwilkens241 | 0:4796574af790 | 33 | #define TCS34725_ENABLE_AEN (0x02) /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */ |
mwilkens241 | 0:4796574af790 | 34 | #define TCS34725_ENABLE_PON (0x01) /* Power on - Writing 1 activates the internal oscillator, 0 disables it */ |
mwilkens241 | 0:4796574af790 | 35 | #define TCS34725_INTEGRATIONTIME_2_4MS 0xFF /**< 2.4ms - 1 cycle - Max Count: 1024 */ |
mwilkens241 | 0:4796574af790 | 36 | #define TCS34725_INTEGRATIONTIME_24MS 0xF6 /**< 24ms - 10 cycles - Max Count: 10240 */ |
mwilkens241 | 0:4796574af790 | 37 | #define TCS34725_INTEGRATIONTIME_50MS 0xEB /**< 50ms - 20 cycles - Max Count: 20480 */ |
mwilkens241 | 0:4796574af790 | 38 | #define TCS34725_INTEGRATIONTIME_101MS 0xD5 /**< 101ms - 42 cycles - Max Count: 43008 */ |
mwilkens241 | 0:4796574af790 | 39 | #define TCS34725_INTEGRATIONTIME_154MS 0xC0 /**< 154ms - 64 cycles - Max Count: 65535 */ |
mwilkens241 | 0:4796574af790 | 40 | #define TCS34725_INTEGRATIONTIME_700MS 0x00 /**< 700ms - 256 cycles - Max Count: 65535 */ |
mwilkens241 | 0:4796574af790 | 41 | #define TCS34725_GAIN_1X 0x00 /**< No gain */ |
mwilkens241 | 0:4796574af790 | 42 | #define TCS34725_GAIN_4X 0x01 /**< 4x gain */ |
mwilkens241 | 0:4796574af790 | 43 | #define TCS34725_GAIN_16X 0x02 /**< 16x gain */ |
mwilkens241 | 0:4796574af790 | 44 | #define TCS34725_GAIN_60X 0x03 /**< 60x gain */ |
mwilkens241 | 0:4796574af790 | 45 | |
mwilkens241 | 3:afb107db7994 | 46 | /** TCS34725 control class. |
mwilkens241 | 3:afb107db7994 | 47 | * |
mwilkens241 | 3:afb107db7994 | 48 | * Example: |
mwilkens241 | 3:afb107db7994 | 49 | * @code |
mwilkens241 | 3:afb107db7994 | 50 | * //Send rgb data to the serial port |
mwilkens241 | 3:afb107db7994 | 51 | * #include "TCS34725.h" |
mwilkens241 | 3:afb107db7994 | 52 | * #include "mbed.h" |
mwilkens241 | 3:afb107db7994 | 53 | * |
mwilkens241 | 3:afb107db7994 | 54 | * TCS34725 colorSens(p9, p10); //I2C sda and scl |
mwilkens241 | 3:afb107db7994 | 55 | * Serial pc(USBTX, USBRX); //USB serial |
mwilkens241 | 3:afb107db7994 | 56 | * |
mwilkens241 | 3:afb107db7994 | 57 | * int main() { |
mwilkens241 | 3:afb107db7994 | 58 | * uint16_t r,g,b,c; |
mwilkens241 | 3:afb107db7994 | 59 | * |
mwilkens241 | 3:afb107db7994 | 60 | * if(!colorSens.init(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_60X)){ |
mwilkens241 | 3:afb107db7994 | 61 | * pc.printf("ERROR\n"); //check to see if i2c is responding |
mwilkens241 | 3:afb107db7994 | 62 | * } |
mwilkens241 | 3:afb107db7994 | 63 | * |
mwilkens241 | 3:afb107db7994 | 64 | * while(1) { |
mwilkens241 | 4:41f1acad69e5 | 65 | colorSens.getColor(r,g,b,c); //pass variables by reference... |
mwilkens241 | 3:afb107db7994 | 66 | * pc.printf("DATA: r%d g%d b%d c%d", r, g, b, c); |
mwilkens241 | 3:afb107db7994 | 67 | * wait(0.5); |
mwilkens241 | 3:afb107db7994 | 68 | * } |
mwilkens241 | 3:afb107db7994 | 69 | * } |
mwilkens241 | 3:afb107db7994 | 70 | * @endcode |
mwilkens241 | 3:afb107db7994 | 71 | */ |
mwilkens241 | 0:4796574af790 | 72 | |
mwilkens241 | 3:afb107db7994 | 73 | class TCS34725 { |
mwilkens241 | 3:afb107db7994 | 74 | private: |
mwilkens241 | 3:afb107db7994 | 75 | I2C i2c; |
mwilkens241 | 3:afb107db7994 | 76 | uint8_t t_intTime; |
mwilkens241 | 3:afb107db7994 | 77 | uint8_t t_gain; |
mwilkens241 | 3:afb107db7994 | 78 | void i2cWrite8(uint8_t addr, char reg, char data); |
mwilkens241 | 3:afb107db7994 | 79 | uint8_t i2cRead8(uint8_t addr, char reg); |
mwilkens241 | 3:afb107db7994 | 80 | uint16_t i2cRead16(uint8_t addr, char reg); |
mwilkens241 | 3:afb107db7994 | 81 | public: |
mwilkens241 | 3:afb107db7994 | 82 | /** Initialize object with default i2c pins */ |
mwilkens241 | 3:afb107db7994 | 83 | TCS34725(); |
mwilkens241 | 3:afb107db7994 | 84 | |
mwilkens241 | 3:afb107db7994 | 85 | /** Initialize object with specific i2c pins |
mwilkens241 | 3:afb107db7994 | 86 | * |
mwilkens241 | 3:afb107db7994 | 87 | * @param i2c_sda SDA pin |
mwilkens241 | 3:afb107db7994 | 88 | * @param i2c_scl SCL pin |
mwilkens241 | 3:afb107db7994 | 89 | */ |
mwilkens241 | 3:afb107db7994 | 90 | TCS34725(PinName i2c_sda, PinName i2c_scl); |
mwilkens241 | 3:afb107db7994 | 91 | |
mwilkens241 | 3:afb107db7994 | 92 | /** Boot up the sensor and checks if acking (see header for param defines) |
mwilkens241 | 3:afb107db7994 | 93 | * |
mwilkens241 | 3:afb107db7994 | 94 | * @param intTime Integration time for reading (will delay accordingly) |
mwilkens241 | 3:afb107db7994 | 95 | * @param i2c_scl Gain value |
mwilkens241 | 3:afb107db7994 | 96 | */ |
mwilkens241 | 3:afb107db7994 | 97 | bool init(char intTime, char gain); |
mwilkens241 | 3:afb107db7994 | 98 | |
mwilkens241 | 3:afb107db7994 | 99 | /** Configure after initial boot (will restart sensor) |
mwilkens241 | 3:afb107db7994 | 100 | * |
mwilkens241 | 3:afb107db7994 | 101 | * @param intTime Integration time for reading (will delay accordingly) |
mwilkens241 | 3:afb107db7994 | 102 | * @param i2c_scl Gain value |
mwilkens241 | 3:afb107db7994 | 103 | * @return |
mwilkens241 | 3:afb107db7994 | 104 | * 1 if failed |
mwilkens241 | 3:afb107db7994 | 105 | *` 0 if successful |
mwilkens241 | 3:afb107db7994 | 106 | */ |
mwilkens241 | 3:afb107db7994 | 107 | void config(char intTime, char gain); |
mwilkens241 | 3:afb107db7994 | 108 | |
mwilkens241 | 3:afb107db7994 | 109 | /** Returns rgbc reading from the sensor. |
mwilkens241 | 3:afb107db7994 | 110 | * |
mwilkens241 | 3:afb107db7994 | 111 | * @param r Red value (passed by reference) |
mwilkens241 | 3:afb107db7994 | 112 | * @param g Green value (passed by reference) |
mwilkens241 | 3:afb107db7994 | 113 | * @param b Blue value (passed by reference) |
mwilkens241 | 3:afb107db7994 | 114 | * @param c Clear value (all wavelengths - essentially shade) (passed by reference) |
mwilkens241 | 3:afb107db7994 | 115 | */ |
mwilkens241 | 4:41f1acad69e5 | 116 | void getColor(uint16_t &r, uint16_t &g, uint16_t &b, uint16_t &c); |
mwilkens241 | 3:afb107db7994 | 117 | |
mwilkens241 | 3:afb107db7994 | 118 | /** Debug function... probably not useful unless youre debugging your i2c line |
mwilkens241 | 3:afb107db7994 | 119 | * |
mwilkens241 | 3:afb107db7994 | 120 | * @param deb Serial object for debugging (passed by reference) |
mwilkens241 | 3:afb107db7994 | 121 | */ |
mwilkens241 | 3:afb107db7994 | 122 | void DEBUG(Serial *deb); |
mwilkens241 | 3:afb107db7994 | 123 | }; |
mwilkens241 | 2:cc2c0831a763 | 124 | |
mwilkens241 | 3:afb107db7994 | 125 | #endif |