forked from rene0/dcf77pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcf77pi-readpin.c
128 lines (113 loc) · 2.46 KB
/
dcf77pi-readpin.c
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
// Copyright 2013-2020 René Ladan
// SPDX-License-Identifier: BSD-2-Clause
#include "input.h"
#include "json_util.h"
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
static struct json_object *config;
static volatile sig_atomic_t running = 1;
static void
client_cleanup()
{
/* Caller is supposed to exit the program after this. */
cleanup();
free(config);
}
static void
sigint_handler(/*@unused@*/ int sig)
{
running = 0;
}
int
main(int argc, char *argv[])
{
struct sigaction sigact;
struct hardware hw;
int ch, min, res;
bool raw = false, verbose = true;
while ((ch = getopt(argc, argv, "qr")) != -1) {
switch (ch) {
case 'q':
verbose = false;
break;
case 'r':
raw = true;
break;
default:
printf("usage: %s [-qr]\n", argv[0]);
return EX_USAGE;
}
}
config = json_object_from_file(ETCDIR "/config.json");
if (config == NULL) {
client_cleanup();
return EX_NOINPUT;
}
res = set_mode_live(config);
if (res != 0) {
client_cleanup();
return res;
}
hw = get_hardware_parameters();
sigact.sa_handler = sigint_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
min = -1;
while (running == 1) {
struct bitinfo bi;
struct GB_result bit;
if (raw) {
struct timespec slp;
slp.tv_sec = 0;
slp.tv_nsec = 1e9 / hw.freq;
printf("%i", get_pulse());
fflush(stdout);
while (nanosleep(&slp, &slp) > 0)
; /* empty loop */
continue;
}
bit = get_bit_live();
bi = get_bitinfo();
if (verbose) {
if (bi.freq_reset) {
printf("!");
}
/* display first bi->t pulses */
for (unsigned long long i = 0; i < bi.t / 8; i++) {
for (unsigned j = 0; j < 8; j++) {
printf("%c", (bi.signal[i] & (1 << j)) >
0 ? '+' : '-');
}
}
/*
* display pulses in the last partially filled item
* bi.t is 0-based, hence the <= comparison
*/
for (unsigned j = 0; j <= (bi.t & 7); j++) {
printf("%c", (bi.signal[bi.t / 8] & (1 << j)) >
0 ? '+' : '-');
}
printf("\n");
}
if (bit.marker == emark_toolong || bit.marker == emark_late) {
printf("Too long minute!\n");
min++;
}
printf("%i %i %i %u %llu %llu %llu %i:%i\n", bit.bitval, bi.tlow,
bi.tlast0, bi.t, bi.bit0, bi.bit20, bi.realfreq, min,
get_bitpos());
if (bit.marker == emark_minute) {
min++;
}
bit = next_bit();
}
client_cleanup();
printf("done\n");
return 0;
}