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