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.cpp@3:afb107db7994, 2017-01-25 (annotated)
- Committer:
- mwilkens241
- Date:
- Wed Jan 25 20:17:57 2017 +0000
- Revision:
- 3:afb107db7994
- Parent:
- 2:cc2c0831a763
- Child:
- 4:41f1acad69e5
class-ified library and added docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mwilkens241 | 0:4796574af790 | 1 | #include "TCS34725.h" |
mwilkens241 | 0:4796574af790 | 2 | |
mwilkens241 | 3:afb107db7994 | 3 | TCS34725::TCS34725() : i2c(SDA,SCL) {} |
mwilkens241 | 3:afb107db7994 | 4 | TCS34725::TCS34725(PinName i2c_sda, PinName i2c_scl) : i2c(i2c_sda,i2c_scl) {} |
mwilkens241 | 1:06c9bbbdb8b0 | 5 | |
mwilkens241 | 3:afb107db7994 | 6 | void TCS34725::i2cWrite8(uint8_t addr, char reg, char data){ |
mwilkens241 | 2:cc2c0831a763 | 7 | char packet[2] = {reg | COMMAND_BIT ,data & 0xFF}; |
mwilkens241 | 1:06c9bbbdb8b0 | 8 | i2c.write(addr,packet,2,false); |
mwilkens241 | 2:cc2c0831a763 | 9 | wait(0.01); |
mwilkens241 | 0:4796574af790 | 10 | } |
mwilkens241 | 0:4796574af790 | 11 | |
mwilkens241 | 3:afb107db7994 | 12 | uint8_t TCS34725::i2cRead8(uint8_t addr, char reg){ |
mwilkens241 | 2:cc2c0831a763 | 13 | char packet[1] = {reg | COMMAND_BIT}; |
mwilkens241 | 1:06c9bbbdb8b0 | 14 | char data[1] = {0}; |
mwilkens241 | 1:06c9bbbdb8b0 | 15 | i2c.write(addr,packet,1, true); |
mwilkens241 | 1:06c9bbbdb8b0 | 16 | i2c.read(addr,data,1,false); |
mwilkens241 | 1:06c9bbbdb8b0 | 17 | return (uint8_t)data[0]; |
mwilkens241 | 0:4796574af790 | 18 | } |
mwilkens241 | 0:4796574af790 | 19 | |
mwilkens241 | 3:afb107db7994 | 20 | uint16_t TCS34725::i2cRead16(uint8_t addr, char reg){ |
mwilkens241 | 2:cc2c0831a763 | 21 | char packet[1] = {reg | COMMAND_BIT}; |
mwilkens241 | 1:06c9bbbdb8b0 | 22 | char data[2] = {0,0}; |
mwilkens241 | 1:06c9bbbdb8b0 | 23 | i2c.write(addr,packet,1, true); |
mwilkens241 | 1:06c9bbbdb8b0 | 24 | i2c.read(addr,data,2, false); |
mwilkens241 | 1:06c9bbbdb8b0 | 25 | return ((uint16_t)data[1] << 8) | (uint16_t)data[0]; |
mwilkens241 | 0:4796574af790 | 26 | } |
mwilkens241 | 0:4796574af790 | 27 | |
mwilkens241 | 3:afb107db7994 | 28 | bool TCS34725::init(char intTime, char gain){ |
mwilkens241 | 1:06c9bbbdb8b0 | 29 | i2c.frequency(400000); |
mwilkens241 | 1:06c9bbbdb8b0 | 30 | |
mwilkens241 | 1:06c9bbbdb8b0 | 31 | uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID); |
mwilkens241 | 1:06c9bbbdb8b0 | 32 | if(id != 0x44)return false; |
mwilkens241 | 0:4796574af790 | 33 | |
mwilkens241 | 1:06c9bbbdb8b0 | 34 | i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime); |
mwilkens241 | 2:cc2c0831a763 | 35 | t_intTime = intTime; |
mwilkens241 | 1:06c9bbbdb8b0 | 36 | i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain); |
mwilkens241 | 2:cc2c0831a763 | 37 | t_gain = gain; |
mwilkens241 | 1:06c9bbbdb8b0 | 38 | i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON); |
mwilkens241 | 1:06c9bbbdb8b0 | 39 | wait(0.003); |
mwilkens241 | 1:06c9bbbdb8b0 | 40 | i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN); |
mwilkens241 | 1:06c9bbbdb8b0 | 41 | return true; |
mwilkens241 | 0:4796574af790 | 42 | } |
mwilkens241 | 0:4796574af790 | 43 | |
mwilkens241 | 3:afb107db7994 | 44 | void TCS34725::config(char intTime, char gain){ |
mwilkens241 | 1:06c9bbbdb8b0 | 45 | uint8_t reg = i2cRead8(SENSOR_ADDR, TCS34725_ENABLE); |
mwilkens241 | 1:06c9bbbdb8b0 | 46 | i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, reg | ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN)); |
mwilkens241 | 3:afb107db7994 | 47 | init(intTime,gain); |
mwilkens241 | 1:06c9bbbdb8b0 | 48 | } |
mwilkens241 | 1:06c9bbbdb8b0 | 49 | |
mwilkens241 | 3:afb107db7994 | 50 | void TCS34725::getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){ |
mwilkens241 | 1:06c9bbbdb8b0 | 51 | *c = i2cRead16(SENSOR_ADDR, TCS34725_CDATAL); |
mwilkens241 | 1:06c9bbbdb8b0 | 52 | *r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL); |
mwilkens241 | 1:06c9bbbdb8b0 | 53 | *g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL); |
mwilkens241 | 1:06c9bbbdb8b0 | 54 | *b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL); |
mwilkens241 | 2:cc2c0831a763 | 55 | switch(t_intTime){ |
mwilkens241 | 2:cc2c0831a763 | 56 | case TCS34725_INTEGRATIONTIME_2_4MS: |
mwilkens241 | 2:cc2c0831a763 | 57 | wait(0.003); |
mwilkens241 | 2:cc2c0831a763 | 58 | break; |
mwilkens241 | 2:cc2c0831a763 | 59 | case TCS34725_INTEGRATIONTIME_24MS: |
mwilkens241 | 2:cc2c0831a763 | 60 | wait(0.024); |
mwilkens241 | 2:cc2c0831a763 | 61 | break; |
mwilkens241 | 2:cc2c0831a763 | 62 | case TCS34725_INTEGRATIONTIME_50MS: |
mwilkens241 | 2:cc2c0831a763 | 63 | wait(0.05); |
mwilkens241 | 2:cc2c0831a763 | 64 | break; |
mwilkens241 | 2:cc2c0831a763 | 65 | case TCS34725_INTEGRATIONTIME_101MS: |
mwilkens241 | 2:cc2c0831a763 | 66 | wait(0.101); |
mwilkens241 | 2:cc2c0831a763 | 67 | break; |
mwilkens241 | 2:cc2c0831a763 | 68 | case TCS34725_INTEGRATIONTIME_154MS: |
mwilkens241 | 2:cc2c0831a763 | 69 | wait(0.154); |
mwilkens241 | 2:cc2c0831a763 | 70 | break; |
mwilkens241 | 2:cc2c0831a763 | 71 | case TCS34725_INTEGRATIONTIME_700MS: |
mwilkens241 | 2:cc2c0831a763 | 72 | wait(0.7); |
mwilkens241 | 2:cc2c0831a763 | 73 | break; |
mwilkens241 | 2:cc2c0831a763 | 74 | } |
mwilkens241 | 2:cc2c0831a763 | 75 | } |
mwilkens241 | 2:cc2c0831a763 | 76 | |
mwilkens241 | 3:afb107db7994 | 77 | void TCS34725::DEBUG(Serial * deb){ |
mwilkens241 | 2:cc2c0831a763 | 78 | deb->printf("ATIME:%d ENABLE:%d CONTROL:%d ID:%d\n", |
mwilkens241 | 2:cc2c0831a763 | 79 | i2cRead8(SENSOR_ADDR, TCS34725_ATIME), |
mwilkens241 | 2:cc2c0831a763 | 80 | i2cRead8(SENSOR_ADDR, TCS34725_ENABLE), |
mwilkens241 | 2:cc2c0831a763 | 81 | i2cRead8(SENSOR_ADDR, TCS34725_CONTROL), |
mwilkens241 | 2:cc2c0831a763 | 82 | i2cRead16(SENSOR_ADDR, TCS34725_ID)); |
mwilkens241 | 0:4796574af790 | 83 | } |