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
//! A simple GPIO based DCF77 decoder
//!
//! This driver was built using [`embedded-hal`] traits.
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.2

#![deny(warnings)]
#![no_std]

/// A structure to facilitate the decoding of a DCF77 signal which consists of 59 consecutive bits
/// of data 
pub struct DCF77Time(pub u64);

impl DCF77Time {
    /// Generate an empty value for the storage of the DCF77 data
    pub fn new(dcf77bits: u64) -> Self {
        DCF77Time { 0: dcf77bits }
    }

    /// Validate the correct value of the start bit
    pub fn validate_start(&self) -> Result<(), ()> {
        if (self.0 & (1 << 0)) != 0 {
            Err(())
        } else {
            Ok(())
        }
    }

    /// Return whether summer time is signalled (without verifying the information)
    pub fn cest_unchecked(&self) -> bool {
        if (self.0 & (1 << 17)) != 0 {
            return true;
        }

        false
    }

    /// Return whether summer time is signalled with verification of the counter bit
    pub fn cest(&self) -> Result<bool, ()> {
        let cest = self.cest_unchecked();

        if ((self.0 & (1 << 18)) != 0) == cest {
            Err(())
        } else {
            Ok(cest)
        }
    }

    /// Return the current minutes of the hour (without verifying the information)
    pub fn minutes_unchecked(&self) -> u8 {
        let mut minutes = 0;
        if (self.0 & (1 << 21)) != 0 {
            minutes += 1;
        }

        if (self.0 & (1 << 22)) != 0 {
            minutes += 2;
        }

        if (self.0 & (1 << 23)) != 0 {
            minutes += 4;
        }

        if (self.0 & (1 << 24)) != 0 {
            minutes += 8;
        }

        if (self.0 & (1 << 25)) != 0 {
            minutes += 10;
        }

        if (self.0 & (1 << 26)) != 0 {
            minutes += 20;
        }

        if (self.0 & (1 << 27)) != 0 {
            minutes += 40;
        }

        minutes
    }

