NTC_THERMISTOR Driver 0.1.0
API reference
Loading...
Searching...
No Matches
ntc_thermistor.hpp
Go to the documentation of this file.
1
14#ifndef NTC_THERMISTOR_HPP
15#define NTC_THERMISTOR_HPP
16
17#include <cstdint>
18
23#include "ntc_thermistor_version.h"
24
25namespace ntc_thermistor {
26
27namespace detail {
28
29// Optional hooks: a `Declares*` concept only tests the name, the `Has*` concept
30// the exact signature, `noexcept` and the result type; the driver rejects a
31// hook that is declared but does not match (handbook KB-CPP20-007).
32
34template <typename Port>
35concept DeclaresSetBias = requires(Port& port) { port.SetBias(true); };
36
38template <typename Port>
39concept HasSetBias = requires { ExactHook<Port, bool(bool) noexcept>::Take(&Port::SetBias); };
40
42template <typename Port>
43concept DeclaresDelayUs = requires(Port& port) { port.DelayUs(1_u32); };
44
46template <typename Port>
47concept HasDelayUs = requires { ExactHook<Port, void(uint32_t) noexcept>::Take(&Port::DelayUs); };
48
49} // namespace detail
50
73template <AdcPort Port>
75 static_assert(!detail::DeclaresSetBias<Port> || detail::HasSetBias<Port>,
76 "ntc_thermistor: the optional SetBias hook must be `bool "
77 "SetBias(bool on) "
78 "noexcept`, so that a failed pin write is reported");
79 static_assert(!detail::DeclaresDelayUs<Port> || detail::HasDelayUs<Port>,
80 "ntc_thermistor: the DelayUs hook must be `void "
81 "DelayUs(uint32_t us) noexcept`, "
82 "so that no wait is truncated");
83
84public:
93 NtcThermistor(Port& port, const Config& config) noexcept : port_(port), config_(config) {}
94
96 NtcThermistor(const NtcThermistor&) = delete;
99
113
115 [[nodiscard]] bool IsInitialized() const noexcept {
116 return initialized_;
117 }
118
126 [[nodiscard]] Result<Reading> Read() noexcept;
127
133 [[nodiscard]] Result<uint32_t> ReadResistance() noexcept;
134
143 [[nodiscard]] Result<uint32_t> ReadRawCode() noexcept;
144
152 Result<void> SetConfig(const Config& config) noexcept;
153
155 [[nodiscard]] const Config& GetConfig() const& noexcept {
156 return config_;
157 }
160 void GetConfig() const&& = delete;
161
166 [[nodiscard]] uint32_t GetLastCode() const noexcept {
167 return last_code_;
168 }
169
170private:
171 static Error check(const Config& config) noexcept;
172 Result<uint32_t> acquire(uint32_t count) noexcept;
173 Result<uint32_t> acquireBiased(uint32_t count) noexcept;
174 Result<uint32_t> sampleMean(uint32_t count) noexcept;
175
176 Port& port_;
177 Config config_;
178 bool initialized_{false};
179 uint32_t last_code_{0};
180};
181
184} // namespace ntc_thermistor
185
186// codeql::misra_deviation_next_line(MD-CPP-009)
187#include "../src/ntc_thermistor.ipp" // IWYU pragma: export
188
189#endif // NTC_THERMISTOR_HPP
Driver for one NTC thermistor in a resistor divider on an ADC port.
Definition ntc_thermistor.hpp:74
Result< void > EnsureInitialized() noexcept
Start the driver, once: the only way to initialise it.
bool IsInitialized() const noexcept
Definition ntc_thermistor.hpp:115
NtcThermistor(Port &port, const Config &config) noexcept
Bind the driver to a port and store the configuration. No I/O, cannot fail.
Definition ntc_thermistor.hpp:93
NtcThermistor(const NtcThermistor &)=delete
One driver object per thermistor.
Result< Reading > Read() noexcept
Read the temperature: bias on, settle, sample_count conversions, bias off, convert.
Result< void > SetConfig(const Config &config) noexcept
Replace the configuration. It is validated first; on failure the old one stays.
uint32_t GetLastCode() const noexcept
Definition ntc_thermistor.hpp:166
Result< uint32_t > ReadResistance() noexcept
Read the thermistor's resistance in ohms: the divider arithmetic only, no curve and no window.
Result< uint32_t > ReadRawCode() noexcept
One raw conversion, with no averaging and no conversion: bias on, settle, one sample,...
const Config & GetConfig() const &noexcept
Definition ntc_thermistor.hpp:155
void GetConfig() const &&=delete
Not on a temporary driver: the reference would dangle (handbook KB-CPP20-044).
NtcThermistor & operator=(const NtcThermistor &)=delete
One driver object per thermistor.
Error
Why a call failed. Values 0 to 9 are the shared base set; this driver's own values start at 16.
Definition ntc_thermistor_types.hpp:45
The NTC thermistor driver: everything it declares lives here; internals in detail.
Definition ntc_thermistor.hpp:25
The ADC port of the NTC thermistor driver: the CRTP base and the AdcPort concept.
From an ADC code to a temperature: the divider, the curve, the bound, and the helpers around them....
Compile-time helpers of the NTC_THERMISTOR driver: typed integer literals, exact hook matching and en...
Public types of the NTC thermistor driver: errors, Result<T>, the curve and the configuration,...
Everything one thermistor and its divider need. There is no default part: a Config{} fails Validate()...
Definition ntc_thermistor_types.hpp:284
The value of a call, or why there is none.
Definition ntc_thermistor_types.hpp:120