commit 9b2b6d91e8c101d9f50a7af1f6c2fa849d5e8d34
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Sat Nov 10 16:14:03 2012 -0500

    Moved rfid functions into their own file

diff --git a/Makefile b/Makefile
index 87c6d54..4b3225a 100644
--- a/Makefile
+++ b/Makefile
@@ -2,13 +2,13 @@ PART=t1634
 MCU=attiny1634
 #MCU=atmega2560
 F_CPU=1000000
-SRC=main.c serial.c \
+SRC=main.c serial.c rfid.c \
 		freemodbus/port/*.c \
 		freemodbus/modbus/mb.c \
 		freemodbus/modbus/rtu/mbrtu.c \
 		freemodbus/modbus/ascii/mbascii.c \
 		freemodbus/modbus/functions/*.c
-HDR=tooltron_mb.h serial.h freemodbus/port/*.h
+HDR=tooltron_mb.h serial.h rfid.h freemodbus/port/*.h
 
 # TODO we can exclude modbus functions we don't need if the generated hex file
 # ends up too large
diff --git a/main.c b/main.c
index 2490738..2d44bb5 100644
--- a/main.c
+++ b/main.c
@@ -3,81 +3,7 @@
 #include "mb.h"
 #include "mbport.h"
 #include "tooltron_mb.h"
-#include "serial.h"
-
-#define RFID_OK 1
-
-static char rfid_read_cmd[] = {'!', 'R', 'W', 1, 32};
-
-#define RFID_SERNO_SIZE 4
-static char rfid_serno[RFID_SERNO_SIZE];
-
-static void rfid_zero_serno() {
-  int i;
-  for (i = 0; i < RFID_SERNO_SIZE; i++) {
-    rfid_serno[i] = 0;
-  }
-}
-
-static void rfid_read_serno() {
-  int i;
-  for (i = 0; i < RFID_SERNO_SIZE; i++) {
-    rfid_serno[i] = serial_read_blocking();
-  }
-}
-
-static char rfid_check_serno() {
-  int i;
-  for (i = 0; i < RFID_SERNO_SIZE; i++) {
-    if (rfid_serno[i] != serial_read_blocking()) {
-      return 0;
-    }
-  }
-  return 1;
-}
-
-/*
- * When reading, it must read the same value RFID_MIN_OK times. If it
- * encounters RFID_MAX_ERRS read errors first, it will output all 0's.
- */
-#define RFID_MAX_ERRS 10
-#define RFID_MIN_OK 5
-
-static void rfid_read_safe() {
-  char n_err = 0, n_ok = 0;
-  while (1) {
-    serial_write(rfid_read_cmd, sizeof(rfid_read_cmd));
-    if (serial_read_blocking() == RFID_OK) {
-      if (n_ok == 0) {
-        rfid_read_serno();
-      } else if (!rfid_check_serno()) {
-        rfid_zero_serno();
-        return;
-      }
-      n_ok++;
-      if (n_ok >= RFID_MIN_OK) {
-        return;
-      }
-    } else {
-      n_err++;
-      if (n_err >= RFID_MAX_ERRS) {
-        rfid_zero_serno();
-        return;
-      }
-    }
-    _delay_ms(50);
-  }
-}
-
-static void rfid_read() {
-  while (1) {
-    serial_write(rfid_read_cmd, sizeof(rfid_read_cmd));
-    if (serial_read_blocking() == RFID_OK) {
-      rfid_read_serno();
-      return;
-    }
-  }
-}
+#include "rfid.h"
 
 eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT num,
     eMBRegisterMode mode) {
@@ -98,7 +24,6 @@ eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT num) {
   int i;
   switch (addr) {
     case 0:
-      // TODO #define addresses as TYPE_NAME, eg INPUT_SERNO1
       // TODO test whether the following works as expected (8 or 16 bit buf?)
       for (i = 0; i < 2*num; i++) {
         reg_buf[i] = i;
@@ -121,10 +46,8 @@ eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT num,
 }
 
 int main() {
-  // init rfid's serial
-  serial_init();
+  rfid_init();
 
-  // init modbus and register callbacks
   eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
   eMBEnable();
 
diff --git a/rfid.c b/rfid.c
new file mode 100644
index 0000000..35ff932
--- /dev/null
+++ b/rfid.c
@@ -0,0 +1,79 @@
+#include <util/delay.h>
+#include "rfid.h"
+#include "serial.h"
+
+static char read_cmd[] = {'!', 'R', 'W', 1, 32};
+
+static char serno[RFID_SERNO_SIZE];
+
+static void zero_serno() {
+  int i;
+  for (i = 0; i < RFID_SERNO_SIZE; i++) {
+    serno[i] = 0;
+  }
+}
+
+static void read_serno() {
+  int i;
+  for (i = 0; i < RFID_SERNO_SIZE; i++) {
+    serno[i] = serial_read_blocking();
+  }
+}
+
+static char check_serno() {
+  int i;
+  for (i = 0; i < RFID_SERNO_SIZE; i++) {
+    if (serno[i] != serial_read_blocking()) {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+void rfid_init() {
+  serial_init();
+}
+
+void rfid_read_safe() {
+  char n_err = 0, n_ok = 0;
+  while (1) {
+    serial_write(read_cmd, sizeof(read_cmd));
+    if (serial_read_blocking() == RFID_OK) {
+      if (n_ok == 0) {
+        read_serno();
+      } else if (!check_serno()) {
+        zero_serno();
+        return;
+      }
+      n_ok++;
+      if (n_ok >= RFID_MIN_OK) {
+        return;
+      }
+    } else {
+      n_err++;
+      if (n_err >= RFID_MAX_ERRS) {
+        zero_serno();
+        return;
+      }
+    }
+    _delay_ms(40);
+  }
+}
+
+void rfid_read() {
+  while (1) {
+    serial_write(read_cmd, sizeof(read_cmd));
+    if (serial_read_blocking() == RFID_OK) {
+      read_serno();
+      return;
+    }
+    _delay_ms(40);
+  }
+}
+
+void rfid_get_serno(char *buf) {
+  int i;
+  for (i = 0; i < RFID_SERNO_SIZE; i++) {
+    buf[i] = serno[i];
+  }
+}
diff --git a/rfid.h b/rfid.h
new file mode 100644
index 0000000..f3c235f
--- /dev/null
+++ b/rfid.h
@@ -0,0 +1,31 @@
+#ifndef RFID_H
+#define RFID_H
+
+/* First byte back from rfid reader */
+#define RFID_OK 1
+
+/* Bytes in serial number on rfid tags */
+#define RFID_SERNO_SIZE 4
+
+/* In rfid_read_safe, it must read the same value RFID_MIN_OK times. If it
+ * encounters RFID_MAX_ERRS read errors first, it will output all 0's. */
+#define RFID_MAX_ERRS 10
+#define RFID_MIN_OK 5
+
+/* Should be called before anything else */
+void rfid_init();
+
+/* Reads the serial number of an rfid tag into an internal buffer, accessbile
+ * with rfid_get_serno */
+void rfid_read();
+
+/* 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 */
+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 */
+void rfid_get_serno(char *serno);
+
+#endif
