Revision 275
Code cleanup for cardreader
trunk/cardbox/cardreader.c | ||
---|---|---|
5 | 5 |
#define CR_CL (_BV(PIND5)) |
6 | 6 |
#define CR_CLK (_BV(PIND6)) |
7 | 7 |
#define CR_DATA (_BV(PIND7)) |
8 |
#ifdef SD |
|
9 |
#define CR_MAX_IDX 4 |
|
8 |
|
|
9 |
#ifdef TRACK2 |
|
10 |
#define CR_MAX_IDX 4 |
|
11 |
#define CR_SS ':' // Start sentinal |
|
10 | 12 |
#else |
11 |
#define CR_MAX_IDX 6 |
|
13 |
#define CR_MAX_IDX 6 |
|
14 |
#define CR_SS '%' // Start sentinal |
|
12 | 15 |
#endif |
13 |
#define CR_SS '%' // Start sentinal |
|
16 |
|
|
14 | 17 |
#define CR_ES '?' // End sentinal |
15 | 18 |
|
16 | 19 |
// This is only written in the PCINT2 intterupt or in the main loop |
... | ... | |
20 | 23 |
volatile uint16_t cr_buf_idx; |
21 | 24 |
volatile uint8_t read_card; |
22 | 25 |
|
23 |
#ifdef SD
|
|
26 |
#ifdef TRACK2
|
|
24 | 27 |
const char cr_dict[] = {'0', '1', '2', '3', '4', '5', '6', '7', |
25 | 28 |
'8', '9', ':', ';', '<', '=', '>', '?'}; |
26 | 29 |
#else |
... | ... | |
40 | 43 |
|
41 | 44 |
// Turn on pin interrupts for pins D7, D6, D5 |
42 | 45 |
PCICR = _BV(PCIE2); |
43 |
PCMSK2 = /*_BV(PCINT23) |*/ _BV(PCINT22) | _BV(PCINT21);
|
|
46 |
PCMSK2 = _BV(PCINT22) | _BV(PCINT21); |
|
44 | 47 |
|
45 | 48 |
// Make pins D7, D6, D5 inputs |
46 | 49 |
DDRD &= ~_BV(DDD7) & ~_BV(DDD6) & ~_BV(DDD5); |
... | ... | |
49 | 52 |
read_card = 0; |
50 | 53 |
} |
51 | 54 |
|
52 |
ISR(PCINT2_vect) { |
|
53 |
static uint8_t reading = 0; // Currently reading |
|
54 |
|
|
55 |
// Only read card if we are expecting it |
|
56 |
if (!read_card) { |
|
57 |
return; |
|
58 |
} |
|
59 |
|
|
60 |
// Check if the card is inserted or not |
|
61 |
if (!(PIND & CR_CL) && (reading == 0)) { |
|
62 |
cr_flag = CR_NONE; |
|
63 |
cr_buf_idx = 0; |
|
64 |
reading = 1; |
|
65 |
// check if the card has finished swiping |
|
66 |
} else if (PIND & CR_CL) { |
|
67 |
toggle_led(LED_RED, ON); |
|
68 |
reading = 0; |
|
69 |
cr_flag = CR_GOOD; |
|
70 |
return; |
|
71 |
} |
|
72 |
|
|
73 |
// Read data in on the downtick of the clock |
|
74 |
if (!(PIND & (CR_CLK))) { |
|
75 |
// Read data from card reader and flip since ours inverts |
|
76 |
cr_buf[cr_buf_idx] = (PIND & CR_DATA) ? 0 : 0x20; |
|
77 |
cr_buf_idx++; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 | 55 |
int8_t parse_card(uint8_t *buf, uint8_t *buflen) { |
82 | 56 |
int32_t i = cr_buf_idx - 1; |
83 | 57 |
uint8_t j = 0; |
... | ... | |
94 | 68 |
start_sentinal = 1; |
95 | 69 |
|
96 | 70 |
(buf)[j++] = cr_dict[byte]; |
97 |
//rs485_send_byte(cr_dict[byte]); |
|
98 | 71 |
|
99 | 72 |
// Skip parity bit |
100 | 73 |
i--; |
... | ... | |
122 | 95 |
return CR_ERR_BAD_PARITY; |
123 | 96 |
} |
124 | 97 |
|
125 |
//rs485_send_byte(cr_dict[byte]); |
|
126 | 98 |
(buf)[j++] = cr_dict[byte]; |
127 | 99 |
|
128 | 100 |
// Stop at the stop sentinal |
... | ... | |
148 | 120 |
|
149 | 121 |
return CR_ERR_NO_STOP; |
150 | 122 |
} |
123 |
|
|
124 |
ISR(PCINT2_vect) { |
|
125 |
static uint8_t reading = 0; // Currently reading |
|
126 |
|
|
127 |
// Only read card if we are expecting it |
|
128 |
if (!read_card) { |
|
129 |
return; |
|
130 |
} |
|
131 |
|
|
132 |
// Check if the card is inserted or not |
|
133 |
if (!(PIND & CR_CL) && (!reading)) { |
|
134 |
cr_flag = CR_NONE; |
|
135 |
cr_buf_idx = 0; |
|
136 |
reading = 1; |
|
137 |
// check if the card has finished swiping |
|
138 |
} else if (PIND & CR_CL) { |
|
139 |
cr_flag = CR_GOOD; |
|
140 |
reading = 0; |
|
141 |
return; |
|
142 |
} |
|
143 |
|
|
144 |
// Read data in on the downtick of the clock |
|
145 |
if (!(PIND & CR_CLK)) { |
|
146 |
// Read data from card reader and flip since ours inverts |
|
147 |
cr_buf[cr_buf_idx] = (PIND & CR_DATA) ? 0 : 0x20; |
|
148 |
cr_buf_idx++; |
|
149 |
} |
|
150 |
} |
Also available in: Unified diff