This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1820.h
174 lines (161 loc) · 4.6 KB
/
ds1820.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include <DallasTemperature.h>
#define DS_INTERVAL 10000
#define DS_MAX_COUNT 16
#define CFG_SENSORS "/sensors.xml"
struct sensor {
DeviceAddress device;
int16_t map;
String toString() {
char szRet[24];
sprintf(szRet,"%X%X%X%X%X%X%X%X", device[0], device[1], device[2], device[3], device[4], device[5], device[6], device[7]);
return String(szRet);
}
};
OneWire oneWire(OW_PIN);
class TSensors : public DallasTemperature {
public:
TSensors() : DallasTemperature(&oneWire) {
begin();
setResolution(12);
setWaitForConversion(false);
}
};
TSensors* ds;
sensor sens[DS_MAX_COUNT];
uint32_t dsResponse();
uint32_t dsRequest() {
ds->requestTemperatures();
taskAddWithDelay(dsResponse, 250);
return RUN_DELETE;
}
uint32_t dsResponse() {
uint8_t i;
DeviceAddress zerro;
memset(zerro, 0, sizeof(DeviceAddress));
for (i = 0; i < DS_MAX_COUNT; i++) {
if (memcmp(sens[i].device, zerro, sizeof(DeviceAddress)) != 0) {
float t = ds->getTempC(sens[i].device);
if (t != DEVICE_DISCONNECTED_C) {
if (sens[i].map != -1) {
fReg[sens[i].map].set(t);
}
}
}
}
return DS_INTERVAL;
}
bool dsFindNew() {
bool newDeviceAdded = false;
for (uint8_t i = 0; i < ds->getDeviceCount(); i++) {
DeviceAddress deviceFound;
ds->getAddress(deviceFound, i);
uint8_t j = 0;
for (j; j < DS_MAX_COUNT; j++) {
if (memcmp(sens[j].device, deviceFound, sizeof(DeviceAddress)) == 0) break;
}
if (j >= DS_MAX_COUNT) {
DeviceAddress zerro;
memset(zerro, 0, sizeof(DeviceAddress));
for (j = 0; j < DS_MAX_COUNT; j++) {
if (memcmp(sens[j].device, zerro, sizeof(DeviceAddress)) == 0) {
memcpy(sens[j].device, deviceFound, sizeof(DeviceAddress));
newDeviceAdded = true;
Serial.println(sens[j].toString());
sens[j].map = 0;
break;
}
}
}
}
return newDeviceAdded;
}
uint32_t dsInit() {
ds = new TSensors();
for (uint8_t i = 0; i < DS_MAX_COUNT; i++) {
sens[i].map = -1;
memset(sens[i].device, 0, sizeof(DeviceAddress)); // Fill device id with 0
}
dsFindNew();
taskAdd(dsRequest);
return RUN_DELETE;
}
/*
extern TinyXML xml;
extern String xmlOpen;
extern String xmlTag;
extern String xmlData;
extern String xmlAttrib;
extern void XML_callback(uint8_t statusflags, char* tagName, uint16_t tagNameLen, char* data, uint16_t dataLen);
bool saveSensors() {
File configFile = SPIFFS.open(F(CFG_SENSORS), "w");
if (configFile) {
char buf[200];
sprintf_P(buf, PSTR("<?xml version = \"1.0\" ?>\n<sensors>\n"));
configFile.write((uint8_t*)buf, strlen(buf));
for (uint8_t i = 0; i < DEVICE_MAX_COUNT; i++) {
sprintf_P(buf, PSTR("<sensor><id>%02X%02X%02X%02X%02X%02X%02X%02X</id><gid>%d</gid><name>%s</name></sensor>\n"),
sens[i].device[0], sens[i].device[1], sens[i].device[2], sens[i].device[3], sens[i].device[4], sens[i].device[5], sens[i].device[6], sens[i].device[7],
sens[i].gid, sens[i].name.c_str());
configFile.write((uint8_t*)buf, strlen(buf));
}
sprintf_P(buf, PSTR("</sensors>"));
configFile.write((uint8_t*)buf, strlen(buf));
configFile.close();
return true;
}
return false;
}
bool readSensors() {
int16_t i;
for (i = 0; i < DEVICE_MAX_COUNT; i++) {
sens[i].tCurrent = DEVICE_DISCONNECTED_C;
sens[i].name = String(i);
sens[i].gid = 0;
sens[i].age = DEVICE_AGE_DEF;
memset(sens[i].device, 0, sizeof(DeviceAddress)); //Fill device id with 0
}
i = 0;
bool sensorOpen = false;
File configFile = SPIFFS.open(CFG_SENSORS, "r");
if (configFile) {
xml.reset();
xmlTag = "";
xmlOpen = "";
char c;
while (configFile.read((uint8_t*)&c, 1) == 1) {
xml.processChar(c);
if (xmlTag != "" || xmlOpen != "") {
if
(xmlOpen.endsWith(F("/sensor"))) {
if (sensorOpen) {
i++;
} else {
sensorOpen = true;
}
//xmlOpen = "";
if (i >= DEVICE_MAX_COUNT) {
break;
}
} else if
(xmlTag.endsWith(F("/id"))) {
for (uint8_t j = 0; j < 8; j++) {
sens[i].device[j] = strtol(xmlData.substring(j*2, j*2+2).c_str(), NULL, 16);
}
} else if
(xmlTag.endsWith(F("/gid"))) {
sens[i].gid = xmlData.toInt();
} else if
(xmlTag.endsWith(F("/name"))) {
sens[i].name = xmlData;
}
}
xmlTag = "";
xmlOpen = "";
}
configFile.close();
return true;
}
return false;
}
*/