Commits

Trammell Hudson committed 4e091db Draft

word time works

Comments (0)

Files changed (1)

 	}
 }
 
+#define CHAR_RAM 0x18
+
 
 #define ADDR_0	0xF4
 #define ADDR_1	0xF5
 #define CLKSEL	0xD6
 
 static const char numbers[][16] = {
+	"",
 	"one",
 	"two",
 	"three",
 	"ten",
 	"eleven",
 	"twelve",
-	"thirty",
+	"thirteen",
 	"fourteen",
 	"fifteen",
 	"sixteen",
 	"twenty seven",
 	"twenty eight",
 	"twenty nine",
+	"half",
 };
 
 
 	out(WRCE, 1);
 }
 
+static uint8_t ms;
+static uint8_t sec;
+static uint8_t min;
+static uint8_t hour;
 
-static char output[64];
+static void
+update_time(void)
+{
+	if (++ms < 200)
+		return;
+
+	ms = 0;
+	if (++sec< 60)
+		return;
+
+	sec = 0;
+	if (++min < 60)
+		return;
+
+	min = 0;
+	if (++hour < 24)
+		return;
+
+	hour = 0;
+}
+
+
+static char scroll_output[64];
+static uint8_t scroll_offset;
+
+static void
+scroll(void)
+{
+	uint8_t did_reset = 0;
+
+	for (int i = 0 ; i < 8 ; i++)
+	{
+		char c = scroll_output[scroll_offset+i];
+		if (did_reset || c == '\0')
+		{
+			did_reset = 1;
+			c = ' ';
+		}
+	
+		lcd_write(CHAR_RAM + i, c);
+	}
+
+	if (did_reset)
+		scroll_offset = 0;
+	else
+		scroll_offset++;
+}
+
+
+static void
+draw_hms(void)
+{
+	lcd_write(CHAR_RAM | 0, '0' + (hour / 10) % 10);
+	lcd_write(CHAR_RAM | 1, '0' + (hour / 1) % 10);
+	lcd_write(CHAR_RAM | 2, ':');
+	lcd_write(CHAR_RAM | 3, '0' + (min / 10) % 10);
+	lcd_write(CHAR_RAM | 4, '0' + (min / 1) % 10);
+	lcd_write(CHAR_RAM | 5, ':');
+	lcd_write(CHAR_RAM | 6, '0' + (sec / 10) % 10);
+	lcd_write(CHAR_RAM | 7, '0' + (sec / 1) % 10);
+}
+
+
+static void
+word_clock(void)
+{
+	scroll_output[0] = '\0';
+	strcat(scroll_output, "       ");
+
+	if (min <= 30)
+	{
+		if (min != 0)
+		{
+			strcat(scroll_output, numbers[min]);
+			strcat(scroll_output, " past ");
+		}
+
+		if (hour == 12)
+			strcat(scroll_output, "noon");
+		else
+		if (hour == 0)
+			strcat(scroll_output, "midnight");
+		else
+			strcat(scroll_output, numbers[hour]);
+	} else {
+		strcat(scroll_output, numbers[60-min]);
+		strcat(scroll_output, " before ");
+		if (hour == 11)
+			strcat(scroll_output, "noon");
+		else
+		if (hour == 23)
+			strcat(scroll_output, "midnight");
+		else
+			strcat(scroll_output, numbers[hour+1]);
+	}
+
+	strcat(scroll_output, "      ");
+}
+
 
 int
 main(void)
 	_delay_ms(10);
 	out(RESET, 1);
 
-#define CHAR_RAM 0x18
-
 	// We should be good to go
 	lcd_write(0x20, 0x00); // normal operation
 
 	lcd_write(CHAR_RAM | 0, 'A');
-	lcd_write(CHAR_RAM | 1, 'B');
-	lcd_write(CHAR_RAM | 2, 'C');
-	lcd_write(CHAR_RAM | 3, 'D');
-	lcd_write(CHAR_RAM | 4, 'E');
-	lcd_write(CHAR_RAM | 5, 'F');
-	lcd_write(CHAR_RAM | 6, 'G');
-	lcd_write(CHAR_RAM | 7, 'H');
+	lcd_write(CHAR_RAM | 1, 'l');
+	lcd_write(CHAR_RAM | 2, 'p');
+	lcd_write(CHAR_RAM | 3, 'h');
+	lcd_write(CHAR_RAM | 4, 'a');
+	lcd_write(CHAR_RAM | 5, 'C');
+	lcd_write(CHAR_RAM | 6, 'L');
+	lcd_write(CHAR_RAM | 7, 'K');
 
         // Timer 0 is used for a 64 Hz control loop timer.
         // Clk/1024 == 15.625 KHz, count up to 244
 	usb_serial_flush_input();
 
 	send_str(PSTR("alphawatch\r\n"));
-	uint32_t ticks = 0;
-
-	strcpy(output, "      the quick brown fox jumps over the lazy dog      ");
-	uint16_t offset = 0;
+	char line[16];
+	uint8_t line_offset = 0;
 
 	while (1)
 	{
 		int c = usb_serial_getchar();
 		if (c != -1)
 		{
-			lcd_write(CHAR_RAM | 5, c);
 			usb_serial_putchar(c);
+			if (c == '\r')
+			{
+				hour = (line[0] - '0') * 10 + (line[1] - '0');
+				min = (line[2] - '0') * 10 + (line[3] - '0');
+				sec = (line[4] - '0') * 10 + (line[5] - '0');
+				line_offset = 0;
+			} else {
+				line[line_offset++] = c;
+			}
 		}
 
-		if (bit_is_set(TIFR0, OCF0A))
+		if (bit_is_clear(TIFR0, OCF0A))
+			continue;
+		sbi(TIFR0, OCF0A); // reset the bit
+
+		update_time();
+
+		static uint8_t rate = 0;
+		if ((rate++) < 40)
+			continue;
+		rate = 0;
+
+		const uint8_t mode = 1;
+
+		if (mode == 0)
+			draw_hms();
+		else
+		if (mode == 1)
 		{
-			sbi(TIFR0, OCF0A); // reset the bit
-			static uint8_t rate = 0;
-			if ((rate++) < 40)
-				continue;
-			rate = 0;
-
-			// do periodic stuff;
-			for (int i = 0 ; i < 8 ; i++)
-			{
-				char c = output[offset + i];
-				if (c == '\0')
-				{
-					offset = 0;
-					c = ' ';
-				}
-			
-				lcd_write(CHAR_RAM + i, c);
-			}
-
-			offset++;
+			word_clock();
+			scroll();
 		}
-
-/*
-		ticks++;
-		lcd_write(CHAR_RAM | 7, hexdigit(ticks >> 0x00));
-		lcd_write(CHAR_RAM | 6, hexdigit(ticks >> 0x04));
-		lcd_write(CHAR_RAM | 5, hexdigit(ticks >> 0x08));
-		lcd_write(CHAR_RAM | 4, hexdigit(ticks >> 0x0C));
-		lcd_write(CHAR_RAM | 3, hexdigit(ticks >> 0x10));
-		lcd_write(CHAR_RAM | 2, hexdigit(ticks >> 0x14));
-		lcd_write(CHAR_RAM | 1, hexdigit(ticks >> 0x18));
-		lcd_write(CHAR_RAM | 0, hexdigit(ticks >> 0x1C));
-*/
 	}
 }