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