root / trunk / cardbox / main.c @ 150
History | View | Annotate | Download (4.39 KB)
1 |
/********
|
---|---|
2 |
* This file is part of Tooltron.
|
3 |
*
|
4 |
* Tooltron is free software: you can redistribute it and/or modify
|
5 |
* it under the terms of the Lesser GNU General Public License as published by
|
6 |
* the Free Software Foundation, either version 3 of the License, or
|
7 |
* (at your option) any later version.
|
8 |
*
|
9 |
* Tooltron is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* Lesser GNU General Public License for more details.
|
13 |
* You should have received a copy of the Lesser GNU General Public License
|
14 |
* along with Tooltron. If not, see <http://www.gnu.org/licenses/>.
|
15 |
*
|
16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com>
|
17 |
*
|
18 |
********/
|
19 |
#include <util/delay.h> |
20 |
#include "serial.h" |
21 |
#include <avr/interrupt.h> |
22 |
#include <avr/io.h> |
23 |
|
24 |
#include "tooltron.h" |
25 |
#include "timer.h" |
26 |
|
27 |
/***** Keypad definitions ******/
|
28 |
/** @brief ROW1 of the keypad */
|
29 |
#define ROW4 (_BV(PB0))
|
30 |
/** @brief ROW2 of the keypad */
|
31 |
#define ROW3 (_BV(PB1))
|
32 |
/** @brief ROW3 of the keypad */
|
33 |
#define ROW2 (_BV(PB2))
|
34 |
/** @brief ROW4 of the keypad */
|
35 |
#define ROW1 (_BV(PB3))
|
36 |
/** @brief COL1 of the keypad */
|
37 |
#define COL4 (_BV(PB4))
|
38 |
/** @brief COL2 of the keypad */
|
39 |
|
40 |
#define COL3 (_BV(PB5))
|
41 |
/** @brief COL3 of the keypad */
|
42 |
#define COL2 (_BV(PB6))
|
43 |
/** @brief COL4 of the keypad */
|
44 |
#define COL1 (_BV(PB7))
|
45 |
|
46 |
/***** LED definitions *****/
|
47 |
#define LED_RED (_BV(PC5))
|
48 |
#define LED_YELLOW (_BV(PC4))
|
49 |
#define LED_GREEN (_BV(PC3))
|
50 |
|
51 |
|
52 |
/***** Global variables *****/
|
53 |
|
54 |
void init_pins(void) { |
55 |
DDRB = 0;
|
56 |
PORTB = (COL1|COL2|COL3|COL4); |
57 |
|
58 |
DDRC |= LED_RED | LED_YELLOW | LED_GREEN; |
59 |
|
60 |
PORTC |= LED_RED | LED_GREEN | LED_YELLOW; |
61 |
|
62 |
return;
|
63 |
} |
64 |
|
65 |
|
66 |
char get_button(void) { |
67 |
|
68 |
char ret = ' '; |
69 |
|
70 |
// Row 1 Strobe
|
71 |
|
72 |
|
73 |
DDRB = (ROW1); |
74 |
PORTB = (COL1|COL2|COL3|COL4); |
75 |
|
76 |
if(!(PINB&(COL1)))
|
77 |
ret ='1';
|
78 |
else if(!(PINB&(COL2))) |
79 |
ret ='2';
|
80 |
else if(!(PINB&(COL3))) |
81 |
ret ='3';
|
82 |
else if(!(PINB&(COL4))) |
83 |
ret ='A';
|
84 |
else {
|
85 |
|
86 |
DDRB = (ROW2); |
87 |
PORTB = (COL1|COL2|COL3|COL4); |
88 |
// Row 2 Strobe
|
89 |
|
90 |
if(!(PINB&(COL1)))
|
91 |
ret ='4';
|
92 |
else if(!(PINB&(COL2))) |
93 |
ret ='5';
|
94 |
else if(!(PINB&(COL3))) |
95 |
ret ='6';
|
96 |
else if(!(PINB&(COL4))) |
97 |
ret ='B';
|
98 |
else {
|
99 |
|
100 |
// Row 3 Strobe
|
101 |
DDRB = (ROW3); |
102 |
PORTB = (COL1|COL2|COL3|COL4); |
103 |
|
104 |
if(!(PINB&(COL1)))
|
105 |
ret ='7';
|
106 |
else if(!(PINB&(COL2))) |
107 |
ret ='8';
|
108 |
else if(!(PINB&(COL3))) |
109 |
ret ='9';
|
110 |
else if(!(PINB&(COL4))) |
111 |
ret ='C';
|
112 |
else{
|
113 |
|
114 |
// Row 4 Strobe
|
115 |
DDRB = (ROW4); |
116 |
PORTB = (COL1|COL2|COL3|COL4); |
117 |
|
118 |
if(!(PINB&(COL1)))
|
119 |
ret ='*';
|
120 |
else if(!(PINB&(COL2))) |
121 |
ret ='0';
|
122 |
else if(!(PINB&(COL3))) |
123 |
ret ='#';
|
124 |
else if(!(PINB&(COL4))) |
125 |
ret ='D';
|
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
|
131 |
DDRB = 0;
|
132 |
PORTB = (COL1|COL2|COL3|COL4); |
133 |
|
134 |
return ret;
|
135 |
} |
136 |
|
137 |
int main(void) |
138 |
{ |
139 |
|
140 |
char c;
|
141 |
serial_init(BAUD9600); |
142 |
init_pins(); |
143 |
init_timer(); |
144 |
sei(); |
145 |
|
146 |
while(1) |
147 |
{ |
148 |
|
149 |
PORTC |= LED_RED | LED_GREEN | LED_YELLOW; |
150 |
|
151 |
//wait for key request from server
|
152 |
while(serial_getchar()!=TT_GET_KEY);
|
153 |
|
154 |
PORTC &=~LED_YELLOW; |
155 |
reset_timer(); |
156 |
reset_timeout_flag(); |
157 |
start_timer(); |
158 |
|
159 |
c = ' ';
|
160 |
serial_disable_rx(); |
161 |
while(c ==' ') { |
162 |
if (seconds > TIMEOUT_SECONDS) {
|
163 |
set_timeout_flag(); |
164 |
serial_putchar(TT_TIMEOUT); |
165 |
serial_enable_rx(); |
166 |
break;
|
167 |
} |
168 |
|
169 |
c = get_button(); |
170 |
_delay_ms(100);
|
171 |
} |
172 |
serial_enable_rx(); |
173 |
|
174 |
|
175 |
if (timeout_flag == 0) { |
176 |
//respond with key pressed
|
177 |
serial_putchar(c); |
178 |
|
179 |
//wait for response
|
180 |
c=0;
|
181 |
reset_timeout_flag(); |
182 |
reset_timer(); |
183 |
while(!c && seconds < TIMEOUT_SECONDS)
|
184 |
c = serial_getchar_nb(); |
185 |
|
186 |
PORTC |= LED_YELLOW; |
187 |
|
188 |
switch(c)
|
189 |
{ |
190 |
case TT_ACK:
|
191 |
PORTC &= ~LED_GREEN; |
192 |
break;
|
193 |
|
194 |
case TT_NACK:
|
195 |
PORTC &= ~LED_RED; |
196 |
break;
|
197 |
|
198 |
case TT_GET_KEY: //this means the states are messed up |
199 |
PORTC &= (~LED_RED & ~LED_YELLOW & ~LED_GREEN); |
200 |
break;
|
201 |
|
202 |
case 0: //timeout |
203 |
PORTC &= (~LED_RED & ~LED_YELLOW); |
204 |
break;
|
205 |
|
206 |
default: //bad packet |
207 |
PORTC &= (~LED_RED & ~LED_GREEN); |
208 |
break;
|
209 |
} |
210 |
|
211 |
stop_timer(); |
212 |
_delay_ms(1000);
|
213 |
|
214 |
} |
215 |
} |
216 |
return 0; |
217 |
|
218 |
} |