-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtime-signal.c
170 lines (144 loc) · 4.36 KB
/
time-signal.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
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
/*
time-signal.c - a JJY/MSF/WWVB/DCF77 radio transmitter for Raspberry Pi
Copyright (C) 2023 Pierre Brial <[email protected]>
Parts of this code is based on txtempus code written by Henner Zeller
Source: https://github.com/hzeller/txtempus
Copyright (C) 2018 Henner Zeller <[email protected]>
Licensed under the GNU General Public License, version 3 or later
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, either version 3 of the License, or
(at your option) any later version.
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 <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <sched.h>
#include <signal.h>
#include <string.h>
#include <getopt.h>
#include "time-services.h"
#include "clock-control.h"
int usage(const char *msg, const char *progname)
{
fprintf(stderr, "%susage: %s [options]\n"
"Options:\n"
"\t-s <service> : Service; one of "
"'DCF77', 'WWVB', 'JJY40', 'JJY60', 'MSF'\n"
"\t-v : Verbose.\n"
"\t-c : Carrier wave only.\n"
"\t-h : This help.\n",
msg, progname);
return 1;
}
void signaux(int sigtype)
{
switch(sigtype)
{
case SIGINT : printf ("\nSIGINT");break;
case SIGTERM : printf ("\nSIGTERM");break;
default : printf ("\nUnknow %d",sigtype);
}
StopClock();
printf (" signal received - Programme terminé\n");
exit(0);
}
int main(int argc, char *argv[])
{
bool
verbose = false,
carrier_only = false;
int
modulation,
frequency=60000,
opt;
uint64_t minute_bits;
char
*time_source="",
date_string[] = "1969-07-21 00:00:00"; //,*lineptr;
//size_t n=0;
enum time_service service;
time_t now,minute_start;
struct timespec target_wait;
struct tm tv;
//FILE *f;
signal(SIGINT,signaux);
signal(SIGTERM,signaux);
puts("time-signal, a JJY/MSF/WWVB/DCF77 radio transmitter");
puts("Copyright (C) 2023 Pierre Brial");
puts("This program comes with ABSOLUTELY NO WARRANTY.");
puts("This is free software, and you are welcome to");
puts("redistribute it under certain conditions.\n");
while ((opt = getopt(argc, argv, "vs:hc")) != -1)
{
switch (opt)
{
case 'v':
verbose = true;
break;
case 's':
time_source = optarg;
break;
case 'c':
carrier_only = true;
break;
default:
return usage("", argv[0]);
}
}
if (strcasecmp(time_source, "DCF77") == 0)
{
frequency = 77500;
service = DCF77;
}
else if (strcasecmp(time_source, "WWVB") == 0) service = WWVB;
else if (strcasecmp(time_source, "JJY40") == 0)
{
frequency = 40000;
service = JJY;
}
else if (strcasecmp(time_source, "JJY60") == 0) service = JJY;
else if (strcasecmp(time_source, "MSF") == 0) service = MSF;
else return usage("Please choose a service name with -s option\n", argv[0]);
GPIO_init ();
StartClock(frequency);
if (carrier_only) EnableClockOutput(1);
// Give max priority to this programm
struct sched_param sp;
sp.sched_priority = 99;
sched_setscheduler(0, SCHED_FIFO, &sp);
now = time(NULL);
minute_start = now - now % 60; // round to minute
while(1)
{
if (carrier_only) continue;
localtime_r(&minute_start, &tv);
strftime(date_string, sizeof(date_string), "%Y-%m-%d %H:%M:%S", &tv);
printf("%s\n",date_string);
minute_bits = prepareMinute(service,minute_start);
for (int second = 0; second < 60; ++second)
{
modulation = getModulationForSecond(service,minute_bits,second);
// First, let's wait until we reach the beginning of that second
target_wait.tv_sec = minute_start + second;
target_wait.tv_nsec = 0;
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target_wait, NULL);
if (service == JJY) EnableClockOutput(1); // Set signal to HIGH
else EnableClockOutput(0); // Set signal to LOW
if (verbose)
{
fprintf(stderr,"%03d ",modulation);
if ((second+1)%15 == 0) fprintf(stderr,"\n");
}
target_wait.tv_nsec = modulation*1e6;
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target_wait, NULL);
if (service == JJY) EnableClockOutput(0); //signal to LOW
else EnableClockOutput(1); // Set signal to HIGH
}
minute_start += 60;
}
}