aboutsummaryrefslogtreecommitdiff
path: root/clock-disp.c
blob: d7986b9cf54d7abe4159de7f756e9875a99805fb (plain)
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
#include <SDL.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>

enum ALIGN_MODE {
	ALIGN_MODE_LEFT,
	ALIGN_MODE_CENTRE,
	ALIGN_MODE_RIGHT,
};

//#define DEBUG
#ifdef DEBUG
# define DEBUG_P(x...)  printf("DEBUG: "x)
#else
# define DEBUG_P(x...)
#endif

#define WINDOW_TITLE "Clock Display"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 480

#define TIME_X 0
#define TIME_Y -50

#define NO_LOCK_Y 150

#define DATE_1_X 20
#define DATE_1_Y 295
#define DATE_2_X 20
#define DATE_2_Y 350

#define SECONDS_X SCREEN_WIDTH - 80
#define SECONDS_Y SCREEN_HEIGHT - 55

#define NTP_INFO_X 240
#define NTP_INFO_Y SCREEN_HEIGHT - 55
#define SAT_INFO_X 20
#define SAT_INFO_Y NTP_INFO_Y

#define SUN_INFO_X SCREEN_WIDTH -20
#define SUN_INFO_1_Y DATE_1_Y
#define SUN_INFO_2_Y DATE_2_Y

/* Common colours used */
#define COLOUR_WHITE  ((SDL_Color){255,255,255,255})
#define COLOUR_YELLOW ((SDL_Color){255,191, 32,255})
#define COLOUR_40GRAY ((SDL_Color){100,100,100,255})

static sem_t data_lock;
struct clock_data {
	int is_unread;
	char sunrise_time[8];
	char sunset_time[8];
	int sat_seen;
	int sat_used;
	int leap_second;
	int offset;
	char offset_units[8];
	int no_gps_lock;
};

static volatile struct clock_data shared_data;

/**
 * Publish a struct clock_data to the volatile global clock data object.
 * Caller MUST already hold data_lock
 */
void publish_clock_data(struct clock_data *l)
{
	char *d = (char*)&shared_data;
	char *s = (char*)l;
	size_t i = 0;
	for (i = 0; i < sizeof(struct clock_data); i++) {
		d[i] = s[i];
	}
	shared_data.is_unread = 1;
}

/**
 * Copy the published struct clock_data to a non-volatile clock data object.
 * Caller MUST already hold data_lock
 */
void get_clock_data(struct clock_data *l)
{
	char *d = (char*)l;
	char *s = (char*)&shared_data;
	size_t i = 0;
	for (i = 0; i < sizeof(struct clock_data); i++) {
		d[i] = s[i];
	}
}

int calculate_brightness(int midday_mins, int now_mins)
{
#define MIN_BRIGHTNESS 11
#define MAX_BRIGHTNESS 255
#define MIN_BRIGHT_VIRTUAL (-150)
#define MAX_BRIGHT_VIRTUAL 400
	int ret = 0;

	DEBUG_P("midday %d, now %d minutes\n", midday_mins, now_mins);

	ret = MAX_BRIGHT_VIRTUAL - ((MAX_BRIGHT_VIRTUAL - MIN_BRIGHT_VIRTUAL) * abs(now_mins - midday_mins)) / midday_mins;

	if (ret < MIN_BRIGHTNESS)
		ret = MIN_BRIGHTNESS;

	if (ret > MAX_BRIGHTNESS)
		ret = MAX_BRIGHTNESS;

	return ret;
}

/**
 * Read clock's statistics from files and push them into shared data area for
 * display thread to pick up
 */
