Statistics
| Branch: | Revision:

root / rfid.c @ 9b2b6d91

History | View | Annotate | Download (1.35 KB)

1
#include <util/delay.h>
2
#include "rfid.h"
3
#include "serial.h"
4

    
5
static char read_cmd[] = {'!', 'R', 'W', 1, 32};
6

    
7
static char serno[RFID_SERNO_SIZE];
8

    
9
static void zero_serno() {
10
  int i;
11
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
12
    serno[i] = 0;
13
  }
14
}
15

    
16
static void read_serno() {
17
  int i;
18
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
19
    serno[i] = serial_read_blocking();
20
  }
21
}
22

    
23
static char check_serno() {
24
  int i;
25
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
26
    if (serno[i] != serial_read_blocking()) {
27
      return 0;
28
    }
29
  }
30
  return 1;
31
}
32

    
33
void rfid_init() {
34
  serial_init();
35
}
36

    
37
void rfid_read_safe() {
38
  char n_err = 0, n_ok = 0;
39
  while (1) {
40
    serial_write(read_cmd, sizeof(read_cmd));
41
    if (serial_read_blocking() == RFID_OK) {
42
      if (n_ok == 0) {
43
        read_serno();
44
      } else if (!check_serno()) {
45
        zero_serno();
46
        return;
47
      }
48
      n_ok++;
49
      if (n_ok >= RFID_MIN_OK) {
50
        return;
51
      }
52
    } else {
53
      n_err++;
54
      if (n_err >= RFID_MAX_ERRS) {
55
        zero_serno();
56
        return;
57
      }
58
    }
59
    _delay_ms(40);
60
  }
61
}
62

    
63
void rfid_read() {
64
  while (1) {
65
    serial_write(read_cmd, sizeof(read_cmd));
66
    if (serial_read_blocking() == RFID_OK) {
67
      read_serno();
68
      return;
69
    }
70
    _delay_ms(40);
71
  }
72
}
73

    
74
void rfid_get_serno(char *buf) {
75
  int i;
76
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
77
    buf[i] = serno[i];
78
  }
79
}