-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyDCF77.cpp
More file actions
executable file
·477 lines (439 loc) · 9.04 KB
/
Copy pathMyDCF77.cpp
File metadata and controls
executable file
·477 lines (439 loc) · 9.04 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* MyDCF77.cpp
*
* Klasse fuer die Auswertung des DCF77-Zeitsignals.
* Diese Klasse geht von einem 'sauberen' Signal aus.
* Bei schlechten Empfangsbedingungen muesste man eine
* Unschaerfe zulassen.
*
* @mc Arduino/RBBB
* @autor Christian Aschoff / caschoff _AT_ mac _DOT_ com
* @version 1.1
* @datum 2.3.2011
*
* Versionshistorie:
* V 1.1: - Fehler in der Array-Laenge und in toString() behoben.
*/
#include "MyDCF77.h"
//#define DEBUG
/**
* Initialisierung mit dem Pin, an dem das Signal des Empfaengers anliegt
*/
MyDCF77::MyDCF77(int signalPin) {
_signalPin = signalPin;
#ifndef MyDCF77_SIGNAL_IS_ANALOG
pinMode(_signalPin, INPUT);
#endif
for(int i=0; i<MYDCF77_TELEGRAMMLAENGE; i++) {
_bits[i] = 0;
}
_bitsPointer = 0;
_oldSignalState = false;
_signalStartedAtMillis = 0;
_signalStoppedAtMillis = 0;
}
/**
* Liegt ein Signal vom Empfaenger an?
*/
boolean MyDCF77::signal() {
#ifdef MyDCF77_SIGNAL_IS_ANALOG
return analogRead(_signlPin) > MyDCF77_ANALOG_SIGNAL_TRESHOLD;
#else
return digitalRead(_signalPin) == HIGH;
#endif
}
/**
* Aufsammeln der Bits des DCF77-Signals. Im Prinzip ein grosser Zustandsautomat.
* Zurueckgegeben wird ein Wahrheitswert.
* TRUE bedeutet, das Zeittelegramm wurde korrekt ausgewertet, die Zeitdaten
* koennen mit den Gettern abgerufen werden.
* FALSE bedeutet, die Auswertung laeuft oder war falsch, die Getter liefern
* alte Informationen.
*/
boolean MyDCF77::poll() {
boolean retVal = false;
if(signal()) {
if(!_oldSignalState) {
// vorher kein Signal -> ansteigende Flanke -> Sekundenbeginn
_oldSignalState = true;
_signalStartedAtMillis = millis();
}
}
else { // if signal()
if (_oldSignalState) {
// vorher Signal -> abfallende Flanke -> Bitinfo fertig (Signaldauer 100ms = 0; 200ms = 1)
unsigned long duration = millis()-_signalStartedAtMillis;
if(duration > MYDCF77_200MS_TRESHOLD) {
// Bit ist 1
#ifdef DEBUG
Serial.print("1");
#endif
if(_bitsPointer < MYDCF77_TELEGRAMMLAENGE) {
_bits[_bitsPointer] = 1;
}
_bitsPointer++;
_oldSignalState = false;
_signalStoppedAtMillis = millis();
}
else if (duration > MYDCF77_100MS_TRESHOLD) {
// Bit ist 0
#ifdef DEBUG
Serial.print("0");
#endif
if(_bitsPointer < MYDCF77_TELEGRAMMLAENGE) {
_bits[_bitsPointer] = 0;
}
_bitsPointer++;
_oldSignalState = false;
_signalStoppedAtMillis = millis();
}
else {
// Schrott empfangen
#ifdef DEBUG
Serial.print("-");
#endif
}
}
}
// Synczeitpunkt erreicht oder Telegrammlaenge korrekt...
if(millis()-_signalStoppedAtMillis > MYDCF77_SYNC_PAUSE_TRESHOLD) {
#ifdef DEBUG
Serial.print("\nDuration (pause): ");
Serial.println(millis()-_signalStoppedAtMillis);
Serial.print("Bitcount: ");
Serial.println(_bitsPointer);
#endif
if(_bitsPointer == MYDCF77_TELEGRAMMLAENGE) {
retVal = decode();
}
// Bitarray und Pointer zuruecksetzen...
for(int i=0; i<MYDCF77_TELEGRAMMLAENGE; i++) {
_bits[i] = 0;
}
_bitsPointer = 0;
_oldSignalState = false;
_signalStartedAtMillis = millis();
_signalStoppedAtMillis = millis();
#ifdef DEBUG
// Von oben nach unten lesen. Zuerst Bitnummer, dann Bedeutung.
Serial.println(" 1111111111222222222233333333334444444444555555555");
Serial.println("01234567890123456789012345678901234567890123456789012345678");
Serial.println("---------------RAZZASMMMMMMMPSSSSSSPDDDDDDWWWMMMMMYYYYYYYYP");
Serial.println(" 1122 12481241124812212481212412481124812483");
Serial.println(" 000 00 00 0 0000 ");
#endif
}
return retVal;
}
/**
* Decodierung des Telegramms...
*/
boolean MyDCF77::decode() {
int c = 0; // bitcount for checkbit
boolean ok = true;
#ifdef DEBUG
Serial.println("Decoding telegram...");
#endif
if(_bits[20] != 1) {
ok = false;
#ifdef DEBUG
Serial.println("Check-bit S failed.");
#endif
}
if(_bits[17] == _bits[18]) {
ok = false;
#ifdef DEBUG
Serial.println("Check Z1 != Z2 failed.");
#endif
}
//
// minutes
//
_minutes = 0;
c = 0;
if(_bits[21] == 1) {
c++;
_minutes += _bits[21] * 1;
}
if(_bits[22] == 1) {
c++;
_minutes += _bits[22] * 2;
}
if(_bits[23] == 1) {
c++;
_minutes += _bits[23] * 4;
}
if(_bits[24] == 1) {
c++;
_minutes += _bits[24] * 8;
}
if(_bits[25] == 1) {
c++;
_minutes += _bits[25] * 10;
}
if(_bits[26]) {
c++;
_minutes += _bits[26] * 20;
}
if(_bits[27]) {
c++;
_minutes += _bits[27] * 40;
}
#ifdef DEBUG
Serial.print("Minutes: ");
Serial.println(_minutes);
#endif
if((c + _bits[28]) % 2 != 0) {
ok = false;
#ifdef DEBUG
Serial.println("Check-bit P1: minutes failed.");
#endif
}
//
// hour
//
_hours = 0;
c = 0;
if(_bits[29] == 1) {
c++;
_hours += _bits[29] * 1;
}
if (_bits[30] == 1) {
c++;
_hours += _bits[30] * 2;
}
if (_bits[31] == 1) {
c++;
_hours += _bits[31] * 4;
}
if (_bits[32] == 1) {
c++;
_hours += _bits[32] * 8;
}
if (_bits[33] == 1) {
c++;
_hours += _bits[33] * 10;
}
if (_bits[34] == 1) {
c++;
_hours += _bits[34] * 20;
}
#ifdef DEBUG
Serial.print("Hours: ");
Serial.println(_hours);
#endif
if((c + _bits[35]) % 2 != 0) {
ok = false;
#ifdef DEBUG
Serial.println("Check-bit P2: hours failed.");
#endif
}
//
// date
//
_date = 0;
c = 0;
if(_bits[36] == 1) {
c++;
_date += _bits[36] * 1;
}
if(_bits[37] == 1) {
c++;
_date += _bits[37] * 2;
}
if(_bits[38] == 1) {
c++;
_date += _bits[38] * 4;
}
if(_bits[39] == 1) {
c++;
_date += _bits[39] * 8;
}
if(_bits[40] == 1) {
c++;
_date += _bits[40] * 10;
}
if(_bits[41] == 1) {
c++;
_date += _bits[41] * 20;
}
#ifdef DEBUG
Serial.print("Date: ");
Serial.println(_date);
#endif
//
// day of week
//
_dayOfWeek = 0;
if(_bits[42] == 1) {
c++;
_dayOfWeek += _bits[42] * 1;
}
if(_bits[43] == 1) {
c++;
_dayOfWeek += _bits[43] * 2;
}
if(_bits[44] == 1) {
c++;
_dayOfWeek += _bits[44] * 4;
}
#ifdef DEBUG
Serial.print("Day of week: ");
Serial.println(_dayOfWeek);
#endif
//
// month
//
_month = 0;
if(_bits[45] == 1) {
c++;
_month += _bits[45] * 1;
}
if(_bits[46] == 1) {
c++;
_month += _bits[46] * 2;
}
if(_bits[47] == 1) {
c++;
_month += _bits[47] * 4;
}
if(_bits[48] == 1) {
c++;
_month += _bits[48] * 8;
}
if(_bits[49] == 1) {
c++;
_month += _bits[49] * 10;
}
#ifdef DEBUG
Serial.print("Month: ");
Serial.println(_month);
#endif
//
// year
//
_year = 0;
if(_bits[50] == 1) {
c++;
_year += _bits[50] * 1;
}
if(_bits[51] == 1) {
c++;
_year += _bits[51] * 2;
}
if(_bits[52] == 1) {
c++;
_year += _bits[52] * 4;
}
if(_bits[53] == 1) {
c++;
_year += _bits[53] * 8;
}
if(_bits[54] == 1) {
c++;
_year += _bits[54] * 10;
}
if(_bits[55] == 1) {
c++;
_year += _bits[55] * 20;
}
if(_bits[56] == 1) {
c++;
_year += _bits[56] * 40;
}
if(_bits[57] == 1) {
c++;
_year += _bits[57] * 80;
}
_year += 2000;
if (_year < 2011) {
ok = false;
#ifdef DEBUG
Serial.print("Check year >= 2011 failed.");
#endif
}
#ifdef DEBUG
Serial.print("Year: ");
Serial.println(_year);
#endif
if((c + _bits[58]) % 2 != 0) {
ok = false;
#ifdef DEBUG
Serial.println("Check-bit P3: date failed.");
#endif
}
if (!ok) {
// discard date...
_minutes = 0;
_hours = 0;
_date = 0;
_dayOfWeek = 0;
_month = 0;
_year = 0;
}
return ok;
}
/**
* Das Zeittelegramm als String bekommen
*/
char* MyDCF77::asString() {
_cDateTime[0] = 0;
char temp[5];
// build the string...
if (_hours < 10) {
sprintf(temp, "0%d:", _hours);
strncat(_cDateTime, temp, strlen(temp));
}
else {
sprintf(temp, "%d:", _hours);
strncat(_cDateTime, temp, strlen(temp));
}
if (_minutes < 10) {
sprintf(temp, "0%d ", _minutes);
strncat(_cDateTime, temp, strlen(temp));
}
else {
sprintf(temp, "%d ", _minutes);
strncat(_cDateTime, temp, strlen(temp));
}
if (_date < 10) {
sprintf(temp, "0%d.", _date);
strncat(_cDateTime, temp, strlen(temp));
}
else {
sprintf(temp, "%d.", _date);
strncat(_cDateTime, temp, strlen(temp));
}
if (_month < 10) {
sprintf(temp, "0%d.", _month);
strncat(_cDateTime, temp, strlen(temp));
}
else {
sprintf(temp, "%d.", _month);
strncat(_cDateTime, temp, strlen(temp));
}
sprintf(temp, "%d", _year);
strncat(_cDateTime, temp, strlen(temp));
return _cDateTime;
}
//
// Getter
//
int MyDCF77::getMinutes() {
return _minutes;
}
int MyDCF77::getHours() {
return _hours;
}
int MyDCF77::getDate() {
return _date;
}
int MyDCF77::getDayOfWeek() {
return _dayOfWeek;
}
int MyDCF77::getMonth() {
return _month;
}
int MyDCF77::getYear() {
return _year;
}