Project

General

Profile

Revision 139

Added by Kevin Woo over 14 years ago

Added LGPL to all source code except the hardware schematics.

View differences:

main.c
1
#include <util/delay.h>
2
#include "serial.h"
3
#include <avr/interrupt.h>
4
#include <avr/io.h>
5

  
6
#include "tooltron.h"
7
#include "timer.h"
8

  
9
/***** Keypad definitions ******/
10
/** @brief ROW1 of the keypad */
11
#define ROW4 (_BV(PB0))
12
/** @brief ROW2 of the keypad */
13
#define ROW3 (_BV(PB1))
14
/** @brief ROW3 of the keypad */
15
#define ROW2 (_BV(PB2))
16
/** @brief ROW4 of the keypad */
17
#define ROW1 (_BV(PB3))
18
/** @brief COL1 of the keypad */
19
#define COL4 (_BV(PB4))
20
/** @brief COL2 of the keypad */
21

  
22
#define COL3 (_BV(PB5))
23
/** @brief COL3 of the keypad */
24
#define COL2 (_BV(PB6))
25
/** @brief COL4 of the keypad */
26
#define COL1 (_BV(PB7))
27

  
28
/***** LED definitions *****/
29
#define LED_RED (_BV(PC5))
30
#define LED_YELLOW (_BV(PC4))
31
#define LED_GREEN (_BV(PC3))
32

  
33

  
34
/***** Global variables *****/
35

  
36
void init_pins(void) {
37
  DDRB = 0;
38
  PORTB = (COL1|COL2|COL3|COL4);
39

  
40
  DDRC |= LED_RED | LED_YELLOW | LED_GREEN;
41

  
42
  PORTC |= LED_RED | LED_GREEN | LED_YELLOW;
43
  
44
  return;	
45
}
46

  
47

  
48
char get_button(void) {
49

  
50
	char ret = ' ';
51
           
52
    // Row 1 Strobe
53
	
54
	
55
  DDRB = (ROW1);
56
  PORTB = (COL1|COL2|COL3|COL4);
57

  
58
  if(!(PINB&(COL1)))
59
    ret ='1';
60
  else if(!(PINB&(COL2)))
61
    ret ='2';
62
  else if(!(PINB&(COL3)))
63
    ret ='3';
64
  else if(!(PINB&(COL4)))
65
    ret ='A';
66
  else {
67

  
68
    DDRB = (ROW2);
69
    PORTB = (COL1|COL2|COL3|COL4);
70
    // Row 2 Strobe
71

  
72
    if(!(PINB&(COL1)))
73
      ret ='4';
74
    else if(!(PINB&(COL2)))
75
      ret ='5';
76
    else if(!(PINB&(COL3)))
77
      ret ='6';
78
    else if(!(PINB&(COL4)))
79
      ret ='B';
80
    else {
81
  
82
      // Row 3 Strobe
83
      DDRB = (ROW3);
84
      PORTB = (COL1|COL2|COL3|COL4);
85
  
86
      if(!(PINB&(COL1)))
87
        ret ='7';
88
      else if(!(PINB&(COL2)))
89
        ret ='8';
90
      else if(!(PINB&(COL3)))
91
        ret ='9';
92
      else if(!(PINB&(COL4)))
93
        ret ='C';
94
      else{
95
  
96
        // Row 4 Strobe
97
        DDRB = (ROW4);
98
        PORTB = (COL1|COL2|COL3|COL4);
99
  
100
        if(!(PINB&(COL1)))
101
          ret ='*';
102
        else if(!(PINB&(COL2)))
103
          ret ='0';
104
        else if(!(PINB&(COL3)))
105
          ret ='#';
106
        else if(!(PINB&(COL4)))
107
          ret ='D';
108
	    }
109
	  }
110
	}
111
	
112
	
113
  DDRB = 0;
114
  PORTB = (COL1|COL2|COL3|COL4);
115

  
116
  return ret;
117
}
118

  
119
int main(void)
120
{
121

  
122
  char c;
123
  serial_init(BAUD9600);
124
  init_pins();
125
  init_timer();
126
  sei();
127

  
128
  while(1)
129
  {
130

  
131
    PORTC |= LED_RED | LED_GREEN | LED_YELLOW;
132
    
133
    //wait for key request from server
134
    while(serial_getchar()!=TT_GET_KEY);
135

  
136
    PORTC &=~LED_YELLOW;
137
    reset_timer();
138
    reset_timeout_flag();
139
    start_timer();
140

  
141
    c = ' ';
142
    while(c ==' ') {
143
        if (seconds > TIMEOUT_SECONDS) {
144
            set_timeout_flag();
145
	    serial_putchar(TT_TIMEOUT);
146
            break;
147
        }
148

  
149
        c = get_button();
150
        _delay_ms(100);
151
    }
152

  
153

  
154
    if (timeout_flag == 0) {
155
        //respond with key pressed
156
        serial_putchar(c);
157
        
158
        //wait for response
159
        c=0;
160
        reset_timeout_flag();
161
        reset_timer();
162
        while(!c && seconds < TIMEOUT_SECONDS)
163
          c = serial_getchar_nb();
164

  
165
        PORTC |= LED_YELLOW;
166

  
167
        switch(c)
168
        {
169
          case TT_ACK:
170
            PORTC &= ~LED_GREEN;
171
            break;
172
          
173
          case TT_NACK:
174
            PORTC &= ~LED_RED;
175
            break;
176

  
177
          default:
178
            PORTC &= (~LED_RED & ~LED_GREEN);
179
            break;
180
        }
181
        
182
        stop_timer();
183
        _delay_ms(1000);
184
            
185
      }
186
    } 
187
  return 0;
188
    
189
}
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
}

Also available in: Unified diff