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#include <OneWire.h> // for temperature sensing (the bus type is chosen below, with TEMP_SENSOR_PRESENT)
31
46{
47 uint8_t addr[8]{};
48};
49
50// Mock class for OneWire
52class MockOneWire
53{
54public:
55 // Mock constructor
56 MockOneWire() = default;
57
58 void begin(uint8_t) {}
59
60 bool reset()
61 {
62 return true;
63 }
64
65 void skip() {}
66
67 void select(const uint8_t*) {}
68
69 void write(uint8_t) {}
70
71 uint8_t read()
72 {
73 return 0x00;
74 }
75
76 uint8_t crc8(const uint8_t*, uint8_t)
77 {
78 return 0x00;
79 }
80};
82
100template< uint8_t N >
102{
116 using ScratchPad = uint8_t[9];
117
118public:
119 constexpr TemperatureSensing() = delete;
120
131 constexpr TemperatureSensing(uint8_t pin, const DeviceAddress (&ref)[N])
132 : sensorPin{ pin }, sensorAddrs(ref)
133 {
134 }
135
148 {
149 if constexpr (TEMP_SENSOR_PRESENT)
150 {
151 oneWire.reset();
152 oneWire.skip();
154 }
155 }
156
170 {
171 if constexpr (TEMP_SENSOR_PRESENT)
172 {
173 oneWire.begin(sensorPin);
175 }
176 }
177
186 constexpr auto get_size() const
187 {
188 return N;
189 }
190
198 constexpr auto get_pin() const
199 {
200 return sensorPin;
201 }
202
219 [[nodiscard]] int16_t readTemperature(const uint8_t idx) const
220 {
221 static ScratchPad buf;
222
223 if constexpr (TEMP_SENSOR_PRESENT)
224 {
225 if (!oneWire.reset())
226 {
228 }
229 oneWire.select(sensorAddrs[idx].addr);
231
232 for (auto& buf_elem : buf)
233 {
234 buf_elem = oneWire.read();
235 }
236
237 if (!oneWire.reset())
238 {
240 }
241 if (oneWire.crc8(buf, 8) != buf[8])
242 {
244 }
245 }
246
247 // result is temperature x16, multiply by 6.25 to convert to temperature x100
248 int16_t result = (buf[1] << 8) | buf[0];
249 result = (result * 6) + (result >> 2);
250 if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
251 {
252 return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
253 }
254
255 return result;
256 }
257
258private:
259 const uint8_t sensorPin{ unused_pin };
260
262
264};
265
266#endif /* UTILS_TEMP_H */
void requestTemperatures() const
Request temperature conversion for all sensors.
Definition utils_temp.h:147
constexpr TemperatureSensing()=delete
constexpr TemperatureSensing(uint8_t pin, const DeviceAddress(&ref)[N])
Construct a new Temperature Sensing object.
Definition utils_temp.h:131
const DeviceAddress sensorAddrs[N]
Definition utils_temp.h:261
constexpr auto get_size() const
Get the number of sensors.
Definition utils_temp.h:186
void initTemperatureSensors() const
Initialize the Dallas temperature sensors.
Definition utils_temp.h:169
const uint8_t sensorPin
Definition utils_temp.h:259
int16_t readTemperature(const uint8_t idx) const
Reads the temperature of a specific sensor.
Definition utils_temp.h:219
static conditional< TEMP_SENSOR_PRESENT, OneWire, MockOneWire >::type oneWire
Definition utils_temp.h:263
constexpr auto get_pin() const
Get the pin of the sensor(s).
Definition utils_temp.h:198
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:116
Structure representing the address of a device.
Definition utils_temp.h:46
uint8_t addr[8]
Definition utils_temp.h:47
TrueType type
constexpr uint8_t unused_pin
Definition utils_pins.h:17