    /// Return the current minutes of the hour and verify parity and value < 60
    pub fn minutes(&self) -> Result<u8, ()> {
        let mut parity = false;
        if (self.0 & (1 << 21)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 22)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 23)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 24)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 25)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 26)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 27)) != 0 {
            parity ^= true;
        }

        let minutes = self.minutes_unchecked();
        if minutes > 59 {
            return Err(());
        }

        if ((self.0 & (1 << 28)) != 0) != parity {
            Err(())
        } else {
            Ok(minutes)
        }
    }

    /// Return the current hours of the day (without verifying the information)
    pub fn hours_unchecked(&self) -> u8 {
        let mut hours = 0;
        if (self.0 & (1 << 29)) != 0 {
            hours += 1;
        }

        if (self.0 & (1 << 30)) != 0 {
            hours += 2;
        }

        if (self.0 & (1 << 31)) != 0 {
            hours += 4;
        }

        if (self.0 & (1 << 32)) != 0 {
            hours += 8;
        }

        if (self.0 & (1 << 33)) != 0 {
            hours += 10;
        }

        if (self.0 & (1 << 34)) != 0 {
            hours += 20;
        }

        hours
    }

    /// Return the current hours of the day and verify parity and value < 23
    pub fn hours(&self) -> Result<u8, ()> {
        let mut parity = false;
        if (self.0 & (1 << 29)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 30)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 31)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 32)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 33)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 34)) != 0 {
            parity ^= true;
        }

        let hours = self.hours_unchecked();
        if hours > 23 {
            return Err(());
        }

        if ((self.0 & (1 << 35)) != 0) != parity {
            Err(())
        } else {
            Ok(hours)
        }
    }

    /// Return the current day of month (without verifying the information)
    pub fn day_unchecked(&self) -> u8 {
        let mut day = 0;
        if (self.0 & (1 << 36)) != 0 {
            day += 1;
        }

        if (self.0 & (1 << 37)) != 0 {
            day += 2;
        }

        if (self.0 & (1 << 38)) != 0 {
            day += 4;
        }

        if (self.0 & (1 << 39)) != 0 {
            day += 8;
        }

        if (self.0 & (1 << 40)) != 0 {
            day += 10;
        }

        if (self.0 & (1 << 41)) != 0 {
            day += 20;
        }

        day
    }

    /// Return the current day of month and do a basic value check
    pub fn day(&self) -> Result<u8, ()> {
        let day = self.day_unchecked();
        if day > 31 {
            Err(())
        } else {
            Ok(day)
        }
    }

    /// Return the current day of the week (without verifying the information)
    /// 0 meaning Monday
    pub fn weekday_unchecked(&self) -> u8 {
        let mut weekday = 0;
        if (self.0 & (1 << 42)) != 0 {
            weekday += 1;
        }

        if (self.0 & (1 << 43)) != 0 {
            weekday += 2;
        }

        if (self.0 & (1 << 44)) != 0 {
            weekday += 4;
        }
        weekday
    }

    /// Return the current month of the year (without verifying the information)
    pub fn month_unchecked(&self) -> u8 {
        let mut month = 0;
        if (self.0 & (1 << 45)) != 0 {
            month += 1;
        }

        if (self.0 & (1 << 46)) != 0 {
            month += 2;
        }

        if (self.0 & (1 << 47)) != 0 {
            month += 4;
        }

        if (self.0 & (1 << 48)) != 0 {
            month += 8;
        }

        if (self.0 & (1 << 49)) != 0 {
            month += 10;
        }

        month
    }

    /// Return the current year (without verifying the information)
    pub fn year_unchecked(&self) -> u16 {
        let mut year = 2000;
        if (self.0 & (1 << 50)) != 0 {
            year += 1;
        }

        if (self.0 & (1 << 51)) != 0 {
            year += 2;
        }

        if (self.0 & (1 << 52)) != 0 {
            year += 4;
        }

        if (self.0 & (1 << 53)) != 0 {
            year += 8;
        }

        if (self.0 & (1 << 54)) != 0 {
            year += 10;
        }

        if (self.0 & (1 << 55)) != 0 {
            year += 20;
        }

        if (self.0 & (1 << 56)) != 0 {
            year += 40;
        }

        if (self.0 & (1 << 57)) != 0 {
            year += 80;
        }

        year
    }

    /// Return a tuple of (year, month, day, weekday) if it passes a parity check
    pub fn date(&self) -> Result<(u16, u8, u8, u8), ()> {
        let mut parity = false;
        if (self.0 & (1 << 36)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 37)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 38)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 39)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 40)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 41)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 42)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 43)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 44)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 45)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 46)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 47)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 48)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 49)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 50)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 51)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 52)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 53)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 54)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 55)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 56)) != 0 {
            parity ^= true;
        }

        if (self.0 & (1 << 57)) != 0 {
            parity ^= true;
        }

        if ((self.0 & (1 << 58)) != 0) != parity {
            return Err(());
        }

        let year = self.year_unchecked();
        let month = self.month_unchecked();
        let day = self.day_unchecked();
        let weekday = self.weekday_unchecked();

        if year > 2100 || month > 12 || day > 31 || weekday > 7 {
            Err(())
        } else {
            Ok((year, month, day, weekday))
        }
    }
}

enum SimpleDCF77DecoderState {
    WaitingForPhase,
    PhaseFound,
    BitReceived,
    FaultyBit,
    EndOfCycle,
    Idle,
}

/// A structure for a simple timeslot based DCF77 decoder
pub struct SimpleDCF77Decoder  {
    scancount: u8,
    lowcount: u8,
    highcount: u8,
    idlecount: u8,
    state: SimpleDCF77DecoderState,
    data: u64,
    datapos: usize,
}