void *read_stats(void* brightness_f_p)
{
	char buffer[32];
	char *next = buffer;
	FILE *f = NULL;
	struct clock_data l;
	memset(&l, 0, sizeof(l));

	FILE* brightness_f = brightness_f_p;

	while (1) {
		/* Read sunrise, sunset */
		if (f = fopen("/tmp/stats-sun", "r")) {
			if (fgets(buffer, sizeof(buffer), f)) {
				/* format is %d/%d\n, first is used sats, second is total seen */
				if (next = strchr(buffer, ',')) {
					*(next++) = '\0';
					next[strcspn(next, "\n\r\t")] = '\0';
					strncpy(l.sunrise_time, buffer, sizeof(l.sunrise_time));
					strncpy(l.sunset_time, next, sizeof(l.sunset_time));
				}
			}
			fclose(f);
		}

		/* Read satellite stats */
		if (f = fopen("/tmp/stats-sats", "r")) {
			if (fgets(buffer, sizeof(buffer), f)) {
				/* format is %d,%d\n, first is used sats, second is total seen */
				if (next = strchr(buffer, '/')) {
					*(next++) = '\0';
					l.sat_used = strtod(buffer, NULL);
					l.sat_seen = strtod(next, NULL);
				}
			}
			fclose(f);
		}

		/* read leap second status file - also helps determine lock */
		if (f = fopen("/tmp/stats-leap", "r")) {
			if (fgets(buffer, sizeof(buffer), f)) {
				buffer[strcspn(buffer, "\n\r\t")] = '\0';
				l.no_gps_lock = !strcmp(buffer, "Not synchronised");
				l.leap_second = strcmp(buffer, "Normal");
			}
			fclose(f);
		}

		if (f = fopen("/tmp/stats-clock", "r")) {
			if (fgets(buffer, sizeof(buffer), f)) {
				const char *units[] = { "s", "ms", "µs", "ns", "ps", "fs" };
				int i = 0;
				double offset = fabs(strtod(buffer, NULL));
				while (offset < 1 && i < sizeof(units)/sizeof(units[0]) - 1) {
					i++;
					offset *= 1000;
				}
				l.offset = offset;
				strncpy(l.offset_units, units[i], sizeof(l.offset_units));
			}
			fclose(f);
		}

		/* FIXME move decls to top */
		int sunrise_hours = 0;
		int sunrise_minutes = 0;
		int sunset_hours = 0;
		int sunset_minutes = 0;
		if (   sscanf(l.sunrise_time, "%d:%d", &sunrise_hours, &sunrise_minutes) == 2
		    && sscanf(l.sunset_time, "%d:%d", &sunset_hours, &sunset_minutes) == 2) {
			sunrise_minutes += 60 * sunrise_hours;
			sunset_minutes += 60 * sunset_hours;
			int local_midday = (sunrise_minutes + sunset_minutes) / 2;

			time_t t;
			struct tm *time_s;
			t = time(NULL);
			if ((time_s = localtime(&t)) == NULL) {
				/* FIXME error */
				continue;
			}

			int brightness = calculate_brightness(local_midday, time_s->tm_hour*60 + time_s->tm_min);
			DEBUG_P("Calculated brightness %d\n", brightness);
			if (!brightness_f) {
				DEBUG_P("Brightness file not open, not writing to it\n");
			} else {
				snprintf(buffer, sizeof(buffer), "%d\n", brightness);
				if (fwrite(buffer, strlen(buffer), 1, brightness_f) != strlen(buffer)) {
					perror("fwrite");
				}
				rewind(brightness_f);
			}
		}

		sem_wait(&data_lock);
		publish_clock_data(&l);
		sem_post(&data_lock);
		sleep(5);
	}
}

const char *get_th_suffix(unsigned int day)
{
	if (11 <= day && day <= 19) {
		return "th";
	} else {
		switch (day % 10) {
			case 1: return "st";
			case 2: return "nd";
			case 3: return "rd";
			default: /* 0, 4-9 */
				return "th";
		}
	}
}

TTF_Font *loadFont(const char *file, int size)
{
	TTF_Font *font = TTF_OpenFont(file, size);
	if (!font) {
		printf("Error: TTF_OpenFont: %s\n", TTF_GetError());
		return NULL;
	}
	return font;
}

void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y){
	//Setup the destination rectangle to be at the position we want
	SDL_Rect dst;
	dst.x = x;
	dst.y = y;
	//Query the texture to get its width and height to use
	SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
	SDL_RenderCopy(ren, tex, NULL, &dst);
}


SDL_Texture* renderText(const char *message, TTF_Font *font, SDL_Color color, SDL_Renderer *renderer)
{
	//We need to first render to a surface as that's what TTF_RenderText
	//returns, then load that surface into a texture
	SDL_Surface *surf = TTF_RenderUTF8_Blended(font, message, color);
	if (!surf){
		printf("Error in TTF_RenderUTF8_Blended\n");
		return NULL;
	}
	SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
	if (!texture){
		printf("Error in SDL_CreateTextureFromSurface\n");
	}
	//Clean up the surface and font
	SDL_FreeSurface(surf);
	return texture;
}

int drawText(const char *text, TTF_Font *font, SDL_Color color, SDL_Renderer *renderer, int x, int y, enum ALIGN_MODE mode) {
	/* FIXME does this need better error detection */
	int text_w;
	SDL_Texture *image;
	image = renderText(text, font, color, renderer);
	SDL_QueryTexture(image, NULL, NULL, &text_w, NULL);

	if (mode == ALIGN_MODE_CENTRE) {
		x = SCREEN_WIDTH / 2 - text_w / 2;
	} else if (mode == ALIGN_MODE_RIGHT) {
		x -= text_w;
	}
	renderTexture(image, renderer, x, y);
	SDL_DestroyTexture(image);
	return x + (mode == ALIGN_MODE_RIGHT? 0 : 1) * text_w;
}

int main(void)
{
	struct clock_data l;

	/* initial unread indicator value */
	shared_data.is_unread = 0;

	strncpy(l.sunrise_time, "??:??", sizeof(l.sunrise_time));
	strncpy(l.sunset_time, "??:??", sizeof(l.sunset_time));
	l.sat_seen  = 0;
	l.sat_used  = 0;
	l.leap_second = 0;
	l.offset = 0;
	strncpy(l.offset_units, "?", sizeof(l.offset_units));
	l.no_gps_lock = 1;

	if (sem_init(&data_lock, 0, 1)) {
		perror("sem_init");
		return 1;
	}

	int stop = 0;
	SDL_Event e;
	SDL_Window *window;

	SDL_Init(SDL_INIT_VIDEO);
	TTF_Init();

	TTF_Font *mainTimeFont = loadFont("/usr/share/fonts/TTF/RobotoMono-Regular.ttf", 325);
	TTF_Font *noLockFont = loadFont("/usr/share/fonts/TTF/Roboto-Regular.ttf", 130);
	TTF_Font *secondsFont =