3-phase PV router
Loading...
Searching...
No Matches
utils_temp.h
Go to the documentation of this file.
1
12#ifndef UTILS_TEMP_H
13#define UTILS_TEMP_H
14
15#include <Arduino.h>
16
17#include "constants.h"
18
19#ifdef TEMP_ENABLED
20inline constexpr bool TEMP_SENSOR_PRESENT{ true };
21#include <OneWire.h> // for temperature sensing
22#else
23inline constexpr bool TEMP_SENSOR_PRESENT{ false };
24#endif
25
34{
35 uint8_t addr[8];
36};
37
45template< uint8_t N >
47{
48 using ScratchPad = uint8_t[9];
49
50public:
51 constexpr TemperatureSensing() = delete;
52
59 constexpr TemperatureSensing(uint8_t pin, const DeviceAddress (&ref)[N])
60 : sensorPin{ pin }, sensorAddrs(ref)
61 {
62 }
63
69 {
70#ifdef TEMP_ENABLED
71 oneWire.reset();
72 oneWire.skip();
73 oneWire.write(CONVERT_TEMPERATURE);
74#endif
75 }
76
82 {
83#ifdef TEMP_ENABLED
84 oneWire.begin(sensorPin);
86#endif
87 }
88
94 constexpr auto get_size() const
95 {
96 return N;
97 }
98
104 constexpr auto get_pin() const
105 {
106 return sensorPin;
107 }
108
115 int16_t readTemperature(const uint8_t idx)
116 {
117 static ScratchPad buf;
118
119#ifdef TEMP_ENABLED
120 if (!oneWire.reset())
121 {
123 }
124 oneWire.select(sensorAddrs[idx].addr);
125 oneWire.write(READ_SCRATCHPAD);
126
127 for (auto &buf_elem : buf)
128 {
129 buf_elem = oneWire.read();
130 }
131
132 if (!oneWire.reset())
133 {
135 }
136 if (oneWire.crc8(buf, 8) != buf[8])
137 {
139 }
140#endif
141
142 // result is temperature x16, multiply by 6.25 to convert to temperature x100
143 int16_t result = (buf[1] << 8) | buf[0];
144 result = (result * 6) + (result >> 2);
145 if (result <= TEMP_RANGE_LOW || result >= TEMP_RANGE_HIGH)
146 {
147 return OUTOFRANGE_TEMPERATURE; // return value ('Out of range')
148 }
149
150 return result;
151 }
152
153private:
154 const uint8_t sensorPin;
158#ifdef TEMP_ENABLED
159 static inline OneWire oneWire;
160#endif
161};
162
163#endif // UTILS_TEMP_H
This class implements the temperature sensing feature.
Definition: utils_temp.h:47
constexpr TemperatureSensing()=delete
constexpr TemperatureSensing(uint8_t pin, const DeviceAddress(&ref)[N])
Construct a new Temperature Sensing object.
Definition: utils_temp.h:59
const DeviceAddress sensorAddrs[N]
Definition: utils_temp.h:156
constexpr auto get_size() const
Get the number of sensors.
Definition: utils_temp.h:94
const uint8_t sensorPin
Definition: utils_temp.h:154
int16_t readTemperature(const uint8_t idx)
Read temperature of a specific device.
Definition: utils_temp.h:115
uint8_t[9] ScratchPad
Definition: utils_temp.h:48
void requestTemperatures()
Request temperature for all sensors.
Definition: utils_temp.h:68
constexpr auto get_pin() const
Get the pin of the sensor(s)
Definition: utils_temp.h:104
void initTemperatureSensors()
Initialize the Dallas sensors.
Definition: utils_temp.h:81
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
Structure representing the address of a device.
Definition: utils_temp.h:34
uint8_t addr[8]
Definition: utils_temp.h:35
constexpr bool TEMP_SENSOR_PRESENT
Definition: utils_temp.h:23