Statistics
| Branch: | Revision:

root / toolbox / main.c @ 1085ef77

History | View | Annotate | Download (5.55 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_REQ_DIS,
16
  TS_MISSING_ID,
17
  TS_ON
18
};
19

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

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

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

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

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

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

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

    
58
static void tool_main() {
59

    
60
  switch (toolstate) {
61

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

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

    
80
    case TS_WAIT_ACCESS:
81
      led_yellow();
82
      if (get_coil(MB_COIL_EN)) {
83
        tool_enable();
84
        toolstate = TS_ON;
85
      } else if (!get_coil(MB_COIL_NEW)) {
86
        toolstate = TS_OFF;
87
      }
88
      break;
89

    
90
    case TS_REQ_DIS:
91
      if (!get_coil(MB_COIL_EN)) {
92
        tool_disable();
93
        toolstate = TS_OFF;
94
      } else if (!get_coil(MB_COIL_REQ_DIS)) {
95
        toolstate = TS_ON;
96
      } else {
97
        // TODO blink yellow for 10 seconds or something
98
        set_coil(MB_COIL_EN, 0);
99
        set_coil(MB_COIL_REQ_DIS, 0);
100
        tool_disable();
101
        serno_zero(current_user);
102
        toolstate = TS_OFF;
103
      }
104
      break;
105

    
106
    case TS_MISSING_ID:
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_REQ_DIS;
112
      } else if (serno_equal(current_user, latest_reading)) {
113
        toolstate = TS_ON;
114
      } else {
115
        if (led_blink_done()) {
116
          set_coil(MB_COIL_EN, 0);
117
          tool_disable();
118
          serno_zero(current_user);
119
          toolstate = TS_OFF;
120
        }
121
      }
122
      break;
123

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

    
138
  }
139

    
140
}
141

    
142
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
143
    eMBRegisterMode mode) {
144

    
145
  addr--;
146

    
147
  if (addr+n_coils > N_COILS) {
148
    return MB_ENOREG;
149
  }
150

    
151
  if (mode == MB_REG_WRITE) {
152

    
153
    switch (addr) {
154

    
155
      case MB_COIL_NEW:
156
        /* nop */
157
        reg_buf[0] >>= 1;
158
        n_coils--;
159
        if (n_coils == 0) {
160
          return MB_ENOERR;
161
        }
162

    
163
      case MB_COIL_EN:
164
        set_coil(MB_COIL_NEW, 0);
165
        set_coil(MB_COIL_EN, reg_buf[0] & 1);
166
        reg_buf[0] >>= 1;
167
        n_coils--;
168
        if (n_coils == 0) {
169
          return MB_ENOERR;
170
        }
171

    
172
      case MB_COIL_REQ_DIS:
173
        set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1);
174
        reg_buf[0] >>= 1;
175
        n_coils--;
176
        if (n_coils == 0) {
177
          return MB_ENOERR;
178
        }
179

    
180
      case MB_COIL_INIT:
181
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
182
        reg_buf[0] >>= 1;
183
        n_coils--;
184
        if (n_coils == 0) {
185
          return MB_ENOERR;
186
        }
187
    }
188

    
189
  } else if (mode == MB_REG_READ) {
190

    
191
    reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1);
192
    return MB_ENOERR;
193

    
194
  }
195

    
196
  return MB_EIO;
197
}
198

    
199
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) {
200
  return MB_ENOREG;
201
}
202

    
203
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
204

    
205
  addr--;
206

    
207
  switch (addr) {
208

    
209
    case MB_INP_SERNOL:
210
      *reg_buf++ = current_user[0];
211
      *reg_buf++ = current_user[1];
212
      n_regs--;
213
      if (n_regs == 0) {
214
        return MB_ENOERR;
215
      }
216

    
217
    case MB_INP_SERNOH:
218
      *reg_buf++ = current_user[2];
219
      *reg_buf++ = current_user[3];
220
      n_regs--;
221
      if (n_regs == 0) {
222
        return MB_ENOERR;
223
      }
224

    
225
    case MB_INP_CURRENT:
226
      *reg_buf++ = 0;
227
      *reg_buf++ = 0;
228
      n_regs--;
229
      if (n_regs == 0) {
230
        return MB_ENOERR;
231
      }
232

    
233
    default:
234
      return MB_ENOREG;
235
  }
236
}
237

    
238
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
239
    eMBRegisterMode mode) {
240
  if (mode == MB_REG_WRITE) {
241
    return MB_ENOREG;
242
  } else if (mode == MB_REG_READ) {
243
    return MB_ENOREG;
244
  } else {
245
    return MB_EIO;
246
  }
247
}
248

    
249
int main() {
250

    
251
  led_init();
252
  tool_init();
253
  rfid_init();
254

    
255
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
256
  eMBEnable();
257

    
258
  sei();
259

    
260
  rfid_start_read();
261
  while (1) {
262
    if (rfid_poll()) {
263
      rfid_get_serno(latest_reading);
264
      rfid_start_read();
265
    }
266
    tool_main();
267
    eMBPoll();
268
    _delay_ms(50);
269
  }
270

    
271
  return 0;
272
}