Index: trunk/cardbox/main.c
===================================================================
--- trunk/cardbox/main.c	(revision 277)
+++ trunk/cardbox/main.c	(nonexistent)
@@ -1,277 +0,0 @@
-/********
- * This file is part of Tooltron.
- *
- * Tooltron is free software: you can redistribute it and/or modify
- * it under the terms of the Lesser GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Tooltron is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * Lesser GNU General Public License for more details.
- * You should have received a copy of the Lesser GNU General Public License
- * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
- *
- * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
- *
- ********/
-#include "main.h"
-
-/***** Keypad definitions ******/
-#define KEYPORT PORTB
-#define KEYPIN PINB
-#define KEYDDR  DDRB
-/** @brief ROW1 of the keypad */
-#define ROW4 (_BV(PB0))
-/** @brief ROW2 of the keypad */
-#define ROW3 (_BV(PB1))
-/** @brief ROW3 of the keypad */
-#define ROW2 (_BV(PB2))
-/** @brief ROW4 of the keypad */
-#define ROW1 (_BV(PB3))
-/** @brief COL1 of the keypad */
-#define COL4 (_BV(PB4))
-/** @brief COL2 of the keypad */
-#define COL3 (_BV(PB5))
-/** @brief COL3 of the keypad */
-#define COL2 (_BV(PB6))
-/** @brief COL4 of the keypad */
-#define COL1 (_BV(PB7))
-
-#define ADDR 2
-
-/***** Global variables *****/
-
-void init_pins(void) {
-  KEYDDR = ROW1 | ROW2 | ROW3 | ROW4;
-  //KEYDDR = 0;
-  KEYPORT = COL1 | COL2 | COL3 | COL4;
-  LEDDDR = LED_RED | LED_YELLOW | LED_GREEN;
-
-  // Turn the LEDs off
-  LEDPORT |= LED_RED | LED_GREEN | LED_YELLOW;
-  
-  return;	
-}
-
-/**
- * @brief Sets the LED to the specified state
- *
- * This sets LED which to the specified state. You can use this to set
- * multiple LEDs if you OR the LEDs desired into the which argument.
- *
- * @param which The LEDs to set
- * @parma state The state ON or OFF to set to the LEDs to
- * @return void
- */
-void toggle_led(uint8_t which, uint8_t state) {
-    if (state == ON) {
-        LEDPORT &= ~(which);
-    } else {
-        LEDPORT |= (which);
-    }
-}
-
-char get_button(void) {
-
-	char ret = ' ';
-           
-    // Row 1 Strobe
-	
-	
-  DDRB = (ROW1);
-  PORTB = (COL1|COL2|COL3|COL4);
-
-  if(!(PINB&(COL1)))
-    ret ='1';
-  else if(!(PINB&(COL2)))
-    ret ='2';
-  else if(!(PINB&(COL3)))
-    ret ='3';
-  else if(!(PINB&(COL4)))
-    ret ='A';
-  else {
-
-    DDRB = (ROW2);
-    PORTB = (COL1|COL2|COL3|COL4);
-    // Row 2 Strobe
-
-    if(!(PINB&(COL1)))
-      ret ='4';
-    else if(!(PINB&(COL2)))
-      ret ='5';
-    else if(!(PINB&(COL3)))
-      ret ='6';
-    else if(!(PINB&(COL4)))
-      ret ='B';
-    else {
-  
-      // Row 3 Strobe
-      DDRB = (ROW3);
-      PORTB = (COL1|COL2|COL3|COL4);
-  
-      if(!(PINB&(COL1)))
-        ret ='7';
-      else if(!(PINB&(COL2)))
-        ret ='8';
-      else if(!(PINB&(COL3)))
-        ret ='9';
-      else if(!(PINB&(COL4)))
-        ret ='C';
-      else{
-  
-        // Row 4 Strobe
-        DDRB = (ROW4);
-        PORTB = (COL1|COL2|COL3|COL4);
-  
-        if(!(PINB&(COL1)))
-          ret ='*';
-        else if(!(PINB&(COL2)))
-          ret ='0';
-        else if(!(PINB&(COL3)))
-          ret ='#';
-        else if(!(PINB&(COL4)))
-          ret ='D';
-	    }
-	  }
-	}
-	
-	
-  DDRB = 0;
-  PORTB = (COL1|COL2|COL3|COL4);
-
-  return ret;
-}
-
-typedef enum {
-    req,
-    press,
-    send,
-    rsp
-} state_t;
-
-
-int main(void) {
-    uint8_t mbuf[PROGD_PACKET_SIZE];
-    uint8_t cbuf[20];
-    uint8_t clen;
-    int8_t cret;
-    uint8_t resp;
-    uint8_t  c;
-    uint8_t len;
-    uint8_t retries = 0;
-    state_t state = req;
-
-    rs485_init(BAUD9600); 
-    init_pins();
-    init_timer();
-    card_reader_setup();
-    sei();
-
-    toggle_led(LED_GREEN|LED_YELLOW|LED_RED, OFF);
-    read_card = 0;
-    rs485_send_byte('s');
-
-    while(1) {
-        while(1) {
-            read_card = 1;
-            if (cr_flag != CR_NONE) {
-                read_card = 0;
-                if ((cret = parse_card(cbuf, &clen)) < 0) {
-                    switch (cret) {
-                        case CR_ERR_BAD_PARITY:
-                            toggle_led(LED_GREEN, ON);
-                            break;
-                        case CR_ERR_NO_START:
-                            toggle_led(LED_YELLOW, ON);
-                            break;
-                        case CR_ERR_NO_STOP:
-                            toggle_led(LED_RED, ON);
-                            break;
-                    }
-                    rs485_send_byte('F');
-                } else {
-                    for (c = 0; c < clen; c++) {
-                        rs485_send_byte(cbuf[c]);
-                    }
-                }
-                cr_flag = CR_NONE;
-            }
-        }
-
-
-
-        switch(state) {
-            case req:
-                toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
-                      
-                // Wait for a packet
-                resp = parse_packet(mbuf, &len, ADDR);
-
-                if (resp == TT_GET_KEY) {
-                    _delay_ms(50);
-                    send_packet(TT_ACK, ADDR, NULL, 0);
-                    toggle_led(LED_YELLOW, ON);
-                    reset_timer();
-                    reset_timeout_flag();
-                    start_timer();
-                    c = ' ';
-                    state = press;
-                } else if(resp == TT_PING){
-                    _delay_ms(50);
-                    send_packet(TT_ACK, ADDR, NULL, 0);
-                }
-                break;
-            case press:
-                c = get_button();
-                retries = 0;
-        
-                if (seconds > TIMEOUT_SECONDS) {
-                    set_timeout_flag();
-                    state = send;
-                } else if (c != ' ') {
-                    state = send;
-                } else {
-                    _delay_ms(100);
-                }
-
-                break;
-            case send:
-                if (get_timeout_flag() == 1) {
-                    _delay_ms(50);
-                    send_packet(TT_TIMEOUT, ADDR, NULL, 0);
-                    state = req;
-                    break;
-                } else {
-                    _delay_ms(50);
-                    send_packet(TT_SEND_KEY, ADDR, &c, 1);
-                }
-
-                state = rsp;
-                break;
-            case rsp:
-                resp = parse_packet(mbuf, &len,  ADDR);
-
-                if (resp == TT_ACK) {
-                    toggle_led(LED_GREEN, ON);
-                } else if ((resp == TT_NACK) || (retries == TT_MAX_RETRY)) { 
-                    toggle_led(LED_RED, ON);
-                } else {
-                    retries++;
-                    state = send;
-                    break;
-                }
-
-                _delay_ms(1000);
-                toggle_led(LED_RED, OFF);
-                toggle_led(LED_GREEN, ON);
-                state = req;
-                break;
-            default:
-                state = req;
-                break;
-        }
-    } 
-    return 0;    
-}
Index: trunk/cardbox/cardreader.h
===================================================================
--- trunk/cardbox/cardreader.h	(revision 277)
+++ trunk/cardbox/cardreader.h	(revision 278)
@@ -4,7 +4,7 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <stdint.h>
-#include "main.h"
+#include "cardbox.h"
 #include "rs485_int.h"
 
 #define CR_ERR_BAD_PARITY   -1
