Source

watches / lunar / lunar.c

Full commit
  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
/** \file
 * Draw a lunar face clock as well as the sun-up, sun-down times.
 */
#include <string.h>
#include "main.h"
#include "draw.h"
#include "image.h"
#include "sin_table.h"


static uint8_t last_sec;
static PulseResource image_id;
#define CX (SCREEN_WIDTH/2)
#define CY (81/2 + 8)
#define IMG_X (CX - 81/2)
#define IMG_Y (CY - 81/2)


// Inspired by http://www.colourlovers.com/palette/1391455/lunar_eclipse
static const color24_t night_color = { 42, 35, 53 };
// static const color24_t day_color = { 183, 64, 50 };
static const color24_t day_color = { 99, 45, 59 };
static const color24_t date_color = { 65, 34, 52 };
static const color24_t time_color = { 248, 90, 64 };


void
button_down(void)
{
	init();
	last_sec = -1;
}


void
button_up(
	uint32_t ms
)
{
	// Nothing to do
	(void) ms;
}




static void
circle_line(
	color24_t color,
	int32_t x0,
	int32_t y0,
	int32_t radius
)
{
	int32_t f = 1 - radius;
	int ddF_x = 1;
	int ddF_y = -2 * radius;
	int x = 0;
	int y = radius;

	draw_pixel(color, x0, y0 + radius);
	draw_pixel(color, x0, y0 - radius);
	draw_pixel(color, x0 + radius, y0);
	draw_pixel(color, x0 - radius, y0);

	while (x < y)
	{
		if (f >= 0)
		{
			y--;
			ddF_y += 2;
			f += ddF_y;
		}
		x++;
		ddF_x += 2;
		f += ddF_x;

		draw_pixel(color, x0 + x, y0 + y);
		draw_pixel(color, x0 + x, y0 - y);
		draw_pixel(color, x0 - x, y0 + y);
		draw_pixel(color, x0 - x, y0 - y);

		draw_pixel(color, x0 + y, y0 + x);
		draw_pixel(color, x0 + y, y0 - x);
		draw_pixel(color, x0 - y, y0 + x);
		draw_pixel(color, x0 - y, y0 - x);
	}
}


void
fill(
	color24_t color,
	int32_t x0,
	int32_t y0,
	int32_t x1,
	int32_t y1
)
{
	int32_t temp;
	if (x0 > x1)
	{
		temp = x0;
		x0 = x1;
		x1 = temp;
	}
	if (y0 > y1)
	{
		temp = y0;
		y0 = y1;
		y1 = temp;
	}

	if (x1 < 0 || y1 < 0)
		return;
	if (x0 >= SCREEN_WIDTH || y1 >= SCREEN_HEIGHT)
		return;
	if (x0 < 0)
		x0 = 0;
	if (y0 < 0)
		y0 = 0;
	if (x1 >= SCREEN_WIDTH)
		x1 = SCREEN_WIDTH - 1;
	if (y1 >= SCREEN_HEIGHT)
		y1 = SCREEN_HEIGHT - 1;

	int32_t h = y1 - y0 + 1;
	int32_t w = x1 - x0 + 1;
	uint16_t num = w * h;
	if (num == 0)
		return;
	
	pulse_set_draw_window(x0, y0, x1, y1);

	while (num--)
		pulse_draw_point24(color);
}


static void
circle_fill(
	color24_t color,
	int32_t x0,
	int32_t y0,
	int32_t radius
)
{
	int32_t f = 1 - radius;
	int ddF_x = 1;
	int ddF_y = -2 * radius;
	int x = 0;
	int y = radius;

	fill(color, x0 - radius, y0, x0 + radius, y0);

	while (x < y)
	{
		if (f >= 0)
		{
			y--;
			ddF_y += 2;
			f += ddF_y;
		}
		x++;
		ddF_x += 2;
		f += ddF_x;

		fill(color, x0 - x, y0 + y, x0 + x, y0 + y);
		fill(color, x0 - x, y0 - y, x0 + x, y0 - y);
		fill(color, x0 - y, y0 + x, x0 + y, y0 + x);
		fill(color, x0 - y, y0 - x, x0 + y, y0 - x);
	}
}


static void
draw_arc(
	color24_t color,
	int32_t cx,
	int32_t cy,
	int32_t radius,
	int32_t dot_radius,
	uint32_t angle1,
	uint32_t angle2
)
{
	const uint16_t step = 128;

	for (uint32_t t = angle1 ; t < angle2  ; t += step)
	{
		const uint16_t angle = t;
		const int16_t sx = (radius * sin_lookup(angle)) / 32768;
		const int16_t sy = (radius * cos_lookup(angle)) / 32768;

		circle_fill(
			color,
			cx + sx,
			cy - sy,
			dot_radius
		);
	}
}


static inline uint16_t
ntohs(
	uint16_t v
)
{
	return (v >> 8) | (v << 8);
}


