|
| 1 | +// |
| 2 | +// FILE: TSL235R.cpp |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// PURPOSE: library fot the TSL235R light to frequency convertor |
| 6 | +// |
| 7 | +// HISTORY: |
| 8 | +// 0.1.0 2020-05-29 initial version |
| 9 | + |
| 10 | + |
| 11 | +#include "TSL235R.h" |
| 12 | + |
| 13 | + |
| 14 | +TSL235R::TSL235R(float voltage) |
| 15 | +{ |
| 16 | + _voltage = voltage; |
| 17 | + calculateFactor(); |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +float TSL235R::irradiance(uint32_t Hz) |
| 22 | +{ |
| 23 | + return Hz * _factor; |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +float TSL235R::irradiance(uint32_t pulses, uint32_t milliseconds) |
| 28 | +{ |
| 29 | + return (pulses * 1000.0 * _factor) / milliseconds; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +void TSL235R::setWavelength(uint16_t wavelength) |
| 34 | +{ |
| 35 | + _waveLength = wavelength; |
| 36 | + calculateFactor(); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +void TSL235R::setVoltage(float voltage) |
| 41 | +{ |
| 42 | + _voltage = voltage; |
| 43 | + calculateFactor(); |
| 44 | +} |
| 45 | + |
| 46 | +void TSL235R::calculateFactor() |
| 47 | +{ |
| 48 | + // figure 1 datasheet |
| 49 | + // 1 Khz crosses the line at 35/230 between 1 and 10. |
| 50 | + // so the correctiion factor is 10^0.15217 = 1.419659 = 1.42 |
| 51 | + // as the graph is in kHz we need to correct a factor 1000 |
| 52 | + // as the irradiance function gets Hz |
| 53 | + const float cf = 0.00142; |
| 54 | + _waveLengthFactor = calcWLF(_waveLength); |
| 55 | + |
| 56 | + _voltageFactor = 0.988 + (_voltage - 2.7) * (0.015 / 2.8); |
| 57 | + _factor = cf * _waveLengthFactor * _voltageFactor; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +float TSL235R::calcWLF(uint16_t _waveLength) |
| 62 | +{ |
| 63 | + // figure 2 datasheet |
| 64 | + // 635 nm is reference 1.000 |
| 65 | + // remaining is linear interpolated between points in the graph |
| 66 | + float in[] = { 300, 350, 400, 500, 600, 635, 700, 750, 800, 850, 900, 1000, 1100}; |
| 67 | + 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}; |
| 68 | + return 1.0 / multiMap(_waveLength, in, out, 13); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +float TSL235R::multiMap(float val, float * _in, float * _out, uint8_t size) |
| 73 | +{ |
| 74 | + // take care the value is within range |
| 75 | + // val = constrain(val, _in[0], _in[size-1]); |
| 76 | + if (val <= _in[0]) return _out[0]; |
| 77 | + if (val >= _in[size-1]) return _out[size-1]; |
| 78 | + |
| 79 | + // search right interval |
| 80 | + uint8_t pos = 1; // _in[0] allready tested |
| 81 | + while(val > _in[pos]) pos++; |
| 82 | + |
| 83 | + // this will handle all exact "points" in the _in array |
| 84 | + if (val == _in[pos]) return _out[pos]; |
| 85 | + |
| 86 | + // interpolate in the right segment for the rest |
| 87 | + return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1]; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +// -- END OF FILE -- |
0 commit comments