root / toolbox / main.c @ 07718da3
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 |
|
10 |
enum toolstate_t {
|
11 |
TS_INIT, |
12 |
TS_OFF, |
13 |
TS_WAIT_ACCESS, |
14 |
TS_REQ_DIS, |
15 |
TS_MISSING_ID, |
16 |
TS_ON |
17 |
}; |
18 |
|
19 |
static enum toolstate_t toolstate = TS_INIT; |
20 |
static char coils; |
21 |
static char current_user[RFID_SERNO_SIZE]; |
22 |
|
23 |
static inline void set_coil(char coil, char bit) { |
24 |
coils |= (bit << coil); |
25 |
} |
26 |
static inline char get_coil(char coil) { |
27 |
return (coils >> coil) & 1; |
28 |
} |
29 |
|
30 |
static inline void tool_init() {DDRA |= _BV(DDA1);} |
31 |
static inline void tool_enable() {PORTA |= _BV(PA1);} |
32 |
static inline void tool_disable() {PORTA &= _BV(PA1);} |
33 |
|
34 |
static void tool_main() { |
35 |
|
36 |
switch (toolstate) {
|
37 |
|
38 |
case TS_INIT:
|
39 |
if (get_coil(MB_COIL_INIT)) {
|
40 |
set_coil(MB_COIL_NEW, 0);
|
41 |
set_coil(MB_COIL_EN, 0);
|
42 |
set_coil(MB_COIL_REQ_DIS, 0);
|
43 |
toolstate = TS_OFF; |
44 |
} |
45 |
break;
|
46 |
|
47 |
case TS_OFF:
|
48 |
if (rfid_nonzero()) {
|
49 |
rfid_get_serno(current_user); |
50 |
set_coil(MB_COIL_NEW, 1);
|
51 |
toolstate = TS_WAIT_ACCESS; |
52 |
} |
53 |
break;
|
54 |
|
55 |
case TS_WAIT_ACCESS:
|
56 |
if (get_coil(MB_COIL_EN)) {
|
57 |
tool_enable(); |
58 |
toolstate = TS_ON; |
59 |
} |
60 |
break;
|
61 |
|
62 |
case TS_REQ_DIS:
|
63 |
if (!get_coil(MB_COIL_EN)) {
|
64 |
tool_disable(); |
65 |
toolstate = TS_OFF; |
66 |
} else if (!get_coil(MB_COIL_REQ_DIS)) { |
67 |
toolstate = TS_ON; |
68 |
} else {
|
69 |
// TODO blink yellow for 10 seconds or something
|
70 |
set_coil(MB_COIL_EN, 0);
|
71 |
set_coil(MB_COIL_REQ_DIS, 0);
|
72 |
tool_disable(); |
73 |
toolstate = TS_OFF; |
74 |
} |
75 |
break;
|
76 |
|
77 |
case TS_MISSING_ID:
|
78 |
if (!get_coil(MB_COIL_EN)) {
|
79 |
tool_disable(); |
80 |
toolstate = TS_OFF; |
81 |
} else if (get_coil(MB_COIL_REQ_DIS)) { |
82 |
toolstate = TS_REQ_DIS; |
83 |
} else if (rfid_check_serno(current_user)) { |
84 |
toolstate = TS_ON; |
85 |
} else {
|
86 |
// TODO blink yellow for 10 seconds or something
|
87 |
set_coil(MB_COIL_EN, 0);
|
88 |
tool_disable(); |
89 |
toolstate = TS_OFF; |
90 |
} |
91 |
break;
|
92 |
|
93 |
case TS_ON:
|
94 |
if (!get_coil(MB_COIL_EN)) {
|
95 |
tool_disable(); |
96 |
toolstate = TS_OFF; |
97 |
} else if(get_coil(MB_COIL_REQ_DIS)) { |
98 |
toolstate = TS_REQ_DIS; |
99 |
} else if (!rfid_check_serno(current_user)) { |
100 |
toolstate = TS_MISSING_ID; |
101 |
} |
102 |
break;
|
103 |
|
104 |
} |
105 |
|
106 |
} |
107 |
|
108 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils, |
109 |
eMBRegisterMode mode) { |
110 |
|
111 |
if (addr >= N_COILS || n_coils >= N_COILS-addr) {
|
112 |
return MB_ENOREG;
|
113 |
} |
114 |
|
115 |
if (mode == MB_REG_WRITE) {
|
116 |
|
117 |
switch (addr) {
|
118 |
|
119 |
case MB_COIL_NEW:
|
120 |
/* nop */
|
121 |
reg_buf[0] >>= 1; |
122 |
n_coils--; |
123 |
if (n_coils == 0) { |
124 |
return MB_ENOERR;
|
125 |
} |
126 |
|
127 |
case MB_COIL_EN:
|
128 |
set_coil(MB_COIL_NEW, 0);
|
129 |
set_coil(MB_COIL_EN, reg_buf[0] & 1); |
130 |
reg_buf[0] >>= 1; |
131 |
n_coils--; |
132 |
if (n_coils == 0) { |
133 |
return MB_ENOERR;
|
134 |
} |
135 |
|
136 |
case MB_COIL_REQ_DIS:
|
137 |
set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1); |
138 |
reg_buf[0] >>= 1; |
139 |
n_coils--; |
140 |
if (n_coils == 0) { |
141 |
return MB_ENOERR;
|
142 |
} |
143 |
|
144 |
case MB_COIL_INIT:
|
145 |
set_coil(MB_COIL_INIT, reg_buf[0] & 1); |
146 |
reg_buf[0] >>= 1; |
147 |
n_coils--; |
148 |
if (n_coils == 0) { |
149 |
return MB_ENOERR;
|
150 |
} |
151 |
} |
152 |
|
153 |
} else if (mode == MB_REG_READ) { |
154 |
|
155 |
reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1); |
156 |
return MB_ENOERR;
|
157 |
|
158 |
} |
159 |
|
160 |
return MB_EIO;
|
161 |
} |
162 |
|
163 |
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) { |
164 |
return MB_ENOREG;
|
165 |
} |
166 |
|
167 |
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) { |
168 |
char serno[RFID_SERNO_SIZE];
|
169 |
|
170 |
memcpy(reg_buf, current_user, sizeof(current_user));
|
171 |
|
172 |
switch (addr) {
|
173 |
|
174 |
case MB_INP_SERNOL:
|
175 |
// TODO check that these (and the ones in SERNOH) are in the right order
|
176 |
*reg_buf++ = serno[0];
|
177 |
*reg_buf++ = serno[1];
|
178 |
n_regs--; |
179 |
if (n_regs == 0) { |
180 |
return MB_ENOERR;
|
181 |
} |
182 |
|
183 |
case MB_INP_SERNOH:
|
184 |
*reg_buf++ = serno[2];
|
185 |
*reg_buf++ = serno[3];
|
186 |
n_regs--; |
187 |
if (n_regs == 0) { |
188 |
return MB_ENOERR;
|
189 |
} |
190 |
|
191 |
case MB_INP_CURRENT:
|
192 |
*reg_buf++ = 0;
|
193 |
*reg_buf++ = 0;
|
194 |
n_regs--; |
195 |
if (n_regs == 0) { |
196 |
return MB_ENOERR;
|
197 |
} |
198 |
|
199 |
default:
|
200 |
return MB_ENOREG;
|
201 |
} |
202 |
} |
203 |
|
204 |
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs, |
205 |
eMBRegisterMode mode) { |
206 |
if (mode == MB_REG_WRITE) {
|
207 |
return MB_ENOREG;
|
208 |
} else if (mode == MB_REG_READ) { |
209 |
return MB_ENOREG;
|
210 |
} else {
|
211 |
return MB_EIO;
|
212 |
} |
213 |
} |
214 |
|
215 |
int main() {
|
216 |
|
217 |
tool_init(); |
218 |
rfid_init(); |
219 |
|
220 |
eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
|
221 |
eMBEnable(); |
222 |
|
223 |
sei(); |
224 |
|
225 |
while (1) { |
226 |
rfid_read(); |
227 |
tool_main(); |
228 |
eMBPoll(); |
229 |
_delay_ms(200);
|
230 |
} |
231 |
|
232 |
return 0; |
233 |
} |