Statistics
| Branch: | Revision:

root / toolbox / rfid.c @ 1085ef77

History | View | Annotate | Download (1.07 KB)

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 char n_failures;
12
static uint8_t serno[RFID_SERNO_SIZE];
13

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

    
30
void rfid_start_read() {
31
  n_failures = 0;
32
  restart_read();
33
}
34

    
35
char rfid_poll() {
36
  int c;
37

    
38
  while ((c = serial_read()) >= 0) {
39

    
40
    if (serno_idx < 0) {
41
      if (c != RFID_OK) {
42
        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
      }
51
    } else {
52
      serno[serno_idx] = c;
53
    }
54

    
55
    serno_idx++;
56
    if (serno_idx >= RFID_SERNO_SIZE) {
57
      return 1;
58
    }
59

    
60
  }
61

    
62
  return 0;
63
}
64

    
65
void rfid_get_serno(uint8_t *buf) {
66
  memcpy(buf, serno, sizeof(serno));
67
}