forked from hzeller/txtempus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wwvb-source.cc
53 lines (46 loc) · 2.19 KB
/
wwvb-source.cc
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
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Copyright (C) 2018 Henner Zeller <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
#include "time-signal-source.h"
// WWVB uses BCD, but always has a zero bit between the digits.
// So let's call it 'padded' BCD.
static uint64_t to_padded5_bcd(int n) {
return (((n / 100) % 10) << 10) | (((n / 10) % 10) << 5) | (n % 10);
}
static uint64_t is_leap_year(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
void WWVBTimeSignalSource::PrepareMinute(time_t t) {
struct tm breakdown;
gmtime_r(&t, &breakdown); // Time transmission is always in UTC.
// https://en.wikipedia.org/wiki/WWVB
// The WWVB format uses Bit-Bigendianess, so we'll start with the first
// bit left in our integer in bit 59.
time_bits_ = 0; // All the unused bits are zero.
time_bits_ |= to_padded5_bcd(breakdown.tm_min) << (59 - 8);
time_bits_ |= to_padded5_bcd(breakdown.tm_hour) << (59 - 18);
time_bits_ |= to_padded5_bcd(breakdown.tm_yday + 1) << (59 - 33);
time_bits_ |= to_padded5_bcd(breakdown.tm_year % 100) << (59 - 53);
time_bits_ |= is_leap_year(breakdown.tm_year + 1900) << (59 - 55);
// Need local time to determine DST status. TODO: announcement bits.
localtime_r(&t, &breakdown);
time_bits_ |= (breakdown.tm_isdst ? 0x03 : 0x00) << (59 - 58);
}
TimeSignalSource::SecondModulation
WWVBTimeSignalSource::GetModulationForSecond(int sec) {
if (sec == 0 || sec % 10 == 9 || sec > 59)
return {{CarrierPower::LOW, 800}, {CarrierPower::HIGH, 0}};
const bool bit = time_bits_ & (1LL << (59 - sec));
return {{CarrierPower::LOW, bit ? 500 : 200}, {CarrierPower::HIGH, 0}};
}