Statistics
| Branch: | Revision:

root / toolbox / main.c @ 6ca98a3f

History | View | Annotate | Download (4.75 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 current_user[RFID_SERNO_SIZE];
23

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

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

    
35
static void tool_main() {
36

    
37
  switch (toolstate) {
38

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

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

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

    
63
    case TS_REQ_DIS:
64
      if (!get_coil(MB_COIL_EN)) {
65
        tool_disable();
66
        toolstate = TS_OFF;
67
      } else if (!get_coil(MB_COIL_REQ_DIS)) {
68
        toolstate = TS_ON;
69
      } else {
70
        // TODO blink yellow for 10 seconds or something
71
        set_coil(MB_COIL_EN, 0);
72
        set_coil(MB_COIL_REQ_DIS, 0);
73
        tool_disable();
74
        toolstate = TS_OFF;
75
      }
76
      break;
77

    
78
    case TS_MISSING_ID:
79
      if (!get_coil(MB_COIL_EN)) {
80
        tool_disable();
81
        toolstate = TS_OFF;
82
      } else if (get_coil(MB_COIL_REQ_DIS)) {
83
        toolstate = TS_REQ_DIS;
84
      } else if (rfid_check_serno(current_user)) {
85
        toolstate = TS_ON;
86
      } else {
87
        // TODO blink yellow for 10 seconds or something
88
        set_coil(MB_COIL_EN, 0);
89
        tool_disable();
90
        toolstate = TS_OFF;
91
      }
92
      break;
93

    
94
    case TS_ON:
95
      if (!get_coil(MB_COIL_EN)) {
96
        tool_disable();
97
        toolstate = TS_OFF;
98
      } else if(get_coil(MB_COIL_REQ_DIS)) {
99
        toolstate = TS_REQ_DIS;
100
      } else if (!rfid_check_serno(current_user)) {
101
        toolstate = TS_MISSING_ID;
102
      }
103
      break;
104

    
105
  }
106

    
107
}
108

    
109
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils,
110
    eMBRegisterMode mode) {
111

    
112
  if (addr+n_coils > N_COILS) {
113
    return MB_ENOREG;
114
  }
115

    
116
  if (mode == MB_REG_WRITE) {
117

    
118
    switch (addr) {
119

    
120
      case MB_COIL_NEW:
121
        /* nop */
122
        reg_buf[0] >>= 1;
123
        n_coils--;
124
        if (n_coils == 0) {
125
          return MB_ENOERR;
126
        }
127

    
128
      case MB_COIL_EN:
129
        set_coil(MB_COIL_NEW, 0);
130
        set_coil(MB_COIL_EN, reg_buf[0] & 1);
131
        reg_buf[0] >>= 1;
132
        n_coils--;
133
        if (n_coils == 0) {
134
          return MB_ENOERR;
135
        }
136

    
137
      case MB_COIL_REQ_DIS:
138
        set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1);
139
        reg_buf[0] >>= 1;
140
        n_coils--;
141
        if (n_coils == 0) {
142
          return MB_ENOERR;
143
        }
144

    
145
      case MB_COIL_INIT:
146
        set_coil(MB_COIL_INIT, reg_buf[0] & 1);
147
        reg_buf[0] >>= 1;
148
        n_coils--;
149
        if (n_coils == 0) {
150
          return MB_ENOERR;
151
        }
152
    }
153

    
154
  } else if (mode == MB_REG_READ) {
155

    
156
    reg_buf[0] = (coils >> (addr-1)) & ((1 << n_coils) - 1);
157
    return MB_ENOERR;
158

    
159
  }
160

    
161
  return MB_EIO;
162
}
163

    
164
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) {
165
  return MB_ENOREG;
166
}
167

    
168
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) {
169

    
170
  switch (addr) {
171

    
172
    case MB_INP_SERNOL:
173
      *reg_buf++ = current_user[0];
174
      *reg_buf++ = current_user[1];
175
      n_regs--;
176
      if (n_regs == 0) {
177
        return MB_ENOERR;
178
      }
179

    
180
    case MB_INP_SERNOH:
181
      *reg_buf++ = current_user[2];
182
      *reg_buf++ = current_user[3];
183
      n_regs--;
184
      if (n_regs == 0) {
185
        return MB_ENOERR;
186
      }
187

    
188
    case MB_INP_CURRENT:
189
      *reg_buf++ = 0;
190
      *reg_buf++ = 0;
191
      n_regs--;
192
      if (n_regs == 0) {
193
        return MB_ENOERR;
194
      }
195

    
196
    default:
197
      return MB_ENOREG;
198
  }
199
}
200

    
201
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs,
202
    eMBRegisterMode mode) {
203
  if (mode == MB_REG_WRITE) {
204
    return MB_ENOREG;
205
  } else if (mode == MB_REG_READ) {
206
    return MB_ENOREG;
207
  } else {
208
    return MB_EIO;
209
  }
210
}
211

    
212
int main() {
213

    
214
  led_init();
215
  tool_init();
216
  rfid_init();
217

    
218
  eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
219
  eMBEnable();
220

    
221
  sei();
222

    
223
  rfid_start_read();
224
  while (1) {
225
    if (rfid_poll()) {
226
      rfid_get_serno(current_user);
227
      rfid_start_read();
228
    }
229
    //tool_main();
230
    eMBPoll();
231
    _delay_ms(50);
232
  }
233

    
234
  return 0;
235
}