Index: trunk/cardbox/tooltron.h
===================================================================
--- trunk/cardbox/tooltron.h (revision 212)
+++ trunk/cardbox/tooltron.h (nonexistent)
@@ -1,30 +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 .
- *
- * Copyright 2009 Kevin Woo
- *
- ********/
-#ifndef _TOOLTRON_H_
-#define _TOOLTRON_H_
-
-#define TT_GET_KEY 'k'
-#define TT_ACK 'a'
-#define TT_NACK 'n'
-#define TT_TO 'f'
-#define TT_TIMEOUT 't'
-
-#define TIMEOUT_SECONDS 10
-
-#endif
Index: trunk/cardbox/rs485_int.c
===================================================================
--- trunk/cardbox/rs485_int.c (revision 212)
+++ trunk/cardbox/rs485_int.c (revision 213)
@@ -32,11 +32,11 @@
void rs485_init(uint16_t baud) {
// Set baud rate
- UBRRH = (uint8_t)(baud>>8);
- UBRRL = (uint8_t)baud;
+ UBRR0H = (uint8_t)(baud>>8);
+ UBRR0L = (uint8_t)baud;
// Enable RX/TX and RX/TX Interrupt
- UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXCIE) | _BV(TXEN);
+ UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXCIE0) | _BV(TXEN0);
// Enable the TXEN pin as output
DDRD |= TX_EN;
@@ -46,7 +46,7 @@
received_byte = 0x0;
// Set to receive mode
- rs485_toggle_transmit(TX_OFF);
+ rs485_toggle_transmit(RS485_TX_OFF);
}
int8_t rs485_get_byte(uint8_t *output_byte) {
@@ -61,16 +61,16 @@
void rs485_send_byte(uint8_t data) {
//Waits until current transmit is done
- while (!(UCSRA & _BV(UDRE)));
+ while (!(UCSR0A & _BV(UDRE0)));
// Enable writes and send
- rs485_toggle_transmit(UART_TX_ON);
- UDR = data;
+ rs485_toggle_transmit(RS485_TX_ON);
+ UDR0 = data;
return;
}
void rs485_toggle_transmit(uint8_t state) {
- if (state == UART_TX_ON) {
+ if (state == RS485_TX_ON) {
PORTD |= TX_EN;
} else {
PORTD &= ~TX_EN;
@@ -78,11 +78,11 @@
}
ISR(USART_RX_vect) {
- received_byte = UDR;
+ received_byte = UDR0;
byte_ready = 1;
}
ISR(USART_TX_vect) {
// Re-enable reads
- rs485_toggle_transmit(UART_TX_OFF);
+ rs485_toggle_transmit(RS485_TX_OFF);
}
Index: trunk/cardbox/timer.c
===================================================================
--- trunk/cardbox/timer.c (revision 212)
+++ trunk/cardbox/timer.c (revision 213)
@@ -28,7 +28,12 @@
#include
#include
+char timeout_flag=0;
+char get_timeout_flag(void){
+ return timeout_flag;
+}
+
//Initializes all the timer1 registers
void init_timer() {
//Clears the control registers for timer 1
Index: trunk/cardbox/main.c
===================================================================
--- trunk/cardbox/main.c (revision 212)
+++ trunk/cardbox/main.c (revision 213)
@@ -23,6 +23,7 @@
#include
#include "timer.h"
+#include "packet.h"
/***** Keypad definitions ******/
/** @brief ROW1 of the keypad */
@@ -47,6 +48,7 @@
#define LED_RED (_BV(PC5))
#define LED_YELLOW (_BV(PC4))
#define LED_GREEN (_BV(PC3))
+#define LED_PORT PORTC
#define ADDR 2
@@ -58,7 +60,7 @@
DDRC |= LED_RED | LED_YELLOW | LED_GREEN;
- PORTC |= LED_RED | LED_GREEN | LED_YELLOW;
+ LED_PORT |= LED_RED | LED_GREEN | LED_YELLOW;
return;
}
@@ -167,32 +169,44 @@
uint8_t c;
state_t state = req;
- rs485_init(BAUD9600);
- init_pins();
- init_timer();
- sei();
+ rs485_init(BAUD9600);
+ init_pins();
+ init_timer();
+ sei();
- while(1) {
+ while(1) {
- switch(state) {
- case req:
- toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
+ while(1) {
+ toggle_led(LED_GREEN, ON);
+ send_packet(TT_TIMEOUT, ADDR);
+ _delay_ms(100);
+ toggle_led(LED_GREEN, OFF);
+ _delay_ms(100);
+ }
+
+ switch(state) {
+ case req:
+ toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
- // Wait for a packet
- resp = parse_packet(mbuf, ADDR);
+ // Wait for a packet
+ resp = parse_packet(mbuf, ADDR);
- if (resp == TT_GET_KEY) {
- toggle_led(LED_YELLOW, ON);
- reset_timer();
- reset_timeout_flag();
- start_timer();
- c = ' ';
- state = press;
- }
- break;
- case press:
+ if (resp == TT_GET_KEY) {
+ toggle_led(LED_YELLOW, ON);
+ reset_timer();
+ reset_timeout_flag();
+ start_timer();
+ c = ' ';
+ state = press;
+ }
+ else if(resp != TT_BAD){
+ toggle_led(LED_RED, ON);
+ while(1);
+ }
+ break;
+ case press:
c = get_button();
-
+
if (seconds > TIMEOUT_SECONDS) {
set_timeout_flag();
state = send;
@@ -201,8 +215,8 @@
}
break;
- case send:
- if (timeoutflag == 1) {
+ case send:
+ if (get_timeout_flag() == 1) {
send_packet(TT_TIMEOUT, ADDR);
state = req;
} else {
@@ -211,55 +225,23 @@
state = rsp;
break;
- case rsp:
- resp = parse_packet(mbuf, ADDR);
+ case rsp:
+ resp = parse_packet(mbuf, ADDR);
- if (resp == TT_ACK
+ if (resp == TT_ACK) {
+ toggle_led(LED_GREEN, ON);
+ }
+ else {
+ toggle_led(LED_RED, ON);
+ }
+ _delay_ms(1000);
+ toggle_led(LED_RED, OFF);
+ toggle_led(LED_GREEN, ON);
+ state = req;
+ break;
+ }
+ }
-
-
-
- if (timeout_flag == 0) {
- //respond with key pressed
- uart_send_byte(c);
-
- //wait for response
- c=0;
- reset_timeout_flag();
- reset_timer();
- while((uart_get_byte(&c) < 0) && (seconds < TIMEOUT_SECONDS));
-
- PORTC |= LED_YELLOW;
-
- switch(c)
- {
- case TT_ACK:
- PORTC &= ~LED_GREEN;
- break;
-
- case TT_NACK:
- PORTC &= ~LED_RED;
- break;
-
- case TT_GET_KEY: //this means the states are messed up
- PORTC &= (~LED_RED & ~LED_YELLOW & ~LED_GREEN);
- break;
-
- case 0: //timeout
- PORTC &= (~LED_RED & ~LED_YELLOW);
- break;
-
- default: //bad packet
- PORTC &= (~LED_RED & ~LED_GREEN);
- break;
- }
-
- stop_timer();
- _delay_ms(1000);
-
- }
- }
- return 0;
-
+ return 0;
}
Index: trunk/cardbox/rs485_int.h
===================================================================
--- trunk/cardbox/rs485_int.h (revision 212)
+++ trunk/cardbox/rs485_int.h (revision 213)
@@ -30,13 +30,13 @@
#include
#include
/** **/
-#define UART_TX_OFF 0
-#define UART_TX_ON 1
+#define RS485_TX_OFF 0
+#define RS485_TX_ON 1
/** @brief RX Pin for the UART **/
#define RX _BV(PORTD0)
#define TX _BV(PORTD1)
-#define TX_EN _BV(PORTD5)
+#define TX_EN _BV(PORTD2)
/**
* @brief This si the value to pass into UBRR tos et the baudrate.
Index: trunk/cardbox/timer.h
===================================================================
--- trunk/cardbox/timer.h (revision 212)
+++ trunk/cardbox/timer.h (revision 213)
@@ -32,7 +32,6 @@
/** Globals **/
volatile char seconds; //Seconds elapsed
volatile char minutes; //Minutes exlapsed
-char timeout_flag; //Indicates that timeout has occured
/** @brief Seconds that have elapsed since the timer started */
//extern volatile char seconds;
@@ -52,5 +51,7 @@
void reset_timeout_flag(void);
/** @brief Sets the timeout flag to 1 */
void set_timeout_flag(void);
+/** @brief Gets the timeout flag (nonzero means set) */
+char get_timeout_flag(void);
#endif
Index: trunk/cardbox/packet.c
===================================================================
--- trunk/cardbox/packet.c (revision 212)
+++ trunk/cardbox/packet.c (revision 213)
@@ -121,10 +121,7 @@
case cs:
if (r == crc) {
- if (cmd == TT_RESET) {
- reset();
- }
- return cmd;
+ return cmd;
} else {
return TT_BAD;
}
Index: trunk/cardbox/packet.h
===================================================================
--- trunk/cardbox/packet.h (revision 212)
+++ trunk/cardbox/packet.h (revision 213)
@@ -3,8 +3,7 @@
#include
#include
-#include "rs485_poll.h"
-#include "bootloader.h"
+#include "rs485_int.h"
/** @brief Max time to wait until we exit the packet handler */
#define MAX_TIMEOUT 60000
Index: trunk/swipe/tooltron.py
===================================================================
--- trunk/swipe/tooltron.py (revision 212)
+++ trunk/swipe/tooltron.py (revision 213)
@@ -44,7 +44,6 @@
import getpass
from time import *
-keypad = []
keypadTimeout = 11 #in seconds
#this table maps which keypad button to press for each tool. This
@@ -189,8 +188,8 @@
return
-if len(sys.argv) < 3:
- print "usage: tooltron.py /path/to/keypad/device /path/to/tool/bus/device"
+if len(sys.argv) < 2:
+ print "usage: tooltron.py /path/to/bus/device"
else:
@@ -206,15 +205,7 @@
qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
- # user has 10 seconds to press a key, after that the keypad device
- # should send a timeout packet. This is a non-blocking serial
- # device so we can re-transmit the key request when the user
- # swipes a card instead of being stuck on a blocking read call
- keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 0)
- keypad.flushInput()
- print keypad
-
- bus = serial.Serial(sys.argv[2], BAUD_RATE, timeout = 2)
+ bus = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 2)
bus.flushInput()
print bus
@@ -226,6 +217,60 @@
bus.write(msg)
return
+ def sendKeyRequest():
+ tn = 2 #2 is the ID for the cardbox
+ msg = '^' + chr(1) + chr(tn) + TT_GET_KEY + chr(1 ^ tn ^ ord(TT_GET_KEY))
+ print "seding key request"
+ bus.write(msg)
+ return
+
+ def sendAck(toolNum):
+ msg = '^' + chr(1) + chr(toolNum) + TT_ACK + chr(1 ^ tn ^ ord(TT_ACK))
+ print "seding ACK to",toolNum
+ bus.write(msg)
+ return
+
+ def sendNack(toolNum):
+ msg = '^' + chr(1) + chr(toolNum) + TT_NACK + chr(1 ^ tn ^ ord(TT_NACK))
+ print "seding ACK to",toolNum
+ bus.write(msg)
+ return
+
+
+ #returns key if the key_send packet is recived from tool ID 2
+ # otherwise returns 0
+ def readKey():
+ startDelim = bus.read(1)
+ if startDelim == '^':
+ src = bus.read(1)
+ if src == 0:
+ return 0
+ dest = bus.read(1)
+ if dest == 0:
+ return 0
+ packetType = bus.read(1)
+ if packetType == 0:
+ return 0
+ key = bus.read(1)
+ if key == 0:
+ return 0
+ crc = bus.read(1)
+
+ print "got packet"
+ if src != 2 or dest != 1 or packetType != TT_KEY_SEND:
+ print "Bad packet! ^",src,dest,packetType,key,crc
+
+ if chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)) == crc:
+ return key
+ else:
+ print "xor fail. got", crc, "should have been got",(chr(ord(src) ^ ord(dest) ^ ord(packetType) ^ ord(key)))
+
+ return 0
+
+ else:
+ print "did not get start delim!: ", ord(startDelim)
+ return 0
+
#returns [src, data] or [] on error
def readTool():
ret = [0,0,0]
@@ -263,11 +308,9 @@
if id != None:
print "\n-----------\nid# ", id.group(1)
+ sendKeyRequest()
+ startTime = time.time()
- print "sending key request"
- keypad.flushInput()
- keypad.write(TT_GET_KEY)
-
cursor.execute(qry + id.group(1))
result = cursor.fetchall()
@@ -318,10 +361,11 @@
print user
- startTime = time.time();
- resp = ""
- while resp=="":
- resp = keypad.read(1)
+
+ resp = 0
+ while resp==0:
+ resp = readKey()
+
#no lock since we are just reading and we can afford to
#miss a loop
if idready == True:
@@ -334,7 +378,7 @@
logMessage("keypad timed out")
break
- #if we have a vaid response
+ #if we have a valid response
#sometimes the FTDI chip seems to give a zero as a string when power is reset
if resp != "" and ord(resp) != 0 and resp != TT_TIMEOUT:
@@ -343,7 +387,7 @@
print "request:",resp,"(",ord(resp),") ",toolNameLong
if acl.count(resp) > 0:
- keypad.write(TT_ACK)
+ sendAck(2)
sendTool(resp)
print "ACCESS GRANTED"
logMessage(user+" ACCESSED tool "+toolNameLong)
@@ -361,7 +405,7 @@
str(MAX_TOOL_FAILS)+" times in a row.")
else:
- keypad.write(TT_NACK)
+ sendNack(2)
print "ACCESS DENIED!!"
logMessage(user + " DENIED on tool "+toolNameLong)
@@ -368,7 +412,7 @@
clear_id()
elif resp == 0 or ord(resp) == 0: #if we get noise, send timeout to reset and sync states
- keypad.write(TT_TIMEOUT);
+ keypad.write(TT_TIMEOUT)
print "ERROR: got strange byte, timing out"
logMessage("ERROR: got 0 as a byte, sending timeout (cardbox power issue?)")
else:
Index: trunk/common/tooltron.h
===================================================================
--- trunk/common/tooltron.h (revision 212)
+++ trunk/common/tooltron.h (revision 213)
@@ -28,6 +28,7 @@
// These are the bytes used in the messaage types
#define TT_GET_KEY 'k' // Get a key from the keyboard
+#define TT_SEND_KEY 's' // Send a Key back (has 1 extra data packet)
#define TT_ACK 'a' // Ack
#define TT_NACK 'n' // Nack
#define TT_ON 'o' // Turn the tool on
@@ -42,6 +43,7 @@
// Number of bytes that the PROGx packets have in the payload
#define PROGM_PACKET_SIZE 2
#define PROGD_PACKET_SIZE 32
+#define SEND_KEY_PACKET_SIZE 1
// Tool timeout?
#define TIMEOUT_SECONDS 10
Index: trunk/programmer/test/main.c
===================================================================
--- trunk/programmer/test/main.c (revision 212)
+++ trunk/programmer/test/main.c (revision 213)
@@ -32,8 +32,7 @@
#define RELAY _BV(PORTD4)
#define VAC_SENSE _BV(PIND3)
-#define BUT_RED _BV(PINB4)
-#define BUT_BLACK _BV(PINB3)
+#define BUT_RED _BV(PINB4)#define BUT_BLACK _BV(PINB3)
#define LED_GREEN _BV(PORTB2)
#define LED_YELLOW _BV(PORTB1)
#define LED_RED _BV(PORTB0)
@@ -108,21 +107,22 @@
uint8_t r;
- //eeprom_write_byte(1, 18);
+ //eeprom_write_byte(1, 12);
PORTB = 0x7;
- /* myaddr = eeprom_read_byte(1); */
- /* if (myaddr == 18) { */
- /* PORTB = 0x7; */
- /* } */
- /* _delay_ms(1000); */
- /* PORTB |= _BV(PORTB0); */
- /* _delay_ms(1000); */
- /* reset(); */
+ myaddr = eeprom_read_byte(1);
+ if (myaddr == 11) {
+ PORTB = 0;
+ while(1);
+ }
+ _delay_ms(1000);
+ PORTB |= _BV(PORTB0);
+ _delay_ms(1000);
+ reset();
while(1) {
- _delay_ms(1000);
+ _delay_ms(1000);
PORTB |= _BV(PORTB0);
_delay_ms(1000);
PORTB &= ~_BV(PORTB0);