MCP9700 Driver
API reference
Loading...
Searching...
No Matches
mcp9700_thermistor.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <cmath>
14#include <cstdint>
15
16#include "mcp9700_version.h"
17
18namespace hf {
19namespace mcp9700 {
20
22inline constexpr float kDefaultVoltsAtZeroC = 0.5F;
23
25inline constexpr float kDefaultVoltsPerCelsius = 0.01F;
26
33inline constexpr float kDefaultSpecifiedMinCelsius = -40.0F;
34
36inline constexpr float kDefaultSpecifiedMaxCelsius = 125.0F;
37
45inline constexpr float kDefaultRangeMarginCelsius = 8.0F;
46
48enum class Error : uint8_t {
49 kNone = 0,
53 kAdcError,
55};
56
67template <typename AdcType>
69public:
76 Mcp9700Thermistor(AdcType* adc_interface, uint8_t adc_channel,
77 float v_zero_c = kDefaultVoltsAtZeroC,
78 float v_per_c = kDefaultVoltsPerCelsius) noexcept;
79
82
83 ~Mcp9700Thermistor() noexcept = default;
84
86 bool Initialize() noexcept;
87
89 bool Deinitialize() noexcept;
90
99 bool ReadTemperatureCelsius(float* temperature_celsius) noexcept;
100
109 bool ReadVoltage(float* voltage_v) noexcept;
110
121 bool SetSpecifiedRange(float min_celsius, float max_celsius,
122 float margin_celsius = kDefaultRangeMarginCelsius) noexcept;
123
125 [[nodiscard]] float SpecifiedMinCelsius() const noexcept { return min_c_; }
126
128 [[nodiscard]] float SpecifiedMaxCelsius() const noexcept { return max_c_; }
129
131 [[nodiscard]] float RangeMarginCelsius() const noexcept { return margin_c_; }
132
134 [[nodiscard]] Error LastError() const noexcept { return last_error_; }
135
142 [[nodiscard]] bool HasValidCoefficients() const noexcept {
143 return std::isfinite(v_zero_c_) && std::isfinite(v_per_c_) && v_per_c_ > 0.0F;
144 }
145
147 [[nodiscard]] float VoltsAtZeroC() const noexcept { return v_zero_c_; }
148
150 [[nodiscard]] float VoltsPerCelsius() const noexcept { return v_per_c_; }
151
152private:
153 AdcType* adc_interface_;
154 uint8_t adc_channel_;
155 float v_zero_c_;
156 float v_per_c_;
157 float min_c_;
158 float max_c_;
159 float margin_c_;
160 Error last_error_;
161 bool initialized_;
162};
163
164} // namespace mcp9700
165} // namespace hf
166
168inline const char* GetMcp9700DriverVersion() noexcept {
169 return HF_MCP9700_VERSION_STRING;
170}
171
172// NOLINTNEXTLINE(bugprone-suspicious-include) — template implementation
173#include "../src/mcp9700_thermistor.ipp"
Template driver for MCP9700 family sensors on a multiplexed ADC.
Definition mcp9700_thermistor.hpp:68
float SpecifiedMaxCelsius() const noexcept
Upper end of the specified range (°C).
Definition mcp9700_thermistor.hpp:128
Mcp9700Thermistor & operator=(const Mcp9700Thermistor &)=delete
float RangeMarginCelsius() const noexcept
Margin added on both sides of the specified range (°C).
Definition mcp9700_thermistor.hpp:131
Mcp9700Thermistor(const Mcp9700Thermistor &)=delete
bool HasValidCoefficients() const noexcept
True when the coefficients can be used: a finite offset and a finite slope above zero.
Definition mcp9700_thermistor.hpp:142
float SpecifiedMinCelsius() const noexcept
Lower end of the specified range (°C).
Definition mcp9700_thermistor.hpp:125
Error LastError() const noexcept
Why the last call that can fail failed; Error::kNone after a success.
Definition mcp9700_thermistor.hpp:134
~Mcp9700Thermistor() noexcept=default
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
float VoltsAtZeroC() const noexcept
Last configured output at 0 °C (volts).
Definition mcp9700_thermistor.hpp:147
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
float VoltsPerCelsius() const noexcept
Last configured sensitivity (volts per °C).
Definition mcp9700_thermistor.hpp:150
const char * GetMcp9700DriverVersion() noexcept
Driver version string (from the generated mcp9700_version.h).
Definition mcp9700_thermistor.hpp:168
constexpr float kDefaultSpecifiedMaxCelsius
Default upper end of the specified temperature range (°C).
Definition mcp9700_thermistor.hpp:36
constexpr float kDefaultVoltsAtZeroC
Default output voltage at 0 °C (500 mV typical, MCP9700/MCP9700A).
Definition mcp9700_thermistor.hpp:22
Error
Why the last call failed (see Mcp9700Thermistor::LastError()).
Definition mcp9700_thermistor.hpp:48
@ 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 kDefaultVoltsPerCelsius
Default sensitivity (10 mV/°C typical).
Definition mcp9700_thermistor.hpp:25
constexpr float kDefaultSpecifiedMinCelsius
Default lower end of the specified temperature range (°C).
Definition mcp9700_thermistor.hpp:33
Definition mcp9700_thermistor.hpp:18