root / toolbox / rfid.c @ 1b054655
History | View | Annotate | Download (857 Bytes)
1 |
#include <string.h> |
---|---|
2 |
#include <stdint.h> |
3 |
#include <util/delay.h> |
4 |
#include "rfid.h" |
5 |
#include "serial.h" |
6 |
|
7 |
static uint8_t read_cmd[] = {'!', 'R', 'W', 1, 32}; |
8 |
|
9 |
static int serno_idx; |
10 |
static uint8_t serno[RFID_SERNO_SIZE];
|
11 |
|
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 |
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 |
while ((c = serial_read()) >= 0) { |
33 |
|
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 |
} |
47 |
|
48 |
} |
49 |
|
50 |
return 0; |
51 |
} |
52 |
|
53 |
void rfid_get_serno(uint8_t *buf) {
|
54 |
memcpy(buf, serno, sizeof(serno));
|
55 |
} |