root / toolbox / main.c @ dc472500
History | View | Annotate | Download (4.85 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 |
} else if (!get_coil(MB_COIL_NEW)) { |
61 |
toolstate = TS_OFF; |
62 |
} |
63 |
break;
|
64 |
|
65 |
case TS_REQ_DIS:
|
66 |
if (!get_coil(MB_COIL_EN)) {
|
67 |
tool_disable(); |
68 |
toolstate = TS_OFF; |
69 |
} else if (!get_coil(MB_COIL_REQ_DIS)) { |
70 |
toolstate = TS_ON; |
71 |
} else {
|
72 |
// TODO blink yellow for 10 seconds or something
|
73 |
set_coil(MB_COIL_EN, 0);
|
74 |
set_coil(MB_COIL_REQ_DIS, 0);
|
75 |
tool_disable(); |
76 |
toolstate = TS_OFF; |
77 |
} |
78 |
break;
|
79 |
|
80 |
case TS_MISSING_ID:
|
81 |
if (!get_coil(MB_COIL_EN)) {
|
82 |
tool_disable(); |
83 |
toolstate = TS_OFF; |
84 |
} else if (get_coil(MB_COIL_REQ_DIS)) { |
85 |
toolstate = TS_REQ_DIS; |
86 |
} else if (rfid_check_serno(current_user)) { |
87 |
toolstate = TS_ON; |
88 |
} else {
|
89 |
if (led_blink_done()) {
|
90 |
set_coil(MB_COIL_EN, 0);
|
91 |
tool_disable(); |
92 |
toolstate = TS_OFF; |
93 |
} |
94 |
} |
95 |
break;
|
96 |
|
97 |
case TS_ON:
|
98 |
if (!get_coil(MB_COIL_EN)) {
|
99 |
tool_disable(); |
100 |
toolstate = TS_OFF; |
101 |
} else if(get_coil(MB_COIL_REQ_DIS)) { |
102 |
toolstate = TS_REQ_DIS; |
103 |
} else if (!rfid_check_serno(current_user)) { |
104 |
toolstate = TS_MISSING_ID; |
105 |
led_blink_start(); |
106 |
} |
107 |
break;
|
108 |
|
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils, |
114 |
eMBRegisterMode mode) { |
115 |
|
116 |
addr--; |
117 |
|
118 |
if (addr+n_coils > N_COILS) {
|
119 |
return MB_ENOREG;
|
120 |
} |
121 |
|
122 |
if (mode == MB_REG_WRITE) {
|
123 |
|
124 |
switch (addr) {
|
125 |
|
126 |
case MB_COIL_NEW:
|
127 |
/* nop */
|
128 |
reg_buf[0] >>= 1; |
129 |
n_coils--; |
130 |
if (n_coils == 0) { |
131 |
return MB_ENOERR;
|
132 |
} |
133 |
|
134 |
case MB_COIL_EN:
|
135 |
set_coil(MB_COIL_NEW, 0);
|
136 |
set_coil(MB_COIL_EN, reg_buf[0] & 1); |
137 |
reg_buf[0] >>= 1; |
138 |
n_coils--; |
139 |
if (n_coils == 0) { |
140 |
return MB_ENOERR;
|
141 |
} |
142 |
|
143 |
case MB_COIL_REQ_DIS:
|
144 |
set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1); |
145 |
reg_buf[0] >>= 1; |
146 |
n_coils--; |
147 |
if (n_coils == 0) { |
148 |
return MB_ENOERR;
|
149 |
} |
150 |
|
151 |
case MB_COIL_INIT:
|
152 |
set_coil(MB_COIL_INIT, reg_buf[0] & 1); |
153 |
reg_buf[0] >>= 1; |
154 |
n_coils--; |
155 |
if (n_coils == 0) { |
156 |
return MB_ENOERR;
|
157 |
} |
158 |
} |
159 |
|
160 |
} else if (mode == MB_REG_READ) { |
161 |
|
162 |
reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1); |
163 |
return MB_ENOERR;
|
164 |
|
165 |
} |
166 |
|
167 |
return MB_EIO;
|
168 |
} |
169 |
|
170 |
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) { |
171 |
return MB_ENOREG;
|
172 |
} |
173 |
|
174 |
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) { |
175 |
|
176 |
addr--; |
177 |
|
178 |
switch (addr) {
|
179 |
|
180 |
case MB_INP_SERNOL:
|
181 |
*reg_buf++ = current_user[0];
|
182 |
*reg_buf++ = current_user[1];
|
183 |
n_regs--; |
184 |
if (n_regs == 0) { |
185 |
return MB_ENOERR;
|
186 |
} |
187 |
|
188 |
case MB_INP_SERNOH:
|
189 |
*reg_buf++ = current_user[2];
|
190 |
*reg_buf++ = current_user[3];
|
191 |
n_regs--; |
192 |
if (n_regs == 0) { |
193 |
return MB_ENOERR;
|
194 |
} |
195 |
|
196 |
case MB_INP_CURRENT:
|
197 |
*reg_buf++ = 0;
|
198 |
*reg_buf++ = 0;
|
199 |
n_regs--; |
200 |
if (n_regs == 0) { |
201 |
return MB_ENOERR;
|
202 |
} |
203 |
|
204 |
default:
|
205 |
return MB_ENOREG;
|
206 |
} |
207 |
} |
208 |
|
209 |
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs, |
210 |
eMBRegisterMode mode) { |
211 |
if (mode == MB_REG_WRITE) {
|
212 |
return MB_ENOREG;
|
213 |
} else if (mode == MB_REG_READ) { |
214 |
return MB_ENOREG;
|
215 |
} else {
|
216 |
return MB_EIO;
|
217 |
} |
218 |
} |
219 |
|
220 |
int main() {
|
221 |
|
222 |
led_init(); |
223 |
tool_init(); |
224 |
rfid_init(); |
225 |
|
226 |
eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
|
227 |
eMBEnable(); |
228 |
|
229 |
sei(); |
230 |
|
231 |
rfid_start_read(); |
232 |
while (1) { |
233 |
if (rfid_poll()) {
|
234 |
rfid_get_serno(current_user); |
235 |
rfid_start_read(); |
236 |
} |
237 |
tool_main(); |
238 |
eMBPoll(); |
239 |
_delay_ms(50);
|
240 |
} |
241 |
|
242 |
return 0; |
243 |
} |