Revision bbf2f5ad

View differences:

toolbox/main.c
101 101
        toolstate = TS_OFF;
102 102
        serno_zero(current_user);
103 103
      }
104
      break;
104 105

  
105 106
    case TS_REQ_DIS:
106 107
      if (!get_coil(MB_COIL_EN)) {
......
126 127
        toolstate = TS_REQ_DIS;
127 128
      } else if (serno_equal(current_user, latest_reading)) {
128 129
        toolstate = TS_ON;
129
      } else {
130
        if (led_blink_done()) {
131
          set_coil(MB_COIL_EN, 0);
132
          tool_disable();
133
          serno_zero(current_user);
134
          toolstate = TS_OFF;
135
        }
130
      } else if (led_blink_done()) {
131
        set_coil(MB_COIL_EN, 0);
132
        tool_disable();
133
        serno_zero(current_user);
134
        toolstate = TS_OFF;
136 135
      }
137 136
      break;
138 137

  
......
146 145
        toolstate = TS_REQ_DIS;
147 146
      } else if (!serno_equal(current_user, latest_reading)) {
148 147
        toolstate = TS_MISSING_ID;
149
        led_blink_start(666, 15, YELLOW);
148
        led_blink_start(500, 6, YELLOW); // TODO made 10 seconds
150 149
      }
151 150
      break;
152 151

  
......
262 261
}
263 262

  
264 263
int main() {
265
  char rfid_ticks = 0;
264
  char rfid_ticks = 0, rfid_restart = 0;
266 265

  
267 266
  time_init();
268 267
  led_init();
......
277 276

  
278 277
  rfid_start_read();
279 278
  while (1) {
280
    if (++rfid_ticks >= RFID_PERIOD/TICK_MS)
279
    if (rfid_poll()) {
280
      rfid_get_serno(latest_reading);
281
      rfid_restart = 1;
282
    }
283
    if (++rfid_ticks >= RFID_PERIOD/TICK_MS && rfid_restart)
281 284
    {
282 285
      rfid_ticks = 0;
283
      if (rfid_poll()) {
284
        rfid_get_serno(latest_reading);
285
        rfid_start_read();
286
      } else {
287
        /* TODO count # times rfid_poll returns each value and see how often we
288
         * have to wait */
289
      }
286
      rfid_restart = 0;
287
      rfid_start_read();
290 288
    }
291 289
    current = current_read();
292 290
    tool_tick();
toolbox/rfid.c
8 8
static uint8_t read_cmd[] = {'!', 'R', 'W', 1, 32};
9 9

  
10 10
static int serno_idx;
11
static char n_failures;
12 11
static uint8_t serno[RFID_SERNO_SIZE];
13 12

  
14 13
static void zero_serno() {
......
22 21
  serial_init();
23 22
}
24 23

  
25
static void restart_read() {
24
void rfid_start_read() {
26 25
  serno_idx = -1;
26
  serial_flush();
27 27
  serial_write(read_cmd, sizeof(read_cmd));
28 28
}
29 29

  
30
void rfid_start_read() {
31
  n_failures = 0;
32
  restart_read();
33
}
34

  
35 30
char rfid_poll() {
36 31
  int c;
37 32

  
......
39 34

  
40 35
    if (serno_idx < 0) {
41 36
      if (c != RFID_OK) {
42
        n_failures++;
43
        if (n_failures >= RFID_N_FAILURES) {
44
          zero_serno();
45
          return 1;
46
        } else {
47
          restart_read();
48
          return 0;
49
        }
37
        zero_serno();
38
        return 1;
50 39
      }
51 40
    } else {
52 41
      serno[serno_idx] = c;
toolbox/rfid.h
4 4
/* Bytes in serial number on rfid tags */
5 5
#define RFID_SERNO_SIZE 4
6 6

  
7
/* Number of consective read failures before giving up and returning all 0s */
8
#define RFID_N_FAILURES 5
9

  
10 7
/* Should be called before anything else */
11 8
void rfid_init();
12 9

  
toolbox/time.c
7 7
#define OCR (F_CPU / PRESCALE / 1000UL)
8 8
#define ERROR (F_CPU / PRESCALE - OCR * 1000UL)
9 9

  
10
static char ready, ms;
10
static volatile char ready;
11
static char ms;
11 12
static uint16_t error;
12 13

  
13 14
ISR(TIMER0_COMPA_vect) {

Also available in: Unified diff