1 /*
2  * Tlf - contest logging program for amateur radio operators
3  * Copyright (C) 2001-2003 Rein Couperus <pa0rct@amsat.org>
4  *               2011-2012 Thomas Beierlein <tb@forth-ev.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 /* ------------------------------------------------------------
21  *
22  *              Update time
23  *
24  *              also updates windows every second
25  *--------------------------------------------------------------*/
26 
27 
28 #include <string.h>
29 #include <math.h>
30 
31 #include "bandmap.h"
32 #include "clusterinfo.h"
33 #include "freq_display.h"
34 #include "get_time.h"
35 #include "getwwv.h"
36 #include "globalvars.h"		// Includes glib.h and tlf.h
37 #include "lancode.h"
38 #include "printcall.h"
39 #include "showscore.h"
40 #include "tlf_curses.h"
41 #include "trx_memory.h"
42 #include "ui_utils.h"
43 
44 /** broadcast to LAN
45  *
46  * every 120s broadcast frequency via LAN and
47  * act as time master if allowed */
broadcast_lan(void)48 void broadcast_lan(void) {
49 
50     extern int time_master;
51     static int frcounter = 0;
52 
53     frcounter++;
54 
55     if (frcounter >= 60) {	// every 60 calls
56 	frcounter = 0;
57 	if (lan_active != 0) {
58 	    send_freq(freq);
59 	    if (time_master == 1)
60 		send_time();
61 	}
62     }
63 }
64 
65 
66 /** update band, date and time */
update_line(char * timestr)67 void update_line(char *timestr) {
68 
69     extern struct tm *time_ptr;
70 
71     char time_buf[40];
72 
73     attron(COLOR_PAIR(C_WINDOW) | A_STANDOUT);
74 
75     mvaddstr(12, 0, band[bandinx]);
76 
77     strncpy(time_buf, timestr, 8);
78     mvaddstr(12, 17, time_buf);
79     strftime(time_buf, 60, "%d-%b-%y", time_ptr);
80     mvaddstr(12, 7, time_buf);
81 }
82 
83 const char *FREQ_DISPLAY_FORMAT = " %s: %7.1f";
84 
85 bool force_show_freq = false;
86 
87 /** show frequency and memory if rig control is active */
show_freq(void)88 void show_freq(void) {
89 
90     extern int trx_control;
91 
92     attron(modify_attr(COLOR_PAIR(C_LOG)));
93 
94     freq_t memfreq = 0;
95 
96     if (trx_control) {
97 	mvprintw(13, 67, FREQ_DISPLAY_FORMAT, "TRX", freq / 1000.0);
98 	memfreq = memory_get_freq();
99     } else {
100 	mvprintw(13, 67, spaces(80 - 67));
101     }
102 
103     if (memfreq > 0) {
104 	mvprintw(14, 67, FREQ_DISPLAY_FORMAT, "MEM", memfreq / 1000.0);
105     } else {
106 	mvprintw(14, 67, spaces(80 - 67));
107     }
108 
109 }
110 
111 
time_update(void)112 void time_update(void) {
113 
114     extern struct tm *time_ptr;
115     extern char qsonrstr[];
116     extern int bandinx;
117     extern int this_second;
118     extern int system_secs;
119     extern int miniterm;
120 
121     char time_buf[11];
122     int currentterm = 0;
123     static int s = 0;
124     static int bm_timeout = 0;
125     static int oldsecs = -1;  	/* trigger immediate update */
126 
127     get_time();
128     this_second = time_ptr->tm_sec;		/* seconds */
129     // used in background_process
130     system_secs = time_ptr->tm_min * 60 + time_ptr->tm_sec;
131 
132     // force frequency display if it has changed (don't wait until next second)
133     static freq_t old_freq = 0;
134     if (freq > 0 && fabs(freq - old_freq) >= 100) {
135 	force_show_freq = true;
136 	old_freq = freq;
137     }
138 
139     if (force_show_freq) {
140 	show_freq();
141 	clusterinfo();
142     }
143 
144     if (this_second == oldsecs) {   // still in the same second, no action
145 	force_show_freq = false;
146 	return;
147     }
148 
149     /* do it every second */
150     oldsecs = this_second;
151 
152     if (wpx == 1) {
153 	if (minute_timer > 0)
154 	    minute_timer--;
155     }
156 
157     if (!force_show_freq) {     // do not show again if it was forced
158 	show_freq();
159     }
160     force_show_freq = false;
161 
162     bandmap_age();		/* age bandmap spots every second */
163     clusterinfo();		/* update cluster and bandmap display */
164 
165     /* write bandmap spots to file every 10s */
166     bm_timeout = (bm_timeout + 1) % 10;
167     if (bm_timeout == 0) {
168 
169 	bmdata_write_file();
170     }
171 
172     s = (s + 1) % 2;
173     if (s > 0) {		/* every 2 seconds */
174 
175 	strftime(time_buf, 10, "%H:%M:%S", time_ptr);
176 	time_buf[5] = '\0';
177 
178 	static time_t prev_wwv_time = 0;
179 	if (lastwwv_time > prev_wwv_time) { // is there a newer WWV message?
180 	    prev_wwv_time = lastwwv_time;
181 	    attron(COLOR_PAIR(C_HEADER) | A_STANDOUT);
182 	    mvprintw(LINES - 1, 0, backgrnd_str);
183 	    wwv_show_footer();              // print WWV info
184 	}
185 
186 	currentterm = miniterm;
187 	miniterm = 0;
188 
189 	broadcast_lan();
190 	update_line(time_buf);
191 
192 	attron(COLOR_PAIR(C_LOG) | A_STANDOUT);
193 
194 	mvprintw(7, 0, logline0);
195 	mvprintw(8, 0, logline1);
196 	mvprintw(9, 0, logline2);
197 	mvprintw(10, 0, logline3);
198 	mvprintw(11, 0, logline4);
199 	mvprintw(13, 0, spaces(67));
200 	attron(COLOR_PAIR(C_WINDOW));
201 	mvprintw(12, 23, qsonrstr);
202 	printcall();
203 
204 	showscore();	/* update  score  window every 2 seconds */
205 	miniterm = currentterm;
206     }
207 }
208