/
MSFTime.cpp
587 lines (454 loc) · 13.7 KB
/
MSFTime.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// MSFTime
// Jarkman, 01/2011
// http://www.jarkman.co.uk/catalog/robots/msftime.htm
// Decodes MSF time signals from a receiver like this:
// http://www.pvelectronics.co.uk/index.php?main_page=product_info&cPath=9&products_id=2
// and integrates with the Time library to provide a real-time clock
// The algorithm used here is vulnerable to noise pulses, which will cause it to give up decoding the current signal.
// It needs a whole minute of good signal to get a time fix. I've found that with my receiver, at some times of day and in some places,
// it never gets a fix at all, because each minute gets at least one noise pulse.
// Evenings work best.
// It would be possible to rework the algorithm to use an averaged signal,
// which would be much more immune to noise, but this is good enough for my purposes.
// Turn these on to see more of what we are receiving.
//#define DEBUG
//#define EXTRA_DEBUG
#define USE_AVR_INTERRUPTS // define this to use AVR interrupts, with the module wired to analogue pin 0
// Or undefine it to use Arduino interrupts, with the module wired to in digital pin 2
#include <Arduino.h>
//#include <wiring.h>
#ifdef DEBUG
#include <HardwareSerial.h> // for Serial for logging
#endif
#include <Time.h> // from http://www.pjrc.com/teensy/td_libs_Time.html - unzip it into your libraries folder
#ifdef USE_AVR_INTERRUPTS
#include <avr/interrupt.h>
#endif
#include "MSFTime.h"
#define PULSE_MARGIN 30 // The leeway we allow our pulse lengths
#define PULSE_OFF_OFFSET 20 // arbitrary offset to compensate for assymetric behaviour of my receiver,
// which tends to deliver an 'off' period that is too short
// adjust this so the short off pulses measure about 100mS in the stateChange() debug logging
#define PULSE_IGNORE_WIDTH 30 // just ignore very short pulses
MSFTime::MSFTime()
{
}
MSFTime *sMSF = 0;
void MSFTime::init(byte ledPin)
{
mLedPin = ledPin;
mOffStartMillis = 0;
mOnStartMillis = 0;
mPrevOffStartMillis = 0;
mPrevOnStartMillis = 0;
mOnWidth = 0;
mFixMillis = 0;
mIsReading = false;
mBitIndex = 0;
mGoodPulses = 0;
for( int i = 0; i < 7; i ++ )
{
mABits[i] = 0;
mBBits[i] = 0;
}
if( ledPin != 255 )
pinMode(mLedPin, OUTPUT);
sMSF = this;
#ifdef USE_AVR_INTERRUPTS
// see
// http://www.me.ucsb.edu/~me170c/Code/How_to_Enable_Interrupts_on_ANY_pin.pdf
// for a good descripton of the AVR pin interrupt malarkey
// Switching this stuff to a different pin requires reading the table there quite carefully
// You will need to change the block below, and also the ISR(PCINT?_vect) line below.
// I chose digital 8 as my input because it is one of the few pins that the LOLShield does not use.
// for analogue pin 0:
mInputPin = 14;
pinMode(mInputPin, INPUT);
PCICR |= (1<<PCIE1);
PCMSK1 |= (1<<PCINT8);
MCUCR = (1<<ISC01) | (1<<ISC00);
/*
// for digital pin 7:
mInputPin = 7;
pinMode(mInputPin, INPUT);
PCICR |= (1<<PCIE2);
PCMSK2 |= (1<<PCINT23);
MCUCR = (1<<ISC01) | (1<<ISC00);
*/
// enable interrupts
interrupts();
#else
mInputPin = 2;
pinMode(mInputPin, INPUT);
interruptIndex = 0; // the Arduino interrupt for pin 2
attachInterrupt(interruptIndex, sStateChange, CHANGE);
#endif
}
#ifdef USE_AVR_INTERRUPTS
ISR(PCINT1_vect) // for analogue 0 (digital 14)
//ISR(PCINT0_vect) // for digital pin 8
//ISR(PCINT2_vect) // for digital pin 7
{
//todo - check our pin changed
if( sMSF )
sMSF->stateChange();
}
#else
void sStateChange() // static fn for the interrupt handler
{
if( sMSF )
sMSF->stateChange();
}
#endif
byte oldVal = 3;
void MSFTime::stateChange() // interrupt routine called on every state change
{
byte val = digitalRead(mInputPin);
if( val == oldVal )
return;
oldVal = val;
long millisNow = millis();
if( mLedPin != 255 )
digitalWrite(mLedPin, val);
// see here:
// http://www.pvelectronics.co.uk/rftime/msf/MSF_Time_Date_Code.pdf
// for an explanation of the format
// Carrier goes off for 100, 200, 300, or 500 mS during every second
// Our input is inverted by our receiver, so val != 0 when carrier is off
if (val != 0) // carrier is off, start timing
{
if( millisNow - mOnStartMillis < PULSE_IGNORE_WIDTH)
{
// ignore this transition plus the previous one
#ifdef EXTRA_DEBUG
Serial.print("Ignoring on pulse len ");
Serial.println(millisNow - mOnStartMillis);
#endif
mOnStartMillis = mPrevOnStartMillis;
return;
}
mPrevOffStartMillis = mOffStartMillis;
mOffStartMillis = millisNow;
mOnWidth = mOffStartMillis - mOnStartMillis;
return;
}
if( millisNow - mOffStartMillis < PULSE_IGNORE_WIDTH)
{
#ifdef EXTRA_DEBUG
Serial.print("Ignoring off pulse len ");
Serial.println(millisNow - mOffStartMillis);
#endif
// ignore this transition plus the previous one
mOffStartMillis = mPrevOffStartMillis;
return;
}
mPrevOnStartMillis = mOnStartMillis;
mOnStartMillis = millisNow;
long offWidth = millisNow - mOffStartMillis - PULSE_OFF_OFFSET;
/* check the width of the off-pulse; according to the specifications, a
* pulse must be 0.1 or 0.2 or 0.3 or 0.5 seconds
*/
boolean is500 = abs(offWidth-500) < PULSE_MARGIN;
boolean is300 = abs(offWidth-300) < PULSE_MARGIN;
boolean is200 = abs(offWidth-200) < PULSE_MARGIN;
boolean is100 = abs(offWidth-100) < PULSE_MARGIN;
long onWidth = mOnWidth;
boolean onWas100 = (onWidth > 5) && (onWidth < 200);
boolean onWasNormal = (onWidth > 400) && (onWidth < (900 + PULSE_MARGIN));
#ifdef EXTRA_DEBUG
Serial.print("Sum ");
Serial.print(offWidth + onWidth);
Serial.print(" offWidth ");
Serial.print(offWidth);
Serial.print(" onWidth ");
Serial.print(onWidth);
Serial.print(" mBitIndex ");
Serial.println((int)mBitIndex);
#endif
if( (onWasNormal || onWas100) && (is100 || is200 || is300 || is500 ))
{
mGoodPulses++;
}
else
{
#ifdef EXTRA_DEBUG
Serial.println("Bad pulse!!!!!! ");
#endif
mGoodPulses = 0;
}
/*
Cases:
a 500mS carrier-off marks the start of a minute
a 300mS carrier-off means bits 1 1
a 200mS carrier-off means bits 1 0
a 100mS carrier-off followed by a 900mS carrier-on means bits 0 0
a 100mS carrier-off followed by a 100mS carrier-on followed by a 100mS carrier-off means bits 0 1
*/
if( is500 ) // minute marker
{
if( mIsReading )
doDecode();
mIsReading = true; // and get ready to read the next minute's worth
mBitIndex = 1; // the NPL docs number bits from 1, so we will too
}
else
if( mIsReading )
{
if( mBitIndex < 60 && onWasNormal && (is100 || is200 || is300 )) // we got a sensible pair of bits, 00 or 01 or 11
{
if( is100 )
{
setBit( mABits, mBitIndex, 0 );
setBit( mBBits, mBitIndex++, 0 );
}
if( is200 )
{
setBit( mABits, mBitIndex, 1 );
setBit( mBBits, mBitIndex++, 0 );
}
if( is300 )
{
setBit( mABits, mBitIndex, 1 );
setBit( mBBits, mBitIndex++, 1 );
}
#ifdef EXTRA_DEBUG
if( getBit( mABits, mBitIndex - 1 ))
Serial.println(" A = 1");
else
Serial.println(" A = 0");
if( getBit( mBBits, mBitIndex - 1 ))
Serial.println(" B = 1");
else
Serial.println(" B = 0");
#endif
}
else if( mBitIndex < 60 && onWas100 && is100 && mBitIndex > 0 ) // tricky - we got a second bit for the preceding pair
{
setBit( mBBits, mBitIndex - 1, 1 );
#ifdef EXTRA_DEBUG
if( getBit( mBBits, mBitIndex - 1 ))
Serial.println(" B = 1");
else
Serial.println(" B = 0");
#endif
}
else // bad pulse, give up
{
#ifdef DEBUG
if( mIsReading || ! (is100 || is200 || is300 || is500))
{
Serial.println("Bad pulse len");
Serial.print(" offWidth ");
Serial.println(offWidth);
Serial.print(" onWidth ");
Serial.println(onWidth);
Serial.print(" mBitIndex ");
Serial.println((int)mBitIndex);
}
#endif
mIsReading = false;
mBitIndex = 0;
}
}
}
void MSFTime::doDecode()
{
if( mBitIndex != 60 ) // there are always 59 bits, barring leap-seconds
{
#ifdef DEBUG
Serial.println("Wrong number of bits ");
#endif
return;
}
if( ! checkValid())
{
#ifdef DEBUG
Serial.println("Not valid");
#endif
return;
}
mFixMillis = millis() - 500L;
mFixYear = decodeBCD( mABits, 24, 17 ); // 0-99
mFixMonth = decodeBCD( mABits, 29, 25 ); // 1-12
mFixDayOfMonth = decodeBCD( mABits, 35, 30 ); // 1-31
mFixDayOfWeek = decodeBCD( mABits, 38, 36 );
mFixHour = decodeBCD( mABits, 44, 39 ); // 0-23
mFixMinute = decodeBCD( mABits, 51, 45 ); // 0-59
#ifdef DEBUG
Serial.println("Decoded");
Serial.print(2000+(int)mFixYear);
Serial.print("/");
Serial.print((int)mFixMonth);
Serial.print("/");
Serial.println((int)mFixDayOfMonth);
//Serial.println((int)mFixDayOfWeek);
Serial.print((int)mFixHour);
Serial.print(":");
Serial.println((int)mFixMinute);
#endif
}
boolean MSFTime::checkValid()
{
boolean result = true;
if( getBit( mABits, 52 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 52A not 0!");
#endif
result = false;
}
if( ! getBit( mABits, 53 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 53A not 1!");
#endif
result = false;
}
if( ! getBit( mABits, 54 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 54A not 1!");
#endif
result = false;
}
if( ! getBit( mABits, 55 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 55A not 1!");
#endif
result = false;
}
if( ! getBit( mABits, 56 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 56A not 1!");
#endif
result = false;
}
if( ! getBit( mABits, 57 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 57A not 1!");
#endif
result = false;
}
if( ! getBit( mABits, 58 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 58A not 1!");
#endif
result = false;
}
if( getBit( mABits, 59 ))
{
#ifdef EXTRA_DEBUG
Serial.println("bit 59A not 0!");
#endif
result = false;
}
if( ! checkParity( mABits, 17, 24, getBit( mBBits, 54 )))
result = false;
if( ! checkParity( mABits, 25,35, getBit( mBBits, 55 )))
result = false;
if( ! checkParity( mABits, 36,38, getBit( mBBits, 56 )))
result = false;
if( ! checkParity( mABits, 39, 51, getBit( mBBits, 57 )))
result = false;
return result;
}
boolean MSFTime::checkParity( byte *bits, int from, int to, boolean p )
{
int set = 0;
int b;
for( b = from; b <= to; b ++ )
if( getBit( bits, b ))
set++;
if( p )
set ++;
if( set & 0x01 ) // must be an odd number of set bits
return true;
#ifdef EXTRA_DEBUG
Serial.print("Failed parity for bits ");
Serial.print(from);
Serial.print("->");
Serial.println(to);
#endif
}
byte MSFTime::getProgess()
{
if( mIsReading )
return mBitIndex;
else
return mGoodPulses;
}
long MSFTime::getFixAge()
{
if( mFixMillis == 0 )
return 0;
return millis() - mFixMillis;
}
byte MSFTime::getStatus()
{
byte result = 0;
if( (millis() - mOffStartMillis) < 5000L)
result = result | MSF_STATUS_CARRIER; // got radio activity of some sort
if( mBitIndex > 1 )
result = result | MSF_STATUS_READING;
else
if( result & MSF_STATUS_CARRIER )
result = result | MSF_STATUS_WAITING;
if( (millis() - mFixMillis) < 60L * 60000L )
result = result | MSF_STATUS_FIX; // got a fix that's less than an hour old
if( (millis() - mFixMillis) < 62000L )
result = result | MSF_STATUS_FRESH_FIX; // got a fix on our last cycle
return result;
}
time_t MSFTime::getTime()
{
tmElements_t tm;
if( mFixMillis == 0 )
{
#ifdef DEBUG
//Serial.println("getTime - no fix");
#endif
return (time_t) 0; // not got a fix
}
tm.Year = mFixYear + 2000 - 1970; // convert from MSF's years since 2000 into Time's years since 1970
tm.Month = mFixMonth; // 1-12
tm.Day = mFixDayOfMonth; // 1-31
tm.Hour = mFixHour; // 0-23
tm.Minute = mFixMinute; // 0-59
tm.Second = 0; // 0-59
time_t time = makeTime(tm) + (millis() - mFixMillis) / 1000; // add the time at the last fix to the interval since the last fix
#ifdef DEBUG
Serial.println("getTime has time");
Serial.println(time);
#endif
return time;
}
void MSFTime::setBit( byte*bits, int bitIndex, byte value )
{
byte mask = 1 << (bitIndex & 0x7);
if( value )
bits[bitIndex>>3] = bits[bitIndex>>3] | mask;
else
bits[bitIndex>>3] = bits[bitIndex>>3] & ( ~ mask );
}
boolean MSFTime::getBit( byte*bits, int bitIndex )
{
byte mask = 1 << (bitIndex & 0x7);
return (bits[bitIndex>>3] & mask) != 0 ;
}
byte BCD[] = { 1,2,4,8,10,20,40,80 };
byte MSFTime::decodeBCD( byte *bits, byte lsb, byte msb )
{
byte result = 0;
byte b = lsb;
byte d = 0;
for( ; b >= msb; b --, d ++ )
{
if( getBit( bits, b ))
result += BCD[ d ];
}
return result;
}