aboutsummaryrefslogtreecommitdiff
path: root/chrono.c
blob: 1a3de698e9b6b5f345b0f51e559bf897b2f2a391 (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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <geometry.h>

#define HZ2MS(hz)	(1000/(hz))
#define max(a, b)	((a) > (b)? (a): (b))

enum {
	Stop,
	Pause,
	Run,
};

typedef struct Stopwatch Stopwatch;
struct Stopwatch
{
	uvlong elapsed;		/* in ms */
	char hms[3][6+1];	/* HH MM SS.ss */
	int state;

	void (*start)(Stopwatch*);
	void (*stop)(Stopwatch*);
	void (*pause)(Stopwatch*);
	void (*update)(Stopwatch*, uvlong);
	void (*draw)(Stopwatch*, Image*, Point, double);
};

Rectangle UR = {0,0,1,1};
char deffont[] = "/lib/font/bit/lucida/unicode.32.font";
Image *d7bg, *d7fg;
int d7scale = 60;

Image *screenb;
Keyboardctl *kc;
Mousectl *mc;
Channel *drawc;
Stopwatch *chrono;

uvlong nanosec(void);

static Image *
eallocimage(Display *d, Rectangle r, ulong chan, int repl, ulong col)
{
	Image *i;

	i = allocimage(d, r, chan, repl, col);
	if(i == nil)
		sysfatal("allocimage: %r");
	return i;
}

/*
 * 7-segment display interface
 *
 * 	   A
 * 	   ----
 * 	  |    | B
 * 	F | G  |
 * 	   ----
 * 	  |    | C
 * 	E |    |
 * 	   ----   [] H
 * 	   D
 *
 * bits: 7 6 5 4 3 2 1 0
 * func: H G F E D C B A
 */
static Point
d7(Image *dst, Point dp, uchar bits, int scale, Image *fg, Image *bg)
{
	enum { TV, TH, TD, NSEGS };
	struct {
		Point2 poly[6];
		Point pts[6+1];
		int npts;
	} segs[NSEGS] = {	/* segment parameters */
	 [TV]	{ .poly = {
			{ 1,   0, 1 },
			{ 1.5, 1, 1 },
			{ 1.5, 5, 1 },
			{ 1,   6, 1 },
			{ 0.5, 5, 1 },
			{ 0.5, 1, 1 },
		}, .npts = 6+1 },
	 [TH]	{ .npts = 6+1 },
	 [TD]	{ .poly = {
			{ 0, 0, 1 },
			{ 1, 0, 1 },
			{ 1, 1, 1 },
			{ 0, 1, 1 },
		}, .npts = 4+1 },
	};
	struct {
		Point p;
		int segtype;
	} loc[8] = {		/* segment locations (layout) */
		{ 1,  2, TH },	/* A */
		{ 6,  1, TV },	/* B */
		{ 6,  7, TV },	/* C */
		{ 1, 14, TH },	/* D */
		{ 0,  7, TV },	/* E */
		{ 0,  1, TV },	/* F */
		{ 1,  8, TH },	/* G */
		{ 8, 13, TD },	/* H (dot) */
	};
	Rectangle bbox = {
		{ 0, 0 },
		{ 9, 14 },
	};
	Point segpt[7];
	double maxlen;
	int i, j;

	maxlen = 0;
	for(i = 0; i < segs[TV].npts-1; i++)
		maxlen = max(maxlen, vec2len(segs[TV].poly[i]));

	bbox.max.x = (double)bbox.max.x/maxlen * scale;
	bbox.max.y = (double)bbox.max.y/maxlen * scale;
	for(i = 0; i < nelem(loc); i++){
		loc[i].p.x = (double)loc[i].p.x/maxlen * scale;
		loc[i].p.y = (double)loc[i].p.y/maxlen * scale;
	}

	/* normalize TV and build TH out of it */
	for(i = 0; i < segs[TV].npts-1; i++){
		segs[TV].poly[i] = divpt2(segs[TV].poly[i], maxlen);
		segs[TH].poly[i] = Vec2(segs[TV].poly[i].y, -segs[TV].poly[i].x);

		segs[TV].pts[i] = Pt(segs[TV].poly[i].x*scale, segs[TV].poly[i].y*scale);
		segs[TH].pts[i] = Pt(segs[TH].poly[i].x*scale, segs[TH].poly[i].y*scale);
	}
	segs[TH].pts[segs[TH].npts-1] = segs[TH].pts[0];
	segs[TV].pts[segs[TV].npts-1] = segs[TV].pts[0];

	/* normalize TD */
	for(i = 0; i < segs[TD].npts-1; i++){
		segs[TD].poly[i] = divpt2(segs[TD].poly[i], maxlen);
		segs[TD].pts[i] = Pt(segs[TD].poly[i].x*scale, segs[TD].poly[i].y*scale);
	}
	segs[TD].pts[segs[TD].npts-1] = segs[TD].pts[0];

	/* paint case */
	bbox = rectaddpt(bbox, addpt(dst->r.min, dp));
	draw(dst, bbox, bg, nil, ZP);

	/* paint segments */
	for(i = 0; i < nelem(loc); i++){
		if((bits & 1<<i) == 0)
			continue;

		for(j = 0; j < segs[loc[i].segtype].npts-1; j++){
			segpt[j] = addpt(segs[loc[i].segtype].pts[j], loc[i].p);
			segpt[j] = addpt(segpt[j], bbox.min);
		}
		segpt[segs[loc[i].segtype].npts-1] = segpt[0];

		fillpoly(dst, segpt, segs[loc[i].segtype].npts, 0, fg, ZP);
	}

	return Pt(bbox.max.x + 1, bbox.min.y);
}

static Point
string7(Image *dst, Point dp, char *s, int scale, Image *fg, Image *bg)
{
	static struct {
		char c;
		uchar bits;
	} dmap[] = {
		'0', 0x3F,
		'1', 0x06,
		'2', 0x5B,
		'3', 0x4F,
		'4', 0x66,
		'5', 0x6D,
		'6', 0x7D,
		'7', 0x07,
		'8', 0x7F,
		'9', 0x6F,
	};
	uchar bits;
	int i;

	if(s == nil)
		return dp;

	for(; *s != 0; s++)
		for(i = 0; i < nelem(dmap); i++)
			if(dmap[i].c == *s){
				bits = dmap[i].bits;
				if(s[1] == '.')
					bits |= 0x80;
				dp = d7(dst, dp, bits, scale, fg, bg);
				break;
			}
	return dp;
}

static void
stopwatch_start(Stopwatch *self)
{
	if(self->state == Stop)
		self->elapsed = 0;

	self->state = Run;
}

static void
stopwatch_stop(Stopwatch *self)
{
	if(self->state == Run)
		self->state = Stop;
}

static void
stopwatch_pause(Stopwatch *self)
{
	if(self->state == Run)
		self->state = Pause;
}

static void
stopwatch_update(Stopwatch *self, uvlong dt)
{
	int HMS[4], i;
	double t;

	self->elapsed += dt;
	t = self->elapsed;
	t /= 60*60*1000;	HMS[0] = t;	t -= HMS[0];
	t *= 60;		HMS[1] = t;	t -= HMS[1];
	t *= 60;		HMS[2] = t;	t -= HMS[2];
	t *= 1000;		HMS[3] = t;

	for(i = 0; i < nelem(HMS)-2; i++)
		snprint(self->hms[i], sizeof self->hms[i], "%02d", HMS[i]);
	snprint(self->hms[i], sizeof self->hms[i], "%02d.%03d", HMS[i], HMS[i+1]);
}

static void
stopwatch_draw(Stopwatch *self, Image *dst, Point dp, double scale)
{
	int i;

	for(i = 0; i < nelem(self->hms); i++){
		if(i > 0 && i < 3)
			dp.x += scale/3;
		dp = string7(dst, dp, self->hms[i], scale, d7fg, d7bg);
	}
}

void
timer(void *arg)
{
	Stopwatch *s;
	uvlong t0, t1;
	uvlong dt;	/* in ms */

	threadsetname("tic-tac");

	s = arg;
	t0 = nanosec();
	for(;;){
		t1 = nanosec();
		dt = (t1 - t0)/1000000ULL;

		if(s->state == Run){
			s->update(s, dt);
			nbsend(drawc, nil);
		}

		t0 = t1;
		sleep(HZ2MS(13));
	}
}

Stopwatch *
mkstopwatch(void)
{
	Stopwatch *s;

	s = malloc(sizeof *s);
	if(s == nil)
		sysfatal("malloc: %r");

	memset(s, 0, sizeof *s);
	s->start = stopwatch_start;
	s->stop = stopwatch_stop;
	s->pause = stopwatch_pause;
	s->update = stopwatch_update;
	s->draw = stopwatch_draw;

	s->update(s, 0);
	proccreate(timer, s, mainstacksize);

	return s;
}

void
rmstopwatch(Stopwatch *s)
{
	free(s);
}

void
initscreenb(void)
{
	if(screenb != nil)
		freeimage(screenb);

	screenb = eallocimage(display, rectsubpt(screen->r, screen->r.min), screen->chan, 0, DNofill);
}

void
redraw(void)
{
	draw(screenb, screenb->r, display->black, nil, ZP);
	chrono->draw(chrono, screenb, Pt(10, 10), d7scale);
	draw(screen, screen->r, screenb, nil, ZP);
	flushimage(display, 1);
}

void
resize(void)
{
	if(getwindow(display, Refnone) < 0)
		sysfatal("resize failed");

	initscreenb();
	nbsend(drawc, nil);
}

void
mouse(Mousectl *mc)
{
	if(mc->buttons & 8)
		d7scale += 2;
	if(mc->buttons & 16)
		d7scale -= 2;
	if(mc->buttons != 0)
		nbsend(drawc, nil);
}

void
key(Rune r)
{
	switch(r){
	case Kdel:
		threadexitsall(nil);
	case Kesc:
		if(chrono->state == Run)
			chrono->pause(chrono);
		else if(chrono->state == Pause)
			chrono->start(chrono);
		break;
	case ' ':
		if(chrono->state == Run)
			chrono->stop(chrono);
		else if(chrono->state == Stop)
			chrono->start(chrono);
		break;
	}
}

void
usage(void)
{
	fprint(2, "usage: %s\n", argv0);
	exits("usage");
}

void
threadmain(int argc, char *argv[])
{
	Rune r;

	GEOMfmtinstall();
	ARGBEGIN{
	default: usage();
	}ARGEND;

	if(initdraw(nil, deffont, "chrono") < 0)
		sysfatal("initdraw: %r");
	if((mc = initmouse(nil, screen)) == nil)
		sysfatal("initmouse: %r");
	if((kc = initkeyboard(nil)) == nil)
		sysfatal("initkeyboard: %r");

	d7bg = eallocimage(display, UR, XRGB32, 1, 0x333333FF);
	d7fg = eallocimage(display, UR, XRGB32, 1, DRed);

	initscreenb();
	drawc = chancreate(sizeof(void*), 1);
	nbsend(drawc, nil);
	chrono = mkstopwatch();

	enum { MOUSE, RESIZE, KEYS, DRAW, NONE };
	Alt a[] = {
	 [MOUSE]	{mc->c, &mc->Mouse, CHANRCV},
	 [RESIZE]	{mc->resizec, nil, CHANRCV},
	 [KEYS]		{kc->c, &r, CHANRCV},
	 [DRAW]		{drawc, nil, CHANRCV},
	 [NONE]		{nil, nil, CHANEND}
	};
	for(;;)
		switch(alt(a)){
		case MOUSE:
			mouse(mc);
			break;
		case RESIZE:
			resize();
			break;
		case KEYS:
			key(r);
			break;
		case DRAW:
			redraw();
			break;
		default:
			sysfatal("main loop interrupted");
		}
}