Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MF_FUJIKO_BASE STEM_2019 STEM_2020
TCS34725.cpp
00001 #include "TCS34725.h" 00002 00003 TCS34725::TCS34725() : i2c(SDA,SCL) {} 00004 TCS34725::TCS34725(PinName i2c_sda, PinName i2c_scl) : i2c(i2c_sda,i2c_scl) {} 00005 00006 void TCS34725::i2cWrite8(uint8_t addr, char reg, char data){ 00007 char packet[2] = {reg | COMMAND_BIT ,data & 0xFF}; 00008 i2c.write(addr,packet,2,false); 00009 wait(0.01); 00010 } 00011 00012 uint8_t TCS34725::i2cRead8(uint8_t addr, char reg){ 00013 char packet[1] = {reg | COMMAND_BIT}; 00014 char data[1] = {0}; 00015 i2c.write(addr,packet,1, true); 00016 i2c.read(addr,data,1,false); 00017 return (uint8_t)data[0]; 00018 } 00019 00020 uint16_t TCS34725::i2cRead16(uint8_t addr, char reg){ 00021 char packet[1] = {reg | COMMAND_BIT}; 00022 char data[2] = {0,0}; 00023 i2c.write(addr,packet,1, true); 00024 i2c.read(addr,data,2, false); 00025 return ((uint16_t)data[1] << 8) | (uint16_t)data[0]; 00026 } 00027 00028 bool TCS34725::init(char intTime, char gain){ 00029 i2c.frequency(400000); 00030 00031 uint8_t id = i2cRead8(SENSOR_ADDR, TCS34725_ID); 00032 if(id != 0x44)return false; 00033 00034 i2cWrite8(SENSOR_ADDR,TCS34725_ATIME, intTime); 00035 t_intTime = intTime; 00036 i2cWrite8(SENSOR_ADDR,TCS34725_CONTROL, gain); 00037 t_gain = gain; 00038 i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON); 00039 wait(0.003); 00040 i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN); 00041 return true; 00042 } 00043 00044 void TCS34725::config(char intTime, char gain){ 00045 uint8_t reg = i2cRead8(SENSOR_ADDR, TCS34725_ENABLE); 00046 i2cWrite8(SENSOR_ADDR,TCS34725_ENABLE, reg | ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN)); 00047 init(intTime,gain); 00048 } 00049 00050 void TCS34725::getColor(uint16_t &r, uint16_t &g, uint16_t &b, uint16_t &c){ 00051 c = i2cRead16(SENSOR_ADDR, TCS34725_CDATAL); 00052 r = i2cRead16(SENSOR_ADDR, TCS34725_RDATAL); 00053 g = i2cRead16(SENSOR_ADDR, TCS34725_GDATAL); 00054 b = i2cRead16(SENSOR_ADDR, TCS34725_BDATAL); 00055 switch(t_intTime){ 00056 case TCS34725_INTEGRATIONTIME_2_4MS: 00057 wait(0.003); 00058 break; 00059 case TCS34725_INTEGRATIONTIME_24MS: 00060 wait(0.024); 00061 break; 00062 case TCS34725_INTEGRATIONTIME_50MS: 00063 wait(0.05); 00064 break; 00065 case TCS34725_INTEGRATIONTIME_101MS: 00066 wait(0.101); 00067 break; 00068 case TCS34725_INTEGRATIONTIME_154MS: 00069 wait(0.154); 00070 break; 00071 case TCS34725_INTEGRATIONTIME_700MS: 00072 wait(0.7); 00073 break; 00074 } 00075 } 00076 00077 void TCS34725::DEBUG(Serial * deb){ 00078 deb->printf("ATIME:%d ENABLE:%d CONTROL:%d ID:%d\n", 00079 i2cRead8(SENSOR_ADDR, TCS34725_ATIME), 00080 i2cRead8(SENSOR_ADDR, TCS34725_ENABLE), 00081 i2cRead8(SENSOR_ADDR, TCS34725_CONTROL), 00082 i2cRead16(SENSOR_ADDR, TCS34725_ID)); 00083 }
Generated on Fri Jul 15 2022 15:32:01 by
1.7.2