/** Conway's algorithm for determining the phase of the moon */
static uint8_t
moon_phase(
        uint32_t year,
        uint32_t month,
        uint32_t day
)
{
        int32_t r = year % 100;
        r %= 19;
        if (r > 9)
                r -= 19;
        r = ((r * 11) % 30) + month + day;
        if (month < 3)
                r += 2;
        r *= 10;
        r -= 83; // conway uses r -= 8.3, but we don't have floating point
        r = (r + 5) / 10; // floor(r)
        r %= 30;
        return r < 0 ? r + 30 : r;
}


void
init(void)
{
	struct pulse_time_tm now;
	pulse_get_time_date(&now);

	//uint32_t phase = moon_phase(now.tm_year, now.tm_mon, now.tm_mday);
	uint32_t phase = moon_phase(now.tm_year, now.tm_mon + 1, now.tm_mday + 1);
	image_id = IMAGE_M000 + phase;
	if (image_id > IMAGE_M348)
		image_id = IMAGE_M060;

	image_draw_gray(image_id, IMG_X, IMG_Y);

	uint16_t sunrise_times[2];

	// tm_mon goes from 0 to 11
	// tm_mday goes from 0 to 30
	// sunrise times are stored 32/month
	uint32_t offset = image_offset(IMAGE_SUNRISE)
		+ (now.tm_mon * 32 + now.tm_mday) * sizeof(sunrise_times);

	spiflash_read(
		offset,
		sunrise_times,
		sizeof(sunrise_times)
	);

	// Scale the sunrise time to angles
	uint32_t sunrise = ntohs(sunrise_times[0]);
	sunrise = (sunrise * 65536) / (24 * 60);
	uint32_t sunset = ntohs(sunrise_times[1]);
	sunset = (sunset * 65536) / (24 * 60);

	draw_arc(
		night_color,
		CX,
		CY - 1,
		81/2 + 3,
		1,
		sunset,
		sunrise + 65536
	);

	draw_arc(
		day_color,
		CX,
		CY - 1,
		81/2 + 3,
		1,
		sunrise,
		sunset
	);

	// Draw hours?
	for (int hour = 0 ; hour < 23 ; hour++)
	{
	}
}


static void
draw_hour_hand(
	uint16_t angle
)
{
	int32_t rad = 40;
	static uint16_t last_angle;

	if (angle == last_angle)
		return;

	// Fill in the old hand
	draw_line_bg(
		image_id,
		IMG_X,
		IMG_Y,
		CX,
		CY,
		CX + (sin_lookup(last_angle) * rad) / 32768,
		CY - (cos_lookup(last_angle) * rad) / 32768
	);

	draw_line(
		COLOR_BLUE,
		CX,
		CY,
		CX + (sin_lookup(angle) * rad) / 32768,
		CY - (cos_lookup(angle) * rad) / 32768
	);

	last_angle = angle;
}


void
draw(
	uint32_t cur_ms
)
{
	struct pulse_time_tm now;
	pulse_get_time_date(&now);

	if (last_sec == now.tm_sec)
		return;

	// Every night at midnight redraw it all
	// or every time they hit the button
	if (last_sec == -1
	||  (now.tm_hour == 0 && now.tm_min == 0 && now.tm_sec == 0))
		init();
		
	last_sec = now.tm_sec;

	uint32_t hour = now.tm_hour;
	uint32_t min = now.tm_min;
	uint32_t sec = now.tm_sec;
	
	//uint16_t hour_angle = ((hour * 60 + min) * 65536) / (24 * 60);
	uint16_t hour_angle = (sec * 65536) / 60;
	draw_hour_hand(hour_angle);

	// This seems inconsistent sometimes it is full year,
	// other times it is Unix year.
	if (now.tm_year < 1900)
		now.tm_year += 1900;

	char buf[16];
	const uint32_t time_y = SCREEN_HEIGHT - 22;
	sprintf(buf, "%02d", now.tm_hour);
	draw_monostring(
		4,
		time_y,
		time_color,
		FONT_CLOCKOPIA_22,
		12,
		22,
		buf
	);

	sprintf(buf, "%02d", now.tm_min);
	draw_monostring(
		36,
		time_y,
		time_color,
		FONT_CLOCKOPIA_22,
		12,
		22,
		buf
	);

	sprintf(buf, "%02d", now.tm_sec);
	draw_monostring(
		68,
		time_y,
		time_color,
		FONT_CLOCKOPIA_22,
		12,
		22,
		buf
	);

	sprintf(buf, "%04d/%02d/%02d",
		now.tm_year,
		now.tm_mon + 1,
		now.tm_mday + 1
	);
	draw_monostring(
		4,
		SCREEN_HEIGHT - 36,
		date_color,
		FONT_CLOCKOPIA_14,
		9,
		14,
		buf
	);

}