commit 532ba0bd8d407d249fabafff12003498eb352845
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Wed Nov 21 01:52:20 2012 -0500

    Added LED header, and some rfid debugging

diff --git a/toolbox/led.h b/toolbox/led.h
new file mode 100644
index 0000000..79587a2
--- /dev/null
+++ b/toolbox/led.h
@@ -0,0 +1,12 @@
+#ifndef LED_H
+#define LED_H
+
+#include <avr/io.h>
+
+static inline void led_init() {DDRC |= _BV(DDC1) | _BV(DDC0);}
+static inline void led_off() {PORTC &= ~(_BV(PC1) | _BV(PC0));}
+static inline void led_red() {PORTC = (PORTC & ~_BV(PC0)) | _BV(PC1);}
+static inline void led_yellow() {PORTC |= _BV(PC1) | _BV(PC0);}
+static inline void led_green() {PORTC = (PORTC & ~_BV(PC1)) | _BV(PC0);}
+
+#endif
diff --git a/toolbox/main.c b/toolbox/main.c
index 328c6ad..c82dedb 100644
--- a/toolbox/main.c
+++ b/toolbox/main.c
@@ -6,6 +6,7 @@
 #include "mbport.h"
 #include "tooltron_mb.h"
 #include "rfid.h"
+#include "led.h"
 
 enum toolstate_t {
   TS_INIT,
@@ -108,7 +109,7 @@ static void tool_main() {
 eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
     eMBRegisterMode mode) {
 
-  if (addr > N_COILS || n_coils >= N_COILS-addr) {
+  if (addr+n_coils > N_COILS) {
     return MB_ENOREG;
   }
 
@@ -211,6 +212,7 @@ eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
 
 int main() {
 
+  led_init();
   tool_init();
   rfid_init();
 
@@ -219,11 +221,15 @@ int main() {
 
   sei();
 
+  rfid_start_read();
   while (1) {
-    rfid_read();
-    tool_main();
+    if (rfid_poll()) {
+      rfid_start_read();
+    }
+    rfid_get_serno(current_user);
+    //tool_main();
     eMBPoll();
-    _delay_ms(100);
+    _delay_ms(50);
   }
 
   return 0;
diff --git a/toolbox/rfid.c b/toolbox/rfid.c
index 4860d85..008c2c9 100644
--- a/toolbox/rfid.c
+++ b/toolbox/rfid.c
@@ -5,6 +5,7 @@
 
 static char read_cmd[] = {'!', 'R', 'W', 1, 32};
 
+static int serno_idx;
 static char serno[RFID_SERNO_SIZE];
 
 static void zero_serno() {
@@ -57,19 +58,41 @@ void rfid_read_safe() {
         return;
       }
     }
-    _delay_ms(40);
+    _delay_ms(10);
   }
 }
 
-void rfid_read() {
-  while (1) {
-    serial_write(read_cmd, sizeof(read_cmd));
-    if (serial_read_blocking() == RFID_OK) {
-      read_serno();
-      return;
+void rfid_start_read() {
+  serno_idx = -1;
+  zero_serno(); // TODO temporary
+  serial_flush();
+  serial_write(read_cmd, sizeof(read_cmd));
+}
+
+char rfid_poll() {
+  int c;
+
+  c = serial_read();
+  while (c >= 0) {
+
+    if (serno_idx < 0) {
+      if (c != RFID_OK) {
+        zero_serno();
+        return 1;
+      }
+    } else {
+      serno[serno_idx] = c;
+    }
+
+    serno_idx++;
+    if (serno_idx >= RFID_SERNO_SIZE) {
+      return 1;
     }
-    _delay_ms(40);
+
+    c = serial_read();
   }
+
+  return 0;
 }
 
 void rfid_get_serno(char *buf) {
diff --git a/toolbox/rfid.h b/toolbox/rfid.h
index f890093..f390bb4 100644
--- a/toolbox/rfid.h
+++ b/toolbox/rfid.h
@@ -15,25 +15,30 @@
 /* Should be called before anything else */
 void rfid_init();
 
-/* Reads the serial number of an rfid tag into an internal buffer, accessible
- * with rfid_get_serno */
-void rfid_read();
+/* Sends the read command to the rfid reader. After, you should call rfid_poll
+ * until it returns nonzero */
+void rfid_start_read();
+
+/* Should only be called after rfid_start_read. Keep calling until it returns
+ * nonzero, which means it is finished reading the serial number, and
+ * rfid_get_serno can be called */
+char rfid_poll();
 
 /* Attempts to read the serial number multiple times, and only accepts it if it
- * is the same every time. Use rfid_read instead until this one is proven
- * necessary */
+ * is the same every time. Don't use it */
 void rfid_read_safe();
 
-/* Call this only after calling rfid_read. This will copy the value it read
- * into serno, which should be at least RFID_SERNO_SIZE bytes */
+/* Call this only after rfid_poll returns nonzero. This will copy the value it
+ * read into serno, which should be at least RFID_SERNO_SIZE bytes */
 void rfid_get_serno(char *serno);
 
-/* Call this only after calling rfid_read. Returns 1 if serno matches the
- * internal buffer of the most recently read serial number */
+/* Call this only after rfid_poll returns nonzero. Returns 1 if serno matches
+ * the internal buffer of the most recently read serial number */
 char rfid_check_serno(char *serno);
 
-/* Call this only after calling rfid_read. Returns 1 if the internal buffer is
- * nonzero, meaning a serial number was successfully read from an rfid tag */
+/* Call this only after rfid_poll returns nonzero. Returns 1 if the internal
+ * buffer is nonzero, meaning a serial number was successfully read from an
+ * rfid tag */
 char rfid_nonzero();
 
 #endif
