-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcf77decoder.cpp
236 lines (187 loc) · 5.84 KB
/
dcf77decoder.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
/*
* dcf77decoder.cpp
*
* Copyright 2014 Jan "aso" Szenborn <aso.gdynia@gmail.com>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it freely,
* subject to the following restrictions:
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
*/
#pragma once
#include <string>
struct DCF77Time {
/*
* Struct to store DCF77 time
*/
unsigned short hour = 0,
minute = 0,
day = 0,
weekday = 0,
month = 0,
year = 0;
void clear() {
/* Simple clear function */
hour = 0; minute = 0;
day = 0; weekday = 0;
month = 0; year = 0;
}
};
struct DCF77Result {
/*
* Struct to store result from decoder
*/
DCF77Time time; // Date and time
bool antenna = 0, // 0 - primary, 1 - secondary (or service maintenance)
timeChange = 0, // Next hour is the time change (CET/CEST)
summerTime = 0; // 1 when CEST time
};
class DCF77Decoder {
/*
* name: DCF77Decoder
* description: decoder for bits received from DCF77
* usage: create object, use set(param) function and getResult()
*/
private:
bool data[59] = {false}; // Binary data
DCF77Result result;
unsigned short bcd2dec(bool one, bool two, bool three, bool four);
bool parityBit(bool *data, unsigned short startIndex, unsigned short endIndex);
public:
DCF77Decoder();
// Overloaded functions for many data types
bool set(bool data[59]);
bool set(unsigned short data[59]);
bool set(std::string data);
DCF77Result getResult();
};
inline unsigned short DCF77Decoder::bcd2dec(bool one, bool two, bool three, bool four) {
/*
* Converts 4-bits data in BCD format to decimal number (range 0-9)
* Order: little endian
*/
return (one*8 + two*4 + three*2 + four*1);
}
bool DCF77Decoder::parityBit(bool *data, unsigned short startIndex, unsigned short endIndex) {
/*
* Returns value of calculated parity bit from range in data array
* true is 1, false is 0
*/
// Just can't proceed when incorrect data
if (startIndex > endIndex)
return false;
// Count of true bits (1)
unsigned char count = 0;
// Iterate over data for count true bits
for (; startIndex <= endIndex; startIndex++)
if (data[startIndex] == 1)
count++;
// When count is even, return 0, else return 1
if (count%2 == 0)
return false;
else
return true;
}
DCF77Decoder::DCF77Decoder() {
// Empty constructor - unused
}
bool DCF77Decoder::set(bool data[59]) {
// Clear result
result.antenna = 0;
result.summerTime = 0;
result.timeChange = 0;
result.time.clear();
// First bit must be 0
if (data[0] != 0) return false;
/*
* Meteo data can't be decoded, so it's skipped
*/
// Antenna (or TX status); 0 - primary, 1 - secondary (or maintenance mode)
result.antenna = data[15];
// True when next hour is hour change
result.timeChange = data[16];
// When 1, it's summer time (CEST), otherwise it's winter time (CET)
result.summerTime = bcd2dec(0, 0, data[18], data[17]) - 1;
// 20nd bit must be 1, it means start time data
if (data[20] != 1) return false;
// Get minutes and check parity bit
result.time.minute = bcd2dec(data[24], data[23], data[22], data[21])
+ (bcd2dec(0, data[27], data[26], data[25]) * 10);
if (parityBit(data, 21, 27) != data[28]) return false;
// Get hour
result.time.hour = bcd2dec(data[32], data[31], data[30], data[29])
+ (bcd2dec(0, 0, data[34], data[33]) * 10);
if (parityBit(data, 29, 34) != data[35]) return false;
// Get day of month
result.time.day = bcd2dec(data[39], data[38], data[37], data[36])
+ (bcd2dec(0, 0, data[41], data[40]) * 10);
// Weekday (1 - monday, 7 - sunday)
result.time.weekday = bcd2dec(0, data[44], data[43], data[42]);
// Month (1 - jan, 12 - dec)
result.time.month = bcd2dec(data[48], data[47], data[46], data[45])
+ (bcd2dec(0, 0, 0, data[49]) * 10);
// Two digits of year in current century (example (19)96, (20)14)
result.time.year = bcd2dec(data[53], data[52], data[51], data[50])
+ (bcd2dec(data[57], data[56], data[55], data[54]) * 10);
// Parity bit for date
if (parityBit(data, 36, 57) != data[58]) return false;
// Validate decoded data
if ( (result.time.minute > 60) ||
(result.time.hour > 24) ||
(result.time.day > 31) ||
(result.time.weekday > 7) ||
(result.time.month > 12))
return false;
// Data correct - return true
return true;
}
bool DCF77Decoder::set(unsigned short data[59]) {
/*
* Overloaded function set(bool[59]) for int array
*/
bool newData[59];
for (unsigned char i = 0; i <= 58; i++) {
if (data[i] == 1)
newData[i] = true;
else
newData[i] = false;
}
// Call original function
return set(newData);
}
bool DCF77Decoder::set(std::string data) {
/*
* Overloaded function set(bool[59]) for string
* String must have 58/59 characters
*/
bool newData[59];
// Validate string length
if (data.length() == 58) data = '0' + data;
else if (data.length() != 59) return false;
for (unsigned char i = 0; i <= 58; i++) {
if (data[i] == '1')
newData[i] = true;
else
newData[i] = false;
}
// Call original function
return set(newData);
}
inline DCF77Result DCF77Decoder::getResult() {
/*
* Just returns data...
*/
return result;
}