Statistics
| Branch: | Revision:

root / toolbox / main.c @ 532ba0bd

History | View | Annotate | Download (4.8 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 char coils;
22
static char current_user[RFID_SERNO_SIZE];
23

    
24
static inline void set_coil(char coil, char bit) {
25
  coils |= (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
      // TODO check that these (and the ones in SERNOH) are in the right order
174
      *reg_buf++ = current_user[0];
175
      *reg_buf++ = current_user[1];
176
      n_regs--;
177
      if (n_regs == 0) {
178
        return MB_ENOERR;
179
      }
180

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

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

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

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

    
213
int main() {
214

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

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

    
222
  sei();
223

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

    
235
  return 0;
236
}