Statistics
| Branch: | Revision:

root / toolbox / rfid.c @ bbf2f5ad

History | View | Annotate | Download (875 Bytes)

1
#include <string.h>
2
#include <stdint.h>
3
#include <util/delay.h>
4
#include "rfid.h"
5
#include "serial.h"
6

    
7
#define RFID_OK 1
8
static uint8_t read_cmd[] = {'!', 'R', 'W', 1, 32};
9

    
10
static int serno_idx;
11
static uint8_t serno[RFID_SERNO_SIZE];
12

    
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
void rfid_start_read() {
25
  serno_idx = -1;
26
  serial_flush();
27
  serial_write(read_cmd, sizeof(read_cmd));
28
}
29

    
30
char rfid_poll() {
31
  int c;
32

    
33
  while ((c = serial_read()) >= 0) {
34

    
35
    if (serno_idx < 0) {
36
      if (c != RFID_OK) {
37
        zero_serno();
38
        return 1;
39
      }
40
    } else {
41
      serno[serno_idx] = c;
42
    }
43

    
44
    serno_idx++;
45
    if (serno_idx >= RFID_SERNO_SIZE) {
46
      return 1;
47
    }
48

    
49
  }
50

    
51
  return 0;
52
}
53

    
54
void rfid_get_serno(uint8_t *buf) {
55
  memcpy(buf, serno, sizeof(serno));
56
}