Commits

Trammell Hudson committed c2fdeca

Added bg filling code to redraw the image; currently use a second hand going around

Comments (0)

Files changed (5)

  * Drawing functions.
  */
 #include "draw.h"
+#include "image.h"
 
 
 void
 	pulse_draw_point24(c);
 }
 
+static inline int32_t
+abs(
+	int32_t x
+)
+{
+	if (x < 0)
+		return -x;
+	return x;
+}
+
+void
+draw_line(
+	color24_t c,
+	int32_t x0,
+	int32_t y0,
+	int32_t x1,
+	int32_t y1
+)
+{
+	const int32_t dx = abs(x1 - x0);
+	const int32_t dy = abs(y1 - y0);
+
+	const int32_t sx = x0 < x1 ? 1 : -1;
+	const int32_t sy = y0 < y1 ? 1 : -1;
+	int32_t err = dx - dy;
+
+#if VSCREEN_SHIFT != 0
+	uint32_t last_px = -1;
+	uint32_t last_py = -1;
+#endif
+
+	while(1)
+	{
+#if VSCREEN_SHIFT != 0
+		uint32_t px = x0 >> VSCREEN_SHIFT;
+		uint32_t py = y0 >> VSCREEN_SHIFT;
+		if (px != last_px || py != last_py)
+		{
+			last_px = px;
+			last_py = py;
+			draw_pixel(c, x0, y0);
+		}
+#else
+		draw_pixel(c, x0, y0);
+#endif
+
+		if (x0 == x1 && y0 == y1)
+			break;
+		int32_t e2 = 2 * err;
+		if (e2 > -dy)
+		{
+			err -= dy;
+			x0 += sx;
+		}
+		if (e2 < +dx)
+		{
+			err += dx;
+			y0 += sy;
+		}
+	}
+}
+
+
+void
+draw_line_bg(
+	PulseResource image_id,
+	int32_t px, // position of image on screen
+	int32_t py,
+	int32_t x0, // position of line on screen
+	int32_t y0,
+	int32_t x1,
+	int32_t y1
+)
+{
+	image_resource_t resource;
+	image_resource(image_id, &resource);
+	
+	uint32_t offset = resource.offset;
+	const uint32_t width = 3 * resource.width;
+
+	const int32_t dx = abs(x1 - x0);
+	const int32_t dy = abs(y1 - y0);
+
+	const int32_t sx = x0 < x1 ? 1 : -1;
+	const int32_t sy = y0 < y1 ? 1 : -1;
+	int32_t err = dx - dy;
+
+	while(1)
+	{
+		uint8_t lum;
+		uint8_t img_x = x0 - px;
+		uint8_t img_y = y0 - py;
+		spiflash_read(
+			offset + width * img_y + img_x,
+			&lum,
+			sizeof(lum)
+		);
+
+		color24_t c = { lum, lum, lum };
+
+		draw_pixel(c, x0, y0);
+
+		if (x0 == x1 && y0 == y1)
+			break;
+		int32_t e2 = 2 * err;
+		if (e2 > -dy)
+		{
+			err -= dy;
+			x0 += sx;
+		}
+		if (e2 < +dx)
+		{
+			err += dx;
+			y0 += sy;
+		}
+	}
+}
 
 
 static inline int32_t
 
 
 void
-draw_line(
+draw_line_aa(
 	color24_t c,
 	int32_t x0,
 	int32_t y0,
 );
 
 
+/** Draw a non-antialised line, filling in from a bg image */
+void
+draw_line_bg(
+	PulseResource image_id,
+	int32_t px, // position of image on screen
+	int32_t py,
+	int32_t x0, // position of line on screen
+	int32_t y0,
+	int32_t x1,
+	int32_t y1
+);
+
+
 void
 draw_monostring(
 	int32_t x,
 	int32_t y
 )
 {
-	
 	image_resource_t resource;
 	image_resource(id, &resource);
 	
 
 
 /** Return the absolute address of the image in the flash */
-static uint32_t
+static inline uint32_t
 image_offset(
 	PulseResource id
 )
 
 static uint8_t last_sec;
 static PulseResource image_id;
-const uint32_t cx = SCREEN_WIDTH/2;
-const uint32_t cy = 81/2 + 8;
+#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
 {
 	init();
 	last_sec = -1;
-	image_id = 0;
 }
 
 
 	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, 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, cx - 81/2, cy - 81/2);
+	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 1 to 31
+	// 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 - 1) * sizeof(sunrise_times);
+		+ (now.tm_mon * 32 + now.tm_mday) * sizeof(sunrise_times);
 
 	spiflash_read(
 		offset,
 
 	draw_arc(
 		night_color,
-		cx,
-		cy - 1,
+		CX,
+		CY - 1,
 		81/2 + 3,
 		1,
 		sunset,
 
 	draw_arc(
 		day_color,
-		cx,
-		cy - 1,
+		CX,
+		CY - 1,
 		81/2 + 3,
 		1,
 		sunrise,
 }
 
 
+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
 		
 	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,
-		SCREEN_HEIGHT - 23,
+		time_y,
 		time_color,
 		FONT_CLOCKOPIA_22,
 		12,
 	sprintf(buf, "%02d", now.tm_min);
 	draw_monostring(
 		36,
-		SCREEN_HEIGHT - 23,
+		time_y,
 		time_color,
 		FONT_CLOCKOPIA_22,
 		12,
 	sprintf(buf, "%02d", now.tm_sec);
 	draw_monostring(
 		68,
-		SCREEN_HEIGHT - 22,
+		time_y,
 		time_color,
 		FONT_CLOCKOPIA_22,
 		12,
 	sprintf(buf, "%04d/%02d/%02d",
 		now.tm_year,
 		now.tm_mon + 1,
-		now.tm_mday
+		now.tm_mday + 1
 	);
 	draw_monostring(
 		4,