1-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
34class OneWire;
35
50{
51 uint8_t addr[8]{};
52};
53
54// Mock class for OneWire
56class MockOneWire
57{
58public:
59 // Mock constructor
60 MockOneWire() = default;
61
62 void begin(uint8_t) {}
63
64 bool reset()
65 {
66 return true;
67 }
68
69 void skip() {}
70
71 void select(const uint8_t*) {}
72
73 void write(uint8_t) {}
74
75 uint8_t read()
76 {
77 return 0x00;
78 }
79
80 uint8_t crc8(const uint8_t*, uint8_t)
81 {
82 return 0x00;
83 }
84};
86
104template< uint8_t N >
106{
120 using ScratchPad = uint8_t[9];
121
122public:
123 constexpr TemperatureSensing() = delete;
124
135 constexpr TemperatureSensing(uint8_t pin, const DeviceAddress (&ref)[N])
136 : sensorPin{ pin }, sensorAddrs(ref)
137 {
138 }
139
152 {
153 if constexpr (TEMP_SENSOR_PRESENT)
154 {
155 oneWire.reset();
156 oneWire.skip();
158 }
159 }
160
174 {
175 if constexpr (TEMP_SENSOR_PRESENT)
176 {
177 oneWire.begin(sensorPin);
179 }
180 }
181
190 constexpr auto get_size() const
191 {
192 return N;
193 }
194
202 constexpr auto get_pin() const
203 {
204 return sensorPin;
205 }
206
223 [[nodiscard]] int16_t readTemperature(const uint8_t idx) const
224 {
225 static ScratchPad buf;
226
227 if constexpr (TEMP_SENSOR_PRESENT)
228 {
229 if (!oneWire.reset())
230 {
232 }
233 oneWire.select(sensorAddrs[idx].addr);
235
236 for (auto& buf_elem : buf)
237 {
238 buf_elem = oneWire.read();
239 }
240
241 if (!oneWire.reset())
242 {
244 }
245 if (oneWire.crc8(buf, 8) != buf[8])
246 {
248 }
249 }
250
251 // result is temperature x16, multiply by 6.25 to convert to temperature x100
252 int16_t result = (buf[1] << 8) | buf[0];
253 result = (result * 6) + (result >> 2);
254 if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
255 {
256 return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
257 }
258
259 return result;
260 }
261
262private:
263 const uint8_t sensorPin{ unused_pin };
264
266
268};
269
270#endif /* UTILS_TEMP_H */
void requestTemperatures() const
Request temperature conversion for all sensors.
Definition utils_temp.h:151
constexpr TemperatureSensing()=delete
constexpr TemperatureSensing(uint8_t pin, const DeviceAddress(&ref)[N])
Construct a new Temperature Sensing object.
Definition utils_temp.h:135
const DeviceAddress sensorAddrs[N]
Definition utils_temp.h:265
constexpr auto get_size() const
Get the number of sensors.
Definition utils_temp.h:190
void initTemperatureSensors() const
Initialize the Dallas temperature sensors.
Definition utils_temp.h:173
const uint8_t sensorPin
Definition utils_temp.h:263
int16_t readTemperature(const uint8_t idx) const
Reads the temperature of a specific sensor.
Definition utils_temp.h:223
static conditional< TEMP_SENSOR_PRESENT, OneWire, MockOneWire >::type oneWire
Definition utils_temp.h:267
constexpr auto get_pin() const
Get the pin of the sensor(s).
Definition utils_temp.h:202
Configuration values to be set by the end-user.
constexpr bool TEMP_SENSOR_PRESENT
Definition config.h:45
Some constants.
constexpr uint8_t CONVERT_TEMPERATURE
Definition constants.h:16
constexpr int16_t DEVICE_DISCONNECTED_RAW
Definition constants.h:25
constexpr uint8_t READ_SCRATCHPAD
Definition constants.h:17
constexpr int16_t OUTOFRANGE_TEMPERATURE
Definition constants.h:20
constexpr int16_t TEMP_RANGE_HIGH
Definition constants.h:22
uint8_t[9] ScratchPad
Represents a buffer for storing sensor data.
Definition utils_temp.h:120
Structure representing the address of a device.
Definition utils_temp.h:50
uint8_t addr[8]
Definition utils_temp.h:51
TrueType type
constexpr uint8_t unused_pin
Definition utils_pins.h:17