Index: trunk/cardbox/cardbox.c
===================================================================
--- trunk/cardbox/cardbox.c	(nonexistent)
+++ trunk/cardbox/cardbox.c	(revision 278)
@@ -0,0 +1,277 @@
+/********
+ * This file is part of Tooltron.
+ *
+ * Tooltron is free software: you can redistribute it and/or modify
+ * it under the terms of the Lesser GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Tooltron is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * Lesser GNU General Public License for more details.
+ * You should have received a copy of the Lesser GNU General Public License
+ * along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2009 Kevin Woo <kwoo@2ndt.com>
+ *
+ ********/
+#include "cardbox.h"
+
+/***** Keypad definitions ******/
+#define KEYPORT PORTB
+#define KEYPIN PINB
+#define KEYDDR  DDRB
+/** @brief ROW1 of the keypad */
+#define ROW4 (_BV(PB0))
+/** @brief ROW2 of the keypad */
+#define ROW3 (_BV(PB1))
+/** @brief ROW3 of the keypad */
+#define ROW2 (_BV(PB2))
+/** @brief ROW4 of the keypad */
+#define ROW1 (_BV(PB3))
+/** @brief COL1 of the keypad */
+#define COL4 (_BV(PB4))
+/** @brief COL2 of the keypad */
+#define COL3 (_BV(PB5))
+/** @brief COL3 of the keypad */
+#define COL2 (_BV(PB6))
+/** @brief COL4 of the keypad */
+#define COL1 (_BV(PB7))
+
+#define ADDR 2
+
+/***** Global variables *****/
+
+void init_pins(void) {
+  KEYDDR = ROW1 | ROW2 | ROW3 | ROW4;
+  //KEYDDR = 0;
+  KEYPORT = COL1 | COL2 | COL3 | COL4;
+  LEDDDR = LED_RED | LED_YELLOW | LED_GREEN;
+
+  // Turn the LEDs off
+  LEDPORT |= LED_RED | LED_GREEN | LED_YELLOW;
+  
+  return;	
+}
+
+/**
+ * @brief Sets the LED to the specified state
+ *
+ * This sets LED which to the specified state. You can use this to set
+ * multiple LEDs if you OR the LEDs desired into the which argument.
+ *
+ * @param which The LEDs to set
+ * @parma state The state ON or OFF to set to the LEDs to
+ * @return void
+ */
+void toggle_led(uint8_t which, uint8_t state) {
+    if (state == ON) {
+        LEDPORT &= ~(which);
+    } else {
+        LEDPORT |= (which);
+    }
+}
+
+char get_button(void) {
+
+	char ret = ' ';
+           
+    // Row 1 Strobe
+	
+	
+  DDRB = (ROW1);
+  PORTB = (COL1|COL2|COL3|COL4);
+
+  if(!(PINB&(COL1)))
+    ret ='1';
+  else if(!(PINB&(COL2)))
+    ret ='2';
+  else if(!(PINB&(COL3)))
+    ret ='3';
+  else if(!(PINB&(COL4)))
+    ret ='A';
+  else {
+
+    DDRB = (ROW2);
+    PORTB = (COL1|COL2|COL3|COL4);
+    // Row 2 Strobe
+
+    if(!(PINB&(COL1)))
+      ret ='4';
+    else if(!(PINB&(COL2)))
+      ret ='5';
+    else if(!(PINB&(COL3)))
+      ret ='6';
+    else if(!(PINB&(COL4)))
+      ret ='B';
+    else {
+  
+      // Row 3 Strobe
+      DDRB = (ROW3);
+      PORTB = (COL1|COL2|COL3|COL4);
+  
+      if(!(PINB&(COL1)))
+        ret ='7';
+      else if(!(PINB&(COL2)))
+        ret ='8';
+      else if(!(PINB&(COL3)))
+        ret ='9';
+      else if(!(PINB&(COL4)))
+        ret ='C';
+      else{
+  
+        // Row 4 Strobe
+        DDRB = (ROW4);
+        PORTB = (COL1|COL2|COL3|COL4);
+  
+        if(!(PINB&(COL1)))
+          ret ='*';
+        else if(!(PINB&(COL2)))
+          ret ='0';
+        else if(!(PINB&(COL3)))
+          ret ='#';
+        else if(!(PINB&(COL4)))
+          ret ='D';
+	    }
+	  }
+	}
+	
+	
+  DDRB = 0;
+  PORTB = (COL1|COL2|COL3|COL4);
+
+  return ret;
+}
+
+typedef enum {
+    req,
+    press,
+    send,
+    rsp
+} state_t;
+
+
+int main(void) {
+    uint8_t mbuf[PROGD_PACKET_SIZE];
+    uint8_t cbuf[20];
+    uint8_t clen;
+    int8_t cret;
+    uint8_t resp;
+    uint8_t  c;
+    uint8_t len;
+    uint8_t retries = 0;
+    state_t state = req;
+
+    rs485_init(BAUD9600); 
+    init_pins();
+    init_timer();
+    card_reader_setup();
+    sei();
+
+    toggle_led(LED_GREEN|LED_YELLOW|LED_RED, OFF);
+    read_card = 0;
+    rs485_send_byte('s');
+
+    while(1) {
+        while(1) {
+            read_card = 1;
+            if (cr_flag != CR_NONE) {
+                read_card = 0;
+                if ((cret = parse_card(cbuf, &clen)) < 0) {
+                    switch (cret) {
+                        case CR_ERR_BAD_PARITY:
+                            toggle_led(LED_GREEN, ON);
+                            break;
+                        case CR_ERR_NO_START:
+                            toggle_led(LED_YELLOW, ON);
+                            break;
+                        case CR_ERR_NO_STOP:
+                            toggle_led(LED_RED, ON);
+                            break;
+                    }
+                    rs485_send_byte('F');
+                } else {
+                    for (c = 0; c < clen; c++) {
+                        rs485_send_byte(cbuf[c]);
+                    }
+                }
+                cr_flag = CR_NONE;
+            }
+        }
+
+
+
+        switch(state) {
+            case req:
+                toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
+                      
+                // Wait for a packet
+                resp = parse_packet(mbuf, &len, ADDR);
+
+                if (resp == TT_GET_KEY) {
+                    _delay_ms(50);
+                    send_packet(TT_ACK, ADDR, NULL, 0);
+                    toggle_led(LED_YELLOW, ON);
+                    reset_timer();
+                    reset_timeout_flag();
+                    start_timer();
+                    c = ' ';
+                    state = press;
+                } else if(resp == TT_PING){
+                    _delay_ms(50);
+                    send_packet(TT_ACK, ADDR, NULL, 0);
+                }
+                break;
+            case press:
+                c = get_button();
+                retries = 0;
+        
+                if (seconds > TIMEOUT_SECONDS) {
+                    set_timeout_flag();
+                    state = send;
+                } else if (c != ' ') {
+                    state = send;
+                } else {
+                    _delay_ms(100);
+                }
+
+                break;
+            case send:
+                if (get_timeout_flag() == 1) {
+                    _delay_ms(50);
+                    send_packet(TT_TIMEOUT, ADDR, NULL, 0);
+                    state = req;
+                    break;
+                } else {
+                    _delay_ms(50);
+                    send_packet(TT_SEND_KEY, ADDR, &c, 1);
+                }
+
+                state = rsp;
+                break;
+            case rsp:
+                resp = parse_packet(mbuf, &len,  ADDR);
+
+                if (resp == TT_ACK) {
+                    toggle_led(LED_GREEN, ON);
+                } else if ((resp == TT_NACK) || (retries == TT_MAX_RETRY)) { 
+                    toggle_led(LED_RED, ON);
+                } else {
+                    retries++;
+                    state = send;
+                    break;
+                }
+
+                _delay_ms(1000);
+                toggle_led(LED_RED, OFF);
+                toggle_led(LED_GREEN, ON);
+                state = req;
+                break;
+            default:
+                state = req;
+                break;
+        }
+    } 
+    return 0;    
+}