/// The SimpleDCF77Decoder implements a simple state machine to decode a DCF77 signal from a fed-in
/// readout of a GPIO pin connected to a DCF77 receiver. To use this, create the structure, set up
/// the GPIO pin the receiver is connected to as an input and call the `read_bit` method every
/// 10ms with a parameter value of `true` for a high signal level or `false` for a low signal level
impl SimpleDCF77Decoder {
    /// Create a new decoder state machine
    pub fn new() -> Self {
        Self {
            scancount: 0,
            lowcount: 0,
            highcount: 0,
            idlecount: 0,
            state: SimpleDCF77DecoderState::WaitingForPhase,
            data: 0,
            datapos: 0,
        }
    }

    /// Return the raw data as `u64` value for decoding of the current date/time
    pub fn raw_data(&self) -> u64 {
        self.data
    }

    /// Returns true as soon as an individual bit was received
    pub fn bit_complete(&self) -> bool {
        match self.state {
            SimpleDCF77DecoderState::BitReceived => true,
            _ => false,
        }
    }

    /// Returns true if the last bit couldn't be identified as high/low
    pub fn bit_faulty(&self) -> bool {
        match self.state {
            SimpleDCF77DecoderState::FaultyBit => true,
            _ => false,
        }
    }

    /// Returns true if the end of a 59s cycle was detected
    pub fn end_of_cycle(&self) -> bool {
        match self.state {
            SimpleDCF77DecoderState::EndOfCycle => true,
            _ => false,
        }
    }

    /// Returns the value of the latest received bit. Mainly useful for live display of the
    /// received bits
    pub fn latest_bit(&self) -> bool {
        (self.data & (1 << (self.datapos - 1))) != 0
    }

    /// Return the current position of the bit counter after the latest recognized end of a cycle
    /// which is identical to the current second of the minute
    pub fn seconds(&self) -> usize {
        self.datapos
    }

    /// Ingest the latest sample of the GPIO input the DCF77 receiver is connected to judge the /
    /// current position and value of the DCF77 signal bitstream
    pub fn read_bit(&mut self, bit: bool) {
        self.state = match self.state {
            SimpleDCF77DecoderState::EndOfCycle | SimpleDCF77DecoderState::WaitingForPhase | SimpleDCF77DecoderState::FaultyBit => {
                if bit {
                    self.lowcount = 1;
                    self.highcount = 0;
                    self.scancount = 0;
                    SimpleDCF77DecoderState::PhaseFound
                } else {
                    if self.scancount > 180 {
                        self.datapos = 0;
                        self.scancount = 0;

                        SimpleDCF77DecoderState::EndOfCycle
                    } else {
                        SimpleDCF77DecoderState::WaitingForPhase
                    }
                }
            }
            SimpleDCF77DecoderState::PhaseFound => {
                if self.scancount < 20 {
                    if bit {
                        if self.scancount < 10 {
                            self.lowcount += 1;
                        } else {
                            self.highcount += 1;
                        }
                    }
                    SimpleDCF77DecoderState::PhaseFound
                } else {
                    let datapos = self.datapos;
                    self.datapos += 1;
                    if self.highcount > 3 {
                        self.data |= 1 << datapos;
                        SimpleDCF77DecoderState::BitReceived
                    } else if self.lowcount > 3 {
                        self.data &= !(1 << datapos);
                        SimpleDCF77DecoderState::BitReceived
                    } else {
                        // Bad signal, let's continue with the next bit
                        SimpleDCF77DecoderState::FaultyBit
                    }
                }
            }
            SimpleDCF77DecoderState::BitReceived | SimpleDCF77DecoderState::Idle => {
                if bit {
                    self.idlecount += 1;
                }

                if self.scancount >= 90 {
                    if self.idlecount > 10 {
                        self.idlecount = 0;
                        self.scancount = 0;
                    }
                    SimpleDCF77DecoderState::WaitingForPhase
                } else {
                    SimpleDCF77DecoderState::Idle
                }
            }
        };

        self.scancount += 1;
    }
}