2019-12-17 06:06:29 -08:00
|
|
|
/**
|
|
|
|
* @author Matthew Kennedy, (c) 2019
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "resistance_func.h"
|
|
|
|
|
|
|
|
void ResistanceFunc::configure(float supplyVoltage, float pullupResistor) {
|
|
|
|
m_pullupResistor = pullupResistor;
|
|
|
|
m_supplyVoltage = supplyVoltage;
|
|
|
|
}
|
|
|
|
|
|
|
|
SensorResult ResistanceFunc::convert(float raw) const {
|
|
|
|
// If the voltage is very low, the sensor is a dead short.
|
|
|
|
if (raw < 0.05f) {
|
2022-07-28 00:04:28 -07:00
|
|
|
return UnexpectedCode::Low;
|
2019-12-17 06:06:29 -08:00
|
|
|
}
|
|
|
|
|
2020-04-07 13:07:09 -07:00
|
|
|
// If the voltage is very high (98% VCC), the sensor is open circuit.
|
|
|
|
if (raw > (m_supplyVoltage * 0.98f)) {
|
2022-07-28 00:04:28 -07:00
|
|
|
return UnexpectedCode::High;
|
2019-12-17 06:06:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Voltage is in a sensible range - convert
|
|
|
|
float resistance = m_pullupResistor / (m_supplyVoltage / raw - 1);
|
|
|
|
|
2020-04-18 22:53:04 -07:00
|
|
|
return resistance;
|
2019-12-17 06:06:29 -08:00
|
|
|
}
|