3-phase PV router
Loading...
Searching...
No Matches
utils_temp.h
Go to the documentation of this file.
1
21
22#ifndef UTILS_TEMP_H
23#define UTILS_TEMP_H
24
25#include <Arduino.h>
26
27#include "constants.h"
28#include "config.h"
29
30#if TEMP_SENSOR_PRESENT
31#include <OneWire.h> // for temperature sensing
32#endif
33
48{
49 uint8_t addr[8]{};
50};
51
52// Mock class for OneWire
54class MockOneWire
55{
56public:
57 // Mock constructor
58 MockOneWire() = default;
59
60 void begin(uint8_t) {}
61
62 bool reset()
63 {
64 return true;
65 }
66
67 void skip() {}
68
69 void select(const uint8_t*) {}
70
71 void write(uint8_t) {}
72
73 uint8_t read()
74 {
75 return 0x00;
76 }
77
78 uint8_t crc8(const uint8_t*, uint8_t)
79 {
80 return 0x00;
81 }
82};
84
85// Include the real OneWire library if needed
86#if TEMP_SENSOR_PRESENT
87#include <OneWire.h> // for temperature sensing
88using OneWireType = OneWire; // Use the real implementation
89#else
90using OneWireType = MockOneWire; // Use the mock implementation
91#endif
92
110template< uint8_t N >
112{
126 using ScratchPad = uint8_t[9];
127
128public:
129 constexpr TemperatureSensing() = delete;
130
141 constexpr TemperatureSensing(uint8_t pin, const DeviceAddress (&ref)[N])
142 : sensorPin{ pin }, sensorAddrs(ref)
143 {
144 }
145
158 {
159 if constexpr (TEMP_SENSOR_PRESENT)
160 {
161 oneWire.reset();
162 oneWire.skip();
164 }
165 }
166
180 {
181 if constexpr (TEMP_SENSOR_PRESENT)
182 {
183 oneWire.begin(sensorPin);
185 }
186 }
187
196 constexpr auto get_size() const
197 {
198 return N;
199 }
200
208 constexpr auto get_pin() const
209 {
210 return sensorPin;
211 }
212
224 template< uint8_t IDX >
225 [[nodiscard]] int16_t readTemperature() const
226 {
227 static_assert(IDX < N, "Sensor index out of bounds! Check your sensor configuration.");
228 return readTemperature(IDX);
229 }
230
248 [[nodiscard]] int16_t readTemperature(const uint8_t idx) const
249 {
250 static ScratchPad buf;
251
252 if constexpr (TEMP_SENSOR_PRESENT)
253 {
254 if (!oneWire.reset())
255 {
257 }
258 oneWire.select(sensorAddrs[idx].addr);
260
261 for (auto& buf_elem : buf)
262 {
263 buf_elem = oneWire.read();
264 }
265
266 if (!oneWire.reset())
267 {
269 }
270 if (oneWire.crc8(buf, 8) != buf[8])
271 {
273 }
274 }
275 else
276 {
277 // Suppress unused parameter warning when temperature sensing is disabled
278 (void)idx;
279 }
280
281 // result is temperature x16, multiply by 6.25 to convert to temperature x100
282 int16_t result = (buf[1] << 8) | buf[0];
283 result = (result * 6) + (result >> 2);
284 if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
285 {
286 return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
287 }
288
289 return result;
290 }
291
292private:
293 const uint8_t sensorPin{ unused_pin };
294
296
297 static inline OneWireType oneWire;
298};
299
300#endif /* UTILS_TEMP_H */
void requestTemperatures() const
Request temperature conversion for all sensors.
Definition utils_temp.h:157
constexpr TemperatureSensing()=delete
constexpr TemperatureSensing(uint8_t pin, const DeviceAddress(&ref)[N])
Construct a new Temperature Sensing object.
Definition utils_temp.h:141
const DeviceAddress sensorAddrs[N]
Definition utils_temp.h:295
constexpr auto get_size() const
Get the number of sensors.
Definition utils_temp.h:196
static OneWireType oneWire
Definition utils_temp.h:297
void initTemperatureSensors() const
Initialize the Dallas temperature sensors.
Definition utils_temp.h:179
const uint8_t sensorPin
Definition utils_temp.h:293
int16_t readTemperature(const uint8_t idx) const
Reads the temperature of a specific sensor (runtime bounds checking).
Definition utils_temp.h:248
int16_t readTemperature() const
Read temperature from a specific sensor (compile-time bounds checking).
Definition utils_temp.h:225
constexpr auto get_pin() const
Get the pin of the sensor(s).
Definition utils_temp.h:208
Standard three-phase PVRouter setup with 2 dump loads.
constexpr bool TEMP_SENSOR_PRESENT
Definition config.h:74
Some constants.
constexpr uint8_t CONVERT_TEMPERATURE
Definition constants.h:18
constexpr int16_t DEVICE_DISCONNECTED_RAW
Definition constants.h:27
constexpr uint8_t READ_SCRATCHPAD
Definition constants.h:19
constexpr int16_t OUTOFRANGE_TEMPERATURE
Definition constants.h:22
constexpr int16_t TEMP_RANGE_HIGH
Definition constants.h:24
uint8_t[9] ScratchPad
Represents a buffer for storing sensor data.
Definition utils_temp.h:126
Structure representing the address of a device.
Definition utils_temp.h:48
uint8_t addr[8]
Definition utils_temp.h:49
constexpr uint8_t unused_pin
Definition utils_pins.h:17
MockOneWire OneWireType
Definition utils_temp.h:90