Class which provides functions to control a TAOS TCS3472 Color Light-to-Digital Converter with IR Filter via I2C.
Dependents: Chipin_Main Test_Color LAB_10_control FINAL_PROJECT ... more
TCS3472_I2C.cpp@6:6d5bb4ad7d6e, 2014-04-24 (annotated)
- Committer:
- karlmaxwell67
- Date:
- Thu Apr 24 09:14:25 2014 +0000
- Revision:
- 6:6d5bb4ad7d6e
- Parent:
- 5:d4cf0fa1a182
Class documentation added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
karlmaxwell67 | 1:70d7d9f1af01 | 1 | #include "TCS3472_I2C.h" |
karlmaxwell67 | 1:70d7d9f1af01 | 2 | |
karlmaxwell67 | 3:6a89ac4a1979 | 3 | TCS3472_I2C::TCS3472_I2C( PinName sda, PinName scl ) : i2c( sda, scl ){ |
karlmaxwell67 | 5:d4cf0fa1a182 | 4 | i2c.frequency(100000); |
karlmaxwell67 | 3:6a89ac4a1979 | 5 | enablePowerAndRGBC(); |
karlmaxwell67 | 1:70d7d9f1af01 | 6 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 7 | |
karlmaxwell67 | 6:6d5bb4ad7d6e | 8 | TCS3472_I2C::~TCS3472_I2C(){ |
karlmaxwell67 | 6:6d5bb4ad7d6e | 9 | |
karlmaxwell67 | 6:6d5bb4ad7d6e | 10 | } |
karlmaxwell67 | 6:6d5bb4ad7d6e | 11 | |
karlmaxwell67 | 1:70d7d9f1af01 | 12 | int TCS3472_I2C::writeSingleRegister( char address, char data ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 13 | char tx[2] = { address | 160, data }; //0d160 = 0b10100000 |
karlmaxwell67 | 3:6a89ac4a1979 | 14 | int ack = i2c.write( SLAVE_ADDRESS << 1, tx, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 15 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 16 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 17 | |
karlmaxwell67 | 3:6a89ac4a1979 | 18 | int TCS3472_I2C::writeMultipleRegisters( char address, char* data, int quantity ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 19 | char tx[ quantity + 1 ]; |
karlmaxwell67 | 3:6a89ac4a1979 | 20 | tx[0] = address | 160; |
karlmaxwell67 | 3:6a89ac4a1979 | 21 | for ( int i = 1; i <= quantity; i++ ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 22 | tx[ i ] = data[ i - 1 ]; |
karlmaxwell67 | 3:6a89ac4a1979 | 23 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 24 | int ack = i2c.write( SLAVE_ADDRESS << 1, tx, quantity + 1 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 25 | return ack; |
karlmaxwell67 | 1:70d7d9f1af01 | 26 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 27 | |
karlmaxwell67 | 1:70d7d9f1af01 | 28 | char TCS3472_I2C::readSingleRegister( char address ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 29 | char output = 255; |
karlmaxwell67 | 1:70d7d9f1af01 | 30 | char command = address | 160; //0d160 = 0b10100000 |
karlmaxwell67 | 3:6a89ac4a1979 | 31 | i2c.write( SLAVE_ADDRESS << 1, &command, 1, true ); |
karlmaxwell67 | 3:6a89ac4a1979 | 32 | i2c.read( SLAVE_ADDRESS << 1, &output, 1 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 33 | return output; |
karlmaxwell67 | 1:70d7d9f1af01 | 34 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 35 | |
karlmaxwell67 | 1:70d7d9f1af01 | 36 | int TCS3472_I2C::readMultipleRegisters( char address, char* output, int quantity ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 37 | char command = address | 160; //0d160 = 0b10100000 |
karlmaxwell67 | 3:6a89ac4a1979 | 38 | i2c.write( SLAVE_ADDRESS << 1, &command, 1, true ); |
karlmaxwell67 | 3:6a89ac4a1979 | 39 | int ack = i2c.read( SLAVE_ADDRESS << 1, output, quantity ); |
karlmaxwell67 | 1:70d7d9f1af01 | 40 | return ack; |
karlmaxwell67 | 1:70d7d9f1af01 | 41 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 42 | |
karlmaxwell67 | 6:6d5bb4ad7d6e | 43 | void TCS3472_I2C::getAllColors( int* readings ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 44 | char buffer[8] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 45 | |
karlmaxwell67 | 1:70d7d9f1af01 | 46 | readMultipleRegisters( CDATA, buffer, 8 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 47 | |
karlmaxwell67 | 1:70d7d9f1af01 | 48 | readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 49 | readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; |
karlmaxwell67 | 1:70d7d9f1af01 | 50 | readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; |
karlmaxwell67 | 1:70d7d9f1af01 | 51 | readings[3] = (int)buffer[7] << 8 | (int)buffer[6]; |
karlmaxwell67 | 1:70d7d9f1af01 | 52 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 53 | |
karlmaxwell67 | 1:70d7d9f1af01 | 54 | int TCS3472_I2C::getClearData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 55 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 56 | readMultipleRegisters( CDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 57 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 58 | return reading; |
karlmaxwell67 | 1:70d7d9f1af01 | 59 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 60 | |
karlmaxwell67 | 1:70d7d9f1af01 | 61 | int TCS3472_I2C::getRedData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 62 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 63 | readMultipleRegisters( RDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 64 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 65 | return reading; |
karlmaxwell67 | 1:70d7d9f1af01 | 66 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 67 | |
karlmaxwell67 | 1:70d7d9f1af01 | 68 | int TCS3472_I2C::getGreenData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 69 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 70 | readMultipleRegisters( GDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 71 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 72 | return reading; |
karlmaxwell67 | 1:70d7d9f1af01 | 73 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 74 | |
karlmaxwell67 | 1:70d7d9f1af01 | 75 | int TCS3472_I2C::getBlueData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 76 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 77 | readMultipleRegisters( BDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 78 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 79 | return reading; |
karlmaxwell67 | 2:38d5187a4e7b | 80 | } |
karlmaxwell67 | 2:38d5187a4e7b | 81 | |
karlmaxwell67 | 3:6a89ac4a1979 | 82 | int TCS3472_I2C::enablePower(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 83 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 84 | char enable_new = enable_old | 1; // sets PON (bit 0) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 85 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 86 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 87 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 88 | |
karlmaxwell67 | 3:6a89ac4a1979 | 89 | int TCS3472_I2C::disablePower(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 90 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 91 | char enable_new = enable_old & 254; // sets PON (bit 0) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 92 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 93 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 94 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 95 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 96 | bool TCS3472_I2C::isPowerEnabled(){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 97 | char enable = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 98 | char pon = enable << 7; |
karlmaxwell67 | 4:5d1f8d7d81ff | 99 | pon = pon >> 7; // gets PON (bit 0) from ENABLE register byte |
karlmaxwell67 | 4:5d1f8d7d81ff | 100 | return (bool)pon; |
karlmaxwell67 | 4:5d1f8d7d81ff | 101 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 102 | |
karlmaxwell67 | 3:6a89ac4a1979 | 103 | int TCS3472_I2C::enableRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 104 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 105 | char enable_new = enable_old | 2; // sets AEN (bit 1) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 106 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 107 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 108 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 109 | |
karlmaxwell67 | 3:6a89ac4a1979 | 110 | int TCS3472_I2C::disableRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 111 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 112 | char enable_new = enable_old & 253; // sets AEN (bit 1) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 113 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 114 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 115 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 116 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 117 | bool TCS3472_I2C::isRGBCEnabled(){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 118 | char enable = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 119 | char aen = enable << 6; |
karlmaxwell67 | 4:5d1f8d7d81ff | 120 | aen = aen >> 7; // gets AEN (bit 1) from ENABLE register byte |
karlmaxwell67 | 4:5d1f8d7d81ff | 121 | return (bool)aen; |
karlmaxwell67 | 4:5d1f8d7d81ff | 122 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 123 | |
karlmaxwell67 | 3:6a89ac4a1979 | 124 | int TCS3472_I2C::enablePowerAndRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 125 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 126 | char enable_new = enable_old | 3; // sets PON (bit 0) and AEN (bit 1) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 127 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 128 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 129 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 130 | |
karlmaxwell67 | 3:6a89ac4a1979 | 131 | int TCS3472_I2C::disablePowerAndRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 132 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 133 | char enable_new = enable_old & 252; // sets PON (bit 0) and AEN (bit 1) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 134 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 135 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 136 | } |
karlmaxwell67 | 2:38d5187a4e7b | 137 | |
karlmaxwell67 | 2:38d5187a4e7b | 138 | int TCS3472_I2C::enableWait(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 139 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 140 | char enable_new = enable_old | 8; // sets WEN (bit 3) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 141 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 142 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 143 | } |
karlmaxwell67 | 2:38d5187a4e7b | 144 | |
karlmaxwell67 | 2:38d5187a4e7b | 145 | int TCS3472_I2C::disableWait(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 146 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 147 | char enable_new = enable_old & 247; // sets WEN (bit 3) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 148 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 149 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 150 | } |
karlmaxwell67 | 2:38d5187a4e7b | 151 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 152 | bool TCS3472_I2C::isWaitEnabled(){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 153 | char enable = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 154 | char wen = enable << 4; |
karlmaxwell67 | 4:5d1f8d7d81ff | 155 | wen = wen >> 7; // gets WEN (bit 3) from ENABLE register byte |
karlmaxwell67 | 4:5d1f8d7d81ff | 156 | return (bool)wen; |
karlmaxwell67 | 4:5d1f8d7d81ff | 157 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 158 | |
karlmaxwell67 | 2:38d5187a4e7b | 159 | int TCS3472_I2C::enableInterrupt(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 160 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 161 | char enable_new = enable_old | 16; // sets AIEN (bit 4) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 162 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 163 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 164 | } |
karlmaxwell67 | 2:38d5187a4e7b | 165 | |
karlmaxwell67 | 2:38d5187a4e7b | 166 | int TCS3472_I2C::disableInterrupt(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 167 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 168 | char enable_new = enable_old & 239; // sets AIEN (bit 4) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 169 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 170 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 171 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 172 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 173 | bool TCS3472_I2C::isInterruptEnabled(){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 174 | char enable = readSingleRegister( ENABLE ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 175 | char aien = enable << 3; |
karlmaxwell67 | 4:5d1f8d7d81ff | 176 | aien = aien >> 7; // gets AIEN (bit 4) from ENABLE register byte |
karlmaxwell67 | 4:5d1f8d7d81ff | 177 | return (bool)aien; |
karlmaxwell67 | 4:5d1f8d7d81ff | 178 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 179 | |
karlmaxwell67 | 3:6a89ac4a1979 | 180 | int TCS3472_I2C::setIntegrationTime( const float itime ){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 181 | char atime = 256 - roundTowardsZero( itime / 2.4 ); // rounding ensures nearest value of atime is used |
karlmaxwell67 | 3:6a89ac4a1979 | 182 | int ack = writeSingleRegister( ATIME, atime ); |
karlmaxwell67 | 2:38d5187a4e7b | 183 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 184 | } |
karlmaxwell67 | 2:38d5187a4e7b | 185 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 186 | float TCS3472_I2C::readIntegrationTime(){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 187 | float itime = 0; |
karlmaxwell67 | 4:5d1f8d7d81ff | 188 | char atime = readSingleRegister( ATIME ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 189 | itime = 2.4 * ( 256 - atime ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 190 | return itime; |
karlmaxwell67 | 4:5d1f8d7d81ff | 191 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 192 | |
karlmaxwell67 | 2:38d5187a4e7b | 193 | int TCS3472_I2C::setWaitTime( const float time ){ |
karlmaxwell67 | 2:38d5187a4e7b | 194 | int ack = 1; |
karlmaxwell67 | 2:38d5187a4e7b | 195 | char wtime = 0; |
karlmaxwell67 | 4:5d1f8d7d81ff | 196 | if ( time >= 2.39 && time <= 614.4 ){ // 2.39 instead of 2.4 to allow for float accuracy errors |
karlmaxwell67 | 3:6a89ac4a1979 | 197 | ack = writeSingleRegister( CONFIG, 0 ); // sets WLONG to 0 |
karlmaxwell67 | 4:5d1f8d7d81ff | 198 | wtime = 256 - roundTowardsZero( time / 2.4 ); |
karlmaxwell67 | 2:38d5187a4e7b | 199 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 200 | else if ( time > 614.4 && time <= 7400.1 ){ // 7400.1 instead of 7400 to allow for float accuracy errors |
karlmaxwell67 | 3:6a89ac4a1979 | 201 | ack = writeSingleRegister( CONFIG, 2 ); // sets WLONG to 1 |
karlmaxwell67 | 4:5d1f8d7d81ff | 202 | wtime = 256 - roundTowardsZero( time / 28.8 ); |
karlmaxwell67 | 2:38d5187a4e7b | 203 | } |
karlmaxwell67 | 2:38d5187a4e7b | 204 | ack = ack || writeSingleRegister( WTIME, wtime ); |
karlmaxwell67 | 2:38d5187a4e7b | 205 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 206 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 207 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 208 | float TCS3472_I2C::readWaitTime(){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 209 | float time = 0; |
karlmaxwell67 | 4:5d1f8d7d81ff | 210 | char wtime = readSingleRegister( WTIME ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 211 | char config = readSingleRegister( CONFIG ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 212 | int wlong = ( config << 6 ) >> 7; // gets WLONG (bit 1) from CONFIG register byte |
karlmaxwell67 | 4:5d1f8d7d81ff | 213 | if ( wlong == 0 ){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 214 | time = 2.4 * ( 256 - wtime ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 215 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 216 | else if ( wlong == 1 ){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 217 | time = 28.8 * ( 256 - wtime ); // 28.8 = 2.4 * 12 |
karlmaxwell67 | 4:5d1f8d7d81ff | 218 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 219 | return time; |
karlmaxwell67 | 4:5d1f8d7d81ff | 220 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 221 | |
karlmaxwell67 | 3:6a89ac4a1979 | 222 | char TCS3472_I2C::readEnableRegister(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 223 | return readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 224 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 225 | |
karlmaxwell67 | 3:6a89ac4a1979 | 226 | int TCS3472_I2C::readLowInterruptThreshold(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 227 | char buffer[2] = { 0 }; |
karlmaxwell67 | 3:6a89ac4a1979 | 228 | readMultipleRegisters( AILTL, buffer, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 229 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 3:6a89ac4a1979 | 230 | return reading; |
karlmaxwell67 | 3:6a89ac4a1979 | 231 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 232 | |
karlmaxwell67 | 3:6a89ac4a1979 | 233 | int TCS3472_I2C::readHighInterruptThreshold(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 234 | char buffer[2] = { 0 }; |
karlmaxwell67 | 3:6a89ac4a1979 | 235 | readMultipleRegisters( AIHTL, buffer, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 236 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 3:6a89ac4a1979 | 237 | return reading; |
karlmaxwell67 | 3:6a89ac4a1979 | 238 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 239 | |
karlmaxwell67 | 3:6a89ac4a1979 | 240 | int TCS3472_I2C::setLowInterruptThreshold( const int threshold ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 241 | char threshold_bytes[2]; |
karlmaxwell67 | 3:6a89ac4a1979 | 242 | threshold_bytes[0] = threshold; // take lowest 8 bits of threshold |
karlmaxwell67 | 3:6a89ac4a1979 | 243 | threshold_bytes[1] = threshold >> 8; // take highest 8 bits of threshold |
karlmaxwell67 | 3:6a89ac4a1979 | 244 | int ack = writeMultipleRegisters( AILTL, threshold_bytes, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 245 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 246 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 247 | |
karlmaxwell67 | 3:6a89ac4a1979 | 248 | int TCS3472_I2C::setHighInterruptThreshold( const int threshold ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 249 | char threshold_bytes[2]; |
karlmaxwell67 | 3:6a89ac4a1979 | 250 | threshold_bytes[0] = threshold; |
karlmaxwell67 | 3:6a89ac4a1979 | 251 | threshold_bytes[1] = threshold >> 8; |
karlmaxwell67 | 3:6a89ac4a1979 | 252 | int ack = writeMultipleRegisters( AIHTL, threshold_bytes, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 253 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 254 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 255 | |
karlmaxwell67 | 3:6a89ac4a1979 | 256 | int TCS3472_I2C::readInterruptPersistence(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 257 | char pers = readSingleRegister( PERS ); |
karlmaxwell67 | 3:6a89ac4a1979 | 258 | char persistence_bits = ( pers << 4 ) >> 4; // discard bits 4 to 7, keep only bits 0 to 3 |
karlmaxwell67 | 3:6a89ac4a1979 | 259 | int persistence = -1; |
karlmaxwell67 | 3:6a89ac4a1979 | 260 | switch (persistence_bits){ |
karlmaxwell67 | 3:6a89ac4a1979 | 261 | case 0: |
karlmaxwell67 | 3:6a89ac4a1979 | 262 | persistence = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 263 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 264 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 265 | persistence = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 266 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 267 | case 2: |
karlmaxwell67 | 3:6a89ac4a1979 | 268 | persistence = 2; |
karlmaxwell67 | 3:6a89ac4a1979 | 269 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 270 | case 3: |
karlmaxwell67 | 3:6a89ac4a1979 | 271 | persistence = 3; |
karlmaxwell67 | 3:6a89ac4a1979 | 272 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 273 | case 4: |
karlmaxwell67 | 3:6a89ac4a1979 | 274 | persistence = 5; |
karlmaxwell67 | 3:6a89ac4a1979 | 275 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 276 | case 5: |
karlmaxwell67 | 3:6a89ac4a1979 | 277 | persistence = 10; |
karlmaxwell67 | 3:6a89ac4a1979 | 278 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 279 | case 6: |
karlmaxwell67 | 3:6a89ac4a1979 | 280 | persistence = 15; |
karlmaxwell67 | 3:6a89ac4a1979 | 281 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 282 | case 7: |
karlmaxwell67 | 3:6a89ac4a1979 | 283 | persistence = 20; |
karlmaxwell67 | 3:6a89ac4a1979 | 284 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 285 | case 8: |
karlmaxwell67 | 3:6a89ac4a1979 | 286 | persistence = 25; |
karlmaxwell67 | 3:6a89ac4a1979 | 287 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 288 | case 9: |
karlmaxwell67 | 3:6a89ac4a1979 | 289 | persistence = 30; |
karlmaxwell67 | 3:6a89ac4a1979 | 290 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 291 | case 10: |
karlmaxwell67 | 3:6a89ac4a1979 | 292 | persistence = 35; |
karlmaxwell67 | 3:6a89ac4a1979 | 293 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 294 | case 11: |
karlmaxwell67 | 3:6a89ac4a1979 | 295 | persistence = 40; |
karlmaxwell67 | 3:6a89ac4a1979 | 296 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 297 | case 12: |
karlmaxwell67 | 3:6a89ac4a1979 | 298 | persistence = 45; |
karlmaxwell67 | 3:6a89ac4a1979 | 299 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 300 | case 13: |
karlmaxwell67 | 3:6a89ac4a1979 | 301 | persistence = 50; |
karlmaxwell67 | 3:6a89ac4a1979 | 302 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 303 | case 14: |
karlmaxwell67 | 3:6a89ac4a1979 | 304 | persistence = 55; |
karlmaxwell67 | 3:6a89ac4a1979 | 305 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 306 | case 15: |
karlmaxwell67 | 3:6a89ac4a1979 | 307 | persistence = 60; |
karlmaxwell67 | 3:6a89ac4a1979 | 308 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 309 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 310 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 311 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 312 | return persistence; |
karlmaxwell67 | 3:6a89ac4a1979 | 313 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 314 | |
karlmaxwell67 | 3:6a89ac4a1979 | 315 | int TCS3472_I2C::setInterruptPersistence( const int persistence ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 316 | char pers_byte; |
karlmaxwell67 | 3:6a89ac4a1979 | 317 | int ack = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 318 | switch (persistence){ |
karlmaxwell67 | 3:6a89ac4a1979 | 319 | case 0: |
karlmaxwell67 | 3:6a89ac4a1979 | 320 | pers_byte = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 321 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 322 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 323 | pers_byte = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 324 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 325 | case 2: |
karlmaxwell67 | 3:6a89ac4a1979 | 326 | pers_byte = 2; |
karlmaxwell67 | 3:6a89ac4a1979 | 327 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 328 | case 3: |
karlmaxwell67 | 3:6a89ac4a1979 | 329 | pers_byte = 3; |
karlmaxwell67 | 3:6a89ac4a1979 | 330 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 331 | case 5: |
karlmaxwell67 | 3:6a89ac4a1979 | 332 | pers_byte = 4; |
karlmaxwell67 | 3:6a89ac4a1979 | 333 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 334 | case 10: |
karlmaxwell67 | 3:6a89ac4a1979 | 335 | pers_byte = 5; |
karlmaxwell67 | 3:6a89ac4a1979 | 336 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 337 | case 15: |
karlmaxwell67 | 3:6a89ac4a1979 | 338 | pers_byte = 6; |
karlmaxwell67 | 3:6a89ac4a1979 | 339 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 340 | case 20: |
karlmaxwell67 | 3:6a89ac4a1979 | 341 | pers_byte = 7; |
karlmaxwell67 | 3:6a89ac4a1979 | 342 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 343 | case 25: |
karlmaxwell67 | 3:6a89ac4a1979 | 344 | pers_byte = 8; |
karlmaxwell67 | 3:6a89ac4a1979 | 345 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 346 | case 30: |
karlmaxwell67 | 3:6a89ac4a1979 | 347 | pers_byte = 9; |
karlmaxwell67 | 3:6a89ac4a1979 | 348 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 349 | case 35: |
karlmaxwell67 | 3:6a89ac4a1979 | 350 | pers_byte = 10; |
karlmaxwell67 | 3:6a89ac4a1979 | 351 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 352 | case 40: |
karlmaxwell67 | 3:6a89ac4a1979 | 353 | pers_byte = 11; |
karlmaxwell67 | 3:6a89ac4a1979 | 354 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 355 | case 45: |
karlmaxwell67 | 3:6a89ac4a1979 | 356 | pers_byte = 12; |
karlmaxwell67 | 3:6a89ac4a1979 | 357 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 358 | case 50: |
karlmaxwell67 | 3:6a89ac4a1979 | 359 | pers_byte = 13; |
karlmaxwell67 | 3:6a89ac4a1979 | 360 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 361 | case 55: |
karlmaxwell67 | 3:6a89ac4a1979 | 362 | pers_byte = 14; |
karlmaxwell67 | 3:6a89ac4a1979 | 363 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 364 | case 60: |
karlmaxwell67 | 3:6a89ac4a1979 | 365 | pers_byte = 15; |
karlmaxwell67 | 3:6a89ac4a1979 | 366 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 367 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 368 | ack = 2; // 2 used to indicate invalid entry |
karlmaxwell67 | 3:6a89ac4a1979 | 369 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 370 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 371 | if ( ack != 2 ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 372 | ack = writeSingleRegister( PERS, pers_byte ); |
karlmaxwell67 | 3:6a89ac4a1979 | 373 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 374 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 375 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 376 | |
karlmaxwell67 | 3:6a89ac4a1979 | 377 | int TCS3472_I2C::clearInterrupt(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 378 | char tx = 230; |
karlmaxwell67 | 3:6a89ac4a1979 | 379 | int ack = i2c.write( SLAVE_ADDRESS << 1, &tx, 1 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 380 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 381 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 382 | |
karlmaxwell67 | 3:6a89ac4a1979 | 383 | int TCS3472_I2C::readRGBCGain(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 384 | char control = readSingleRegister( CONTROL ); |
karlmaxwell67 | 3:6a89ac4a1979 | 385 | char gain_bits = ( control << 6 ) >> 6; // discard bits 2 to 7, keep only bits 0 & 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 386 | int gain; |
karlmaxwell67 | 3:6a89ac4a1979 | 387 | switch (gain_bits) { |
karlmaxwell67 | 3:6a89ac4a1979 | 388 | case 0: |
karlmaxwell67 | 3:6a89ac4a1979 | 389 | gain = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 390 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 391 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 392 | gain = 4; |
karlmaxwell67 | 3:6a89ac4a1979 | 393 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 394 | case 2: |
karlmaxwell67 | 3:6a89ac4a1979 | 395 | gain = 16; |
karlmaxwell67 | 3:6a89ac4a1979 | 396 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 397 | case 3: |
karlmaxwell67 | 3:6a89ac4a1979 | 398 | gain = 60; |
karlmaxwell67 | 3:6a89ac4a1979 | 399 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 400 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 401 | gain = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 402 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 403 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 404 | return gain; |
karlmaxwell67 | 3:6a89ac4a1979 | 405 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 406 | |
karlmaxwell67 | 3:6a89ac4a1979 | 407 | int TCS3472_I2C::setRGBCGain( const int gain ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 408 | char control; |
karlmaxwell67 | 3:6a89ac4a1979 | 409 | int ack = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 410 | switch (gain){ |
karlmaxwell67 | 3:6a89ac4a1979 | 411 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 412 | control = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 413 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 414 | case 4: |
karlmaxwell67 | 3:6a89ac4a1979 | 415 | control = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 416 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 417 | case 16: |
karlmaxwell67 | 3:6a89ac4a1979 | 418 | control = 2; |
karlmaxwell67 | 3:6a89ac4a1979 | 419 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 420 | case 60: |
karlmaxwell67 | 3:6a89ac4a1979 | 421 | control = 3; |
karlmaxwell67 | 3:6a89ac4a1979 | 422 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 423 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 424 | ack = 2; // 2 used to indicate invalid entry |
karlmaxwell67 | 3:6a89ac4a1979 | 425 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 426 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 427 | if ( ack != 2 ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 428 | ack = writeSingleRegister( CONTROL, control ); |
karlmaxwell67 | 3:6a89ac4a1979 | 429 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 430 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 431 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 432 | |
karlmaxwell67 | 3:6a89ac4a1979 | 433 | char TCS3472_I2C::getDeviceID(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 434 | return readSingleRegister( ID ); |
karlmaxwell67 | 3:6a89ac4a1979 | 435 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 436 | |
karlmaxwell67 | 3:6a89ac4a1979 | 437 | char TCS3472_I2C::readStatusRegister(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 438 | return readSingleRegister( STATUS ); |
karlmaxwell67 | 4:5d1f8d7d81ff | 439 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 440 | |
karlmaxwell67 | 4:5d1f8d7d81ff | 441 | float TCS3472_I2C::roundTowardsZero( const float value ){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 442 | float result = 0; |
karlmaxwell67 | 4:5d1f8d7d81ff | 443 | if ( ( value >= 0 && ( value - (int)value ) < 0.5 ) || ( value < 0 && ( abs(value) - (int)abs(value) ) >= 0.5 ) ){ |
karlmaxwell67 | 4:5d1f8d7d81ff | 444 | result = floor(value); |
karlmaxwell67 | 4:5d1f8d7d81ff | 445 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 446 | else{ |
karlmaxwell67 | 4:5d1f8d7d81ff | 447 | result = ceil(value); |
karlmaxwell67 | 4:5d1f8d7d81ff | 448 | } |
karlmaxwell67 | 4:5d1f8d7d81ff | 449 | return result; |
karlmaxwell67 | 0:453a43c8bf2b | 450 | } |