3-phase PV router
Loading...
Searching...
No Matches
utils_temp.h
Go to the documentation of this file.
1
24
25#ifndef UTILS_TEMP_H
26#define UTILS_TEMP_H
27
28#include <Arduino.h>
29
30#include "constants.h"
31#include "config.h"
32#include "debug.h"
33#include "ds18b20.h"
34#include "onewire_search.h"
35
36// Mock class for OneWire
38class MockOneWire
39{
40public:
41 // Mock constructor
42 MockOneWire() = default;
43
44 void begin(uint8_t) {}
45
46 bool reset()
47 {
48 return true;
49 }
50
51 void skip() {}
52
53 void select(const uint8_t*) {}
54
55 void write(uint8_t) {}
56
57 uint8_t read()
58 {
59 return 0x00;
60 }
61
62 void write_bit(uint8_t) {}
63
64 uint8_t read_bit()
65 {
66 return 0x01;
67 }
68};
70
71// The real OneWire bus only with temperature sensing. TEMP_SENSOR_PRESENT is a constexpr, not a
72// macro: the choice must be made in C++, an #if on it would always pick the mock.
73#include <OneWire.h> // for temperature sensing
75
95template< uint8_t N >
97{
100
101public:
107 explicit constexpr TempSensors(const TemperatureSensing< N >& sensors)
108 : TemperatureSensing< N >(sensors)
109 {
110 }
111
124 {
125 if constexpr (TEMP_SENSOR_PRESENT)
126 {
127 oneWire.reset();
128 oneWire.skip();
130 }
131 }
132
141 void printSensorsOnBus() const
142 {
143 if constexpr (TEMP_SENSOR_PRESENT && CALIBRATION_MODE)
144 {
145 oneWire.begin(sensorPin);
146
147 info(F("Temperature sensors found on pin "));
149 infoln(F(":"));
150
152 DeviceAddress found;
153 uint8_t count{ 0 };
154 // bounded: noise on a long or badly terminated bus could otherwise keep the search going
155 constexpr uint8_t MAX_LISTED{ 16 };
156 static_assert(MAX_LISTED == 16, "update the message below");
157 while (search.next(found.addr))
158 {
159 if (MAX_LISTED == count)
160 {
161 // a 17th device: exactly 16 sensors give no warning
162 infoln(F("\t... more than 16: noise on the bus? Check the wiring"));
163 break;
164 }
165 ++count;
166 info(F("\t{ "));
167 for (uint8_t i = 0; i != 8; ++i)
168 {
169 info(F("0x"));
170 if (found.addr[i] < 0x10)
171 {
172 info('0');
173 }
174 info(found.addr[i], HEX);
175 if (i != 7)
176 {
177 info(F(", "));
178 }
179 }
180 info(F(" }"));
181
182 if (DS18B20::crc8(found.addr, 7) != found.addr[7])
183 {
184 infoln(F(" CRC error: check the wiring"));
185 }
186 else
187 {
188 infoln(isConfigured(found) ? F(" in config.h") : F(" not in config.h"));
189 }
190 }
191 if (0 == count)
192 {
193 infoln(F("\tnone: check the wiring and the pull-up resistor"));
194 }
195 }
196 }
197
206 {
207 if constexpr (TEMP_SENSOR_PRESENT)
208 {
209 oneWire.begin(sensorPin);
210 }
211 }
212
220 [[nodiscard]] bool startRead(const uint8_t idx) const
221 {
222 if constexpr (!TEMP_SENSOR_PRESENT)
223 {
224 (void)idx;
225 return false;
226 }
227 else
228 {
229 if (!oneWire.reset())
230 {
231 return false;
232 }
233 oneWire.write(MATCH_ROM);
234 for (uint8_t i = 0; i != ROM_HALF; ++i)
235 {
236 oneWire.write(sensorAddrs[idx].addr[i]);
237 }
238 return true;
239 }
240 }
241
248 void readScratchpad(const uint8_t idx) const
249 {
250 if constexpr (!TEMP_SENSOR_PRESENT)
251 {
252 (void)idx;
253 }
254 else
255 {
256 for (uint8_t i = ROM_HALF; i != sizeof(sensorAddrs[idx].addr); ++i)
257 {
258 oneWire.write(sensorAddrs[idx].addr[i]);
259 }
261
262 for (auto& buf_elem : scratchPad)
263 {
264 buf_elem = oneWire.read();
265 }
266 }
267 }
268
276 [[nodiscard]] int16_t endRead() const
277 {
278 if constexpr (!TEMP_SENSOR_PRESENT)
279 {
280 return 0;
281 }
282 else
283 {
284 if (!oneWire.reset())
285 {
287 }
289 }
290 }
291
303 template< uint8_t IDX >
304 [[nodiscard]] int16_t readTemperature() const
305 {
306 static_assert(IDX < N, "Sensor index out of bounds! Check your sensor configuration.");
307 return readTemperature(IDX);
308 }
309
329 [[nodiscard]] int16_t readTemperature(const uint8_t idx) const
330 {
331 if (!startRead(idx))
332 {
334 }
335 readScratchpad(idx);
336 return endRead();
337 }
338
339private:
341 bool isConfigured(const DeviceAddress& device) const
342 {
343 for (uint8_t idx = 0; idx != N; ++idx)
344 {
345 if (0 == memcmp(sensorAddrs[idx].addr, device.addr, sizeof(device.addr)))
346 {
347 return true;
348 }
349 }
350 return false;
351 }
352
353 static constexpr uint8_t MATCH_ROM{ 0x55 };
354 static constexpr uint8_t ROM_HALF{ 4 };
355
356 static inline OneWireType oneWire;
358};
359
361template< uint8_t N >
363
364#endif /* UTILS_TEMP_H */
The successive searches of a 1-Wire bus, one device each.
Implements temperature sensing functionality for multiple sensors.
Definition utils_temp.h:97
bool isConfigured(const DeviceAddress &device) const
Whether config.h lists this sensor.
Definition utils_temp.h:341
int16_t readTemperature() const
Read temperature from a specific sensor (compile-time bounds checking).
Definition utils_temp.h:304
void initTemperatureSensors() const
Initialize the Dallas temperature sensors.
Definition utils_temp.h:205
void requestTemperatures() const
Request temperature conversion for all sensors.
Definition utils_temp.h:123
static constexpr uint8_t MATCH_ROM
Definition utils_temp.h:353
int16_t readTemperature(const uint8_t idx) const
Reads the temperature of a specific sensor (runtime bounds checking).
Definition utils_temp.h:329
void readScratchpad(const uint8_t idx) const
READ, the second transaction: the second half of the ROM code, Read Scratchpad, the 9 bytes of the sc...
Definition utils_temp.h:248
int16_t endRead() const
CLOSE, the last transaction: a reset (the sensor still there?), the reading decoded.
Definition utils_temp.h:276
constexpr TempSensors(const TemperatureSensing< N > &sensors)
The sensors of config.h.
Definition utils_temp.h:107
void printSensorsOnBus() const
Lists the sensors on the bus, with their ROM codes as config.h takes them.
Definition utils_temp.h:141
static constexpr uint8_t ROM_HALF
Definition utils_temp.h:354
static OneWireType oneWire
Definition utils_temp.h:356
bool startRead(const uint8_t idx) const
ADDRESS, the first transaction of a sensor's read: reset, Match ROM, the first half of its ROM code.
Definition utils_temp.h:220
static DS18B20::ScratchPad scratchPad
Definition utils_temp.h:357
The temperature sensors, as in config.h: the pin of the bus and the address of each sensor.
constexpr TemperatureSensing()=delete
const DeviceAddress sensorAddrs[N]
const uint8_t sensorPin
typename conditional< Condition, TrueType, FalseType >::type conditional_t
Router configuration: loads, relays, control pins and features.
constexpr bool CALIBRATION_MODE
Definition config.h:35
constexpr bool TEMP_SENSOR_PRESENT
Definition config.h:65
Some constants.
The text output: informational messages (info) and debug messages (debug)
void infoln(const Args &... args)
Print an informational message and a line end, as Serial.println() does.
Definition debug.h:67
void info(const Args &... args)
Print an informational message, as Serial.print() does.
Definition debug.h:57
The checks of a DS18B20 reading: CRC, conversion to hundredths of °C, range, power-on value.
constexpr uint8_t CONVERT_TEMPERATURE
Definition ds18b20.h:30
constexpr int16_t DEVICE_DISCONNECTED_RAW
Definition ds18b20.h:38
constexpr uint8_t READ_SCRATCHPAD
Definition ds18b20.h:31
uint8_t i
void begin()
Starts counting: from the last instruction on, one count per CPU cycle.
Definition bench_timer.h:37
constexpr uint8_t crc8(const uint8_t *data, uint8_t len)
The Dallas/Maxim CRC-8 (x^8 + x^5 + x^4 + 1), as OneWire::crc8.
Definition ds18b20.h:59
constexpr int16_t decode(const ScratchPad &buf)
The reading of a scratchpad, its CRC checked.
Definition ds18b20.h:101
uint8_t[9] ScratchPad
The scratchpad: temperature LSB and MSB, TH, TL, configuration, 3 reserved bytes, CRC.
Definition ds18b20.h:43
The ROM search of a 1-Wire bus, on the bit primitives of the bus.
Structure representing the address of a device.
uint8_t addr[8]
conditional_t< TEMP_SENSOR_PRESENT, OneWire, MockOneWire > OneWireType
Definition utils_temp.h:74
TempSensors(const TemperatureSensing< N > &) -> TempSensors< N >
Deduction guide: N from the sensors of config.h.