Revision 532ba0bd

View differences:

toolbox/led.h
1
#ifndef LED_H
2
#define LED_H
3

  
4
#include <avr/io.h>
5

  
6
static inline void led_init() {DDRC |= _BV(DDC1) | _BV(DDC0);}
7
static inline void led_off() {PORTC &= ~(_BV(PC1) | _BV(PC0));}
8
static inline void led_red() {PORTC = (PORTC & ~_BV(PC0)) | _BV(PC1);}
9
static inline void led_yellow() {PORTC |= _BV(PC1) | _BV(PC0);}
10
static inline void led_green() {PORTC = (PORTC & ~_BV(PC1)) | _BV(PC0);}
11

  
12
#endif
toolbox/main.c
6 6
#include "mbport.h"
7 7
#include "tooltron_mb.h"
8 8
#include "rfid.h"
9
#include "led.h"
9 10

  
10 11
enum toolstate_t {
11 12
  TS_INIT,
......
108 109
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
109 110
    eMBRegisterMode mode) {
110 111

  
111
  if (addr > N_COILS || n_coils >= N_COILS-addr) {
112
  if (addr+n_coils > N_COILS) {
112 113
    return MB_ENOREG;
113 114
  }
114 115

  
......
211 212

  
212 213
int main() {
213 214

  
215
  led_init();
214 216
  tool_init();
215 217
  rfid_init();
216 218

  
......
219 221

  
220 222
  sei();
221 223

  
224
  rfid_start_read();
222 225
  while (1) {
223
    rfid_read();
224
    tool_main();
226
    if (rfid_poll()) {
227
      rfid_start_read();
228
    }
229
    rfid_get_serno(current_user);
230
    //tool_main();
225 231
    eMBPoll();
226
    _delay_ms(100);
232
    _delay_ms(50);
227 233
  }
228 234

  
229 235
  return 0;
toolbox/rfid.c
5 5

  
6 6
static char read_cmd[] = {'!', 'R', 'W', 1, 32};
7 7

  
8
static int serno_idx;
8 9
static char serno[RFID_SERNO_SIZE];
9 10

  
10 11
static void zero_serno() {
......
57 58
        return;
58 59
      }
59 60
    }
60
    _delay_ms(40);
61
    _delay_ms(10);
61 62
  }
62 63
}
63 64

  
64
void rfid_read() {
65
  while (1) {
66
    serial_write(read_cmd, sizeof(read_cmd));
67
    if (serial_read_blocking() == RFID_OK) {
68
      read_serno();
69
      return;
65
void rfid_start_read() {
66
  serno_idx = -1;
67
  zero_serno(); // TODO temporary
68
  serial_flush();
69
  serial_write(read_cmd, sizeof(read_cmd));
70
}
71

  
72
char rfid_poll() {
73
  int c;
74

  
75
  c = serial_read();
76
  while (c >= 0) {
77

  
78
    if (serno_idx < 0) {
79
      if (c != RFID_OK) {
80
        zero_serno();
81
        return 1;
82
      }
83
    } else {
84
      serno[serno_idx] = c;
85
    }
86

  
87
    serno_idx++;
88
    if (serno_idx >= RFID_SERNO_SIZE) {
89
      return 1;
70 90
    }
71
    _delay_ms(40);
91

  
92
    c = serial_read();
72 93
  }
94

  
95
  return 0;
73 96
}
74 97

  
75 98
void rfid_get_serno(char *buf) {
toolbox/rfid.h
15 15
/* Should be called before anything else */
16 16
void rfid_init();
17 17

  
18
/* Reads the serial number of an rfid tag into an internal buffer, accessible
19
 * with rfid_get_serno */
20
void rfid_read();
18
/* Sends the read command to the rfid reader. After, you should call rfid_poll
19
 * until it returns nonzero */
20
void rfid_start_read();
21

  
22
/* Should only be called after rfid_start_read. Keep calling until it returns
23
 * nonzero, which means it is finished reading the serial number, and
24
 * rfid_get_serno can be called */
25
char rfid_poll();
21 26

  
22 27
/* Attempts to read the serial number multiple times, and only accepts it if it
23
 * is the same every time. Use rfid_read instead until this one is proven
24
 * necessary */
28
 * is the same every time. Don't use it */
25 29
void rfid_read_safe();
26 30

  
27
/* Call this only after calling rfid_read. This will copy the value it read
28
 * into serno, which should be at least RFID_SERNO_SIZE bytes */
31
/* Call this only after rfid_poll returns nonzero. This will copy the value it
32
 * read into serno, which should be at least RFID_SERNO_SIZE bytes */
29 33
void rfid_get_serno(char *serno);
30 34

  
31
/* Call this only after calling rfid_read. Returns 1 if serno matches the
32
 * internal buffer of the most recently read serial number */
35
/* Call this only after rfid_poll returns nonzero. Returns 1 if serno matches
36
 * the internal buffer of the most recently read serial number */
33 37
char rfid_check_serno(char *serno);
34 38

  
35
/* Call this only after calling rfid_read. Returns 1 if the internal buffer is
36
 * nonzero, meaning a serial number was successfully read from an rfid tag */
39
/* Call this only after rfid_poll returns nonzero. Returns 1 if the internal
40
 * buffer is nonzero, meaning a serial number was successfully read from an
41
 * rfid tag */
37 42
char rfid_nonzero();
38 43

  
39 44
#endif

Also available in: Unified diff