adafruit / Adafruit_TCS34725

Arduino library driver for Adafruit's TCS34725 RGB Color Sensor Breakout
http://www.adafruit.com/products/1334
Other
151 stars 151 forks source link

Read TCS34725 CRGB registry adresses at once #29

Open rongith opened 5 years ago

rongith commented 5 years ago

I added 2 new functions to read TCS34725 CRGB registry adresses at once. Because I wanted to read data as quickly as possible from sensors on a TCA9548A multiplexer.

CRGB addresses in the TCS34725 are consecutive then it is not necessary to do many write, requestFrom and read. All can be done at once. The I2C bus is also less busy.

Here the different times I measured on a Arduino Uno Rev3: tcs.read16 x4 (CRGB) : 552us x 4 = 2.208ms tcs.read16 x3 (RGB) : 552us x 3 = 1.656ms getFastRawRGBC : 1.150ms getFastRawRGB : 0.950ms Then with 8 sensors on a TCA9548A and as I don't need Clear value, I went down to 9.4ms from 19.5ms to get whole RGB data. A tcaSelect function adds 0.231ms per sensor: (2.208+0.231)8 = 19.5ms (0.950+0.231)8 = 9.4ms

I don't know if there are some downside to that. Code could be optimized. I didn't exactly understand the TCS34725_COMMAND_BIT. From the Datasheet / Figure 25, I see it does a "Repeated byte protocol". I would have done an "Auto-increment protocol transaction" with a 0b10100000 instead but it seems to change nothing and the 0b10000000 seems OK, then... But I don't like to not understand what I'm doing.

/*!
*/
void Adafruit_TCS34725::getFastRawRGBC(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c) {
  uint16_t t1,t2,t3,t4,t5,t6,t7,t8;

  _wire->beginTransmission(_i2caddr);
  _wire->write(TCS34725_COMMAND_BIT | TCS34725_CDATAL);
  _wire->endTransmission();
  _wire->requestFrom(_i2caddr, 8);
  t1 = _wire->read();
  t2 = _wire->read();
  t3 = _wire->read();
  t4 = _wire->read();
  t5 = _wire->read();
  t6 = _wire->read();
  t7 = _wire->read();
  t8 = _wire->read();

  t2 <<= 8;
  t2 |= t1;
  *c = t2;

  t4 <<= 8;
  t4 |= t3;
  *r = t4;

  t6 <<= 8;
  t6 |= t5;
  *g = t6;

  t8 <<= 8;
  t8 |= t7;
  *b = t8;
}

/*!
*/
void Adafruit_TCS34725::getFastRawRGB(uint16_t *r, uint16_t *g, uint16_t *b) {
  uint16_t t1,t2,t3,t4,t5,t6;

  _wire->beginTransmission(_i2caddr);
  _wire->write(TCS34725_COMMAND_BIT | TCS34725_RDATAL);
  _wire->endTransmission();
  _wire->requestFrom(_i2caddr, 6);
  t1 = _wire->read();
  t2 = _wire->read();
  t3 = _wire->read();
  t4 = _wire->read();
  t5 = _wire->read();
  t6 = _wire->read();

  t2 <<= 8;
  t2 |= t1;
  *r = t2;

  t4 <<= 8;
  t4 |= t3;
  *g = t4;

  t6 <<= 8;
  t6 |= t5;
  *b = t6;
}