Statistics
| Branch: | Revision:

root / toolbox / main.c @ 36f3b65f

History | View | Annotate | Download (6.27 KB)

1 20e5429c Tom Mullins
#include <stdint.h>
2 12ea39cc Tom Mullins
#include <string.h>
3
#include <avr/io.h>
4 20e5429c Tom Mullins
#include <util/delay.h>
5
#include "mb.h"
6
#include "mbport.h"
7
#include "tooltron_mb.h"
8 9b2b6d91 Tom Mullins
#include "rfid.h"
9 532ba0bd Tom Mullins
#include "led.h"
10 10936c07 Tom Mullins
#include "current.h"
11 94548bf4 Tom Mullins
#include "time.h"
12 20e5429c Tom Mullins
13 a96c5547 Tom Mullins
#if TOOL_ADDRESS < 0
14
#error Please define TOOL_ADDRESS
15
#endif
16
17 12ea39cc Tom Mullins
enum toolstate_t {
18
  TS_INIT,
19
  TS_OFF,
20
  TS_WAIT_ACCESS,
21 f5d1f56a Tom Mullins
  TS_DENY,
22 12ea39cc Tom Mullins
  TS_REQ_DIS,
23
  TS_MISSING_ID,
24
  TS_ON
25
};
26
27
static enum toolstate_t toolstate = TS_INIT;
28 6ca98a3f Tom Mullins
static uint8_t coils;
29 1b054655 Tom Mullins
static uint8_t latest_reading[RFID_SERNO_SIZE];
30 6ca98a3f Tom Mullins
static uint8_t current_user[RFID_SERNO_SIZE];
31 10936c07 Tom Mullins
static uint16_t current;
32 f4df665c Tom Mullins
//static uint16_t current_max_warn, current_max_hard;
33 208a6fb2 Tom Mullins
34
static inline void set_coil(char coil, char bit) {
35 6ca98a3f Tom Mullins
  coils = (coils & ~(1 << coil)) | (bit << coil);
36 208a6fb2 Tom Mullins
}
37 12ea39cc Tom Mullins
static inline char get_coil(char coil) {
38
  return (coils >> coil) & 1;
39
}
40
41
static inline void tool_init() {DDRA |= _BV(DDA1);}
42
static inline void tool_enable() {PORTA |= _BV(PA1);}
43 76915f20 Tom Mullins
static inline void tool_disable() {PORTA &= ~ _BV(PA1);}
44 12ea39cc Tom Mullins
45 1085ef77 Tom Mullins
static inline void serno_zero(uint8_t *serno) {
46 1b054655 Tom Mullins
  memset(serno, 0, RFID_SERNO_SIZE);
47
}
48
49
static char serno_is_nonzero(uint8_t *serno) {
50
  int i;
51
  for (i = 0; i < RFID_SERNO_SIZE; i++) {
52
    if (serno[i]) {
53
      return 1;
54
    }
55
  }
56
  return 0;
57
}
58
59
static char serno_equal(uint8_t *a, uint8_t *b) {
60
  return memcmp(a, b, RFID_SERNO_SIZE) == 0;
61
}
62
63
static void serno_cpy(uint8_t *dest, uint8_t *src) {
64
  memcpy(dest, src, RFID_SERNO_SIZE);
65
}
66
67 94548bf4 Tom Mullins
static void tool_tick() {
68 12ea39cc Tom Mullins
69
  switch (toolstate) {
70
71
    case TS_INIT:
72
      if (get_coil(MB_COIL_INIT)) {
73
        set_coil(MB_COIL_NEW, 0);
74
        set_coil(MB_COIL_EN, 0);
75
        set_coil(MB_COIL_REQ_DIS, 0);
76
        toolstate = TS_OFF;
77
      }
78
      break;
79
80
    case TS_OFF:
81 f5d1f56a Tom Mullins
      led_off();
82 4b7d087b Tom Mullins
      set_coil(MB_COIL_EN, 0);
83 1b054655 Tom Mullins
      if (serno_is_nonzero(latest_reading)) {
84
        serno_cpy(current_user, latest_reading);
85 12ea39cc Tom Mullins
        set_coil(MB_COIL_NEW, 1);
86
        toolstate = TS_WAIT_ACCESS;
87
      }
88
      break;
89
90
    case TS_WAIT_ACCESS:
91 1b054655 Tom Mullins
      led_yellow();
92 12ea39cc Tom Mullins
      if (get_coil(MB_COIL_EN)) {
93
        tool_enable();
94
        toolstate = TS_ON;
95 dc472500 Tom Mullins
      } else if (!get_coil(MB_COIL_NEW)) {
96 f5d1f56a Tom Mullins
        toolstate = TS_DENY;
97 4b7d087b Tom Mullins
      } else if (!serno_equal(current_user, latest_reading)) {
98
        set_coil(MB_COIL_NEW, 0);
99
        toolstate = TS_OFF;
100 12ea39cc Tom Mullins
      }
101
      break;
102
103 f5d1f56a Tom Mullins
    case TS_DENY:
104
      led_red();
105
      if (!serno_equal(current_user, latest_reading)) {
106
        toolstate = TS_OFF;
107
        serno_zero(current_user);
108
      }
109 bbf2f5ad Tom Mullins
      break;
110 f5d1f56a Tom Mullins
111 12ea39cc Tom Mullins
    case TS_REQ_DIS:
112 e53aa5c5 Tom Mullins
      if (!get_coil(MB_COIL_EN)) {
113
        tool_disable();
114
        toolstate = TS_OFF;
115
      } else if (!get_coil(MB_COIL_REQ_DIS)) {
116
        toolstate = TS_ON;
117
      } else {
118
        // TODO blink yellow for 10 seconds or something
119
        set_coil(MB_COIL_EN, 0);
120
        set_coil(MB_COIL_REQ_DIS, 0);
121
        tool_disable();
122 1b054655 Tom Mullins
        serno_zero(current_user);
123 e53aa5c5 Tom Mullins
        toolstate = TS_OFF;
124
      }
125 12ea39cc Tom Mullins
      break;
126
127
    case TS_MISSING_ID:
128 e53aa5c5 Tom Mullins
      if (!get_coil(MB_COIL_EN)) {
129
        tool_disable();
130
        toolstate = TS_OFF;
131
      } else if (get_coil(MB_COIL_REQ_DIS)) {
132
        toolstate = TS_REQ_DIS;
133 1b054655 Tom Mullins
      } else if (serno_equal(current_user, latest_reading)) {
134 12ea39cc Tom Mullins
        toolstate = TS_ON;
135 bbf2f5ad Tom Mullins
      } else if (led_blink_done()) {
136
        set_coil(MB_COIL_EN, 0);
137
        tool_disable();
138
        serno_zero(current_user);
139
        toolstate = TS_OFF;
140 12ea39cc Tom Mullins
      }
141
      break;
142
143
    case TS_ON:
144 1b054655 Tom Mullins
      led_green();
145 12ea39cc Tom Mullins
      if (!get_coil(MB_COIL_EN)) {
146
        tool_disable();
147 1b054655 Tom Mullins
        serno_zero(current_user);
148 12ea39cc Tom Mullins
        toolstate = TS_OFF;
149 e53aa5c5 Tom Mullins
      } else if(get_coil(MB_COIL_REQ_DIS)) {
150 12ea39cc Tom Mullins
        toolstate = TS_REQ_DIS;
151 1b054655 Tom Mullins
      } else if (!serno_equal(current_user, latest_reading)) {
152 12ea39cc Tom Mullins
        toolstate = TS_MISSING_ID;
153 bbf2f5ad Tom Mullins
        led_blink_start(500, 6, YELLOW); // TODO made 10 seconds
154 12ea39cc Tom Mullins
      }
155
      break;
156
157
  }
158
159
}
160 208a6fb2 Tom Mullins
161 04b8cd51 Tom Mullins
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
162 20e5429c Tom Mullins
    eMBRegisterMode mode) {
163 208a6fb2 Tom Mullins
164 dc472500 Tom Mullins
  addr--;
165
166 532ba0bd Tom Mullins
  if (addr+n_coils > N_COILS) {
167 20e5429c Tom Mullins
    return MB_ENOREG;
168 208a6fb2 Tom Mullins
  }
169
170
  if (mode == MB_REG_WRITE) {
171
172
    switch (addr) {
173
174
      case MB_COIL_NEW:
175
        /* nop */
176
        reg_buf[0] >>= 1;
177
        n_coils--;
178
        if (n_coils == 0) {
179
          return MB_ENOERR;
180
        }
181
182
      case MB_COIL_EN:
183
        set_coil(MB_COIL_NEW, 0);
184
        set_coil(MB_COIL_EN, reg_buf[0] & 1);
185
        reg_buf[0] >>= 1;
186
        n_coils--;
187
        if (n_coils == 0) {
188
          return MB_ENOERR;
189
        }
190
191
      case MB_COIL_REQ_DIS:
192 12ea39cc Tom Mullins
        set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1);
193 208a6fb2 Tom Mullins
        reg_buf[0] >>= 1;
194
        n_coils--;
195
        if (n_coils == 0) {
196
          return MB_ENOERR;
197
        }
198
199
      case MB_COIL_INIT:
200
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
201
        reg_buf[0] >>= 1;
202
        n_coils--;
203
        if (n_coils == 0) {
204
          return MB_ENOERR;
205
        }
206
    }
207
208 20e5429c Tom Mullins
  } else if (mode == MB_REG_READ) {
209 208a6fb2 Tom Mullins
210 dc472500 Tom Mullins
    reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1);
