commit bbf2f5ad4ee456d5e8d4b623b7b6685bca29641a
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Thu Dec 13 16:04:56 2012 -0500

    Added flushing before write to rfid reader
    
    And other various fixes and changes, which I don't remember. Changed
    rfid reading to read every tick, but only try writing if enough ticks
    have passed.

diff --git a/toolbox/main.c b/toolbox/main.c
index bd092d4..76b8530 100644
--- a/toolbox/main.c
+++ b/toolbox/main.c
@@ -101,6 +101,7 @@ static void tool_tick() {
         toolstate = TS_OFF;
         serno_zero(current_user);
       }
+      break;
 
     case TS_REQ_DIS:
       if (!get_coil(MB_COIL_EN)) {
@@ -126,13 +127,11 @@ static void tool_tick() {
         toolstate = TS_REQ_DIS;
       } else if (serno_equal(current_user, latest_reading)) {
         toolstate = TS_ON;
-      } else {
-        if (led_blink_done()) {
-          set_coil(MB_COIL_EN, 0);
-          tool_disable();
-          serno_zero(current_user);
-          toolstate = TS_OFF;
-        }
+      } else if (led_blink_done()) {
+        set_coil(MB_COIL_EN, 0);
+        tool_disable();
+        serno_zero(current_user);
+        toolstate = TS_OFF;
       }
       break;
 
@@ -146,7 +145,7 @@ static void tool_tick() {
         toolstate = TS_REQ_DIS;
       } else if (!serno_equal(current_user, latest_reading)) {
         toolstate = TS_MISSING_ID;
-        led_blink_start(666, 15, YELLOW);
+        led_blink_start(500, 6, YELLOW); // TODO made 10 seconds
       }
       break;
 
@@ -262,7 +261,7 @@ eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
 }
 
 int main() {
-  char rfid_ticks = 0;
+  char rfid_ticks = 0, rfid_restart = 0;
 
   time_init();
   led_init();
@@ -277,16 +276,15 @@ int main() {
 
   rfid_start_read();
   while (1) {
-    if (++rfid_ticks >= RFID_PERIOD/TICK_MS)
+    if (rfid_poll()) {
+      rfid_get_serno(latest_reading);
+      rfid_restart = 1;
+    }
+    if (++rfid_ticks >= RFID_PERIOD/TICK_MS && rfid_restart)
     {
       rfid_ticks = 0;
-      if (rfid_poll()) {
-        rfid_get_serno(latest_reading);
-        rfid_start_read();
-      } else {
-        /* TODO count # times rfid_poll returns each value and see how often we
-         * have to wait */
-      }
+      rfid_restart = 0;
+      rfid_start_read();
     }
     current = current_read();
     tool_tick();
diff --git a/toolbox/rfid.c b/toolbox/rfid.c
index 250744f..30638d9 100644
--- a/toolbox/rfid.c
+++ b/toolbox/rfid.c
@@ -8,7 +8,6 @@
 static uint8_t read_cmd[] = {'!', 'R', 'W', 1, 32};
 
 static int serno_idx;
-static char n_failures;
 static uint8_t serno[RFID_SERNO_SIZE];
 
 static void zero_serno() {
@@ -22,16 +21,12 @@ void rfid_init() {
   serial_init();
 }
 
-static void restart_read() {
+void rfid_start_read() {
   serno_idx = -1;
+  serial_flush();
   serial_write(read_cmd, sizeof(read_cmd));
 }
 
-void rfid_start_read() {
-  n_failures = 0;
-  restart_read();
-}
-
 char rfid_poll() {
   int c;
 
@@ -39,14 +34,8 @@ char rfid_poll() {
 
     if (serno_idx < 0) {
       if (c != RFID_OK) {
-        n_failures++;
-        if (n_failures >= RFID_N_FAILURES) {
-          zero_serno();
-          return 1;
-        } else {
-          restart_read();
-          return 0;
-        }
+        zero_serno();
+        return 1;
       }
     } else {
       serno[serno_idx] = c;
diff --git a/toolbox/rfid.h b/toolbox/rfid.h
index 01d3068..72902cf 100644
--- a/toolbox/rfid.h
+++ b/toolbox/rfid.h
@@ -4,9 +4,6 @@
 /* Bytes in serial number on rfid tags */
 #define RFID_SERNO_SIZE 4
 
-/* Number of consective read failures before giving up and returning all 0s */
-#define RFID_N_FAILURES 5
-
 /* Should be called before anything else */
 void rfid_init();
 
diff --git a/toolbox/time.c b/toolbox/time.c
index 92e7638..c681414 100644
--- a/toolbox/time.c
+++ b/toolbox/time.c
@@ -7,7 +7,8 @@
 #define OCR (F_CPU / PRESCALE / 1000UL)
 #define ERROR (F_CPU / PRESCALE - OCR * 1000UL)
 
-static char ready, ms;
+static volatile char ready;
+static char ms;
 static uint16_t error;
 
 ISR(TIMER0_COMPA_vect) {
