Statistics
| Branch: | Revision:

root / toolbox / main.c @ bbf2f5ad

History | View | Annotate | Download (6.24 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
#include "current.h"
11
#include "time.h"
12

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

    
23
static enum toolstate_t toolstate = TS_INIT;
24
static uint8_t coils;
25
static uint8_t latest_reading[RFID_SERNO_SIZE];
26
static uint8_t current_user[RFID_SERNO_SIZE];
27
static uint16_t current;
28

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

    
36
static inline void tool_init() {DDRA |= _BV(DDA1);}
37
static inline void tool_enable() {PORTA |= _BV(PA1);}
38
static inline void tool_disable() {PORTA &= ~ _BV(PA1);}
39

    
40
static inline void serno_zero(uint8_t *serno) {
41
  memset(serno, 0, RFID_SERNO_SIZE);
42
}
43

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

    
54
static char serno_equal(uint8_t *a, uint8_t *b) {
55
  return memcmp(a, b, RFID_SERNO_SIZE) == 0;
56
}
57

    
58
static void serno_cpy(uint8_t *dest, uint8_t *src) {
59
  memcpy(dest, src, RFID_SERNO_SIZE);
60
}
61

    
62
static void tool_tick() {
63

    
64
  switch (toolstate) {
65

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

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

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

    
98
    case TS_DENY:
99
      led_red();
100
      if (!serno_equal(current_user, latest_reading)) {
101
        toolstate = TS_OFF;
102
        serno_zero(current_user);
103
      }
104
      break;
105

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

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

    
138
    case TS_ON:
139
      led_green();
140
      if (!get_coil(MB_COIL_EN)) {
141
        tool_disable();
142
        serno_zero(current_user);
143
        toolstate = TS_OFF;
144
      } else if(get_coil(MB_COIL_REQ_DIS)) {
145
        toolstate = TS_REQ_DIS;
146
      } else if (!serno_equal(current_user, latest_reading)) {
147
        toolstate = TS_MISSING_ID;
148
        led_blink_start(500, 6, YELLOW); // TODO made 10 seconds
149
      }
150
      break;
151

    
152
  }
153

    
154
}
155

    
156
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
157
    eMBRegisterMode mode) {
158

    
159
  addr--;
160

    
161
  if (addr+n_coils > N_COILS) {
162
    return MB_ENOREG;
163
  }
164

    
165
  if (mode == MB_REG_WRITE) {
166

    
167
    switch (addr) {
168

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

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

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

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

    
203
  } else if (mode == MB_REG_READ) {
204

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

    
208
  }
209

    
210
  return MB_EIO;
211
}
212

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

    
217
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
218

    
219
  addr--;
220

    
221
  switch (addr) {
222

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

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

    
239
    case MB_INP_CURRENT:
240
      *reg_buf++ = (uint8_t)(current >> 8);
241
      *reg_buf++ = (uint8_t)current;
242
      n_regs--;
243
      if (n_regs == 0) {
244
        return MB_ENOERR;
245
      }
246

    
247
    default:
248
      return MB_ENOREG;
249
  }
250
}
251

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

    
263
int main() {
264
  char rfid_ticks = 0, rfid_restart = 0;
265

    
266
  time_init();
267
  led_init();
268
  tool_init();
269
  rfid_init();
270
  current_init();
271

    
272
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
273
  eMBEnable();
274

    
275
  sei();
276

    
277
  rfid_start_read();
278
  while (1) {
279
    if (rfid_poll()) {
280
      rfid_get_serno(latest_reading);
281
      rfid_restart = 1;
282
    }
283
    if (++rfid_ticks >= RFID_PERIOD/TICK_MS && rfid_restart)
284
    {
285
      rfid_ticks = 0;
286
      rfid_restart = 0;
287
      rfid_start_read();
288
    }
289
    current = current_read();
290
    tool_tick();
291
    led_tick();
292
    eMBPoll();
293
    time_wait();
294
  }
295

    
296
  return 0;
297
}