root / trunk / cardbox / main.c @ 211
History | View | Annotate | Download (5.6 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 "rs485_int.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 | #define ADDR 2 |
| 52 | |
| 53 | /***** Global variables *****/
|
| 54 | |
| 55 | void init_pins(void) { |
| 56 | DDRB = 0;
|
| 57 | PORTB = (COL1|COL2|COL3|COL4); |
| 58 | |
| 59 | DDRC |= LED_RED | LED_YELLOW | LED_GREEN; |
| 60 | |
| 61 | PORTC |= LED_RED | LED_GREEN | LED_YELLOW; |
| 62 | |
| 63 | return;
|
| 64 | } |
| 65 | |
| 66 | /**
|
| 67 | * @brief Sets the LED to the specified state |
| 68 | * |
| 69 | * This sets LED which to the specified state. You can use this to set |
| 70 | * multiple LEDs if you OR the LEDs desired into the which argument. |
| 71 | * |
| 72 | * @param which The LEDs to set |
| 73 | * @parma state The state ON or OFF to set to the LEDs to |
| 74 | * @return void |
| 75 | */ |
| 76 | void toggle_led(uint8_t which, uint8_t state) {
|
| 77 | if (state == ON) {
|
| 78 | LED_PORT &= ~(which); |
| 79 | } else {
|
| 80 | LED_PORT |= (which); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | char get_button(void) { |
| 86 | |
| 87 | char ret = ' '; |
| 88 | |
| 89 | // Row 1 Strobe
|
| 90 | |
| 91 | |
| 92 | DDRB = (ROW1); |
| 93 | PORTB = (COL1|COL2|COL3|COL4); |
| 94 | |
| 95 | if(!(PINB&(COL1)))
|
| 96 | ret ='1';
|
| 97 | else if(!(PINB&(COL2))) |
| 98 | ret ='2';
|
| 99 | else if(!(PINB&(COL3))) |
| 100 | ret ='3';
|
| 101 | else if(!(PINB&(COL4))) |
| 102 | ret ='A';
|
| 103 | else {
|
| 104 | |
| 105 | DDRB = (ROW2); |
| 106 | PORTB = (COL1|COL2|COL3|COL4); |
| 107 | // Row 2 Strobe
|
| 108 | |
| 109 | if(!(PINB&(COL1)))
|
| 110 | ret ='4';
|
| 111 | else if(!(PINB&(COL2))) |
| 112 | ret ='5';
|
| 113 | else if(!(PINB&(COL3))) |
| 114 | ret ='6';
|
| 115 | else if(!(PINB&(COL4))) |
| 116 | ret ='B';
|
| 117 | else {
|
| 118 | |
| 119 | // Row 3 Strobe
|
| 120 | DDRB = (ROW3); |
| 121 | PORTB = (COL1|COL2|COL3|COL4); |
| 122 | |
| 123 | if(!(PINB&(COL1)))
|
| 124 | ret ='7';
|
| 125 | else if(!(PINB&(COL2))) |
| 126 | ret ='8';
|
| 127 | else if(!(PINB&(COL3))) |
| 128 | ret ='9';
|
| 129 | else if(!(PINB&(COL4))) |
| 130 | ret ='C';
|
| 131 | else{
|
| 132 | |
| 133 | // Row 4 Strobe
|
| 134 | DDRB = (ROW4); |
| 135 | PORTB = (COL1|COL2|COL3|COL4); |
| 136 | |
| 137 | if(!(PINB&(COL1)))
|
| 138 | ret ='*';
|
| 139 | else if(!(PINB&(COL2))) |
| 140 | ret ='0';
|
| 141 | else if(!(PINB&(COL3))) |
| 142 | ret ='#';
|
| 143 | else if(!(PINB&(COL4))) |
| 144 | ret ='D';
|
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | DDRB = 0;
|
| 151 | PORTB = (COL1|COL2|COL3|COL4); |
| 152 | |
| 153 | return ret;
|
| 154 | } |
| 155 | |
| 156 | typedef enum { |
| 157 | req, |
| 158 | press, |
| 159 | send, |
| 160 | rsp |
| 161 | } state_t; |
| 162 | |
| 163 | |
| 164 | int main(void) { |
| 165 | uint8_t mbuf[PROGD_PACKET_SIZE]; |
| 166 | uint8_t resp; |
| 167 | uint8_t c; |
| 168 | state_t state = req; |
| 169 | |
| 170 | rs485_init(BAUD9600); |
| 171 | init_pins(); |
| 172 | init_timer(); |
| 173 | sei(); |
| 174 | |
| 175 | while(1) { |
| 176 | |
| 177 | switch(state) {
|
| 178 | case req: |
| 179 | toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF); |
| 180 | |
| 181 | // Wait for a packet
|
| 182 | resp = parse_packet(mbuf, ADDR); |
| 183 | |
| 184 | if (resp == TT_GET_KEY) {
|
| 185 | toggle_led(LED_YELLOW, ON); |
| 186 | reset_timer(); |
| 187 | reset_timeout_flag(); |
| 188 | start_timer(); |
| 189 | c = ' ';
|
| 190 | state = press; |
| 191 | } |
| 192 | break;
|
| 193 | case press: |
| 194 | c = get_button(); |
| 195 | |
| 196 | if (seconds > TIMEOUT_SECONDS) {
|
| 197 | set_timeout_flag(); |
| 198 | state = send; |
| 199 | } else if (c != ' ') { |
| 200 | state = send; |
| 201 | } |
| 202 | |
| 203 | break;
|
| 204 | case send: |
| 205 | if (timeoutflag == 1) { |
| 206 | send_packet(TT_TIMEOUT, ADDR); |
| 207 | state = req; |
| 208 | } else {
|
| 209 | send_packet_data(TT_SEND_KEY, ADDR, &c, 1);
|
| 210 | } |
| 211 | |
| 212 | state = rsp; |
| 213 | break;
|
| 214 | case rsp: |
| 215 | resp = parse_packet(mbuf, ADDR); |
| 216 | |
| 217 | if (resp == TT_ACK
|
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | if (timeout_flag == 0) { |
| 224 | //respond with key pressed
|
| 225 | uart_send_byte(c); |
| 226 | |
| 227 | //wait for response
|
| 228 | c=0;
|
| 229 | reset_timeout_flag(); |
| 230 | reset_timer(); |
| 231 | while((uart_get_byte(&c) < 0) && (seconds < TIMEOUT_SECONDS)); |
| 232 | |
| 233 | PORTC |= LED_YELLOW; |
| 234 | |
| 235 | switch(c)
|
| 236 | {
|
| 237 | case TT_ACK: |
| 238 | PORTC &= ~LED_GREEN; |
| 239 | break;
|
| 240 | |
| 241 | case TT_NACK: |
| 242 | PORTC &= ~LED_RED; |
| 243 | break;
|
| 244 | |
| 245 | case TT_GET_KEY: //this means the states are messed up |
| 246 | PORTC &= (~LED_RED & ~LED_YELLOW & ~LED_GREEN); |
| 247 | break;
|
| 248 | |
| 249 | case 0: //timeout |
| 250 | PORTC &= (~LED_RED & ~LED_YELLOW); |
| 251 | break;
|
| 252 | |
| 253 | default: //bad packet |
| 254 | PORTC &= (~LED_RED & ~LED_GREEN); |
| 255 | break;
|
| 256 | } |
| 257 | |
| 258 | stop_timer(); |
| 259 | _delay_ms(1000);
|
| 260 | |
| 261 | } |
| 262 | } |
| 263 | return 0; |
| 264 | |
| 265 | } |