Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Data Methods #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Adafruit_TCS34725.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ boolean Adafruit_TCS34725::begin(void)
}
_tcs34725Initialised = true;

/* Use default values from Fig. 4 in datasheet */
_tcs34725RedSensitivity = 76.5;
_tcs34725ClearRedReference = 1.38;
_tcs34725GreenSensitivity = 72.5;
_tcs34725ClearGreenReference = 1.66;
_tcs34725BlueSensitivity = 95;
_tcs34725ClearBlueReference = 1.95;

/* Set default integration time and gain */
setIntegrationTime(_tcs34725IntegrationTime);
setGain(_tcs34725Gain);
Expand Down Expand Up @@ -242,6 +250,30 @@ void Adafruit_TCS34725::setGain(tcs34725Gain_t gain)
_tcs34725Gain = gain;
}

/**************************************************************************/
/*!
Sets the clear reference points for getting unit results
*/
/**************************************************************************/
void Adafruit_TCS34725::setClearReference(uint16_t redReference, uint16_t greenReference, uint16_t blueReference)
{
_tcs34725ClearRedReference = redReference;
_tcs34725ClearGreenReference = greenReference;
_tcs34725ClearBlueReference = blueReference;
}

/**************************************************************************/
/*!
Sets the sensitivity points for getting unit results
*/
/**************************************************************************/
void Adafruit_TCS34725::setSensitivity(uint16_t redSensitivity, uint16_t greenSensitivity, uint16_t blueSensitivity)
{
_tcs34725RedSensitivity = redSensitivity;
_tcs34725GreenSensitivity = greenSensitivity;
_tcs34725BlueSensitivity = blueSensitivity;
}

/**************************************************************************/
/*!
@brief Reads the raw red, green, blue and clear channel values
Expand Down Expand Up @@ -280,6 +312,7 @@ void Adafruit_TCS34725::getRawData (uint16_t *r, uint16_t *g, uint16_t *b, uint1
}
}


/**************************************************************************/
/*!
@brief Reads the raw red, green, blue and clear channel values in
Expand All @@ -295,6 +328,72 @@ void Adafruit_TCS34725::getRawDataOneShot (uint16_t *r, uint16_t *g, uint16_t *b
disable();
}

/**************************************************************************/
/*!
@brief Reads the raw red, green, blue and clear channel values with uw/cm^2 units
*/
/**************************************************************************/
void Adafruit_TCS34725::getUnitDataOneShot (float *r, float *g, float *b, float *c)
{
if (!_tcs34725Initialised) begin();

float f_integration_multiple = 1;
uint16_t ui_gain_multiple = 1;
uint16_t ui_clearRaw, ui_redRaw, ui_greenRaw, ui_blueRaw;
getRawDataOneShot(&ui_redRaw, &ui_greenRaw, &ui_blueRaw, &ui_clearRaw);

/* Set a multiplier for the gain */
switch(_tcs34725Gain)
{
case TCS34725_GAIN_1X:
ui_gain_multiple = 1;
break;
case TCS34725_GAIN_4X:
ui_gain_multiple = 4;
break;
case TCS34725_GAIN_16X:
ui_gain_multiple = 16;
break;
case TCS34725_GAIN_60X:
ui_gain_multiple = 40;
break;
}
/* Set a multiplier for the integration time */
switch (_tcs34725IntegrationTime)
{
case TCS34725_INTEGRATIONTIME_2_4MS:
break;
case TCS34725_INTEGRATIONTIME_24MS:
f_integration_multiple = 24/2.4;
break;
case TCS34725_INTEGRATIONTIME_50MS:
f_integration_multiple = 50/2.4;
break;
case TCS34725_INTEGRATIONTIME_101MS:
f_integration_multiple = 101/2.4;
break;
case TCS34725_INTEGRATIONTIME_154MS:
f_integration_multiple = 154/2.4;
break;
case TCS34725_INTEGRATIONTIME_700MS:
f_integration_multiple = 700/2.4;
break;
}

/* Use sensitivity for each channel (e.g., ratio of respected channel with resepect to clear channel, these should fall within ranges in Fig. 4 from datasheed).
Use clear channel reference for the particular color (again should be in range from Fig. 4, BUT this will depend on the wavelength somewhat so we will assume a perfect match to the table)
Next, scale based on the gain and integration time
Finally, assume that clear channel is just the sum of the RGB channels since we used the sensitivity percentage scalar.

This all should be calibrated or the values will be off since the range in the Fig. 4 table is quite large. The default values for sensitivity and reference are based on the 'typical'
values from Fig 4. */
*r = ui_redRaw / _tcs34725RedSensitivity * _tcs34725ClearRedReference * f_integration_multiple * ui_gain_multiple;
*b = ui_blueRaw / _tcs34725BlueSensitivity * _tcs34725ClearBlueReference * f_integration_multiple * ui_gain_multiple;
*g = ui_greenRaw / _tcs34725GreenSensitivity * _tcs34725ClearGreenReference * f_integration_multiple * ui_gain_multiple;
*c = *r + *g + *b;

}

/**************************************************************************/
/*!
@brief Converts the raw R/G/B values to color temperature in degrees
Expand Down
10 changes: 9 additions & 1 deletion Adafruit_TCS34725.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,20 @@ class Adafruit_TCS34725 {
void setIntLimits(uint16_t l, uint16_t h);
void enable(void);
void disable(void);
void getUnitDataOneShot (float *r, float *g, float *b, float *c);
void setSensitivity(uint16_t redSensitivity, uint16_t greenSensitivity, uint16_t blueSensitivity);
void setClearReference(uint16_t redReference, uint16_t greenReference, uint16_t blueReference);

private:
boolean _tcs34725Initialised;
tcs34725Gain_t _tcs34725Gain;
tcs34725IntegrationTime_t _tcs34725IntegrationTime;

float _tcs34725RedSensitivity;
float _tcs34725ClearRedReference;
float _tcs34725GreenSensitivity;
float _tcs34725ClearGreenReference;
float _tcs34725BlueSensitivity;
float _tcs34725ClearBlueReference;
};

#endif