-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLaCrosse.cpp
262 lines (215 loc) · 6.31 KB
/
LaCrosse.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "LaCrosse.h"
/*
* Message Format:
*
* .- [0] -. .- [1] -. .- [2] -. .- [3] -. .- [4] -.
* | | | | | | | | | |
* SSSS.DDDD DDN_.TTTT TTTT.TTTT WHHH.HHHH CCCC.CCCC
* | | | || | | | | | | || | | |
* | | | || | | | | | | || | `--------- CRC
* | | | || | | | | | | |`-------- Humidity
* | | | || | | | | | | |
* | | | || | | | | | | `---- weak battery
* | | | || | | | | | |
* | | | || | | | | `----- Temperature T * 0.1
* | | | || | | | |
* | | | || | | `---------- Temperature T * 1
* | | | || | |
* | | | || `--------------- Temperature T * 10
* | | | | `--- new battery
* | | `---------- ID
* `---- START = 9
*
*/
bool LaCrosse::USE_OLD_ID_CALCULATION = false;
byte LaCrosse::CalculateCRC(byte data[]) {
return SensorBase::CalculateCRC(data, FRAME_LENGTH - 1);
}
void LaCrosse::EncodeFrame(struct Frame *frame, byte bytes[5]) {
for (int i = 0; i < 5; i++) { bytes[i] = 0; }
// ID
bytes[0] = 9 << 4;
bytes[0] |= frame->ID >> 2;
bytes[1] = (frame->ID & 0b00000011) << 6;
// NewBatteryFlag
bytes[1] |= frame->NewBatteryFlag << 5;
// Bit12
bytes[1] |= frame->Bit12 << 4;
// Temperature
float temp = frame->Temperature + 40.0;
bytes[1] |= (int)(temp / 10);
bytes[2] |= ((int)temp % 10) << 4;
bytes[2] |= (int)(fmod(temp, 1) * 10 + 0.5);
// Humidity
bytes[3] = frame->Humidity;
// WeakBatteryFlag
bytes[3] |= frame->WeakBatteryFlag << 7;
// CRC
bytes[4] = CalculateCRC(bytes);
}
void LaCrosse::DecodeFrame(byte *bytes, struct Frame *frame) {
frame->IsValid = true;
frame->CRC = bytes[4];
if (frame->CRC != CalculateCRC(bytes)) {
frame->IsValid = false;
}
// SSSS.DDDD DDN_.TTTT TTTT.TTTT WHHH.HHHH CCCC.CCCC
frame->ID = 0;
frame->ID |= (bytes[0] & 0xF) << 2;
if (USE_OLD_ID_CALCULATION) {
// This is the way how the initial release calculated th ID
// It's wrong because the two bits must be moved to the right
frame->ID |= (bytes[1] & 0xC0);
}
else {
// The new ID calculation. The order of the bits is respected
frame->ID |= (bytes[1] & 0xC0) >> 6;
}
frame->Header = (bytes[0] & 0xF0) >> 4;
if (frame->Header != 9) {
frame->IsValid = false;
}
frame->NewBatteryFlag = (bytes[1] & 0x20) >> 5;
frame->Bit12 = (bytes[1] & 0x10) >> 4;
byte bcd[3];
bcd[0] = bytes[1] & 0xF;
bcd[1] = (bytes[2] & 0xF0) >> 4;
bcd[2] = (bytes[2] & 0xF);
float t = 0;
t += bcd[0] * 100.0;
t += bcd[1] * 10.0;
t += bcd[2] * 1.0;
t = t / 10;
t -= 40;
frame->Temperature = t;
frame->WeakBatteryFlag = (bytes[3] & 0x80) >> 7;
frame->Humidity = bytes[3] & 0b01111111;
}
String LaCrosse::GetFhemDataString(struct Frame *frame) {
// Format
//
// OK 9 56 1 4 156 37 ID = 56 T: 18.0 H: 37 no NewBatt
// OK 9 49 1 4 182 54 ID = 49 T: 20.6 H: 54 no NewBatt
// OK 9 55 129 4 192 56 ID = 55 T: 21.6 H: 56 WITH NewBatt
// OK 9 ID XXX XXX XXX XXX
// | | | | | | |
// | | | | | | --- Humidity incl. WeakBatteryFlag
// | | | | | |------ Temp * 10 + 1000 LSB
// | | | | |---------- Temp * 10 + 1000 MSB
// | | | |-------------- Sensor type (1 or 2) +128 if NewBatteryFlag
// | | |----------------- Sensor ID
// | |------------------- fix "9"
// |---------------------- fix "OK"
String pBuf;
pBuf += "OK 9 ";
pBuf += frame->ID;
pBuf += ' ';
// bogus check humidity + eval 2 channel TX25IT
// TBD .. Dont understand the magic here!?
if ((frame->Humidity >= 0 && frame->Humidity <= 99)
|| frame->Humidity == 106
|| (frame->Humidity >= 128 && frame->Humidity <= 227)
|| frame->Humidity == 234) {
pBuf += frame->NewBatteryFlag ? 129 : 1;
pBuf += ' ';
}
else if (frame->Humidity == 125 || frame->Humidity == 253) {
pBuf += 2 | frame->NewBatteryFlag ? 130 : 2;
pBuf += ' ';
}
else {
return "";
}
// add temperature
uint16_t pTemp = (uint16_t)(frame->Temperature * 10 + 1000);
pBuf += (byte)(pTemp >> 8);
pBuf += ' ';
pBuf += (byte)(pTemp);
pBuf += ' ';
// bogus check temperature
if (frame->Temperature >= 60 || frame->Temperature <= -40)
return "";
// add humidity
byte hum = frame->Humidity;
if (frame->WeakBatteryFlag) {
hum |= 0x80;
}
pBuf += hum;
return pBuf;
}
bool LaCrosse::DisplayFrame(byte *data, struct Frame &frame, bool fOnlyIfValid) {
byte filter[5];
filter[0] = 0;
filter[1] = 0;
filter[2] = 0;
filter[3] = 0;
filter[4] = 0;
bool hideIt = false;
for (int f = 0; f < 5; f++) {
if (frame.ID == filter[f]) {
hideIt = true;
break;
}
}
if (!hideIt && fOnlyIfValid && !frame.IsValid) {
hideIt = true;
}
if (!hideIt) {
// MilliSeconds, raw data and crc ok
static unsigned long lastMillis;
SensorBase::DisplayFrame(lastMillis, "LaCrosse", frame.IsValid, data, FRAME_LENGTH);
if (frame.IsValid) {
// Start
Serial.print(" S:");
Serial.print(frame.Header, DEC);
// Sensor ID
Serial.print(" ID:");
Serial.print(frame.ID, DEC);
// New battery flag
Serial.print(" NewBatt:");
Serial.print(frame.NewBatteryFlag, DEC);
// Bit 12
Serial.print(" Bit12:");
Serial.print(frame.Bit12, DEC);
// Temperature
Serial.print(" Temp:");
Serial.print(frame.Temperature);
// Weak battery flag
Serial.print(" WeakBatt:");
Serial.print(frame.WeakBatteryFlag, DEC);
// Humidity
Serial.print(" Hum:");
Serial.print(frame.Humidity, DEC);
// CRC
Serial.print(" CRC:");
Serial.print(frame.CRC, DEC);
}
Serial.println();
}
return !hideIt;
}
void LaCrosse::AnalyzeFrame(byte *data, bool fOnlyIfValid) {
struct Frame frame;
DecodeFrame(data, &frame);
DisplayFrame(data, frame, fOnlyIfValid);
}
bool LaCrosse::TryHandleData(byte *data, bool fFhemDisplay) {
if ((data[0] & 0xF0) >> 4 == 9) {
struct Frame frame;
DecodeFrame(data, &frame);
if (frame.IsValid) {
if (fFhemDisplay) {
String fhemString = "";
fhemString = GetFhemDataString(&frame);
if (fhemString.length() > 0) {
Serial.println(fhemString);
}
return fhemString.length() > 0;
}
else {
return DisplayFrame(data, frame);
}
}
}
return false;
}