GY-63_MS5611/libraries/TSL235R/TSL235R .cpp

97 lines
2.3 KiB
C++
Raw Normal View History

2021-05-30 08:16:15 -04:00
//
// FILE: TSL235R.cpp
// AUTHOR: Rob Tillaart
2022-11-26 11:40:57 -05:00
// VERSION: 0.1.3
2021-12-29 07:37:09 -05:00
// PURPOSE: library for the TSL235R light to frequency convertor
2021-05-30 08:16:15 -04:00
#include "TSL235R.h"
TSL235R::TSL235R(float voltage)
{
_voltage = voltage;
calculateFactor();
}
float TSL235R::irradiance(uint32_t Hz)
{
return Hz * _factor;
}
float TSL235R::irradiance(uint32_t pulses, uint32_t milliseconds)
{
return (pulses * 1000.0 * _factor) / milliseconds;
}
2021-06-04 09:58:39 -04:00
float TSL235R::irradiance_HS(uint32_t pulses, uint32_t microseconds)
{
return (pulses * 1000000.0 * _factor) / microseconds;
}
2021-05-30 08:16:15 -04:00
void TSL235R::setWavelength(uint16_t wavelength)
{
_waveLength = wavelength;
calculateFactor();
}
void TSL235R::setVoltage(float voltage)
{
_voltage = voltage;
calculateFactor();
}
2021-12-29 07:37:09 -05:00
2021-05-30 08:16:15 -04:00
void TSL235R::calculateFactor()
{
2022-11-26 11:40:57 -05:00
// figure 1 datasheet
// 1 KHz crosses the line at 35/230 between 1 and 10.
// so the correction factor is 10^0.15217 = 1.419659 = 1.42 (as all math has 3 decimals)
// as the graph is in kHz we need to correct a factor 1000
// as the irradiance function gets Hz
2021-05-30 08:16:15 -04:00
const float cf = 0.00142;
2022-11-26 11:40:57 -05:00
_waveLengthFactor = calculateWaveLengthFactor(_waveLength);
2021-05-30 08:16:15 -04:00
_voltageFactor = 0.988 + (_voltage - 2.7) * (0.015 / 2.8);
_factor = cf * _waveLengthFactor * _voltageFactor;
}
2022-11-26 11:40:57 -05:00
float TSL235R::calculateWaveLengthFactor(uint16_t _waveLength)
2021-05-30 08:16:15 -04:00
{
2022-11-26 11:40:57 -05:00
// figure 2 datasheet
// 635 nm is reference 1.000
// remaining is linear interpolated between points in the graph
2021-05-30 08:16:15 -04:00
float in[] = { 300, 350, 400, 500, 600, 635, 700, 750, 800, 850, 900, 1000, 1100};
float out[] = { 0.1, 0.35, 0.5, 0.75, 0.93, 1.00, 1.15, 1.20, 1.15, 1.10, 0.95, 0.40, 0.10};
return 1.0 / multiMap(_waveLength, in, out, 13);
}
2021-12-29 07:37:09 -05:00
float TSL235R::multiMap(float value, float * _in, float * _out, uint8_t size)
2021-05-30 08:16:15 -04:00
{
2022-11-26 11:40:57 -05:00
// take care the value is within range
// value = constrain(value, _in[0], _in[size-1]);
2021-12-29 07:37:09 -05:00
if (value <= _in[0]) return _out[0];
if (value >= _in[size-1]) return _out[size-1];
2021-05-30 08:16:15 -04:00
2022-11-26 11:40:57 -05:00
// search right interval
2021-12-29 07:37:09 -05:00
uint8_t pos = 1; // _in[0] already tested
while(value > _in[pos]) pos++;
2021-05-30 08:16:15 -04:00
2022-11-26 11:40:57 -05:00
// this will handle all exact "points" in the _in array
2021-12-29 07:37:09 -05:00
if (value == _in[pos]) return _out[pos];
2021-05-30 08:16:15 -04:00
2022-11-26 11:40:57 -05:00
// interpolate in the right segment for the rest
2021-12-29 07:37:09 -05:00
uint8_t pos1 = pos - 1;
return (value - _in[pos1]) * (_out[pos] - _out[pos1]) / (_in[pos] - _in[pos1]) + _out[pos1];
2021-05-30 08:16:15 -04:00
}
2022-11-26 11:40:57 -05:00
// -- END OF FILE --
2021-12-29 07:37:09 -05:00