NTC Thermistor Driver
API reference
Loading...
Searching...
No Matches
ntc_thermistor.hpp
Go to the documentation of this file.
1
6#pragma once
7#include <cstdint>
8#include <memory>
9
10#include "ntc_adc_interface.hpp"
11#include "ntc_thermistor_version.h"
12#include "ntc_types.hpp"
13
14//--------------------------------------
15// NtcThermistor Class
16//--------------------------------------
17
75template <typename AdcType> class NtcThermistor {
76public:
77 //==============================================================//
78 // CONSTRUCTORS AND DESTRUCTOR
79 //==============================================================//
80
97 NtcThermistor(NtcType ntc_type, AdcType *adc_interface) noexcept;
98
105 NtcThermistor(const ntc_config_t &config, AdcType *adc_interface) noexcept;
106
110 NtcThermistor(const NtcThermistor &) = delete;
111
116
120 NtcThermistor(NtcThermistor &&) noexcept = default;
121
125 NtcThermistor &operator=(NtcThermistor &&) noexcept = default;
126
130 virtual ~NtcThermistor() noexcept = default;
131
132 //==============================================================//
133 // INITIALIZATION AND CONFIGURATION
134 //==============================================================//
135
140 bool Initialize() noexcept;
141
146 bool Deinitialize() noexcept;
147
152 [[nodiscard]] bool IsInitialized() const noexcept;
153
159 NtcError GetConfiguration(ntc_config_t *config) const noexcept;
160
166 NtcError SetConfiguration(const ntc_config_t &config) noexcept;
167
168 //==============================================================//
169 // TEMPERATURE READING
170 //==============================================================//
171
188 NtcError ReadTemperatureCelsius(float *temperature_celsius) noexcept;
189
195 NtcError ReadTemperatureFahrenheit(float *temperature_fahrenheit) noexcept;
196
202 NtcError ReadTemperatureKelvin(float *temperature_kelvin) noexcept;
203
209 NtcError ReadTemperature(ntc_reading_t *reading) noexcept;
210
211 //==============================================================//
212 // RESISTANCE AND VOLTAGE
213 //==============================================================//
214
220 NtcError GetResistance(float *resistance_ohms) noexcept;
221
227 NtcError GetVoltage(float *voltage_volts) noexcept;
228
234 NtcError GetRawAdcValue(uint32_t *adc_value) noexcept;
235
236 //==============================================================//
237 // CALIBRATION
238 //==============================================================//
239
250 NtcError Calibrate(float reference_temperature_celsius) noexcept;
251
257 NtcError SetCalibrationOffset(float offset_celsius) noexcept;
258
264 NtcError GetCalibrationOffset(float *offset_celsius) const noexcept;
265
270 NtcError ResetCalibration() noexcept;
271
272 //==============================================================//
273 // CONFIGURATION METHODS
274 //==============================================================//
275
282
288 NtcError SetVoltageDivider(float series_resistance) noexcept;
289
295 NtcError SetReferenceVoltage(float reference_voltage) noexcept;
296
302 NtcError SetBetaValue(float beta_value) noexcept;
303
309 NtcError SetAdcChannel(uint8_t adc_channel) noexcept;
310
318 NtcError SetSamplingParameters(uint32_t sample_count,
319 uint32_t sample_delay_ms) noexcept;
320
327 NtcError SetFiltering(bool enable, float alpha = 0.1F) noexcept;
328
329 //==============================================================//
330 // UTILITY FUNCTIONS
331 //==============================================================//
332
338 static float CelsiusToFahrenheit(float celsius) noexcept;
339
345 static float FahrenheitToCelsius(float fahrenheit) noexcept;
346
352 static float CelsiusToKelvin(float celsius) noexcept;
353
359 static float KelvinToCelsius(float kelvin) noexcept;
360
366 static const char *GetErrorString(NtcError error) noexcept;
367
373 static const char *GetTypeString(NtcType type) noexcept;
374
375 // ===========================================================================
376 // Driver Version
377 // ===========================================================================
378
380 static constexpr const char* GetDriverVersion() noexcept {
381 return HF_NTC_THERMISTOR_VERSION_STRING;
382 }
383
385 static constexpr uint8_t GetDriverVersionMajor() noexcept {
386 return HF_NTC_THERMISTOR_VERSION_MAJOR;
387 }
388
390 static constexpr uint8_t GetDriverVersionMinor() noexcept {
391 return HF_NTC_THERMISTOR_VERSION_MINOR;
392 }
393
395 static constexpr uint8_t GetDriverVersionPatch() noexcept {
396 return HF_NTC_THERMISTOR_VERSION_PATCH;
397 }
398
399private:
400 //==============================================================//
401 // PRIVATE MEMBER VARIABLES
402 //==============================================================//
403
404 ntc_config_t config_;
405 AdcType *adc_interface_;
406 bool initialized_;
407
408 // Filtering
409 float filtered_temperature_;
410 bool filter_initialized_;
411
412 //==============================================================//
413 // PRIVATE HELPER METHODS
414 //==============================================================//
415
421 [[nodiscard]] NtcError
422 validateConfiguration(const ntc_config_t &config) const noexcept;
423
429 NtcError readAdcVoltage(float *voltage_volts) noexcept;
430
437 NtcError calculateResistance(float voltage_volts,
438 float *resistance_ohms) noexcept;
439
446 NtcError convertResistanceToTemperature(float resistance_ohms,
447 float *temperature_celsius) noexcept;
448
454 float applyFiltering(float new_temperature) noexcept;
455
460 void delayBetweenSamples() noexcept;
461
467 static void initializeConfigForType(NtcType ntc_type,
468 ntc_config_t *config) noexcept;
469};
470
471// =============================================================================
472// Free-function version accessor (no namespace — class is at global scope)
473// =============================================================================
474
476inline const char* GetNtcThermistorDriverVersion() noexcept {
477 return HF_NTC_THERMISTOR_VERSION_STRING;
478}
479
480// Include template implementation
481#define NTC_THERMISTOR_HEADER_INCLUDED
482// NOLINTNEXTLINE(bugprone-suspicious-include) - Template implementation file
483#include "../src/ntc_thermistor.ipp"
484#undef NTC_THERMISTOR_HEADER_INCLUDED
Hardware-agnostic NTC thermistor driver.
Definition ntc_thermistor.hpp:75
NtcError SetVoltageDivider(float series_resistance) noexcept
Set voltage divider parameters.
Definition ntc_thermistor.ipp:438
NtcError SetBetaValue(float beta_value) noexcept
Set beta value.
Definition ntc_thermistor.ipp:459
NtcError GetVoltage(float *voltage_volts) noexcept
Get voltage across thermistor.
Definition ntc_thermistor.ipp:291
NtcError SetFiltering(bool enable, float alpha=0.1F) noexcept
Enable/disable filtering.
Definition ntc_thermistor.ipp:492
NtcError SetSamplingParameters(uint32_t sample_count, uint32_t sample_delay_ms) noexcept
Set sampling parameters.
Definition ntc_thermistor.ipp:480
static constexpr uint8_t GetDriverVersionPatch() noexcept
Get the compiled driver patch version number.
Definition ntc_thermistor.hpp:395
NtcError ReadTemperatureCelsius(float *temperature_celsius) noexcept
Read temperature in Celsius.
Definition ntc_thermistor.ipp:144
static float FahrenheitToCelsius(float fahrenheit) noexcept
Convert Fahrenheit to Celsius.
Definition ntc_thermistor.ipp:520
NtcError SetConfiguration(const ntc_config_t &config) noexcept
Set configuration.
Definition ntc_thermistor.ipp:123
static constexpr const char * GetDriverVersion() noexcept
Get the compiled driver version string.
Definition ntc_thermistor.hpp:380
NtcError SetReferenceVoltage(float reference_voltage) noexcept
Set reference voltage.
Definition ntc_thermistor.ipp:449
static float CelsiusToKelvin(float celsius) noexcept
Convert Celsius to Kelvin.
Definition ntc_thermistor.ipp:525
NtcThermistor(NtcThermistor &&) noexcept=default
Move constructor.
NtcError SetConversionMethod(NtcConversionMethod method) noexcept
Set conversion method.
Definition ntc_thermistor.ipp:430
static const char * GetTypeString(NtcType type) noexcept
Get NTC type string.
Definition ntc_thermistor.ipp:575
NtcError ReadTemperatureFahrenheit(float *temperature_fahrenheit) noexcept
Read temperature in Fahrenheit.
Definition ntc_thermistor.ipp:195
NtcError SetCalibrationOffset(float offset_celsius) noexcept
Set calibration offset.
Definition ntc_thermistor.ipp:404
NtcError GetCalibrationOffset(float *offset_celsius) const noexcept
Get calibration offset.
Definition ntc_thermistor.ipp:410
NtcError GetResistance(float *resistance_ohms) noexcept
Get thermistor resistance.
Definition ntc_thermistor.ipp:272
static float KelvinToCelsius(float kelvin) noexcept
Convert Kelvin to Celsius.
Definition ntc_thermistor.ipp:530
NtcError ReadTemperature(ntc_reading_t *reading) noexcept
Read complete temperature information.
Definition ntc_thermistor.ipp:226
bool IsInitialized() const noexcept
Check if initialized.
Definition ntc_thermistor.ipp:106
NtcError GetConfiguration(ntc_config_t *config) const noexcept
Get current configuration.
Definition ntc_thermistor.ipp:112
static const char * GetErrorString(NtcError error) noexcept
Get error string.
Definition ntc_thermistor.ipp:535
NtcError SetAdcChannel(uint8_t adc_channel) noexcept
Set ADC channel.
Definition ntc_thermistor.ipp:469
static float CelsiusToFahrenheit(float celsius) noexcept
Convert Celsius to Fahrenheit.
Definition ntc_thermistor.ipp:515
bool Initialize() noexcept
Initialize the NTC thermistor driver.
Definition ntc_thermistor.ipp:56
NtcError GetRawAdcValue(uint32_t *adc_value) noexcept
Get raw ADC value.
Definition ntc_thermistor.ipp:304
NtcError ReadTemperatureKelvin(float *temperature_kelvin) noexcept
Read temperature in Kelvin.
Definition ntc_thermistor.ipp:210
static constexpr uint8_t GetDriverVersionMajor() noexcept
Get the compiled driver major version number.
Definition ntc_thermistor.hpp:385
NtcError ResetCalibration() noexcept
Reset calibration.
Definition ntc_thermistor.ipp:421
NtcThermistor(const NtcThermistor &)=delete
Copy constructor is deleted.
bool Deinitialize() noexcept
Deinitialize the NTC thermistor driver.
Definition ntc_thermistor.ipp:93
static constexpr uint8_t GetDriverVersionMinor() noexcept
Get the compiled driver minor version number.
Definition ntc_thermistor.hpp:390
NtcError Calibrate(float reference_temperature_celsius) noexcept
Calibrate the thermistor.
Definition ntc_thermistor.ipp:381
NtcThermistor & operator=(const NtcThermistor &)=delete
Assignment operator is deleted.
CRTP-based template interface for ADC used by NTC thermistor driver.
const char * GetNtcThermistorDriverVersion() noexcept
Get the NTC Thermistor driver version string (free function).
Definition ntc_thermistor.hpp:476
Type definitions for NTC thermistor driver.
NtcError
NTC thermistor error codes.
Definition ntc_types.hpp:24
NtcConversionMethod
Temperature conversion methods.
Definition ntc_types.hpp:82
NtcType
NTC thermistor types.
Definition ntc_types.hpp:58
NTC thermistor configuration structure.
Definition ntc_types.hpp:107
NTC thermistor reading structure.
Definition ntc_types.hpp:136