Revision 12ea39cc

View differences:

toolbox/main.c
1 1
#include <stdint.h>
2
#include <string.h>
3
#include <avr/io.h>
2 4
#include <util/delay.h>
3 5
#include "mb.h"
4 6
#include "mbport.h"
5 7
#include "tooltron_mb.h"
6 8
#include "rfid.h"
7 9

  
10
enum toolstate_t {
11
  TS_INIT,
12
  TS_OFF,
13
  TS_WAIT_ACCESS,
14
  TS_REQ_DIS,
15
  TS_MISSING_ID,
16
  TS_ON
17
};
18

  
19
static enum toolstate_t toolstate = TS_INIT;
8 20
static char coils;
21
static char current_user[RFID_SERNO_SIZE];
9 22

  
10 23
static inline void set_coil(char coil, char bit) {
11 24
  coils |= (bit << coil);
12 25
}
26
static inline char get_coil(char coil) {
27
  return (coils >> coil) & 1;
28
}
29

  
30
static inline void tool_init() {DDRA |= _BV(DDA1);}
31
static inline void tool_enable() {PORTA |= _BV(PA1);}
32
static inline void tool_disable() {PORTA &= _BV(PA1);}
33

  
34
static void tool_main() {
35

  
36
  switch (toolstate) {
37

  
38
    case TS_INIT:
39
      if (get_coil(MB_COIL_INIT)) {
40
        set_coil(MB_COIL_NEW, 0);
41
        set_coil(MB_COIL_EN, 0);
42
        set_coil(MB_COIL_REQ_DIS, 0);
43
        toolstate = TS_OFF;
44
      }
45
      break;
46

  
47
    case TS_OFF:
48
      if (rfid_nonzero()) {
49
        rfid_get_serno(current_user);
50
        set_coil(MB_COIL_NEW, 1);
51
        toolstate = TS_WAIT_ACCESS;
52
      }
53
      break;
54

  
55
    case TS_WAIT_ACCESS:
56
      if (get_coil(MB_COIL_EN)) {
57
        tool_enable();
58
        toolstate = TS_ON;
59
      }
60
      break;
61

  
62
    case TS_REQ_DIS:
63
      // TODO blink yellow for 10 seconds or something
64
      set_coil(MB_COIL_EN, 0);
65
      tool_disable();
66
      toolstate = TS_OFF;
67
      break;
68

  
69
    case TS_MISSING_ID:
70
      if (rfid_check_serno(current_user)) {
71
        toolstate = TS_ON;
72
      } else {
73
        // TODO blink yellow for 10 seconds or something
74
        set_coil(MB_COIL_EN, 0);
75
        tool_disable();
76
        toolstate = TS_OFF;
77
      }
78
      break;
79

  
80
    case TS_ON:
81
      if (!get_coil(MB_COIL_EN)) {
82
        tool_disable();
83
        toolstate = TS_OFF;
84
      }
85
      if (get_coil(MB_COIL_REQ_DIS)) {
86
        toolstate = TS_REQ_DIS;
87
      }
88
      if (!rfid_check_serno(current_user)) {
89
        toolstate = TS_MISSING_ID;
90
      }
91
      break;
92

  
93
  }
94

  
95
}
13 96

  
14 97
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
15 98
    eMBRegisterMode mode) {
......
40 123
        }
41 124

  
42 125
      case MB_COIL_REQ_DIS:
43
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
126
        set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1);
44 127
        reg_buf[0] >>= 1;
45 128
        n_coils--;
46 129
        if (n_coils == 0) {
......
73 156
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
74 157
  char serno[RFID_SERNO_SIZE];
75 158

  
76
  rfid_get_serno(serno);
159
  memcpy(reg_buf, current_user, sizeof(current_user));
77 160

  
78 161
  switch (addr) {
79 162

  
......
119 202
}
120 203

  
121 204
int main() {
205

  
206
  tool_init();
122 207
  rfid_init();
123 208

  
124 209
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
125 210
  eMBEnable();
126 211

  
127
  /* Set pin controlling relay to output */
128
  DDRA |= _BV(DDA1);
129

  
130 212
  sei();
131 213

  
132 214
  while (1) {
133 215
    rfid_read();
134
    /* TODO act on rfid input and coil state */
216
    tool_main();
135 217
    eMBPoll();
136 218
    _delay_ms(200);
137 219
  }
toolbox/rfid.c
1
#include <string.h>
1 2
#include <util/delay.h>
2 3
#include "rfid.h"
3 4
#include "serial.h"
......
72 73
}
73 74

  
74 75
void rfid_get_serno(char *buf) {
76
  memcpy(buf, serno, sizeof(serno));
77
}
78

  
79
char rfid_check_serno(char *buf) {
80
  return memcmp(buf, serno, sizeof(serno)) == 0;
81
}
82

  
83
char rfid_nonzero() {
75 84
  int i;
76
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
77
    buf[i] = serno[i];
85
  for (i = 0; i < sizeof(serno); i++) {
86
    if (serno[i]) {
87
      return 1;
88
    }
78 89
  }
90
  return 0;
79 91
}
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, accessbile
18
/* Reads the serial number of an rfid tag into an internal buffer, accessible
19 19
 * with rfid_get_serno */
20 20
void rfid_read();
21 21

  
......
28 28
 * into serno, which should be at least RFID_SERNO_SIZE bytes */
29 29
void rfid_get_serno(char *serno);
30 30

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

  
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 */
37
char rfid_nonzero();
38

  
31 39
#endif

Also available in: Unified diff