Project

General

Profile

Statistics
| Branch: | Revision:

root / toolbox / main.c @ 4b7d087b

History | View | Annotate | Download (5.88 KB)

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

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

    
21
static enum toolstate_t toolstate = TS_INIT;
22
static uint8_t coils;
23
static uint8_t latest_reading[RFID_SERNO_SIZE];
24
static uint8_t current_user[RFID_SERNO_SIZE];
25

    
26
static inline void set_coil(char coil, char bit) {
27
  coils = (coils & ~(1 << coil)) | (bit << coil);
28
}
29
static inline char get_coil(char coil) {
30
  return (coils >> coil) & 1;
31
}
32

    
33
static inline void tool_init() {DDRA |= _BV(DDA1);}
34
static inline void tool_enable() {PORTA |= _BV(PA1);}
35
static inline void tool_disable() {PORTA &= ~ _BV(PA1);}
36

    
37
static inline void serno_zero(uint8_t *serno) {
38
  memset(serno, 0, RFID_SERNO_SIZE);
39
}
40

    
41
static char serno_is_nonzero(uint8_t *serno) {
42
  int i;
43
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
44
    if (serno[i]) {
45
      return 1;
46
    }
47
  }
48
  return 0;
49
}
50

    
51
static char serno_equal(uint8_t *a, uint8_t *b) {
52
  return memcmp(a, b, RFID_SERNO_SIZE) == 0;
53
}
54

    
55
static void serno_cpy(uint8_t *dest, uint8_t *src) {
56
  memcpy(dest, src, RFID_SERNO_SIZE);
57
}
58

    
59
static void tool_main() {
60

    
61
  switch (toolstate) {
62

    
63
    case TS_INIT:
64
      if (get_coil(MB_COIL_INIT)) {
65
        set_coil(MB_COIL_NEW, 0);
66
        set_coil(MB_COIL_EN, 0);
67
        set_coil(MB_COIL_REQ_DIS, 0);
68
        toolstate = TS_OFF;
69
      }
70
      break;
71

    
72
    case TS_OFF:
73
      led_off();
74
      set_coil(MB_COIL_EN, 0);
75
      if (serno_is_nonzero(latest_reading)) {
76
        serno_cpy(current_user, latest_reading);
77
        set_coil(MB_COIL_NEW, 1);
78
        toolstate = TS_WAIT_ACCESS;
79
      }
80
      break;
81

    
82
    case TS_WAIT_ACCESS:
83
      led_yellow();
84
      if (get_coil(MB_COIL_EN)) {
85
        tool_enable();
86
        toolstate = TS_ON;
87
      } else if (!get_coil(MB_COIL_NEW)) {
88
        toolstate = TS_DENY;
89
      } else if (!serno_equal(current_user, latest_reading)) {
90
        set_coil(MB_COIL_NEW, 0);
91
        toolstate = TS_OFF;
92
      }
93
      break;
94

    
95
    case TS_DENY:
96
      led_red();
97
      if (!serno_equal(current_user, latest_reading)) {
98
        toolstate = TS_OFF;
99
        serno_zero(current_user);
100
      }
101

    
102
    case TS_REQ_DIS:
103
      if (!get_coil(MB_COIL_EN)) {
104
        tool_disable();
105
        toolstate = TS_OFF;
106
      } else if (!get_coil(MB_COIL_REQ_DIS)) {
107
        toolstate = TS_ON;
108
      } else {
109
        // TODO blink yellow for 10 seconds or something
110
        set_coil(MB_COIL_EN, 0);
111
        set_coil(MB_COIL_REQ_DIS, 0);
112
        tool_disable();
113
        serno_zero(current_user);
114
        toolstate = TS_OFF;
115
      }
116
      break;
117

    
118
    case TS_MISSING_ID:
119
      if (!get_coil(MB_COIL_EN)) {
120
        tool_disable();
121
        toolstate = TS_OFF;
122
      } else if (get_coil(MB_COIL_REQ_DIS)) {
123
        toolstate = TS_REQ_DIS;
124
      } else if (serno_equal(current_user, latest_reading)) {
125
        toolstate = TS_ON;
126
      } else {
127
        if (led_blink_done()) {
128
          set_coil(MB_COIL_EN, 0);
129
          tool_disable();
130
          serno_zero(current_user);
131
          toolstate = TS_OFF;
132
        }
133
      }
134
      break;
135

    
136
    case TS_ON:
137
      led_green();
138
      if (!get_coil(MB_COIL_EN)) {
139
        tool_disable();
140
        serno_zero(current_user);
141
        toolstate = TS_OFF;
142
      } else if(get_coil(MB_COIL_REQ_DIS)) {
143
        toolstate = TS_REQ_DIS;
144
      } else if (!serno_equal(current_user, latest_reading)) {
145
        toolstate = TS_MISSING_ID;
146
        led_blink_start(666, 15, YELLOW);
147
      }
148
      break;
149

    
150
  }
151

    
152
}
153

    
154
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
155
    eMBRegisterMode mode) {
156

    
157
  addr--;
158

    
159
  if (addr+n_coils > N_COILS) {
160
    return MB_ENOREG;
161
  }
162

    
163
  if (mode == MB_REG_WRITE) {
164

    
165
    switch (addr) {
166

    
167
      case MB_COIL_NEW:
168
        /* nop */
169
        reg_buf[0] >>= 1;
170
        n_coils--;
171
        if (n_coils == 0) {
172
          return MB_ENOERR;
173
        }
174

    
175
      case MB_COIL_EN:
176
        set_coil(MB_COIL_NEW, 0);
177
        set_coil(MB_COIL_EN, reg_buf[0] & 1);
178
        reg_buf[0] >>= 1;
179
        n_coils--;
180
        if (n_coils == 0) {
181
          return MB_ENOERR;
182
        }
183

    
184
      case MB_COIL_REQ_DIS:
185
        set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1);
186
        reg_buf[0] >>= 1;
187
        n_coils--;
188
        if (n_coils == 0) {
189
          return MB_ENOERR;
190
        }
191

    
192
      case MB_COIL_INIT:
193
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
194
        reg_buf[0] >>= 1;
195
        n_coils--;
196
        if (n_coils == 0) {
197
          return MB_ENOERR;
198
        }
199
    }
200

    
201
  } else if (mode == MB_REG_READ) {
202

    
203
    reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1);
