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
229 [[nodiscard]] int16_t readTemperature(const uint8_t idx) const
230 {
231 static ScratchPad buf;
232
233 if constexpr (TEMP_SENSOR_PRESENT)
234 {
235 if (!oneWire.reset())
236 {
238 }
239 oneWire.select(sensorAddrs[idx].addr);
241
242 for (auto& buf_elem : buf)
243 {
244 buf_elem = oneWire.read();
245 }
246
247 if (!oneWire.reset())
248 {
250 }
251 if (oneWire.crc8(buf, 8) != buf[8])
252 {
254 }
255 }
256
257 // result is temperature x16, multiply by 6.25 to convert to temperature x100
258 int16_t result = (buf[1] << 8) | buf[0];
259 result = (result * 6) + (result >> 2);
260 if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
261 {
262 return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
263 }
264
265 return result;
266 }
267
268private:
269 const uint8_t sensorPin{ unused_pin };
270
272
273 static inline OneWireType oneWire;
274};
275
276#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:271
constexpr auto get_size() const
Get the number of sensors.
Definition utils_temp.h:196
static OneWireType oneWire
Definition utils_temp.h:273
void initTemperatureSensors() const
Initialize the Dallas temperature sensors.
Definition utils_temp.h:179
const uint8_t sensorPin
Definition utils_temp.h:269
int16_t readTemperature(const uint8_t idx) const
Reads the temperature of a specific sensor.
Definition utils_temp.h:229
constexpr auto get_pin() const
Get the pin of the sensor(s).
Definition utils_temp.h:208
Configuration values to be set by the end-user.
constexpr bool TEMP_SENSOR_PRESENT
Definition config.h:42
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