Trammell Hudson avatar Trammell Hudson committed a7b997d

Draw time, arcs, etc

Comments (0)

Files changed (1)

 /** \file
  * Draw a lunar face clock as well as the sun-up, sun-down times.
- * None of that is ready yet; this is a first import with copy.
  */
 #include <string.h>
 #include "main.h"
 }
 
 
-#if 0
 static void
-draw_gradient_arc(
+draw_arc(
+	color24_t color,
+	int32_t cx,
+	int32_t cy,
 	int32_t radius,
 	int32_t dot_radius,
-	uint16_t initial_angle
+	uint32_t angle1,
+	uint32_t angle2
 )
 {
-	const uint16_t arc_len = 0xB000;
 	const uint16_t step = 128;
 
-	for (uint32_t t = 0 ; t < arc_len  ; t += step)
+	for (uint32_t t = angle1 ; t < angle2  ; t += step)
 	{
-		const uint16_t angle = initial_angle + t - arc_len;
+		const uint16_t angle = t;
 		const int16_t sx = (radius * sin_lookup(angle)) / 32768;
 		const int16_t sy = (radius * cos_lookup(angle)) / 32768;
 
-		color24_t color = {
-			(t * dot_color.red) / arc_len,
-			(t * dot_color.green) / arc_len,
-			(t * dot_color.blue) / arc_len,
-		};
-
 		circle_fill(
 			color,
-			SCREEN_WIDTH/2 + sx,
-			SCREEN_HEIGHT/2 - sy,
+			cx + sx,
+			cy - sy,
 			dot_radius
 		);
 	}
 }
-#endif
 
 
 static inline uint16_t
 }
 
 
+/** 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
 draw(
 	uint32_t cur_ms
 )
 {
-	uint32_t ms = cur_ms % 1000; cur_ms /= 1000;
-	const uint32_t new_sec = cur_ms % 60; cur_ms /= 60;
-	const uint8_t new_min = cur_ms % 60; cur_ms /= 60;
-	const uint8_t new_hour = cur_ms % 12; // cur_ms %= 24;
+	struct pulse_time_tm now;
+	pulse_get_time_date(&now);
 
-	if (last_sec == new_sec)
+	if (last_sec == now.tm_sec)
 		return;
-	last_sec = new_sec;
+	last_sec = now.tm_sec;
 
-	if (lunar_mode)
-	{
-		if (image_id < IMAGE_M000
-		||  image_id >= IMAGE_M346)
-			image_id = IMAGE_M000;
+	// Inspired by http://www.colourlovers.com/palette/1391455/lunar_eclipse
+	color24_t night_color = { 42, 35, 53 };
+	//color24_t day_color = { 183, 64, 50 };
+	color24_t day_color = { 99, 45, 59 };
+	color24_t date_color = { 99, 45, 59 };
+	color24_t time_color = { 248, 90, 64 };
 
-		image_draw_gray(image_id, (SCREEN_WIDTH - 81)/2, 0);
-		image_id++;
-		return;
-	}
+	char buf[16];
+	sprintf(buf, "%04d/%02d/%02d",
+		now.tm_year,
+		now.tm_mon,
+		now.tm_mday
+	);
+	draw_monostring(0, SCREEN_HEIGHT - 35,
+		date_color,
+		FONT_CLOCKOPIA_14,
+		9,
+		buf
+	);
 
-	// Not in lunar mode; draw the 24-hour face
-	pulse_blank_canvas();
-	static uint8_t month, day;
+	sprintf(buf, "%02d:%02d:%02d",
+		now.tm_hour,
+		now.tm_min,
+		now.tm_sec
+	);
+
+	draw_monostring(0, SCREEN_HEIGHT - 21,
+		time_color,
+		FONT_CLOCKOPIA_20,
+		12,
+		buf
+	);
+
+	static uint8_t day, month;
+
+	//uint32_t phase = moon_phase(now.tm_year, now.tm_mon, now.tm_mday);
+	uint32_t phase = moon_phase(now.tm_year, month, day);
+	image_id = IMAGE_M000 + phase;
+
+	const uint32_t cx = SCREEN_WIDTH/2;
+	const uint32_t cy = 81/2 + 8;
+	image_draw_gray(image_id, cx - 81/2, cy - 81/2);
+
 	uint16_t sunrise_times[2];
 	uint32_t offset = image_offset(IMAGE_SUNRISE);
 	spiflash_read(
 		sunrise_times,
 		sizeof(sunrise_times)
 	);
-	printf("%d %d\n%04x\n%04x\n", month+1, day+1, sunrise_times[0], sunrise_times[1]);
 
-	// Scale the sunrise time to screen height
+	// Scale the sunrise time to angles
 	uint32_t sunrise = ntohs(sunrise_times[0]);
-	sunrise = (sunrise * VSCREEN_HEIGHT) / (24 * 60);
+	sunrise = (sunrise * 65536) / (24 * 60);
 	uint32_t sunset = ntohs(sunrise_times[1]);
-	sunset = (sunset * VSCREEN_HEIGHT) / (24 * 60);
-	fill(
-		COLOR_BLUE,
-		VSCREEN_WIDTH/2,
+	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,
-		VSCREEN_WIDTH/2 + (8 << VSCREEN_SHIFT),
 		sunset
 	);
 
 			month = 0;
 	}
 
-#if 0
-	const uint16_t sec_angle = ((new_sec * 6 + (ms*6) / 1000) * 32768) / 180;
-	const uint16_t min_angle = ((new_min * 6 + (new_sec*6) / 60) * 32768) / 180;
-	const uint16_t hour_angle = ((new_hour * 30 + (new_min*30) / 60) * 32768) / 180;
-
-	const int32_t sec_radius = SCREEN_WIDTH/2;
-	const int32_t sec_dot_radius = 3;
-	if (sec_angle != last_sec_angle || first_time)
-	{
-		draw_gradient_arc(
-			sec_radius,
-			sec_dot_radius,
-			sec_angle
-		);
-		last_sec_angle = sec_angle;
-	}
-
-	const int32_t min_dot_radius =  8;
-	const int32_t min_radius = sec_radius - sec_dot_radius - min_dot_radius - 1;
-	if (min_angle != last_min_angle || first_time)
-	{
-		draw_gradient_arc(
-			min_radius,
-			min_dot_radius,
-			min_angle
-		);
-		last_min_angle = min_angle;
-	}
-
-	const int32_t hour_dot_radius = 12;
-	const int32_t hour_radius = min_radius - min_dot_radius - hour_dot_radius - 1;
-	if (hour_angle != last_hour_angle || first_time)
-	{
-		draw_gradient_arc(
-			hour_radius,
-			hour_dot_radius,
-			hour_angle
-		);
-		last_hour_angle = hour_angle;
-	}
-
-	first_time = 0;
-#endif
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.