211 208a6fb2 Tom Mullins
    return MB_ENOERR;
212
213 20e5429c Tom Mullins
  }
214 208a6fb2 Tom Mullins
215
  return MB_EIO;
216 20e5429c Tom Mullins
}
217
218 04b8cd51 Tom Mullins
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) {
219 20e5429c Tom Mullins
  return MB_ENOREG;
220
}
221
222 04b8cd51 Tom Mullins
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
223
224 dc472500 Tom Mullins
  addr--;
225
226 20e5429c Tom Mullins
  switch (addr) {
227 04b8cd51 Tom Mullins
228
    case MB_INP_SERNOL:
229 76915f20 Tom Mullins
      *reg_buf++ = current_user[0];
230
      *reg_buf++ = current_user[1];
231 04b8cd51 Tom Mullins
      n_regs--;
232
      if (n_regs == 0) {
233
        return MB_ENOERR;
234
      }
235
236
    case MB_INP_SERNOH:
237 76915f20 Tom Mullins
      *reg_buf++ = current_user[2];
238
      *reg_buf++ = current_user[3];
239 04b8cd51 Tom Mullins
      n_regs--;
240
      if (n_regs == 0) {
241
        return MB_ENOERR;
242
      }
243
244
    case MB_INP_CURRENT:
245 10936c07 Tom Mullins
      *reg_buf++ = (uint8_t)(current >> 8);
246
      *reg_buf++ = (uint8_t)current;
247 04b8cd51 Tom Mullins
      n_regs--;
248
      if (n_regs == 0) {
249
        return MB_ENOERR;
250 20e5429c Tom Mullins
      }
251 04b8cd51 Tom Mullins
252 20e5429c Tom Mullins
    default:
253
      return MB_ENOREG;
254
  }
