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@3:6a89ac4a1979, 2014-03-20 (annotated)
- Committer:
- karlmaxwell67
- Date:
- Thu Mar 20 16:56:56 2014 +0000
- Revision:
- 3:6a89ac4a1979
- Parent:
- 2:38d5187a4e7b
- Child:
- 4:5d1f8d7d81ff
Added more functions to increase the functionality of the class.
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 | 3:6a89ac4a1979 | 4 | i2c.frequency(400000); |
karlmaxwell67 | 3:6a89ac4a1979 | 5 | enablePowerAndRGBC(); |
karlmaxwell67 | 1:70d7d9f1af01 | 6 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 7 | |
karlmaxwell67 | 1:70d7d9f1af01 | 8 | int TCS3472_I2C::writeSingleRegister( char address, char data ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 9 | char tx[2] = { address | 160, data }; //0d160 = 0b10100000 |
karlmaxwell67 | 3:6a89ac4a1979 | 10 | int ack = i2c.write( SLAVE_ADDRESS << 1, tx, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 11 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 12 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 13 | |
karlmaxwell67 | 3:6a89ac4a1979 | 14 | int TCS3472_I2C::writeMultipleRegisters( char address, char* data, int quantity ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 15 | char tx[ quantity + 1 ]; |
karlmaxwell67 | 3:6a89ac4a1979 | 16 | tx[0] = address | 160; |
karlmaxwell67 | 3:6a89ac4a1979 | 17 | for ( int i = 1; i <= quantity; i++ ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 18 | tx[ i ] = data[ i - 1 ]; |
karlmaxwell67 | 3:6a89ac4a1979 | 19 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 20 | int ack = i2c.write( SLAVE_ADDRESS << 1, tx, quantity + 1 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 21 | return ack; |
karlmaxwell67 | 1:70d7d9f1af01 | 22 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 23 | |
karlmaxwell67 | 1:70d7d9f1af01 | 24 | char TCS3472_I2C::readSingleRegister( char address ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 25 | char output = 255; |
karlmaxwell67 | 1:70d7d9f1af01 | 26 | char command = address | 160; //0d160 = 0b10100000 |
karlmaxwell67 | 3:6a89ac4a1979 | 27 | i2c.write( SLAVE_ADDRESS << 1, &command, 1, true ); |
karlmaxwell67 | 3:6a89ac4a1979 | 28 | i2c.read( SLAVE_ADDRESS << 1, &output, 1 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 29 | return output; |
karlmaxwell67 | 1:70d7d9f1af01 | 30 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 31 | |
karlmaxwell67 | 1:70d7d9f1af01 | 32 | int TCS3472_I2C::readMultipleRegisters( char address, char* output, int quantity ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 33 | char command = address | 160; //0d160 = 0b10100000 |
karlmaxwell67 | 3:6a89ac4a1979 | 34 | i2c.write( SLAVE_ADDRESS << 1, &command, 1, true ); |
karlmaxwell67 | 3:6a89ac4a1979 | 35 | int ack = i2c.read( SLAVE_ADDRESS << 1, output, quantity ); |
karlmaxwell67 | 1:70d7d9f1af01 | 36 | return ack; |
karlmaxwell67 | 1:70d7d9f1af01 | 37 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 38 | |
karlmaxwell67 | 1:70d7d9f1af01 | 39 | int TCS3472_I2C::getAllColours( int* readings ){ |
karlmaxwell67 | 1:70d7d9f1af01 | 40 | char buffer[8] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 41 | |
karlmaxwell67 | 1:70d7d9f1af01 | 42 | readMultipleRegisters( CDATA, buffer, 8 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 43 | |
karlmaxwell67 | 1:70d7d9f1af01 | 44 | readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 45 | readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; |
karlmaxwell67 | 1:70d7d9f1af01 | 46 | readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; |
karlmaxwell67 | 1:70d7d9f1af01 | 47 | readings[3] = (int)buffer[7] << 8 | (int)buffer[6]; |
karlmaxwell67 | 0:453a43c8bf2b | 48 | |
karlmaxwell67 | 1:70d7d9f1af01 | 49 | return 0; |
karlmaxwell67 | 1:70d7d9f1af01 | 50 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 51 | |
karlmaxwell67 | 1:70d7d9f1af01 | 52 | int TCS3472_I2C::getClearData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 53 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 54 | readMultipleRegisters( CDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 55 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 56 | return reading; |
karlmaxwell67 | 1:70d7d9f1af01 | 57 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 58 | |
karlmaxwell67 | 1:70d7d9f1af01 | 59 | int TCS3472_I2C::getRedData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 60 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 61 | readMultipleRegisters( RDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 62 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 63 | return reading; |
karlmaxwell67 | 1:70d7d9f1af01 | 64 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 65 | |
karlmaxwell67 | 1:70d7d9f1af01 | 66 | int TCS3472_I2C::getGreenData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 67 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 68 | readMultipleRegisters( GDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 69 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 70 | return reading; |
karlmaxwell67 | 1:70d7d9f1af01 | 71 | } |
karlmaxwell67 | 1:70d7d9f1af01 | 72 | |
karlmaxwell67 | 1:70d7d9f1af01 | 73 | int TCS3472_I2C::getBlueData(){ |
karlmaxwell67 | 1:70d7d9f1af01 | 74 | char buffer[2] = { 0 }; |
karlmaxwell67 | 1:70d7d9f1af01 | 75 | readMultipleRegisters( BDATA, buffer, 2 ); |
karlmaxwell67 | 1:70d7d9f1af01 | 76 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 1:70d7d9f1af01 | 77 | return reading; |
karlmaxwell67 | 2:38d5187a4e7b | 78 | } |
karlmaxwell67 | 2:38d5187a4e7b | 79 | |
karlmaxwell67 | 3:6a89ac4a1979 | 80 | int TCS3472_I2C::enablePower(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 81 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 82 | char enable_new = enable_old | 1; // sets PON (bit 0) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 83 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 84 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 85 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 86 | |
karlmaxwell67 | 3:6a89ac4a1979 | 87 | int TCS3472_I2C::disablePower(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 88 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 89 | char enable_new = enable_old & 254; // sets PON (bit 0) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 90 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 91 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 92 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 93 | |
karlmaxwell67 | 3:6a89ac4a1979 | 94 | int TCS3472_I2C::enableRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 95 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 96 | char enable_new = enable_old | 2; // sets AEN (bit 1) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 97 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 98 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 99 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 100 | |
karlmaxwell67 | 3:6a89ac4a1979 | 101 | int TCS3472_I2C::disableRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 102 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 103 | char enable_new = enable_old & 253; // sets AEN (bit 1) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 104 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 105 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 106 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 107 | |
karlmaxwell67 | 3:6a89ac4a1979 | 108 | int TCS3472_I2C::enablePowerAndRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 109 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 110 | char enable_new = enable_old | 3; // sets PON (bit 0) and AEN (bit 1) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 111 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 112 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 113 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 114 | |
karlmaxwell67 | 3:6a89ac4a1979 | 115 | int TCS3472_I2C::disablePowerAndRGBC(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 116 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 117 | char enable_new = enable_old | 252; // sets PON (bit 0) and AEN (bit 1) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 118 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 119 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 120 | } |
karlmaxwell67 | 2:38d5187a4e7b | 121 | |
karlmaxwell67 | 2:38d5187a4e7b | 122 | int TCS3472_I2C::enableWait(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 123 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 2:38d5187a4e7b | 124 | char enable_new = enable_old | 8; // sets WEN (bit 4) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 125 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 126 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 127 | } |
karlmaxwell67 | 2:38d5187a4e7b | 128 | |
karlmaxwell67 | 2:38d5187a4e7b | 129 | int TCS3472_I2C::disableWait(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 130 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 2:38d5187a4e7b | 131 | char enable_new = enable_old & 247; // sets WEN (bit 4) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 132 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 133 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 134 | } |
karlmaxwell67 | 2:38d5187a4e7b | 135 | |
karlmaxwell67 | 2:38d5187a4e7b | 136 | int TCS3472_I2C::enableInterrupt(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 137 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 2:38d5187a4e7b | 138 | char enable_new = enable_old | 16; // sets AIEN (bit 5) to 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 139 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 2:38d5187a4e7b | 140 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 141 | } |
karlmaxwell67 | 2:38d5187a4e7b | 142 | |
karlmaxwell67 | 2:38d5187a4e7b | 143 | int TCS3472_I2C::disableInterrupt(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 144 | char enable_old = readSingleRegister( ENABLE ); |
karlmaxwell67 | 2:38d5187a4e7b | 145 | char enable_new = enable_old & 239; // sets AIEN (bit 5) to 0 |
karlmaxwell67 | 3:6a89ac4a1979 | 146 | int ack = writeSingleRegister( ENABLE, enable_new ); |
karlmaxwell67 | 3:6a89ac4a1979 | 147 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 148 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 149 | |
karlmaxwell67 | 3:6a89ac4a1979 | 150 | int TCS3472_I2C::setIntegrationTime( const float itime ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 151 | char atime = 256 - itime / 2.4; |
karlmaxwell67 | 3:6a89ac4a1979 | 152 | int ack = writeSingleRegister( ATIME, atime ); |
karlmaxwell67 | 2:38d5187a4e7b | 153 | return ack; |
karlmaxwell67 | 2:38d5187a4e7b | 154 | } |
karlmaxwell67 | 2:38d5187a4e7b | 155 | |
karlmaxwell67 | 2:38d5187a4e7b | 156 | int TCS3472_I2C::setWaitTime( const float time ){ |
karlmaxwell67 | 2:38d5187a4e7b | 157 | int ack = 1; |
karlmaxwell67 | 2:38d5187a4e7b | 158 | char wtime = 0; |
karlmaxwell67 | 2:38d5187a4e7b | 159 | if ( time >= 2.4 && time <= 614.4 ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 160 | ack = writeSingleRegister( CONFIG, 0 ); // sets WLONG to 0 |
karlmaxwell67 | 2:38d5187a4e7b | 161 | wtime = 256 - time / 2.4; |
karlmaxwell67 | 2:38d5187a4e7b | 162 | } |
karlmaxwell67 | 2:38d5187a4e7b | 163 | else if ( time > 614.4 && time <= 7400 ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 164 | ack = writeSingleRegister( CONFIG, 2 ); // sets WLONG to 1 |
karlmaxwell67 | 2:38d5187a4e7b | 165 | wtime = 256 - ( time / 12 ) / 2.4; |
karlmaxwell67 | 2:38d5187a4e7b | 166 | } |
karlmaxwell67 | 2:38d5187a4e7b | 167 | ack = ack || writeSingleRegister( WTIME, wtime ); |
karlmaxwell67 | 2:38d5187a4e7b | 168 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 169 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 170 | |
karlmaxwell67 | 3:6a89ac4a1979 | 171 | char TCS3472_I2C::readEnableRegister(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 172 | return readSingleRegister( ENABLE ); |
karlmaxwell67 | 3:6a89ac4a1979 | 173 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 174 | |
karlmaxwell67 | 3:6a89ac4a1979 | 175 | int TCS3472_I2C::readLowInterruptThreshold(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 176 | char buffer[2] = { 0 }; |
karlmaxwell67 | 3:6a89ac4a1979 | 177 | readMultipleRegisters( AILTL, buffer, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 178 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 3:6a89ac4a1979 | 179 | return reading; |
karlmaxwell67 | 3:6a89ac4a1979 | 180 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 181 | |
karlmaxwell67 | 3:6a89ac4a1979 | 182 | int TCS3472_I2C::readHighInterruptThreshold(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 183 | char buffer[2] = { 0 }; |
karlmaxwell67 | 3:6a89ac4a1979 | 184 | readMultipleRegisters( AIHTL, buffer, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 185 | int reading = (int)buffer[1] << 8 | (int)buffer[0]; |
karlmaxwell67 | 3:6a89ac4a1979 | 186 | return reading; |
karlmaxwell67 | 3:6a89ac4a1979 | 187 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 188 | |
karlmaxwell67 | 3:6a89ac4a1979 | 189 | int TCS3472_I2C::setLowInterruptThreshold( const int threshold ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 190 | char threshold_bytes[2]; |
karlmaxwell67 | 3:6a89ac4a1979 | 191 | threshold_bytes[0] = threshold; // take lowest 8 bits of threshold |
karlmaxwell67 | 3:6a89ac4a1979 | 192 | threshold_bytes[1] = threshold >> 8; // take highest 8 bits of threshold |
karlmaxwell67 | 3:6a89ac4a1979 | 193 | int ack = writeMultipleRegisters( AILTL, threshold_bytes, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 194 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 195 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 196 | |
karlmaxwell67 | 3:6a89ac4a1979 | 197 | int TCS3472_I2C::setHighInterruptThreshold( const int threshold ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 198 | char threshold_bytes[2]; |
karlmaxwell67 | 3:6a89ac4a1979 | 199 | threshold_bytes[0] = threshold; |
karlmaxwell67 | 3:6a89ac4a1979 | 200 | threshold_bytes[1] = threshold >> 8; |
karlmaxwell67 | 3:6a89ac4a1979 | 201 | int ack = writeMultipleRegisters( AIHTL, threshold_bytes, 2 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 202 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 203 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 204 | |
karlmaxwell67 | 3:6a89ac4a1979 | 205 | int TCS3472_I2C::readInterruptPersistence(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 206 | char pers = readSingleRegister( PERS ); |
karlmaxwell67 | 3:6a89ac4a1979 | 207 | char persistence_bits = ( pers << 4 ) >> 4; // discard bits 4 to 7, keep only bits 0 to 3 |
karlmaxwell67 | 3:6a89ac4a1979 | 208 | int persistence = -1; |
karlmaxwell67 | 3:6a89ac4a1979 | 209 | switch (persistence_bits){ |
karlmaxwell67 | 3:6a89ac4a1979 | 210 | case 0: |
karlmaxwell67 | 3:6a89ac4a1979 | 211 | persistence = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 212 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 213 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 214 | persistence = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 215 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 216 | case 2: |
karlmaxwell67 | 3:6a89ac4a1979 | 217 | persistence = 2; |
karlmaxwell67 | 3:6a89ac4a1979 | 218 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 219 | case 3: |
karlmaxwell67 | 3:6a89ac4a1979 | 220 | persistence = 3; |
karlmaxwell67 | 3:6a89ac4a1979 | 221 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 222 | case 4: |
karlmaxwell67 | 3:6a89ac4a1979 | 223 | persistence = 5; |
karlmaxwell67 | 3:6a89ac4a1979 | 224 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 225 | case 5: |
karlmaxwell67 | 3:6a89ac4a1979 | 226 | persistence = 10; |
karlmaxwell67 | 3:6a89ac4a1979 | 227 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 228 | case 6: |
karlmaxwell67 | 3:6a89ac4a1979 | 229 | persistence = 15; |
karlmaxwell67 | 3:6a89ac4a1979 | 230 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 231 | case 7: |
karlmaxwell67 | 3:6a89ac4a1979 | 232 | persistence = 20; |
karlmaxwell67 | 3:6a89ac4a1979 | 233 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 234 | case 8: |
karlmaxwell67 | 3:6a89ac4a1979 | 235 | persistence = 25; |
karlmaxwell67 | 3:6a89ac4a1979 | 236 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 237 | case 9: |
karlmaxwell67 | 3:6a89ac4a1979 | 238 | persistence = 30; |
karlmaxwell67 | 3:6a89ac4a1979 | 239 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 240 | case 10: |
karlmaxwell67 | 3:6a89ac4a1979 | 241 | persistence = 35; |
karlmaxwell67 | 3:6a89ac4a1979 | 242 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 243 | case 11: |
karlmaxwell67 | 3:6a89ac4a1979 | 244 | persistence = 40; |
karlmaxwell67 | 3:6a89ac4a1979 | 245 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 246 | case 12: |
karlmaxwell67 | 3:6a89ac4a1979 | 247 | persistence = 45; |
karlmaxwell67 | 3:6a89ac4a1979 | 248 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 249 | case 13: |
karlmaxwell67 | 3:6a89ac4a1979 | 250 | persistence = 50; |
karlmaxwell67 | 3:6a89ac4a1979 | 251 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 252 | case 14: |
karlmaxwell67 | 3:6a89ac4a1979 | 253 | persistence = 55; |
karlmaxwell67 | 3:6a89ac4a1979 | 254 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 255 | case 15: |
karlmaxwell67 | 3:6a89ac4a1979 | 256 | persistence = 60; |
karlmaxwell67 | 3:6a89ac4a1979 | 257 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 258 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 259 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 260 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 261 | return persistence; |
karlmaxwell67 | 3:6a89ac4a1979 | 262 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 263 | |
karlmaxwell67 | 3:6a89ac4a1979 | 264 | int TCS3472_I2C::setInterruptPersistence( const int persistence ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 265 | char pers_byte; |
karlmaxwell67 | 3:6a89ac4a1979 | 266 | int ack = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 267 | switch (persistence){ |
karlmaxwell67 | 3:6a89ac4a1979 | 268 | case 0: |
karlmaxwell67 | 3:6a89ac4a1979 | 269 | pers_byte = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 270 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 271 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 272 | pers_byte = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 273 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 274 | case 2: |
karlmaxwell67 | 3:6a89ac4a1979 | 275 | pers_byte = 2; |
karlmaxwell67 | 3:6a89ac4a1979 | 276 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 277 | case 3: |
karlmaxwell67 | 3:6a89ac4a1979 | 278 | pers_byte = 3; |
karlmaxwell67 | 3:6a89ac4a1979 | 279 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 280 | case 5: |
karlmaxwell67 | 3:6a89ac4a1979 | 281 | pers_byte = 4; |
karlmaxwell67 | 3:6a89ac4a1979 | 282 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 283 | case 10: |
karlmaxwell67 | 3:6a89ac4a1979 | 284 | pers_byte = 5; |
karlmaxwell67 | 3:6a89ac4a1979 | 285 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 286 | case 15: |
karlmaxwell67 | 3:6a89ac4a1979 | 287 | pers_byte = 6; |
karlmaxwell67 | 3:6a89ac4a1979 | 288 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 289 | case 20: |
karlmaxwell67 | 3:6a89ac4a1979 | 290 | pers_byte = 7; |
karlmaxwell67 | 3:6a89ac4a1979 | 291 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 292 | case 25: |
karlmaxwell67 | 3:6a89ac4a1979 | 293 | pers_byte = 8; |
karlmaxwell67 | 3:6a89ac4a1979 | 294 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 295 | case 30: |
karlmaxwell67 | 3:6a89ac4a1979 | 296 | pers_byte = 9; |
karlmaxwell67 | 3:6a89ac4a1979 | 297 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 298 | case 35: |
karlmaxwell67 | 3:6a89ac4a1979 | 299 | pers_byte = 10; |
karlmaxwell67 | 3:6a89ac4a1979 | 300 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 301 | case 40: |
karlmaxwell67 | 3:6a89ac4a1979 | 302 | pers_byte = 11; |
karlmaxwell67 | 3:6a89ac4a1979 | 303 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 304 | case 45: |
karlmaxwell67 | 3:6a89ac4a1979 | 305 | pers_byte = 12; |
karlmaxwell67 | 3:6a89ac4a1979 | 306 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 307 | case 50: |
karlmaxwell67 | 3:6a89ac4a1979 | 308 | pers_byte = 13; |
karlmaxwell67 | 3:6a89ac4a1979 | 309 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 310 | case 55: |
karlmaxwell67 | 3:6a89ac4a1979 | 311 | pers_byte = 14; |
karlmaxwell67 | 3:6a89ac4a1979 | 312 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 313 | case 60: |
karlmaxwell67 | 3:6a89ac4a1979 | 314 | pers_byte = 15; |
karlmaxwell67 | 3:6a89ac4a1979 | 315 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 316 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 317 | ack = 2; // 2 used to indicate invalid entry |
karlmaxwell67 | 3:6a89ac4a1979 | 318 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 319 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 320 | if ( ack != 2 ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 321 | ack = writeSingleRegister( PERS, pers_byte ); |
karlmaxwell67 | 3:6a89ac4a1979 | 322 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 323 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 324 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 325 | |
karlmaxwell67 | 3:6a89ac4a1979 | 326 | int TCS3472_I2C::clearInterrupt(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 327 | char tx = 230; |
karlmaxwell67 | 3:6a89ac4a1979 | 328 | int ack = i2c.write( SLAVE_ADDRESS << 1, &tx, 1 ); |
karlmaxwell67 | 3:6a89ac4a1979 | 329 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 330 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 331 | |
karlmaxwell67 | 3:6a89ac4a1979 | 332 | int TCS3472_I2C::readRGBCGain(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 333 | char control = readSingleRegister( CONTROL ); |
karlmaxwell67 | 3:6a89ac4a1979 | 334 | char gain_bits = ( control << 6 ) >> 6; // discard bits 2 to 7, keep only bits 0 & 1 |
karlmaxwell67 | 3:6a89ac4a1979 | 335 | int gain; |
karlmaxwell67 | 3:6a89ac4a1979 | 336 | switch (gain_bits) { |
karlmaxwell67 | 3:6a89ac4a1979 | 337 | case 0: |
karlmaxwell67 | 3:6a89ac4a1979 | 338 | gain = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 339 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 340 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 341 | gain = 4; |
karlmaxwell67 | 3:6a89ac4a1979 | 342 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 343 | case 2: |
karlmaxwell67 | 3:6a89ac4a1979 | 344 | gain = 16; |
karlmaxwell67 | 3:6a89ac4a1979 | 345 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 346 | case 3: |
karlmaxwell67 | 3:6a89ac4a1979 | 347 | gain = 60; |
karlmaxwell67 | 3:6a89ac4a1979 | 348 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 349 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 350 | gain = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 351 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 352 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 353 | return gain; |
karlmaxwell67 | 3:6a89ac4a1979 | 354 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 355 | |
karlmaxwell67 | 3:6a89ac4a1979 | 356 | int TCS3472_I2C::setRGBCGain( const int gain ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 357 | char control; |
karlmaxwell67 | 3:6a89ac4a1979 | 358 | int ack = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 359 | switch (gain){ |
karlmaxwell67 | 3:6a89ac4a1979 | 360 | case 1: |
karlmaxwell67 | 3:6a89ac4a1979 | 361 | control = 0; |
karlmaxwell67 | 3:6a89ac4a1979 | 362 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 363 | case 4: |
karlmaxwell67 | 3:6a89ac4a1979 | 364 | control = 1; |
karlmaxwell67 | 3:6a89ac4a1979 | 365 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 366 | case 16: |
karlmaxwell67 | 3:6a89ac4a1979 | 367 | control = 2; |
karlmaxwell67 | 3:6a89ac4a1979 | 368 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 369 | case 60: |
karlmaxwell67 | 3:6a89ac4a1979 | 370 | control = 3; |
karlmaxwell67 | 3:6a89ac4a1979 | 371 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 372 | default: |
karlmaxwell67 | 3:6a89ac4a1979 | 373 | ack = 2; // 2 used to indicate invalid entry |
karlmaxwell67 | 3:6a89ac4a1979 | 374 | break; |
karlmaxwell67 | 3:6a89ac4a1979 | 375 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 376 | if ( ack != 2 ){ |
karlmaxwell67 | 3:6a89ac4a1979 | 377 | ack = writeSingleRegister( CONTROL, control ); |
karlmaxwell67 | 3:6a89ac4a1979 | 378 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 379 | return ack; |
karlmaxwell67 | 3:6a89ac4a1979 | 380 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 381 | |
karlmaxwell67 | 3:6a89ac4a1979 | 382 | char TCS3472_I2C::getDeviceID(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 383 | return readSingleRegister( ID ); |
karlmaxwell67 | 3:6a89ac4a1979 | 384 | } |
karlmaxwell67 | 3:6a89ac4a1979 | 385 | |
karlmaxwell67 | 3:6a89ac4a1979 | 386 | char TCS3472_I2C::readStatusRegister(){ |
karlmaxwell67 | 3:6a89ac4a1979 | 387 | return readSingleRegister( STATUS ); |
karlmaxwell67 | 0:453a43c8bf2b | 388 | } |