Project

General

Profile

Statistics
| Revision:

root / trunk / cardbox / main.c @ 139

History | View | Annotate | Download (4.08 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
    while(c ==' ') {
161
        if (seconds > TIMEOUT_SECONDS) {
162
            set_timeout_flag();
163
            serial_putchar(TT_TIMEOUT);
164
            break;
165
        }
166

    
167
        c = get_button();
168
        _delay_ms(100);
169
    }
170

    
171

    
172
    if (timeout_flag == 0) {
173
        //respond with key pressed
174
        serial_putchar(c);
175
        
176
        //wait for response
177
        c=0;
178
        reset_timeout_flag();
179
        reset_timer();
180
        while(!c && seconds < TIMEOUT_SECONDS)
181
          c = serial_getchar_nb();
182

    
183
        PORTC |= LED_YELLOW;
184

    
185
        switch(c)
186
        {
187
          case TT_ACK:
188
            PORTC &= ~LED_GREEN;
189
            break;
190
          
191
          case TT_NACK:
192
            PORTC &= ~LED_RED;
193
            break;
194

    
195
          default:
196
            PORTC &= (~LED_RED & ~LED_GREEN);
197
            break;
198
        }
199
        
200
        stop_timer();
201
        _delay_ms(1000);
202
            
203
      }
204
    } 
205
  return 0;
206
    
207
}