255
}
256
257 04b8cd51 Tom Mullins
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
258 20e5429c Tom Mullins
    eMBRegisterMode mode) {
259
  if (mode == MB_REG_WRITE) {
260
    return MB_ENOREG;
261
  } else if (mode == MB_REG_READ) {
262
    return MB_ENOREG;
263
  } else {
264
    return MB_EIO;
265
  }
266
}
267
268
int main() {
269 36f3b65f Tom Mullins
  char rfid_ticks = 0;
270 12ea39cc Tom Mullins
271 94548bf4 Tom Mullins
  time_init();
272 532ba0bd Tom Mullins
  led_init();
273 12ea39cc Tom Mullins
  tool_init();
274 9b2b6d91 Tom Mullins
  rfid_init();
275 94548bf4 Tom Mullins
  current_init();
276 20e5429c Tom Mullins
277 a96c5547 Tom Mullins
  eMBInit(MB_RTU, TOOL_ADDRESS, 0, MB_BAUD, MB_PAR_NONE);
278 20e5429c Tom Mullins
  eMBEnable();
279
280 208a6fb2 Tom Mullins
  sei();
281
282 532ba0bd Tom Mullins
  rfid_start_read();
283 20e5429c Tom Mullins
  while (1) {
284 bbf2f5ad Tom Mullins
    if (rfid_poll()) {
285
      rfid_get_serno(latest_reading);
286
    }
287 36f3b65f Tom Mullins
    if (++rfid_ticks >= RFID_PERIOD/TICK_MS)
288 94548bf4 Tom Mullins
    {
289
      rfid_ticks = 0;
290 bbf2f5ad Tom Mullins
      rfid_start_read();
291 532ba0bd Tom Mullins
    }
292 10936c07 Tom Mullins
    current = current_read();
293 94548bf4 Tom Mullins
    tool_tick();
294
    led_tick();
295 20e5429c Tom Mullins
    eMBPoll();
296 94548bf4 Tom Mullins
    time_wait();
297 20e5429c Tom Mullins
  }
298
299
  return 0;
300
}