root / toolbox / rfid.c @ e8960789
History | View | Annotate | Download (875 Bytes)
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 | 6ca98a3f | Tom Mullins | static uint8_t serno[RFID_SERNO_SIZE];
|
12 | 9b2b6d91 | Tom Mullins | |
13 | static void zero_serno() { |
||
14 | int i;
|
||
15 | for (i = 0; i < RFID_SERNO_SIZE; i++) { |
||
16 | serno[i] = 0;
|
||
17 | } |
||
18 | } |
||
19 | |||
20 | void rfid_init() {
|
||
21 | serial_init(); |
||
22 | } |
||
23 | |||
24 | bbf2f5ad | Tom Mullins | void rfid_start_read() {
|
25 | 532ba0bd | Tom Mullins | serno_idx = -1;
|
26 | bbf2f5ad | Tom Mullins | serial_flush(); |
27 | 532ba0bd | Tom Mullins | serial_write(read_cmd, sizeof(read_cmd));
|
28 | } |
||
29 | |||
30 | char rfid_poll() {
|
||
31 | int c;
|
||
32 | |||
33 | 6ca98a3f | Tom Mullins | while ((c = serial_read()) >= 0) { |
34 | 532ba0bd | Tom Mullins | |
35 | if (serno_idx < 0) { |
||
36 | if (c != RFID_OK) {
|
||
37 | bbf2f5ad | Tom Mullins | zero_serno(); |
38 | return 1; |
||
39 | 532ba0bd | Tom Mullins | } |
40 | } else {
|
||
41 | serno[serno_idx] = c; |
||
42 | } |
||
43 | |||
44 | serno_idx++; |
||
45 | if (serno_idx >= RFID_SERNO_SIZE) {
|
||
46 | return 1; |
||
47 | 9b2b6d91 | Tom Mullins | } |
48 | 532ba0bd | Tom Mullins | |
49 | 9b2b6d91 | Tom Mullins | } |
50 | 532ba0bd | Tom Mullins | |
51 | return 0; |
||
52 | 9b2b6d91 | Tom Mullins | } |
53 | |||
54 | 6ca98a3f | Tom Mullins | void rfid_get_serno(uint8_t *buf) {
|
55 | 12ea39cc | Tom Mullins | memcpy(buf, serno, sizeof(serno));
|
56 | } |