-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecoder.cpp
More file actions
221 lines (198 loc) · 4.67 KB
/
Decoder.cpp
File metadata and controls
221 lines (198 loc) · 4.67 KB
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
/*
* Receiver for data of wireless weather sensors with RX868 and Arduino.
*
* Implementation of decoder module.
*/
#include "Decoder.h"
//#define DEBUG
#ifndef ARDUINO
#ifdef DEBUG
class Serial_t {
public:
void print(const char* str) {
printf("%s", str);
};
void print(int i) {
printf("%d", i);
};
void print(char c) {
printf("%c", c);
}
void println(const char* str) {
printf("%s\n", str);
};
void println(int i) {
printf("%d\n", i);
};
void println() {
printf("\n");
};
};
Serial_t Serial;
#endif // DEBUG
unsigned long millis() {
return clock() / (CLOCKS_PER_SEC / 1000);
}
#endif // ARDUINO
const char* SENSOR_TYPES[] = {"Thermo", "Thermo/Hygro", "Rain(?)", "Wind(?)", "Thermo/Hygro/Baro", "Luminance(?)", "Pyrano(?)", "Kombi"};
const int SENSOR_DATA_COUNT[] = {5, 8, 5, 8, 12, 6, 6, 14};
Decoder::Decoder(unsigned long minLen, unsigned long maxLen)
: minLen(minLen), maxLen(maxLen), decoderState(WAIT), syncCount(0), bufferEnd(0)
{}
bool Decoder::pulse(unsigned long len, unsigned long lo) {
bool hasNewValue = false;
uint8_t val = bitval(len, lo);
if (255 == val) {
if (DATA == decoderState) {
// end of frame?
pushData(1);
hasNewValue = decode();
bufferEnd = 0;
}
decoderState = WAIT;
} else if (WAIT == decoderState) {
if (0 == val) {
// first sync pulse
syncCount = 1;
decoderState = SYNC;
}
} else if (SYNC == decoderState) {
if (0 == val) {
// another sync pulse
syncCount++;
} else if (1 == val && syncCount > 6) {
// got the start bit
syncCount = 0;
decoderState = DATA;
}
} else if (DATA == decoderState) {
pushData(val);
}
return hasNewValue;
}
uint8_t Decoder::bitval(unsigned long len, unsigned long lo) {
uint8_t val = 255;
if (len >= minLen && len <= maxLen) {
if (lo < len/2) {
val = 0;
} else {
val = 1;
}
}
return val;
}
void Decoder::pushData(uint8_t val) {
buffer[bufferEnd++] = val;
if (bufferEnd == BUFLEN) {
bufferEnd = 0;
}
}
int Decoder::popbits(int num) {
int val = 0;
if (bufferEnd - bufferFront < num) {
#ifdef DEBUG
Serial.println("data exhausted");
#endif
return 0;
}
for (int i = 0; i < num; ++i) {
val += buffer[bufferFront] << i;
bufferFront++;
}
return val;
}
bool Decoder::expectEon() {
// check end of nibble (1)
if (popbits(1) != 1) {
#ifdef DEBUG
Serial.println("end of nibble is not 1");
#endif
return false;
}
return true;
}
bool Decoder::decode() {
#ifdef DEBUG
Serial.println("decode: ");
for (int i = 0; i < bufferEnd; ++i) {
Serial.print(char(buffer[i] + '0'));
}
Serial.println();
#endif
bufferFront = 0;
int check = 0;
int sum = 0;
int sensorType = popbits(4) & 7;
if (!expectEon()) {
return false;
}
check ^= sensorType;
sum += sensorType;
// read data as nibbles
int nibbleCount = SENSOR_DATA_COUNT[sensorType];
int *dec = new int[nibbleCount];
for (int i = 0; i < nibbleCount; ++i) {
int nibble = popbits(4);
if (!expectEon()) {
delete[] dec;
return false;
}
dec[i] = nibble;
check ^= nibble;
sum += nibble;
}
// check
if (check != 0) {
#ifdef DEBUG
Serial.print("Check is not 0 but ");
Serial.println(check);
#endif
delete[] dec;
return false;
}
// sum
int sumRead = popbits(4);
sum += 5;
sum &= 0xF;
if (sumRead != sum) {
#ifdef DEBUG
Serial.print("Sum read is ");
Serial.print(sumRead);
Serial.print(" but computed is ");
Serial.println(sum);
#endif
delete[] dec;
return false;
}
// compute values
decoderOutput.sensorType = sensorType;
decoderOutput.sensorTypeStr = SENSOR_TYPES[sensorType];
decoderOutput.address = dec[0] & 7;
decoderOutput.temperature = (dec[3]*10.f + dec[2] + dec[1]/10.f) * (dec[0]&8?-1.f:1.f);
decoderOutput.humidity = 0.f;
decoderOutput.wind = 0.f;
decoderOutput.rainSum = 0;
decoderOutput.rainDetect = 0;
decoderOutput.pressure = 0;
decoderOutput.timeStamp = millis();
if (7 == sensorType) {
// Kombisensor
decoderOutput.humidity = dec[5]*10.f + dec[4];
decoderOutput.wind = dec[8]*10.f + dec[7] + dec[6]/10.f;
decoderOutput.rainSum = dec[11]*16*16 + dec[10]*16 + dec[9];
decoderOutput.rainDetect = dec[0]&2 == 1;
}
if (1 == sensorType || 4 == sensorType) {
// Thermo/Hygro
decoderOutput.humidity = dec[6]*10.f + dec[5] + dec[4]/10.f;
}
if (4 == sensorType) {
// Thermo/Hygro/Baro
decoderOutput.pressure = 200 + dec[9]*100 + dec[8]*10 + dec[7];
}
delete[] dec;
return true;
}
DecoderOutput Decoder::getDecoderOutput() const {
return decoderOutput;
}