root / toolbox / rfid.c @ 4e6334de
History | View | Annotate | Download (1.54 KB)
1 | 12ea39cc | Tom Mullins | #include <string.h> |
---|---|---|---|
2 | 9b2b6d91 | Tom Mullins | #include <util/delay.h> |
3 | #include "rfid.h" |
||
4 | #include "serial.h" |
||
5 | |||
6 | static char read_cmd[] = {'!', 'R', 'W', 1, 32}; |
||
7 | |||
8 | static char serno[RFID_SERNO_SIZE]; |
||
9 | |||
10 | static void zero_serno() { |
||
11 | int i;
|
||
12 | for (i = 0; i < RFID_SERNO_SIZE; i++) { |
||
13 | serno[i] = 0;
|
||
14 | } |
||
15 | } |
||
16 | |||
17 | static void read_serno() { |
||
18 | int i;
|
||
19 | for (i = 0; i < RFID_SERNO_SIZE; i++) { |
||
20 | serno[i] = serial_read_blocking(); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | static char check_serno() { |
||
25 | int i;
|
||
26 | for (i = 0; i < RFID_SERNO_SIZE; i++) { |
||
27 | if (serno[i] != serial_read_blocking()) {
|
||
28 | return 0; |
||
29 | } |
||
30 | } |
||
31 | return 1; |
||
32 | } |
||
33 | |||
34 | void rfid_init() {
|
||
35 | serial_init(); |
||
36 | } |
||
37 | |||
38 | void rfid_read_safe() {
|
||
39 | char n_err = 0, n_ok = 0; |
||
40 | while (1) { |
||
41 | serial_write(read_cmd, sizeof(read_cmd));
|
||
42 | if (serial_read_blocking() == RFID_OK) {
|
||
43 | if (n_ok == 0) { |
||
44 | read_serno(); |
||
45 | } else if (!check_serno()) { |
||
46 | zero_serno(); |
||
47 | return;
|
||
48 | } |
||
49 | n_ok++; |
||
50 | if (n_ok >= RFID_MIN_OK) {
|
||
51 | return;
|
||
52 | } |
||
53 | } else {
|
||
54 | n_err++; |
||
55 | if (n_err >= RFID_MAX_ERRS) {
|
||
56 | zero_serno(); |
||
57 | return;
|
||
58 | } |
||
59 | } |
||
60 | _delay_ms(40);
|
||
61 | } |
||
62 | } |
||
63 | |||
64 | void rfid_read() {
|
||
65 | while (1) { |
||
66 | serial_write(read_cmd, sizeof(read_cmd));
|
||
67 | if (serial_read_blocking() == RFID_OK) {
|
||
68 | read_serno(); |
||
69 | return;
|
||
70 | } |
||
71 | _delay_ms(40);
|
||
72 | } |
||
73 | } |
||
74 | |||
75 | void rfid_get_serno(char *buf) { |
||
76 | 12ea39cc | Tom Mullins | memcpy(buf, serno, sizeof(serno));
|
77 | } |
||
78 | |||
79 | char rfid_check_serno(char *buf) { |
||
80 | return memcmp(buf, serno, sizeof(serno)) == 0; |
||
81 | } |
||
82 | |||
83 | char rfid_nonzero() {
|
||
84 | 9b2b6d91 | Tom Mullins | int i;
|
85 | 12ea39cc | Tom Mullins | for (i = 0; i < sizeof(serno); i++) { |
86 | if (serno[i]) {
|
||
87 | return 1; |
||
88 | } |
||
89 | 9b2b6d91 | Tom Mullins | } |
90 | 12ea39cc | Tom Mullins | return 0; |
91 | 9b2b6d91 | Tom Mullins | } |