Commits

Trammell Hudson  committed eab0665 Draft

report counts and store in eeprom?

  • Participants
  • Parent commits 1c7d6e6

Comments (0)

Files changed (1)

 #include <avr/io.h>
 #include <avr/pgmspace.h>
 #include <avr/interrupt.h>
+#include <avr/eeprom.h>
 #include <stdint.h>
 #include <string.h>
 #include <util/delay.h>
 }
 
 
-static void
-led(
-	uint8_t val
-)
-{
-	if (val)
-		out(LED, 1);
-	else
-		out(LED, 0);
-}
-
 
 /*  4
  * 5 3
 
 
 
-static void
-chase(
-	uint8_t count,
-	uint8_t delay
-)
-{
-	for (uint8_t i = 0 ; i < count ; i++)
-	{
-		uint8_t mask = 1;
-		for (uint8_t j = 0 ; j < 6 ; j++, mask <<= 1)
-		{
-			DDRC = mask;
-			_delay_ms(delay);
-		}
-	}
-}
-
 
 /** "Frame buffer" of output LEDs.
  * Since we can't drive all LEDs simultaneously, instead draw them from
 }
 
 
+static void
+report_count(void)
+{
+	for (uint16_t min = 0 ; min < 512 ; min++)
+	{
+		const uint8_t count = eeprom_read_byte((void*) min);
+		if (count == 0)
+			continue;
+		char buf[8];
+		uint8_t off = 0;
+		buf[off++] = hexdigit(min >> 8);
+		buf[off++] = hexdigit(min >> 4);
+		buf[off++] = hexdigit(min >> 0);
+		buf[off++] = '=';
+		buf[off++] = hexdigit(count >> 4);
+		buf[off++] = hexdigit(count >> 0);
+		buf[off++] = '\r';
+		buf[off++] = '\n';
+		usb_serial_write(buf, off);
+	}
+}
+
+
+static void
+zero_count(void)
+{
+	for (uint16_t min = 0 ; min < 512 ; min++)
+	{
+		eeprom_write_byte((void*) min, 0);
+	}
+}
 
 
 int
 	uint16_t ms = 0;
 	uint16_t sec = TCNT1;
 
+	uint16_t total_count = 0;
+	uint16_t minute_count = 0;
+	uint16_t minute_current = 0;
+
+	for ( ; minute_current < 512 ; minute_current++)
+	{
+		const uint8_t count = eeprom_read_byte((void*) minute_current);
+		if (count == 0)
+			break;
+	}
+	
 	while (1)
 	{
 		int c = usb_serial_getchar();
 			else
 			if (c == ' ')
 			{
-				chase = 1;
+				total_count++;
+				minute_count++;
+			}
+			else
+			if (c == '?')
+			{
+				report_count();
+			} else
+			if (c == '!')
+			{
+				zero_count();
+				minute_count = total_count = 0;
 			}
 		}
 
 		if (++ms == 200)
 		{
 			ms = 0;
-			sec++;
+			if (++sec == 60)
+			{
+				// Write minute_count
+				eeprom_write_byte(minute_current++, minute_count);
+				sec = 0;
+				minute_count = 0;
+				chase = 1;
+			}
 		}
 
 		if (chase)
 			outputs[1] = chases[(chase+1) % 6];
 			outputs[2] = chases[(chase+2) % 6];
 			outputs[3] = chases[(chase+3) % 6];
-			if (chase++ == 6*5)
-			{
+			if (chase++ == 6*2)
 				chase = 0;
-				sec = TCNT1;
-			}
 		
 			continue;
 		}
 
-		uint16_t o = sec;
+		uint16_t o = total_count;
 		outputs[3] = digits[o % 10]; o /= 10;
 		outputs[2] = digits[o % 10]; o /= 10;
 		outputs[1] = digits[o % 10]; o /= 10;