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@1:06c9bbbdb8b0, 2017-01-24 (annotated)
- Committer:
- mwilkens241
- Date:
- Tue Jan 24 23:25:54 2017 +0000
- Revision:
- 1:06c9bbbdb8b0
- Parent:
- 0:4796574af790
- Child:
- 2:cc2c0831a763
i2c library done but not working
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 | 1:06c9bbbdb8b0 | 3 | I2C i2c(SDA, SCL); |
mwilkens241 | 1:06c9bbbdb8b0 | 4 | |
mwilkens241 | 0:4796574af790 | 5 | void i2cWrite8(uint8_t addr, char reg, char data){ |
mwilkens241 | 1:06c9bbbdb8b0 | 6 | char packet[2] = {reg + 0x80,data}; // unclear why 0x80 needs to be added... |
mwilkens241 | 1:06c9bbbdb8b0 | 7 | i2c.write(addr,packet,2,false); |
mwilkens241 | 0:4796574af790 | 8 | } |
mwilkens241 | 0:4796574af790 | 9 | |
mwilkens241 | 0:4796574af790 | 10 | uint8_t i2cRead8(uint8_t addr, char reg){ |
mwilkens241 | 1:06c9bbbdb8b0 | 11 | char packet[1] = {reg + 0x80}; |
mwilkens241 | 1:06c9bbbdb8b0 | 12 | char data[1] = {0}; |
mwilkens241 | 1:06c9bbbdb8b0 | 13 | i2c.write(addr,packet,1, true); |
mwilkens241 | 1:06c9bbbdb8b0 | 14 | i2c.read(addr,data,1,false); |
mwilkens241 | 1:06c9bbbdb8b0 | 15 | return (uint8_t)data[0]; |
mwilkens241 | 0:4796574af790 | 16 | } |
mwilkens241 | 0:4796574af790 | 17 | |
mwilkens241 | 0:4796574af790 | 18 | uint16_t i2cRead16(uint8_t addr, char reg){ |
mwilkens241 | 1:06c9bbbdb8b0 | 19 | char packet[1] = {reg + 0x80}; |
mwilkens241 | 1:06c9bbbdb8b0 | 20 | char data[2] = {0,0}; |
mwilkens241 | 1:06c9bbbdb8b0 | 21 | i2c.write(addr,packet,1, true); |
mwilkens241 | 1:06c9bbbdb8b0 | 22 | i2c.read(addr,data,2, false); |
mwilkens241 | 1:06c9bbbdb8b0 | 23 | return ((uint16_t)data[1] << 8) | (uint16_t)data[0]; |
mwilkens241 | 0:4796574af790 | 24 | } |
mwilkens241 | 0:4796574af790 | 25 | |
mwilkens241 | 1:06c9bbbdb8b0 | 26 | bool TCS34725_init(char intTime, char gain){ |
mwilkens241 | 1:06c9bbbdb8b0 | 27 | i2c.frequency(400000); |
mwilkens241 | 1:06c9bbbdb8b0 | 28 | |
mwilkens241 | 1:06c9bbbdb8b0 | 29 | uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID); |
mwilkens241 | 1:06c9bbbdb8b0 | 30 | if(id != 0x44)return false; |
mwilkens241 | 0:4796574af790 | 31 | |
mwilkens241 | 1:06c9bbbdb8b0 | 32 | i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime); |
mwilkens241 | 1:06c9bbbdb8b0 | 33 | i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain); |
mwilkens241 | 1:06c9bbbdb8b0 | 34 | i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON); |
mwilkens241 | 1:06c9bbbdb8b0 | 35 | wait(0.003); |
mwilkens241 | 1:06c9bbbdb8b0 | 36 | i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN); |
mwilkens241 | 1:06c9bbbdb8b0 | 37 | return true; |
mwilkens241 | 0:4796574af790 | 38 | } |
mwilkens241 | 0:4796574af790 | 39 | |
mwilkens241 | 1:06c9bbbdb8b0 | 40 | void TCS34725_config(char intTime, char gain){ |
mwilkens241 | 1:06c9bbbdb8b0 | 41 | uint8_t reg = i2cRead8(SENSOR_ADDR, TCS34725_ENABLE); |
mwilkens241 | 1:06c9bbbdb8b0 | 42 | i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, reg | ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN)); |
mwilkens241 | 1:06c9bbbdb8b0 | 43 | TCS34725_init(intTime,gain); |
mwilkens241 | 1:06c9bbbdb8b0 | 44 | } |
mwilkens241 | 1:06c9bbbdb8b0 | 45 | |
mwilkens241 | 1:06c9bbbdb8b0 | 46 | void TCS34725_getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){ |
mwilkens241 | 1:06c9bbbdb8b0 | 47 | *c = i2cRead16(SENSOR_ADDR, TCS34725_CDATAL); |
mwilkens241 | 1:06c9bbbdb8b0 | 48 | *r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL); |
mwilkens241 | 1:06c9bbbdb8b0 | 49 | *g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL); |
mwilkens241 | 1:06c9bbbdb8b0 | 50 | *b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL); |
mwilkens241 | 0:4796574af790 | 51 | } |