Project

General

Profile

Revision 285

Added by Kevin Woo almost 11 years ago

Fixes to cardbox not receiving

View differences:

trunk/cardbox/rs485_int.c
84 84

  
85 85
ISR(USART_TX_vect) {
86 86
    // Re-enable reads
87
    //rs485_toggle_transmit(RS485_TX_OFF);
87
    rs485_toggle_transmit(RS485_TX_OFF);
88 88
}
trunk/cardbox/cardbox.c
148 148
    req,
149 149
    press,
150 150
    send,
151
    ack,
151 152
    rsp
152 153
} state_t;
153 154

  
154 155

  
155 156
int main(void) {
156 157
    uint8_t mbuf[PROGD_PACKET_SIZE];
157
    uint8_t cbuf[20];
158
    uint8_t cbuf[21];
158 159
    uint8_t clen;
159
    int8_t cret;
160 160
    uint8_t resp;
161
    uint8_t  c;
161
    uint8_t c;
162 162
    uint8_t len;
163 163
    uint8_t retries = 0;
164 164
    state_t state = req;
......
169 169
    card_reader_setup();
170 170
    sei();
171 171

  
172
    toggle_led(LED_GREEN|LED_YELLOW|LED_RED, OFF);
173
    read_card = 0;
174

  
175 172
    while(1) {
176
        while(1) {
177
            read_card = 1;
178
            if (cr_flag != CR_NONE) {
179
                read_card = 0;
180
                if ((cret = parse_card(cbuf, &clen)) < 0) {
181
                    for(c=0;c < 4;c++) {
182
                        toggle_led(LED_RED,ON);
183
                        _delay_ms(250);
184
                        toggle_led(LED_RED,OFF);
185
                        _delay_ms(250);
186
                    }
173
        switch(state) {
174
            case req:
175
                toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
187 176

  
188
                } else {
189
                    for (c = 0; c < clen; c++) {
190
                        rs485_send_byte(cbuf[c]);
191
                    }
192
                }
193
                cr_flag = CR_NONE;
194
            }
195
        }
177
                // Start reading cards
178
                read_card = 1;
196 179

  
180
                // See if we have a card, otherwise check if we got
181
                // a message
182
                if (cr_flag != CR_NONE) {
183
                    read_card = 0;
197 184

  
185
                    // Reading failed
186
                    if (parse_card(&(cbuf[1]), &clen) < 0) {
187
                        for(c=0;c < 4;c++) {
188
                            toggle_led(LED_RED,ON);
189
                            _delay_ms(250);
190
                            toggle_led(LED_RED,OFF);
191
                            _delay_ms(250);
192
                        }
193
                    // Reading is good, wait for a key
194
                    } else {
195
                        start_timer();
196
                        reset_timer();
197
                        reset_timeout_flag();
198
                        state = press;
199
                    }
198 200

  
199
        switch(state) {
200
            case req:
201
                toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
202
                      
203
                // Wait for a packet
204
                resp = parse_packet(mbuf, &len, ADDR);
201
                    // Reset the card reader flag
202
                    cr_flag = CR_NONE;
203
                } else {
204
                    // Wait for a packet
205
                    resp = parse_packet(mbuf, &len, ADDR);
205 206

  
206
                if (resp == TT_GET_KEY) {
207
                    _delay_ms(50);
208
                    send_packet(TT_ACK, ADDR, NULL, 0);
209
                    toggle_led(LED_YELLOW, ON);
210
                    reset_timer();
211
                    reset_timeout_flag();
212
                    start_timer();
213
                    c = ' ';
214
                    state = press;
215
                } else if(resp == TT_PING){
216
                    _delay_ms(50);
217
                    send_packet(TT_ACK, ADDR, NULL, 0);
207
                    // Ping Request
208
                    if(resp == TT_PING){
209
                        _delay_ms(50);
210
                        send_packet(TT_ACK, ADDR, NULL, 0);
211
                    }
218 212
                }
219 213
                break;
220 214
            case press:
221
                c = get_button();
215
                toggle_led(LED_YELLOW,ON);
216
                cbuf[0] = get_button();
222 217
                retries = 0;
223 218
        
224 219
                if (seconds > TIMEOUT_SECONDS) {
225 220
                    set_timeout_flag();
226 221
                    state = send;
227
                } else if (c != ' ') {
222
                } else if (cbuf[0] != ' ') {
228 223
                    state = send;
229 224
                } else {
230 225
                    _delay_ms(100);
......
233 228
                break;
234 229
            case send:
235 230
                if (get_timeout_flag() == 1) {
236
                    _delay_ms(50);
237
                    send_packet(TT_TIMEOUT, ADDR, NULL, 0);
231
                    toggle_led(LED_RED|LED_YELLOW,ON);
232
                    _delay_ms(500);
233
                    toggle_led(LED_RED|LED_YELLOW,OFF);
234
                    _delay_ms(500);
238 235
                    state = req;
239
                    break;
240 236
                } else {
241 237
                    _delay_ms(50);
242
                    send_packet(TT_SEND_KEY, ADDR, &c, 1);
238
                    send_packet(TT_KC, ADDR, cbuf, 1 + clen);
239
                    state = ack;
243 240
                }
244 241

  
245
                state = rsp;
246 242
                break;
247
            case rsp:
248
                resp = parse_packet(mbuf, &len,  ADDR);
243
            case ack:
244
                resp = parse_packet(mbuf, &len, ADDR);
249 245

  
250 246
                if (resp == TT_ACK) {
251
                    toggle_led(LED_GREEN, ON);
252
                } else if ((resp == TT_NACK) || (retries == TT_MAX_RETRY)) { 
247
                    state = rsp;
248
                    start_timer();
249
                    reset_timer();
250
                    state = rsp;
251
                } else if (retries == TT_MAX_RETRY) {
253 252
                    toggle_led(LED_RED, ON);
253
                    _delay_ms(1000);
254
                    toggle_led(LED_RED, OFF);
255
                    state = req;
254 256
                } else {
255 257
                    retries++;
256 258
                    state = send;
257
                    break;
258 259
                }
260
                break;
259 261

  
260
                _delay_ms(1000);
261
                toggle_led(LED_RED, OFF);
262
                toggle_led(LED_GREEN, ON);
263
                state = req;
262
            case rsp:
263
                resp = parse_packet(mbuf, &len,  ADDR);
264

  
265
                if (resp == TT_GRANT) {
266
                    toggle_led(LED_GREEN, ON);
267
                    _delay_ms(1000);
268
                    toggle_led(LED_GREEN, OFF);
269
                    state = req;
270
                } else if (resp == TT_DENY) { 
271
                    toggle_led(LED_RED, ON);
272
                    _delay_ms(1000);
273
                    toggle_led(LED_RED, OFF);
274
                    state = req;
275
                } else if (seconds > TT_MAX_RETRY) {
276
                    toggle_led(LED_RED,ON);
277
                    _delay_ms(1000);
278
                    toggle_led(LED_RED, OFF);
279
                    state = req;
280
                }
281

  
264 282
                break;
265 283
            default:
266 284
                state = req;

Also available in: Unified diff