Project

General

Profile

Revision 211

Added by Kevin Woo about 14 years ago

broken cardbox, needs to fix the statemachine

View differences:

main.c
17 17
 *
18 18
 ********/
19 19
#include <util/delay.h>
20
#include "uart.h"
20
#include "rs485_int.h"
21 21
#include <avr/interrupt.h>
22 22
#include <avr/io.h>
23 23

  
24
#include "tooltron.h"
24
#include <tooltron.h>
25 25
#include "timer.h"
26 26

  
27 27
/***** Keypad definitions ******/
......
48 48
#define LED_YELLOW (_BV(PC4))
49 49
#define LED_GREEN (_BV(PC3))
50 50

  
51
#define ADDR 2
51 52

  
52 53
/***** Global variables *****/
53 54

  
......
62 63
  return;	
63 64
}
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
}
65 83

  
84

  
66 85
char get_button(void) {
67 86

  
68 87
	char ret = ' ';
......
134 153
  return ret;
135 154
}
136 155

  
137
int main(void)
138
{
156
typedef enum {
157
    req,
158
    press,
159
    send,
160
    rsp
161
} state_t;
139 162

  
140
  char c;
141
  serial_init(BAUD9600);
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); 
142 171
  init_pins();
143 172
  init_timer();
144 173
  sei();
145 174

  
146
  while(1)
147
  {
175
  while(1) {
148 176

  
149
    PORTC |= LED_RED | LED_GREEN | LED_YELLOW;
150
    
151
    //wait for key request from server
152
    while(serial_getchar()!=TT_GET_KEY);
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);
153 183

  
154
    PORTC &=~LED_YELLOW;
155
    reset_timer();
156
    reset_timeout_flag();
157
    start_timer();
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();
158 195

  
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
        }
196
                if (seconds > TIMEOUT_SECONDS) {
197
                    set_timeout_flag();
198
                    state = send;
199
                } else if (c != ' ') {
200
                    state = send;
201
                }
168 202

  
169
        c = get_button();
170
        _delay_ms(100);
171
    }
172
    serial_enable_rx();
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
                }
173 211

  
212
                state = rsp;
213
                break;
214
        case rsp:
215
                resp = parse_packet(mbuf, ADDR);
174 216

  
217
                if (resp == TT_ACK
218

  
219

  
220

  
221

  
222

  
175 223
    if (timeout_flag == 0) {
176 224
        //respond with key pressed
177
        serial_putchar(c);
225
        uart_send_byte(c);
178 226
        
179 227
        //wait for response
180 228
        c=0;
181 229
        reset_timeout_flag();
182 230
        reset_timer();
183
        while(!c && seconds < TIMEOUT_SECONDS)
184
          c = serial_getchar_nb();
231
        while((uart_get_byte(&c) < 0) && (seconds < TIMEOUT_SECONDS));
185 232

  
186 233
        PORTC |= LED_YELLOW;
187 234

  

Also available in: Unified diff