library for the motor driver

Dependents:   4180_final_project

Fork of libTCS34725 by Michael Wilkens

Committer:
mwilkens241
Date:
Wed Jan 25 19:25:28 2017 +0000
Revision:
2:cc2c0831a763
Parent:
1:06c9bbbdb8b0
Child:
3:afb107db7994
i2c library finished & working!!!

Who changed what in which revision?

UserRevisionLine numberNew 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 2:cc2c0831a763 5 uint8_t t_intTime;
mwilkens241 2:cc2c0831a763 6 uint8_t t_gain;
mwilkens241 2:cc2c0831a763 7
mwilkens241 0:4796574af790 8 void i2cWrite8(uint8_t addr, char reg, char data){
mwilkens241 2:cc2c0831a763 9 char packet[2] = {reg | COMMAND_BIT ,data & 0xFF};
mwilkens241 1:06c9bbbdb8b0 10 i2c.write(addr,packet,2,false);
mwilkens241 2:cc2c0831a763 11 wait(0.01);
mwilkens241 0:4796574af790 12 }
mwilkens241 0:4796574af790 13
mwilkens241 0:4796574af790 14 uint8_t i2cRead8(uint8_t addr, char reg){
mwilkens241 2:cc2c0831a763 15 char packet[1] = {reg | COMMAND_BIT};
mwilkens241 1:06c9bbbdb8b0 16 char data[1] = {0};
mwilkens241 1:06c9bbbdb8b0 17 i2c.write(addr,packet,1, true);
mwilkens241 1:06c9bbbdb8b0 18 i2c.read(addr,data,1,false);
mwilkens241 1:06c9bbbdb8b0 19 return (uint8_t)data[0];
mwilkens241 0:4796574af790 20 }
mwilkens241 0:4796574af790 21
mwilkens241 0:4796574af790 22 uint16_t i2cRead16(uint8_t addr, char reg){
mwilkens241 2:cc2c0831a763 23 char packet[1] = {reg | COMMAND_BIT};
mwilkens241 1:06c9bbbdb8b0 24 char data[2] = {0,0};
mwilkens241 1:06c9bbbdb8b0 25 i2c.write(addr,packet,1, true);
mwilkens241 1:06c9bbbdb8b0 26 i2c.read(addr,data,2, false);
mwilkens241 1:06c9bbbdb8b0 27 return ((uint16_t)data[1] << 8) | (uint16_t)data[0];
mwilkens241 0:4796574af790 28 }
mwilkens241 0:4796574af790 29
mwilkens241 1:06c9bbbdb8b0 30 bool TCS34725_init(char intTime, char gain){
mwilkens241 1:06c9bbbdb8b0 31 i2c.frequency(400000);
mwilkens241 1:06c9bbbdb8b0 32
mwilkens241 1:06c9bbbdb8b0 33 uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID);
mwilkens241 1:06c9bbbdb8b0 34 if(id != 0x44)return false;
mwilkens241 0:4796574af790 35
mwilkens241 1:06c9bbbdb8b0 36 i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime);
mwilkens241 2:cc2c0831a763 37 t_intTime = intTime;
mwilkens241 1:06c9bbbdb8b0 38 i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain);
mwilkens241 2:cc2c0831a763 39 t_gain = gain;
mwilkens241 1:06c9bbbdb8b0 40 i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON);
mwilkens241 1:06c9bbbdb8b0 41 wait(0.003);
mwilkens241 1:06c9bbbdb8b0 42 i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
mwilkens241 1:06c9bbbdb8b0 43 return true;
mwilkens241 0:4796574af790 44 }
mwilkens241 0:4796574af790 45
mwilkens241 1:06c9bbbdb8b0 46 void TCS34725_config(char intTime, char gain){
mwilkens241 1:06c9bbbdb8b0 47 uint8_t reg = i2cRead8(SENSOR_ADDR, TCS34725_ENABLE);
mwilkens241 1:06c9bbbdb8b0 48 i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, reg | ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
mwilkens241 1:06c9bbbdb8b0 49 TCS34725_init(intTime,gain);
mwilkens241 1:06c9bbbdb8b0 50 }
mwilkens241 1:06c9bbbdb8b0 51
mwilkens241 1:06c9bbbdb8b0 52 void TCS34725_getColor(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c){
mwilkens241 1:06c9bbbdb8b0 53 *c = i2cRead16(SENSOR_ADDR, TCS34725_CDATAL);
mwilkens241 1:06c9bbbdb8b0 54 *r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL);
mwilkens241 1:06c9bbbdb8b0 55 *g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL);
mwilkens241 1:06c9bbbdb8b0 56 *b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL);
mwilkens241 2:cc2c0831a763 57 switch(t_intTime){
mwilkens241 2:cc2c0831a763 58 case TCS34725_INTEGRATIONTIME_2_4MS:
mwilkens241 2:cc2c0831a763 59 wait(0.003);
mwilkens241 2:cc2c0831a763 60 break;
mwilkens241 2:cc2c0831a763 61 case TCS34725_INTEGRATIONTIME_24MS:
mwilkens241 2:cc2c0831a763 62 wait(0.024);
mwilkens241 2:cc2c0831a763 63 break;
mwilkens241 2:cc2c0831a763 64 case TCS34725_INTEGRATIONTIME_50MS:
mwilkens241 2:cc2c0831a763 65 wait(0.05);
mwilkens241 2:cc2c0831a763 66 break;
mwilkens241 2:cc2c0831a763 67 case TCS34725_INTEGRATIONTIME_101MS:
mwilkens241 2:cc2c0831a763 68 wait(0.101);
mwilkens241 2:cc2c0831a763 69 break;
mwilkens241 2:cc2c0831a763 70 case TCS34725_INTEGRATIONTIME_154MS:
mwilkens241 2:cc2c0831a763 71 wait(0.154);
mwilkens241 2:cc2c0831a763 72 break;
mwilkens241 2:cc2c0831a763 73 case TCS34725_INTEGRATIONTIME_700MS:
mwilkens241 2:cc2c0831a763 74 wait(0.7);
mwilkens241 2:cc2c0831a763 75 break;
mwilkens241 2:cc2c0831a763 76 }
mwilkens241 2:cc2c0831a763 77 }
mwilkens241 2:cc2c0831a763 78
mwilkens241 2:cc2c0831a763 79 void TCS34725_DEBUG(Serial * deb){
mwilkens241 2:cc2c0831a763 80 deb->printf("ATIME:%d ENABLE:%d CONTROL:%d ID:%d\n",
mwilkens241 2:cc2c0831a763 81 i2cRead8(SENSOR_ADDR, TCS34725_ATIME),
mwilkens241 2:cc2c0831a763 82 i2cRead8(SENSOR_ADDR, TCS34725_ENABLE),
mwilkens241 2:cc2c0831a763 83 i2cRead8(SENSOR_ADDR, TCS34725_CONTROL),
mwilkens241 2:cc2c0831a763 84 i2cRead16(SENSOR_ADDR, TCS34725_ID));
mwilkens241 0:4796574af790 85 }