root / toolbox / rfid.c @ 1085ef77
History | View | Annotate | Download (1.07 KB)
1 | 12ea39cc | Tom Mullins | #include <string.h> |
---|---|---|---|
2 | 6ca98a3f | Tom Mullins | #include <stdint.h> |
3 | 9b2b6d91 | Tom Mullins | #include <util/delay.h> |
4 | #include "rfid.h" |
||
5 | #include "serial.h" |
||
6 | |||
7 | 1085ef77 | Tom Mullins | #define RFID_OK 1 |
8 | 6ca98a3f | Tom Mullins | static uint8_t read_cmd[] = {'!', 'R', 'W', 1, 32}; |
9 | 9b2b6d91 | Tom Mullins | |
10 | 532ba0bd | Tom Mullins | static int serno_idx; |
11 | 1085ef77 | Tom Mullins | static char n_failures; |
12 | 6ca98a3f | Tom Mullins | static uint8_t serno[RFID_SERNO_SIZE];
|
13 | 9b2b6d91 | Tom Mullins | |
14 | static void zero_serno() { |
||
15 | int i;
|
||
16 | for (i = 0; i < RFID_SERNO_SIZE; i++) { |
||
17 | serno[i] = 0;
|
||
18 | } |
||
19 | } |
||
20 | |||
21 | void rfid_init() {
|
||
22 | serial_init(); |
||
23 | } |
||
24 | |||
25 | 1085ef77 | Tom Mullins | static void restart_read() { |
26 | 532ba0bd | Tom Mullins | serno_idx = -1;
|
27 | serial_write(read_cmd, sizeof(read_cmd));
|
||
28 | } |
||
29 | |||
30 | 1085ef77 | Tom Mullins | void rfid_start_read() {
|
31 | n_failures = 0;
|
||
32 | restart_read(); |
||
33 | } |
||
34 | |||
35 | 532ba0bd | Tom Mullins | char rfid_poll() {
|
36 | int c;
|
||
37 | |||
38 | 6ca98a3f | Tom Mullins | while ((c = serial_read()) >= 0) { |
39 | 532ba0bd | Tom Mullins | |
40 | if (serno_idx < 0) { |
||
41 | if (c != RFID_OK) {
|
||
42 | 1085ef77 | Tom Mullins | n_failures++; |
43 | if (n_failures >= RFID_N_FAILURES) {
|
||
44 | zero_serno(); |
||
45 | return 1; |
||
46 | } else {
|
||
47 | restart_read(); |
||
48 | return 0; |
||
49 | } |
||
50 | 532ba0bd | Tom Mullins | } |
51 | } else {
|
||
52 | serno[serno_idx] = c; |
||
53 | } |
||
54 | |||
55 | serno_idx++; |
||
56 | if (serno_idx >= RFID_SERNO_SIZE) {
|
||
57 | return 1; |
||
58 | 9b2b6d91 | Tom Mullins | } |
59 | 532ba0bd | Tom Mullins | |
60 | 9b2b6d91 | Tom Mullins | } |
61 | 532ba0bd | Tom Mullins | |
62 | return 0; |
||
63 | 9b2b6d91 | Tom Mullins | } |
64 | |||
65 | 6ca98a3f | Tom Mullins | void rfid_get_serno(uint8_t *buf) {
|
66 | 12ea39cc | Tom Mullins | memcpy(buf, serno, sizeof(serno));
|
67 | } |