MCP9700 Driver
API reference
Loading...
Searching...
No Matches
mcp9700_thermistor.ipp
Go to the documentation of this file.
1#pragma once
2
3namespace hf {
4namespace mcp9700 {
5
6template <typename AdcType>
7Mcp9700Thermistor<AdcType>::Mcp9700Thermistor(AdcType* adc_interface, uint8_t adc_channel,
8 float v_zero_c, float v_per_c) noexcept
9 : adc_interface_(adc_interface)
10 , adc_channel_(adc_channel)
11 , v_zero_c_(v_zero_c)
12 , v_per_c_(v_per_c)
16 , last_error_(Error::kNone)
17 , initialized_(false) {}
18
19template <typename AdcType>
21 if (adc_interface_ == nullptr) {
22 last_error_ = Error::kInvalidArgument;
23 return false;
24 }
25 if (!HasValidCoefficients()) {
26 last_error_ = Error::kInvalidCoefficients;
27 return false;
28 }
29 initialized_ = adc_interface_->EnsureInitialized();
30 last_error_ = initialized_ ? Error::kNone : Error::kAdcError;
31 return initialized_;
32}
33
34template <typename AdcType>
36 initialized_ = false;
37 last_error_ = Error::kNone;
38 return true;
39}
40
41template <typename AdcType>
42bool Mcp9700Thermistor<AdcType>::ReadTemperatureCelsius(float* temperature_celsius) noexcept {
43 if (!initialized_ || adc_interface_ == nullptr) {
44 last_error_ = Error::kNotInitialized;
45 return false;
46 }
47 if (temperature_celsius == nullptr) {
48 last_error_ = Error::kInvalidArgument;
49 return false;
50 }
51 float voltage_v = 0.0F;
52 const int err = adc_interface_->ReadChannelV(adc_channel_, &voltage_v);
53 if (err != 0) {
54 last_error_ = Error::kAdcError;
55 return false;
56 }
57 const float celsius = (voltage_v - v_zero_c_) / v_per_c_;
58 // DS20001942 Section 3.2 / Temperature Characteristics: a working sensor's output stays within
59 // the specified temperature range; anything well outside it is a wiring or scaling fault.
60 if (!std::isfinite(celsius) || celsius < min_c_ - margin_c_ || celsius > max_c_ + margin_c_) {
61 last_error_ = Error::kOutOfRange;
62 return false;
63 }
64 *temperature_celsius = celsius;
65 last_error_ = Error::kNone;
66 return true;
67}
68
69template <typename AdcType>
70bool Mcp9700Thermistor<AdcType>::ReadVoltage(float* voltage_v) noexcept {
71 if (!initialized_ || adc_interface_ == nullptr) {
72 last_error_ = Error::kNotInitialized;
73 return false;
74 }
75 if (voltage_v == nullptr) {
76 last_error_ = Error::kInvalidArgument;
77 return false;
78 }
79 float v = 0.0F;
80 if (adc_interface_->ReadChannelV(adc_channel_, &v) != 0) {
81 last_error_ = Error::kAdcError;
82 return false;
83 }
84 *voltage_v = v;
85 last_error_ = Error::kNone;
86 return true;
87}
88
89template <typename AdcType>
90bool Mcp9700Thermistor<AdcType>::SetSpecifiedRange(float min_celsius, float max_celsius,
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) {
94 last_error_ = Error::kInvalidArgument;
95 return false;
96 }
97 min_c_ = min_celsius;
98 max_c_ = max_celsius;
99 margin_c_ = margin_celsius;
100 last_error_ = Error::kNone;
101 return true;
102}
103
104} // namespace mcp9700
105} // namespace hf
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