HardFOC Flux V1 HAL
Unified HAL API for the HardFOC Flux valve-control platform (ESP32-S3)
Loading...
Searching...
No Matches
TemperatureManager.h
Go to the documentation of this file.
1
21#ifndef FLUX_TEMPERATURE_MANAGER_H_
22#define FLUX_TEMPERATURE_MANAGER_H_
23
24#include <array>
25#include <atomic>
26#include <cstdint>
27#include <optional>
28#include <string_view>
29
30#include "core/hf-core-drivers/internal/hf-internal-interface-wrap/inc/base/BaseTemperature.h"
31#include "handlers/mcp9700/Mcp9700TemperatureHandler.h"
32#include "handlers/ntc/NtcTemperatureHandler.h"
33#include "handlers/logger/Logger.h"
34#include "RtosMutex.h"
36
37//==============================================================================
38// TEMPERATURE ERROR CODES
39//==============================================================================
40
60
66constexpr const char* TemperatureErrorToString(TemperatureError error) noexcept {
67 switch (error) {
68 case TemperatureError::SUCCESS: return "Success";
69 case TemperatureError::NOT_INITIALIZED: return "Not initialized";
70 case TemperatureError::INITIALIZATION_FAILED: return "Initialization failed";
71 case TemperatureError::SENSOR_NOT_FOUND: return "Sensor not found";
72 case TemperatureError::SENSOR_NOT_AVAILABLE: return "Sensor not available";
73 case TemperatureError::READ_FAILED: return "Read failed";
74 case TemperatureError::INVALID_PARAMETER: return "Invalid parameter";
75 case TemperatureError::NO_SENSORS_AVAILABLE: return "No sensors available";
76 case TemperatureError::ALLOCATION_FAILED: return "Allocation failed";
77 case TemperatureError::DEPENDENCY_NOT_READY: return "Dependency not ready";
78 case TemperatureError::MUTEX_LOCK_FAILED: return "Mutex lock failed";
79 default: return "Unknown error";
80 }
81}
82
83//==============================================================================
84// SENSOR TYPE ENUM
85//==============================================================================
86
90enum class FluxTempSensorType : uint8_t {
91 ESP32_INTERNAL = 0,
92 NTC_THERMISTOR = 1,
95};
96
102constexpr const char* FluxTempSensorTypeToString(FluxTempSensorType type) noexcept {
103 switch (type) {
104 case FluxTempSensorType::ESP32_INTERNAL: return "ESP32 Internal";
105 case FluxTempSensorType::NTC_THERMISTOR: return "NTC Thermistor";
106 case FluxTempSensorType::MCP9700_THERMISTOR: return "MCP9700 Thermistor";
107 default: return "Unknown";
108 }
109}
110
111//==============================================================================
112// TEMPERATURE ENTRY — stack-allocated metadata (drivers owned via unique_ptr)
113//==============================================================================
114
118struct TempEntry {
119 std::string_view name;
121 bool registered{false};
123
124 // Statistics
125 uint32_t read_count{0};
126 uint32_t error_count{0};
127};
128
129//==============================================================================
130// TEMPERATURE DIAGNOSTICS
131//==============================================================================
132
151
152//==============================================================================
153// TEMPERATURE MANAGER
154//==============================================================================
155
169public:
170 //==========================================================================
171 // SINGLETON AND LIFECYCLE
172 //==========================================================================
173
175 static TemperatureManager& GetInstance() noexcept;
176
181
186 [[nodiscard]] bool EnsureInitialized() noexcept;
187
192 [[nodiscard]] bool IsInitialized() const noexcept { return initialized_.load(std::memory_order_acquire); }
193
198 [[nodiscard]] bool Deinitialize() noexcept;
199
204 [[nodiscard]] TemperatureError GetLastError() const noexcept { return last_error_.load(std::memory_order_acquire); }
205
210 [[nodiscard]] flux::hal::ManagerError GetManagerError() const noexcept {
211 return manager_error_.load(std::memory_order_acquire);
212 }
213
218 [[nodiscard]] bool IsSensorReady(FluxTempSensorType type) const noexcept;
219
226 [[nodiscard]] bool IsDegraded() const noexcept {
228 }
229
230 //==========================================================================
231 // TEMPERATURE READING
232 //==========================================================================
233
240 [[nodiscard]] TemperatureError ReadTemperatureCelsius(std::string_view name, float& temperature_c) noexcept;
241
248 [[nodiscard]] TemperatureError ReadTemperatureFahrenheit(std::string_view name, float& temperature_f) noexcept;
249
255 [[nodiscard]] TemperatureError ReadInternalTemperature(float& temperature_c) noexcept;
256
262 [[nodiscard]] TemperatureError ReadNtcTemperature(float& temperature_c) noexcept;
263
269 [[nodiscard]] TemperatureError ReadMcp9700Temperature(float& temperature_c) noexcept;
270
278 [[nodiscard]] TemperatureError GetSystemTemperatureStats(float& min_c, float& max_c, float& avg_c) noexcept;
279
280 //==========================================================================
281 // SENSOR INFO
282 //==========================================================================
283
285 [[nodiscard]] size_t GetSensorCount() const noexcept { return registered_count_; }
286
292 [[nodiscard]] const TempEntry* GetSensorEntry(std::string_view name) const noexcept;
293
294 //==========================================================================
295 // DIAGNOSTICS
296 //==========================================================================
297
303 [[nodiscard]] TemperatureError GetSystemDiagnostics(TempSystemDiagnostics& diag) const noexcept;
304
306 void DumpStatistics() const noexcept;
307
308private:
309 TemperatureManager() noexcept = default;
310 ~TemperatureManager() = default;
311
312 bool Initialize() noexcept;
313 void ReleaseLocked() noexcept;
314 bool InitializeEsp32Internal() noexcept;
315 bool InitializeNtcThermistor() noexcept;
316 bool InitializeMcp9700Thermistor() noexcept;
317
318 [[nodiscard]] size_t FindByName(std::string_view name) const noexcept;
319
320 //==========================================================================
321 // STORAGE
322 //==========================================================================
323
324 static constexpr size_t kMaxSensors = static_cast<size_t>(FluxTempSensorType::SENSOR_COUNT);
325 static constexpr size_t kInvalidIndex = SIZE_MAX;
326
329 uint64_t init_time_ms_{0};
330
331 // Sensor driver instances. The MCU die sensor is owned by the platform binding;
332 // the two thermistor handlers are members, constructed in place at bring-up (no heap).
333 BaseTemperature* die_temp_{nullptr};
334 std::optional<NtcTemperatureHandler> ntc_handler_;
335 std::optional<Mcp9700TemperatureHandler> mcp9700_handler_;
336
337 std::atomic<bool> initialized_{false};
338 std::atomic<TemperatureError> last_error_{TemperatureError::SUCCESS};
339 std::atomic<flux::hal::ManagerError> manager_error_{flux::hal::ManagerError::NotInitialized};
340 mutable RtosMutex mutex_;
341};
342
343//==============================================================================
344// CONVENIENCE
345//==============================================================================
346
351[[nodiscard]] inline TemperatureManager& GetTemperatureManager() noexcept {
353}
354
355#endif // FLUX_TEMPERATURE_MANAGER_H_
TemperatureManager & GetTemperatureManager() noexcept
Convenience accessor — equivalent to TemperatureManager::GetInstance().
Definition TemperatureManager.h:351
TemperatureError
Temperature manager error codes for consistent error reporting.
Definition TemperatureManager.h:47
@ NOT_INITIALIZED
Manager not yet initialised.
@ MUTEX_LOCK_FAILED
RTOS mutex acquire timed out.
@ DEPENDENCY_NOT_READY
Required dependency not initialised.
@ SENSOR_NOT_AVAILABLE
Sensor exists but is unavailable.
@ SENSOR_NOT_FOUND
Named sensor does not exist in registry.
@ READ_FAILED
Driver-level read returned an error.
@ NO_SENSORS_AVAILABLE
No sensors were registered at init.
@ SUCCESS
Operation completed successfully.
@ ALLOCATION_FAILED
Heap allocation for a sensor driver failed.
@ INITIALIZATION_FAILED
Sensor initialisation failed.
@ INVALID_PARAMETER
Null pointer or out-of-range argument.
constexpr const char * FluxTempSensorTypeToString(FluxTempSensorType type) noexcept
Convert FluxTempSensorType to a human-readable string.
Definition TemperatureManager.h:102
FluxTempSensorType
Identifies the physical temperature sensor type on the Flux board.
Definition TemperatureManager.h:90
@ SENSOR_COUNT
Number of sensor types (not a valid sensor)
@ MCP9700_THERMISTOR
MCP9700 on ADS7952 multiplexed channel.
@ ESP32_INTERNAL
ESP32 on-chip temperature sensor.
@ NTC_THERMISTOR
External NTC thermistor via ADC.
constexpr const char * TemperatureErrorToString(TemperatureError error) noexcept
Convert TemperatureError to string for debugging.
Definition TemperatureManager.h:66
Singleton manager for all temperature sensors on Flux V1.
Definition TemperatureManager.h:168
TemperatureError GetSystemDiagnostics(TempSystemDiagnostics &diag) const noexcept
Populate a TempSystemDiagnostics snapshot.
Definition TemperatureManager.cpp:370
RtosMutex mutex_
Definition TemperatureManager.h:340
size_t FindByName(std::string_view name) const noexcept
Definition TemperatureManager.cpp:254
TemperatureError GetSystemTemperatureStats(float &min_c, float &max_c, float &avg_c) noexcept
Read all sensors and compute min, max, and average temperatures.
Definition TemperatureManager.cpp:337
TemperatureError ReadTemperatureFahrenheit(std::string_view name, float &temperature_f) noexcept
Read temperature in Fahrenheit from a named sensor.
Definition TemperatureManager.cpp:317
std::optional< Mcp9700TemperatureHandler > mcp9700_handler_
Definition TemperatureManager.h:335
static constexpr size_t kMaxSensors
Definition TemperatureManager.h:324
static TemperatureManager & GetInstance() noexcept
Return the singleton TemperatureManager instance.
Definition TemperatureManager.cpp:43
flux::hal::ManagerError GetManagerError() const noexcept
Lifecycle / bind error of the last EnsureInitialized() (shared catalog).
Definition TemperatureManager.h:210
TemperatureError ReadMcp9700Temperature(float &temperature_c) noexcept
Convenience: read the MCP9700 sensor (ADS7952 CH8).
Definition TemperatureManager.cpp:333
std::atomic< TemperatureError > last_error_
Definition TemperatureManager.h:338
TemperatureManager & operator=(const TemperatureManager &)=delete
bool InitializeNtcThermistor() noexcept
Definition TemperatureManager.cpp:155
bool IsSensorReady(FluxTempSensorType type) const noexcept
Whether one sensor came up during the last bring-up attempt (valid for diagnosis even when the manage...
Definition TemperatureManager.cpp:64
void ReleaseLocked() noexcept
Definition TemperatureManager.cpp:73
uint64_t init_time_ms_
Timestamp (ms) when Initialize() completed.
Definition TemperatureManager.h:329
TemperatureError ReadTemperatureCelsius(std::string_view name, float &temperature_c) noexcept
Read temperature in Celsius from a named sensor.
Definition TemperatureManager.cpp:271
bool InitializeEsp32Internal() noexcept
Definition TemperatureManager.cpp:131
TemperatureManager(const TemperatureManager &)=delete
BaseTemperature * die_temp_
Definition TemperatureManager.h:333
std::optional< NtcTemperatureHandler > ntc_handler_
Definition TemperatureManager.h:334
bool IsInitialized() const noexcept
Check whether the temperature subsystem has been initialised.
Definition TemperatureManager.h:192
size_t registered_count_
Definition TemperatureManager.h:328
size_t GetSensorCount() const noexcept
Get the number of registered temperature sensors.
Definition TemperatureManager.h:285
TemperatureManager(TemperatureManager &&)=delete
std::array< TempEntry, kMaxSensors > entries_
Definition TemperatureManager.h:327
TemperatureError ReadInternalTemperature(float &temperature_c) noexcept
Convenience: read the ESP32 internal temperature sensor.
Definition TemperatureManager.cpp:325
void DumpStatistics() const noexcept
Log temperature sensor statistics and health info to the console.
Definition TemperatureManager.cpp:414
bool Deinitialize() noexcept
Release all temperature sensor resources and reset to uninitialized state.
Definition TemperatureManager.cpp:434
TemperatureManager & operator=(TemperatureManager &&)=delete
bool EnsureInitialized() noexcept
Initialise the temperature subsystem if not already initialised.
Definition TemperatureManager.cpp:52
TemperatureError GetLastError() const noexcept
Get the most recent error code.
Definition TemperatureManager.h:204
std::atomic< flux::hal::ManagerError > manager_error_
Definition TemperatureManager.h:339
TemperatureError ReadNtcTemperature(float &temperature_c) noexcept
Convenience: read the NTC thermistor temperature.
Definition TemperatureManager.cpp:329
bool IsDegraded() const noexcept
Initialised with at least one fitted sensor missing.
Definition TemperatureManager.h:226
bool InitializeMcp9700Thermistor() noexcept
Definition TemperatureManager.cpp:205
static constexpr size_t kInvalidIndex
Definition TemperatureManager.h:325
const TempEntry * GetSensorEntry(std::string_view name) const noexcept
Look up a sensor entry by name.
Definition TemperatureManager.cpp:261
std::atomic< bool > initialized_
Definition TemperatureManager.h:337
bool Initialize() noexcept
Definition TemperatureManager.cpp:86
Shared HAL types for Flux V1: the manager error catalog and the facade result.
ManagerError
Lifecycle / bind failure catalog shared by every manager (GetManagerError()).
Definition flux_hal_types.hpp:37
@ None
No error recorded.
@ NotInitialized
Manager not initialised (or shut down)
A single temperature sensor entry in the fixed-size registry.
Definition TemperatureManager.h:118
uint32_t error_count
Cumulative failed reads.
Definition TemperatureManager.h:126
uint32_t read_count
Cumulative successful reads.
Definition TemperatureManager.h:125
FluxTempSensorType sensor_type
Physical sensor type.
Definition TemperatureManager.h:120
std::string_view name
Human-readable name.
Definition TemperatureManager.h:119
bool registered
Whether this entry is active.
Definition TemperatureManager.h:121
float last_reading_celsius
Most recent cached reading in °C.
Definition TemperatureManager.h:122
Temperature subsystem diagnostics snapshot (cross-HAL compatible format).
Definition TemperatureManager.h:136
uint32_t sensors_by_type[8]
Count of sensors per FluxTempSensorType index.
Definition TemperatureManager.h:139
uint32_t hardware_errors
Errors attributed to hardware faults.
Definition TemperatureManager.h:144
uint32_t total_operations
Cumulative read attempts across all sensors.
Definition TemperatureManager.h:140
float system_avg_temp_celsius
Average temperature across all sensors (°C)
Definition TemperatureManager.h:149
uint64_t system_uptime_ms
Milliseconds since temperature subsystem init.
Definition TemperatureManager.h:145
uint32_t failed_operations
Cumulative failed reads.
Definition TemperatureManager.h:142
uint32_t successful_operations
Cumulative successful reads.
Definition TemperatureManager.h:141
bool system_healthy
True when all registered sensors are responsive.
Definition TemperatureManager.h:137
TemperatureError last_error
Most recent error code from any operation.
Definition TemperatureManager.h:146
float system_max_temp_celsius
Maximum temperature across all sensors (°C)
Definition TemperatureManager.h:148
uint32_t total_sensors_registered
Number of sensors currently registered.
Definition TemperatureManager.h:138
uint32_t communication_errors
Errors attributed to bus communication failures.
Definition TemperatureManager.h:143
float system_min_temp_celsius
Minimum temperature across all sensors (°C)
Definition TemperatureManager.h:147