root / trunk / cardbox / main.c @ 213
History | View | Annotate | Download (5.08 KB)
1 | 139 | kwoo | /********
|
---|---|---|---|
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 | 211 | kwoo | #include "rs485_int.h" |
21 | 139 | kwoo | #include <avr/interrupt.h> |
22 | #include <avr/io.h> |
||
23 | |||
24 | 211 | kwoo | #include <tooltron.h> |
25 | 139 | kwoo | #include "timer.h" |
26 | 213 | bneuman | #include "packet.h" |
27 | 139 | kwoo | |
28 | /***** Keypad definitions ******/
|
||
29 | /** @brief ROW1 of the keypad */
|
||
30 | #define ROW4 (_BV(PB0))
|
||
31 | /** @brief ROW2 of the keypad */
|
||
32 | #define ROW3 (_BV(PB1))
|
||
33 | /** @brief ROW3 of the keypad */
|
||
34 | #define ROW2 (_BV(PB2))
|
||
35 | /** @brief ROW4 of the keypad */
|
||
36 | #define ROW1 (_BV(PB3))
|
||
37 | /** @brief COL1 of the keypad */
|
||
38 | #define COL4 (_BV(PB4))
|
||
39 | /** @brief COL2 of the keypad */
|
||
40 | |||
41 | #define COL3 (_BV(PB5))
|
||
42 | /** @brief COL3 of the keypad */
|
||
43 | #define COL2 (_BV(PB6))
|
||
44 | /** @brief COL4 of the keypad */
|
||
45 | #define COL1 (_BV(PB7))
|
||
46 | |||
47 | /***** LED definitions *****/
|
||
48 | #define LED_RED (_BV(PC5))
|
||
49 | #define LED_YELLOW (_BV(PC4))
|
||
50 | #define LED_GREEN (_BV(PC3))
|
||
51 | 213 | bneuman | #define LED_PORT PORTC
|
52 | 139 | kwoo | |
53 | 211 | kwoo | #define ADDR 2 |
54 | 139 | kwoo | |
55 | /***** Global variables *****/
|
||
56 | |||
57 | void init_pins(void) { |
||
58 | DDRB = 0;
|
||
59 | PORTB = (COL1|COL2|COL3|COL4); |
||
60 | |||
61 | DDRC |= LED_RED | LED_YELLOW | LED_GREEN; |
||
62 | |||
63 | 213 | bneuman | LED_PORT |= LED_RED | LED_GREEN | LED_YELLOW; |
64 | 139 | kwoo | |
65 | return;
|
||
66 | } |
||
67 | |||
68 | 211 | kwoo | /**
|
69 | * @brief Sets the LED to the specified state
|
||
70 | *
|
||
71 | * This sets LED which to the specified state. You can use this to set
|
||
72 | * multiple LEDs if you OR the LEDs desired into the which argument.
|
||
73 | *
|
||
74 | * @param which The LEDs to set
|
||
75 | * @parma state The state ON or OFF to set to the LEDs to
|
||
76 | * @return void
|
||
77 | */
|
||
78 | void toggle_led(uint8_t which, uint8_t state) {
|
||
79 | if (state == ON) {
|
||
80 | LED_PORT &= ~(which); |
||
81 | } else {
|
||
82 | LED_PORT |= (which); |
||
83 | } |
||
84 | } |
||
85 | 139 | kwoo | |
86 | 211 | kwoo | |
87 | 139 | kwoo | char get_button(void) { |
88 | |||
89 | char ret = ' '; |
||
90 | |||
91 | // Row 1 Strobe
|
||
92 | |||
93 | |||
94 | DDRB = (ROW1); |
||
95 | PORTB = (COL1|COL2|COL3|COL4); |
||
96 | |||
97 | if(!(PINB&(COL1)))
|
||
98 | ret ='1';
|
||
99 | else if(!(PINB&(COL2))) |
||
100 | ret ='2';
|
||
101 | else if(!(PINB&(COL3))) |
||
102 | ret ='3';
|
||
103 | else if(!(PINB&(COL4))) |
||
104 | ret ='A';
|
||
105 | else {
|
||
106 | |||
107 | DDRB = (ROW2); |
||
108 | PORTB = (COL1|COL2|COL3|COL4); |
||
109 | // Row 2 Strobe
|
||
110 | |||
111 | if(!(PINB&(COL1)))
|
||
112 | ret ='4';
|
||
113 | else if(!(PINB&(COL2))) |
||
114 | ret ='5';
|
||
115 | else if(!(PINB&(COL3))) |
||
116 | ret ='6';
|
||
117 | else if(!(PINB&(COL4))) |
||
118 | ret ='B';
|
||
119 | else {
|
||
120 | |||
121 | // Row 3 Strobe
|
||
122 | DDRB = (ROW3); |
||
123 | PORTB = (COL1|COL2|COL3|COL4); |
||
124 | |||
125 | if(!(PINB&(COL1)))
|
||
126 | ret ='7';
|
||
127 | else if(!(PINB&(COL2))) |
||
128 | ret ='8';
|
||
129 | else if(!(PINB&(COL3))) |
||
130 | ret ='9';
|
||
131 | else if(!(PINB&(COL4))) |
||
132 | ret ='C';
|
||
133 | else{
|
||
134 | |||
135 | // Row 4 Strobe
|
||
136 | DDRB = (ROW4); |
||
137 | PORTB = (COL1|COL2|COL3|COL4); |
||
138 | |||
139 | if(!(PINB&(COL1)))
|
||
140 | ret ='*';
|
||
141 | else if(!(PINB&(COL2))) |
||
142 | ret ='0';
|
||
143 | else if(!(PINB&(COL3))) |
||
144 | ret ='#';
|
||
145 | else if(!(PINB&(COL4))) |
||
146 | ret ='D';
|
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | |||
152 | DDRB = 0;
|
||
153 | PORTB = (COL1|COL2|COL3|COL4); |
||
154 | |||
155 | return ret;
|
||
156 | } |
||
157 | |||
158 | 211 | kwoo | typedef enum { |
159 | req, |
||
160 | press, |
||
161 | send, |
||
162 | rsp |
||
163 | } state_t; |
||
164 | 139 | kwoo | |
165 | 211 | kwoo | |
166 | int main(void) { |
||
167 | uint8_t mbuf[PROGD_PACKET_SIZE]; |
||
168 | uint8_t resp; |
||
169 | uint8_t c; |
||
170 | state_t state = req; |
||
171 | |||
172 | 213 | bneuman | rs485_init(BAUD9600); |
173 | init_pins(); |
||
174 | init_timer(); |
||
175 | sei(); |
||
176 | 139 | kwoo | |
177 | 213 | bneuman | while(1) { |
178 | 139 | kwoo | |
179 | 213 | bneuman | while(1) { |
180 | toggle_led(LED_GREEN, ON); |
||
181 | send_packet(TT_TIMEOUT, ADDR); |
||
182 | _delay_ms(100);
|
||
183 | toggle_led(LED_GREEN, OFF); |
||
184 | _delay_ms(100);
|
||
185 | } |
||
186 | |||
187 | switch(state) {
|
||
188 | case req:
|
||
189 | toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF); |
||
190 | 211 | kwoo | |
191 | 213 | bneuman | // Wait for a packet
|
192 | resp = parse_packet(mbuf, ADDR); |
||
193 | 139 | kwoo | |
194 | 213 | bneuman | if (resp == TT_GET_KEY) {
|
195 | toggle_led(LED_YELLOW, ON); |
||
196 | reset_timer(); |
||
197 | reset_timeout_flag(); |
||
198 | start_timer(); |
||
199 | c = ' ';
|
||
200 | state = press; |
||
201 | } |
||
202 | else if(resp != TT_BAD){ |
||
203 | toggle_led(LED_RED, ON); |
||
204 | while(1); |
||
205 | } |
||
206 | break;
|
||
207 | case press:
|
||
208 | 211 | kwoo | c = get_button(); |
209 | 213 | bneuman | |
210 | 211 | kwoo | if (seconds > TIMEOUT_SECONDS) {
|
211 | set_timeout_flag(); |
||
212 | state = send; |
||
213 | } else if (c != ' ') { |
||
214 | state = send; |
||
215 | } |
||
216 | 139 | kwoo | |
217 | 211 | kwoo | break;
|
218 | 213 | bneuman | case send:
|
219 | if (get_timeout_flag() == 1) { |
||
220 | 211 | kwoo | send_packet(TT_TIMEOUT, ADDR); |
221 | state = req; |
||
222 | } else {
|
||
223 | send_packet_data(TT_SEND_KEY, ADDR, &c, 1);
|
||
224 | } |
||
225 | 139 | kwoo | |
226 | 211 | kwoo | state = rsp; |
227 | break;
|
||
228 | 213 | bneuman | case rsp:
|
229 | resp = parse_packet(mbuf, ADDR); |
||
230 | 139 | kwoo | |
231 | 213 | bneuman | if (resp == TT_ACK) {
|
232 | toggle_led(LED_GREEN, ON); |
||
233 | } |
||
234 | else {
|
||
235 | toggle_led(LED_RED, ON); |
||
236 | } |
||
237 | _delay_ms(1000);
|
||
238 | toggle_led(LED_RED, OFF); |
||
239 | toggle_led(LED_GREEN, ON); |
||
240 | state = req; |
||
241 | break;
|
||
242 | } |
||
243 | 211 | kwoo | |
244 | 213 | bneuman | } |
245 | 211 | kwoo | |
246 | 213 | bneuman | return 0; |
247 | 139 | kwoo | } |