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.
Fork of TCS3472_I2C by
TCS3472_I2C.h
00001 #ifndef TCS3472_I2C_H 00002 #define TCS3472_I2C_H 00003 #include "mbed.h" 00004 00005 //Defines 00006 #define SLAVE_ADDRESS 0x29 00007 00008 #define ENABLE 0x00 00009 #define ATIME 0x01 00010 #define WTIME 0x03 00011 #define AILTL 0x04 00012 #define AIHTL 0x06 00013 #define PERS 0x0C 00014 #define CONFIG 0x0D 00015 #define CONTROL 0x0F 00016 #define ID 0x12 00017 #define STATUS 0x13 00018 #define CDATA 0x14 00019 #define RDATA 0x16 00020 #define GDATA 0x18 00021 #define BDATA 0x1A 00022 00023 /** TCS3472_I2C class. 00024 * Used to read to and write from TCS3472 color sensor. 00025 * 00026 * Example: 00027 * @code 00028 * #include "mbed.h" 00029 * #include "TCS3472_I2C.h" 00030 * 00031 * TCS3472_I2C rgb_sensor( p9, p10 ); 00032 * 00033 * int main() { 00034 * rgb_sensor.enablePowerAndRGBC(); 00035 * rgb_sensor.setIntegrationTime( 100 ); 00036 * 00037 * int rgb_readings[4]; 00038 * while(1) { 00039 * rgb_sensor.getAllColors( rgb_readings ); 00040 * printf( "red: %d, green: %d, blue: %d, clear: %d", rgb_readings[0], rgb_readings[1], rgb_readings[2], rgb_readings[3] ); 00041 * wait_ms( 100 ); 00042 * } 00043 * } 00044 * @endcode 00045 */ 00046 class TCS3472_I2C { 00047 public: 00048 /** Create TCS3472_I2C instance 00049 * 00050 * @param sda sda pin for I2C 00051 * @param scl scl pin for I2C 00052 */ 00053 TCS3472_I2C( PinName sda, PinName scl ); 00054 00055 /** Destructor 00056 */ 00057 ~TCS3472_I2C(); 00058 00059 /** Read red, green, blue and clear values into array 00060 * 00061 * @param readings Array of four integers to store the read data 00062 */ 00063 void getAllColors( int* readings ); 00064 00065 /** Read clear data 00066 * 00067 * @returns 00068 * Clear data reading 00069 */ 00070 int getClearData(); 00071 00072 /** Read red data 00073 * 00074 * @returns 00075 * Red data reading 00076 */ 00077 int getRedData(); 00078 00079 /** Read green data 00080 * 00081 * @returns 00082 * Green data reading 00083 */ 00084 int getGreenData(); 00085 00086 /** Read blue data 00087 * 00088 * @returns 00089 * Blue data reading 00090 */ 00091 int getBlueData(); 00092 00093 /** Power ON. Activates the internal oscillator to permit the timers and ADC channels to operate. 00094 * 00095 * @returns 00096 * 1 if successful 00097 * 0 if otherwise 00098 */ 00099 int enablePower(); 00100 00101 /** Power OFF. Disables the internal oscillator. 00102 * 00103 * @returns 00104 * 1 if successful 00105 * 0 if otherwise 00106 */ 00107 int disablePower(); 00108 00109 /** Checks if power is on (i.e. internal oscillator enabled) 00110 * 00111 * @returns 00112 * 1 if power ON. 00113 * 0 if power OFF. 00114 */ 00115 bool isPowerEnabled(); 00116 00117 /** Activates the two-channel ADC. 00118 * 00119 * @returns 00120 * 1 if successful 00121 * 0 if otherwise 00122 */ 00123 int enableRGBC(); 00124 00125 /** Disables the two-channel ADC. 00126 * 00127 * @returns 00128 * 1 if successful 00129 * 0 if otherwise 00130 */ 00131 int disableRGBC(); 00132 00133 /** Checks if the two-channel ADC is enabled or not. 00134 * 00135 * @returns 00136 * 1 if ADC enabled. 00137 * 0 if ADC not enabled. 00138 */ 00139 bool isRGBCEnabled(); 00140 00141 /** Activates internal oscillator and two-channel ADC simultaneously (both necessary for standard operation). 00142 * 00143 * @returns 00144 * 1 if successful 00145 * 0 if otherwise 00146 */ 00147 int enablePowerAndRGBC(); 00148 00149 /** Disables internal oscillator and two-channel ADC simultaneously. 00150 * 00151 * @returns 00152 * 1 if successful 00153 * 0 if otherwise 00154 */ 00155 int disablePowerAndRGBC(); 00156 00157 /** Activates the wait feature. 00158 * 00159 * @returns 00160 * 1 if successful 00161 * 0 if otherwise 00162 */ 00163 int enableWait(); 00164 00165 /** Disables the wait feature. 00166 * 00167 * @returns 00168 * 1 if successful 00169 * 0 if otherwise 00170 */ 00171 int disableWait(); 00172 00173 /** Checks if wait is enabled 00174 * 00175 * @returns 00176 * 1 if enabled. 00177 * 0 if not enabled. 00178 */ 00179 bool isWaitEnabled(); 00180 00181 /** Permits RGBC interrupts to be generated. 00182 * 00183 * @returns 00184 * 1 if successful 00185 * 0 if otherwise 00186 */ 00187 int enableInterrupt(); 00188 00189 /** Forbids RGBC interrupts to be generated. 00190 * 00191 * @returns 00192 * 1 if successful 00193 * 0 if otherwise 00194 */ 00195 int disableInterrupt(); 00196 00197 /** Checks if RGBC interrupt is enabled. 00198 * 00199 * @returns 00200 * 1 if enabled. 00201 * 0 if not enabled. 00202 */ 00203 bool isInterruptEnabled(); 00204 00205 /** Sets wait time. 00206 * 00207 * @param wtime Wait time to set in milliseconds. Should be in the range 2.4 - 7400ms. 00208 * 00209 * @returns 00210 * 1 if successful 00211 * 0 if otherwise 00212 */ 00213 int setWaitTime( const float wtime ); 00214 00215 /** Reads the current wait time. 00216 * 00217 * @returns 00218 * Wait time in milliseconds 00219 */ 00220 float readWaitTime(); 00221 00222 /** Sets integration time. 00223 * 00224 * @param itime Integration time to set in milliseconds. Should be in the range 2.4 - 614.4ms. 00225 * 00226 * @returns 00227 * 1 if successful 00228 * 0 if otherwise 00229 */ 00230 int setIntegrationTime( const float itime ); 00231 00232 /** Reads the current integration time. 00233 * 00234 * @returns 00235 * Integration time in milliseconds 00236 */ 00237 float readIntegrationTime(); 00238 00239 /** Reads the enable register byte as a char. 00240 * 00241 * @returns 00242 * Enable register byte 00243 */ 00244 char readEnableRegister(); 00245 00246 /** Reads the low trigger point for the comparison function for interrupt generation. 00247 * 00248 * @returns 00249 * Low threshold value 00250 */ 00251 int readLowInterruptThreshold(); 00252 00253 /** Reads the high trigger point for the comparison function for interrupt generation. 00254 * 00255 * @returns 00256 * High threshold value 00257 */ 00258 int readHighInterruptThreshold(); 00259 00260 /** Sets the low trigger point for the comparison function for interrupt generation. 00261 * 00262 * @param threshold Low threshold value 00263 * 00264 * @returns 00265 * 1 if successful 00266 * 0 if otherwise 00267 */ 00268 int setLowInterruptThreshold( const int threshold ); 00269 00270 /** Sets the high trigger point for the comparison function for interrupt generation. 00271 * 00272 * @param threshold High threshold value 00273 * 00274 * @returns 00275 * 1 if successful 00276 * 0 if otherwise 00277 */ 00278 int setHighInterruptThreshold( const int threshold ); 00279 00280 /** Returns the number of consecutive values out of range (set by the low and high thresholds) required to trigger an interrupt 00281 * 00282 * @returns 00283 * interrput persistence 00284 */ 00285 int readInterruptPersistence(); 00286 00287 /** Sets the number of consecutive values out of range (set by the low and high thresholds) required to trigger an interrupt 00288 * 00289 * @param persistence Value to set. Must be 0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 or 60. 00290 * 00291 * @returns 00292 * 1 if successful 00293 * 0 if otherwise 00294 */ 00295 int setInterruptPersistence( const int persistence ); 00296 00297 /** Clears the interrupt bit, allowing normal operation to resume. 00298 * (writes 0b11100110 to the command register to clear interrupt) 00299 * 00300 * @returns 00301 * 1 if successful 00302 * 0 if otherwise 00303 */ 00304 int clearInterrupt(); 00305 00306 /** Read RGBC gain. 00307 * 00308 * @returns 00309 * RGBC gain 00310 */ 00311 int readRGBCGain(); 00312 00313 /** Sets RGBC gain 00314 * 00315 * @param gain Gain to set. Must be 1, 4, 16 or 60. 00316 * 00317 * @returns 00318 * 1 if successful 00319 * 0 if otherwise 00320 */ 00321 int setRGBCGain( const int gain ); 00322 00323 /** Returns Device ID. 00324 * 00325 * @returns 00326 * 0x44 = TCS34721 and TCS34725 00327 * 0x4D = TCS34723 and TCS34727 00328 */ 00329 char getDeviceID(); 00330 00331 /** Reads the status register byte as a char. 00332 * 00333 * @returns 00334 * Status register byte 00335 */ 00336 char readStatusRegister(); 00337 00338 int calculateColorTemperature(int r, int g, int b); 00339 00340 bool verifyConnection(void); 00341 00342 float calculateChromaticityX(int r, int g, int b); 00343 float calculateChromaticityY(int r, int g, int b); 00344 00345 int calculateLux(int r, int g, int b); 00346 00347 private: 00348 I2C i2c; 00349 00350 int writeSingleRegister( char address, char data ); 00351 int writeMultipleRegisters( char address, char* data, int quantity ); 00352 char readSingleRegister( char address ); 00353 int readMultipleRegisters( char address, char* output, int quantity ); 00354 00355 float roundTowardsZero( const float value ); 00356 }; 00357 00358 #endif
Generated on Thu Jul 14 2022 13:26:16 by
1.7.2
