commit 94548bf49434731d0315d219ebc4ab322bfa41e0
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Wed Dec 12 19:29:15 2012 -0500

    Moved timing to time.c, and increased period of rfid polling

diff --git a/toolbox/led.c b/toolbox/led.c
index 95aedc3..988406a 100644
--- a/toolbox/led.c
+++ b/toolbox/led.c
@@ -1,20 +1,9 @@
 #include "led.h"
-#include <avr/io.h>
-#include <avr/interrupt.h>
-
-#define PRESCALE 64
-#define CLOCK_SEL 3
-
-/* F_CPU / PRESCALE = OCR * 1000 + ERROR */
-#define OCR (F_CPU / PRESCALE / 1000UL)
-#define ERROR (F_CPU / PRESCALE - OCR * 1000UL)
+#include "time.h"
 
 char blink_count;
 enum color_t blink_color;
-uint16_t blink_period;
-
-uint16_t ms;
-uint16_t error;
+uint16_t blink_period, blink_ticks;
 
 static void led_color(enum color_t color) {
   switch (color) {
@@ -33,48 +22,25 @@ static void led_color(enum color_t color) {
   }
 }
 
-static void blink() {
-  blink_count--;
-  if (blink_count % 2) {
-    led_color(blink_color);
-  } else {
-    led_off();
-  }
-}
-
-ISR(TIMER0_COMPA_vect) {
-  error += ERROR;
-  if (error >= 1000) {
-    error -= 1000;
-    OCR0A = OCR+1;
-  } else {
-    OCR0A = OCR;
-  }
-  ms++;
-  if (ms == blink_period) {
-    blink();
-    if (blink_count == 0) {
-      TCCR0B = 0;
+void led_tick() {
+  if (blink_count) {
+    if (++blink_ticks >= blink_period) {
+      blink_ticks = 0;
+      if (--blink_count % 2) {
+        led_color(blink_color);
+      } else {
+        led_off();
+      }
     }
-    ms = 0;
   }
 }
 
 void led_blink_start(unsigned int period_ms, char n_times, enum color_t color) {
-
-  ms = 0;
-  error = 0;
-
   blink_count = n_times*2-1;
-  blink_period = period_ms/2;
+  blink_period = period_ms/2/TICK_MS;
+  blink_ticks = 0;
   blink_color = color;
   led_color(color);
-
-  OCR0A = OCR;
-  TIMSK = _BV(OCIE0A);
-  TCCR0A = _BV(WGM01);
-  TCCR0B = CLOCK_SEL;
-
 }
 
 char led_blink_done() {
diff --git a/toolbox/led.h b/toolbox/led.h
index a6963d4..8d9545a 100644
--- a/toolbox/led.h
+++ b/toolbox/led.h
@@ -19,4 +19,7 @@ void led_blink_start(unsigned int period_ms, char n_times, enum color_t color);
 /* Returns nonzero if blinking has finished */
 char led_blink_done();
 
+/* Call this every TICK_MS to handle LED blinking */
+void led_tick();
+
 #endif
diff --git a/toolbox/main.c b/toolbox/main.c
index bde5a30..bd092d4 100644
--- a/toolbox/main.c
+++ b/toolbox/main.c
@@ -8,6 +8,7 @@
 #include "rfid.h"
 #include "led.h"
 #include "current.h"
+#include "time.h"
 
 enum toolstate_t {
   TS_INIT,
@@ -58,7 +59,7 @@ static void serno_cpy(uint8_t *dest, uint8_t *src) {
   memcpy(dest, src, RFID_SERNO_SIZE);
 }
 
-static void tool_main() {
+static void tool_tick() {
 
   switch (toolstate) {
 
@@ -261,10 +262,13 @@ eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
 }
 
 int main() {
+  char rfid_ticks = 0;
 
+  time_init();
   led_init();
   tool_init();
   rfid_init();
+  current_init();
 
   eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
   eMBEnable();
@@ -273,14 +277,22 @@ int main() {
 
   rfid_start_read();
   while (1) {
-    if (rfid_poll()) {
-      rfid_get_serno(latest_reading);
-      rfid_start_read();
+    if (++rfid_ticks >= RFID_PERIOD/TICK_MS)
+    {
+      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 */
+      }
     }
     current = current_read();
-    tool_main();
+    tool_tick();
+    led_tick();
     eMBPoll();
-    _delay_ms(100);
+    time_wait();
   }
 
   return 0;
diff --git a/toolbox/time.c b/toolbox/time.c
new file mode 100644
index 0000000..92e7638
--- /dev/null
+++ b/toolbox/time.c
@@ -0,0 +1,37 @@
+#include <stdint.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include "time.h"
+
+/* F_CPU / PRESCALE = OCR * 1000 + ERROR */
+#define OCR (F_CPU / PRESCALE / 1000UL)
+#define ERROR (F_CPU / PRESCALE - OCR * 1000UL)
+
+static char ready, ms;
+static uint16_t error;
+
+ISR(TIMER0_COMPA_vect) {
+  error += ERROR;
+  if (error >= 1000) {
+    error -= 1000;
+    OCR0A = OCR+1;
+  } else {
+    OCR0A = OCR;
+  }
+  if (++ms == TICK_MS) {
+    ready = 1;
+    ms = 0;
+  }
+}
+
+void time_init() {
+  OCR0A = OCR;
+  TIMSK = _BV(OCIE0A);
+  TCCR0A = _BV(WGM01);
+  TCCR0B = CLOCK_SEL;
+}
+
+void time_wait() {
+  while (!ready);
+  ready = 0;
+}
diff --git a/toolbox/time.h b/toolbox/time.h
new file mode 100644
index 0000000..a90c9ba
--- /dev/null
+++ b/toolbox/time.h
@@ -0,0 +1,16 @@
+#ifndef TIME_H
+#define TIME_H
+
+#define TICK_MS 50
+#define RFID_PERIOD 300
+
+#define PRESCALE 64
+#define CLOCK_SEL 3
+
+/* Initializes timer 0 */
+void time_init();
+
+/* Waits until TICK_MS have passed since the previous call returned */
+void time_wait();
+
+#endif
