6template <
typename AdcType>
8 float v_zero_c,
float v_per_c) noexcept
9 : adc_interface_(adc_interface)
10 , adc_channel_(adc_channel)
17 , initialized_(
false) {}
19template <
typename AdcType>
21 if (adc_interface_ ==
nullptr) {
25 if (!HasValidCoefficients()) {
29 initialized_ = adc_interface_->EnsureInitialized();
34template <
typename AdcType>
41template <
typename AdcType>
43 if (!initialized_ || adc_interface_ ==
nullptr) {
47 if (temperature_celsius ==
nullptr) {
51 float voltage_v = 0.0F;
52 const int err = adc_interface_->ReadChannelV(adc_channel_, &voltage_v);
57 const float celsius = (voltage_v - v_zero_c_) / v_per_c_;
60 if (!std::isfinite(celsius) || celsius < min_c_ - margin_c_ || celsius > max_c_ + margin_c_) {
64 *temperature_celsius = celsius;
69template <
typename AdcType>
71 if (!initialized_ || adc_interface_ ==
nullptr) {
75 if (voltage_v ==
nullptr) {
80 if (adc_interface_->ReadChannelV(adc_channel_, &v) != 0) {
89template <
typename AdcType>
91 float margin_celsius)
noexcept {
92 if (!std::isfinite(min_celsius) || !std::isfinite(max_celsius) || !std::isfinite(margin_celsius) ||
93 min_celsius >= max_celsius || margin_celsius < 0.0F) {
99 margin_c_ = margin_celsius;
bool Initialize() noexcept
Initialize the underlying ADC (calls EnsureInitialized on the adapter).
Definition mcp9700_thermistor.ipp:20
bool SetSpecifiedRange(float min_celsius, float max_celsius, float margin_celsius=kDefaultRangeMarginCelsius) noexcept
Set the temperature range a reading must fall in to be accepted.
Definition mcp9700_thermistor.ipp:90
bool ReadVoltage(float *voltage_v) noexcept
Read the raw sensor output voltage (volts at VOUT, as the adapter reports it).
Definition mcp9700_thermistor.ipp:70
bool ReadTemperatureCelsius(float *temperature_celsius) noexcept
Read temperature in degrees Celsius.
Definition mcp9700_thermistor.ipp:42
bool Deinitialize() noexcept
Release logical ownership; underlying ADC teardown is adapter-specific.
Definition mcp9700_thermistor.ipp:35
Mcp9700Thermistor(AdcType *adc_interface, uint8_t adc_channel, float v_zero_c=kDefaultVoltsAtZeroC, float v_per_c=kDefaultVoltsPerCelsius) noexcept
Definition mcp9700_thermistor.ipp:7
constexpr float kDefaultSpecifiedMaxCelsius
Default upper end of the specified temperature range (°C).
Definition mcp9700_thermistor.hpp:36
@ kNone
The last call succeeded.
@ kInvalidArgument
Null adapter or null output pointer, or an invalid range.
@ kInvalidCoefficients
Offset not finite, or slope not a finite positive number.
@ kNotInitialized
Read before a successful Initialize(), or after Deinitialize().
@ kOutOfRange
The voltage is outside the plausible range: open, shorted or mis-scaled input.
@ kAdcError
EnsureInitialized() returned false or ReadChannelV() returned non-zero.
constexpr float kDefaultRangeMarginCelsius
Default plausibility margin (°C) added on both sides of the specified range.
Definition mcp9700_thermistor.hpp:45
constexpr float kDefaultSpecifiedMinCelsius
Default lower end of the specified temperature range (°C).
Definition mcp9700_thermistor.hpp:33
Definition mcp9700_thermistor.hpp:18