204
    return MB_ENOERR;
205

    
206
  }
207

    
208
  return MB_EIO;
209
}
210

    
211
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) {
212
  return MB_ENOREG;
213
}
214

    
215
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
216

    
217
  addr--;
218

    
219
  switch (addr) {
220

    
221
    case MB_INP_SERNOL:
222
      *reg_buf++ = current_user[0];
223
      *reg_buf++ = current_user[1];
224
      n_regs--;
225
      if (n_regs == 0) {
226
        return MB_ENOERR;
227
      }
228

    
229
    case MB_INP_SERNOH:
230
      *reg_buf++ = current_user[2];
231
      *reg_buf++ = current_user[3];
232
      n_regs--;
233
      if (n_regs == 0) {
234
        return MB_ENOERR;
235
      }
236

    
237
    case MB_INP_CURRENT:
238
      *reg_buf++ = 0;
239
      *reg_buf++ = 0;
240
      n_regs--;
241
      if (n_regs == 0) {
242
        return MB_ENOERR;
243
      }
244

    
245
    default:
246
      return MB_ENOREG;
247
  }
248
}
249

    
250
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
251
    eMBRegisterMode mode) {
252
  if (mode == MB_REG_WRITE) {
253
    return MB_ENOREG;
254
  } else if (mode == MB_REG_READ) {
255
    return MB_ENOREG;
256
  } else {
257
    return MB_EIO;
258
  }
259
}
260

    
261
int main() {
262

    
263
  led_init();
264
  tool_init();
265
  rfid_init();
266

    
267
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
268
  eMBEnable();
269

    
270
  sei();
271

    
272
  rfid_start_read();
273
  while (1) {
274
    if (rfid_poll()) {
275
      rfid_get_serno(latest_reading);
276
      rfid_start_read();
277
    }
278
    tool_main();
279
    eMBPoll();
280
    _delay_ms(50);
281
  }
282

    
283
  return 0;
284
}