root / toolbox / main.c @ db11713d
History | View | Annotate | Download (6.35 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 |
#include "current.h" |
11 |
#include "time.h" |
12 |
|
13 |
#if TOOL_ADDRESS < 0 |
14 |
#error Please define TOOL_ADDRESS
|
15 |
#endif
|
16 |
|
17 |
enum toolstate_t {
|
18 |
TS_INIT, |
19 |
TS_OFF, |
20 |
TS_WAIT_ACCESS, |
21 |
TS_DENY, |
22 |
TS_REQ_DIS, |
23 |
TS_MISSING_ID, |
24 |
TS_ON |
25 |
}; |
26 |
|
27 |
static enum toolstate_t toolstate = TS_INIT; |
28 |
static uint8_t coils;
|
29 |
static uint8_t latest_reading[RFID_SERNO_SIZE];
|
30 |
static uint8_t current_user[RFID_SERNO_SIZE];
|
31 |
static uint16_t current;
|
32 |
//static uint16_t current_max_warn, current_max_hard;
|
33 |
|
34 |
static inline void set_coil(char coil, char bit) { |
35 |
coils = (coils & ~(1 << coil)) | (bit << coil);
|
36 |
} |
37 |
static inline char get_coil(char coil) { |
38 |
return (coils >> coil) & 1; |
39 |
} |
40 |
|
41 |
static inline void tool_init() {DDRA |= _BV(DDA1);} |
42 |
static inline void tool_enable() {PORTA |= _BV(PA1);} |
43 |
static inline void tool_disable() {PORTA &= ~ _BV(PA1);} |
44 |
|
45 |
static inline void serno_zero(uint8_t *serno) { |
46 |
memset(serno, 0, RFID_SERNO_SIZE);
|
47 |
} |
48 |
|
49 |
static char serno_is_nonzero(uint8_t *serno) { |
50 |
int i;
|
51 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
52 |
if (serno[i]) {
|
53 |
return 1; |
54 |
} |
55 |
} |
56 |
return 0; |
57 |
} |
58 |
|
59 |
static char serno_equal(uint8_t *a, uint8_t *b) { |
60 |
return memcmp(a, b, RFID_SERNO_SIZE) == 0; |
61 |
} |
62 |
|
63 |
static void serno_cpy(uint8_t *dest, uint8_t *src) { |
64 |
memcpy(dest, src, RFID_SERNO_SIZE); |
65 |
} |
66 |
|
67 |
static void tool_tick() { |
68 |
|
69 |
switch (toolstate) {
|
70 |
|
71 |
case TS_INIT:
|
72 |
if (get_coil(MB_COIL_INIT)) {
|
73 |
set_coil(MB_COIL_NEW, 0);
|
74 |
set_coil(MB_COIL_EN, 0);
|
75 |
set_coil(MB_COIL_REQ_DIS, 0);
|
76 |
toolstate = TS_OFF; |
77 |
} |
78 |
break;
|
79 |
|
80 |
case TS_OFF:
|
81 |
led_off(); |
82 |
set_coil(MB_COIL_EN, 0);
|
83 |
if (serno_is_nonzero(latest_reading)) {
|
84 |
serno_cpy(current_user, latest_reading); |
85 |
set_coil(MB_COIL_NEW, 1);
|
86 |
toolstate = TS_WAIT_ACCESS; |
87 |
} |
88 |
break;
|
89 |
|
90 |
case TS_WAIT_ACCESS:
|
91 |
led_yellow(); |
92 |
if (get_coil(MB_COIL_EN)) {
|
93 |
tool_enable(); |
94 |
toolstate = TS_ON; |
95 |
} else if (!get_coil(MB_COIL_NEW)) { |
96 |
toolstate = TS_DENY; |
97 |
} else if (!serno_equal(current_user, latest_reading)) { |
98 |
set_coil(MB_COIL_NEW, 0);
|
99 |
toolstate = TS_OFF; |
100 |
} |
101 |
break;
|
102 |
|
103 |
case TS_DENY:
|
104 |
led_red(); |
105 |
if (!serno_equal(current_user, latest_reading)) {
|
106 |
toolstate = TS_OFF; |
107 |
serno_zero(current_user); |
108 |
} |
109 |
break;
|
110 |
|
111 |
case TS_REQ_DIS:
|
112 |
if (!get_coil(MB_COIL_EN)) {
|
113 |
tool_disable(); |
114 |
toolstate = TS_OFF; |
115 |
} else if (!get_coil(MB_COIL_REQ_DIS)) { |
116 |
toolstate = TS_ON; |
117 |
} else {
|
118 |
// TODO blink yellow for 10 seconds or something
|
119 |
set_coil(MB_COIL_EN, 0);
|
120 |
set_coil(MB_COIL_REQ_DIS, 0);
|
121 |
tool_disable(); |
122 |
serno_zero(current_user); |
123 |
toolstate = TS_OFF; |
124 |
} |
125 |
break;
|
126 |
|
127 |
case TS_MISSING_ID:
|
128 |
if (!get_coil(MB_COIL_EN)) {
|
129 |
tool_disable(); |
130 |
toolstate = TS_OFF; |
131 |
} else if (get_coil(MB_COIL_REQ_DIS)) { |
132 |
toolstate = TS_REQ_DIS; |
133 |
} else if (serno_equal(current_user, latest_reading)) { |
134 |
toolstate = TS_ON; |
135 |
} else if (led_blink_done()) { |
136 |
set_coil(MB_COIL_EN, 0);
|
137 |
tool_disable(); |
138 |
serno_zero(current_user); |
139 |
toolstate = TS_OFF; |
140 |
} |
141 |
break;
|
142 |
|
143 |
case TS_ON:
|
144 |
led_green(); |
145 |
if (!get_coil(MB_COIL_EN)) {
|
146 |
tool_disable(); |
147 |
serno_zero(current_user); |
148 |
toolstate = TS_OFF; |
149 |
} else if(get_coil(MB_COIL_REQ_DIS)) { |
150 |
toolstate = TS_REQ_DIS; |
151 |
} else if (!serno_equal(current_user, latest_reading)) { |
152 |
toolstate = TS_MISSING_ID; |
153 |
led_blink_start(500, 6, YELLOW); // TODO made 10 seconds |
154 |
} |
155 |
break;
|
156 |
|
157 |
} |
158 |
|
159 |
} |
160 |
|
161 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils, |
162 |
eMBRegisterMode mode) { |
163 |
|
164 |
addr--; |
165 |
|
166 |
if (addr+n_coils > N_COILS) {
|
167 |
return MB_ENOREG;
|
168 |
} |
169 |
|
170 |
if (mode == MB_REG_WRITE) {
|
171 |
|
172 |
switch (addr) {
|
173 |
|
174 |
case MB_COIL_NEW:
|
175 |
/* nop */
|
176 |
reg_buf[0] >>= 1; |
177 |
n_coils--; |
178 |
if (n_coils == 0) { |
179 |
return MB_ENOERR;
|
180 |
} |
181 |
|
182 |
case MB_COIL_EN:
|
183 |
set_coil(MB_COIL_NEW, 0);
|
184 |
set_coil(MB_COIL_EN, reg_buf[0] & 1); |
185 |
reg_buf[0] >>= 1; |
186 |
n_coils--; |
187 |
if (n_coils == 0) { |
188 |
return MB_ENOERR;
|
189 |
} |
190 |
|
191 |
case MB_COIL_REQ_DIS:
|
192 |
set_coil(MB_COIL_REQ_DIS, reg_buf[0] & 1); |
193 |
reg_buf[0] >>= 1; |
194 |
n_coils--; |
195 |
if (n_coils == 0) { |
196 |
return MB_ENOERR;
|
197 |
} |
198 |
|
199 |
case MB_COIL_INIT:
|
200 |
set_coil(MB_COIL_INIT, reg_buf[0] & 1); |
201 |
reg_buf[0] >>= 1; |
202 |
n_coils--; |
203 |
if (n_coils == 0) { |
204 |
return MB_ENOERR;
|
205 |
} |
206 |
} |
207 |
|
208 |
} else if (mode == MB_REG_READ) { |
209 |
|
210 |
reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1); |
211 |
return MB_ENOERR;
|
212 |
|
213 |
} |
214 |
|
215 |
return MB_EIO;
|
216 |
} |
217 |
|
218 |
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) { |
219 |
return MB_ENOREG;
|
220 |
} |
221 |
|
222 |
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs) { |
223 |
|
224 |
addr--; |
225 |
|
226 |
switch (addr) {
|
227 |
|
228 |
case MB_INP_SERNOL:
|
229 |
*reg_buf++ = current_user[0];
|
230 |
*reg_buf++ = current_user[1];
|
231 |
n_regs--; |
232 |
if (n_regs == 0) { |
233 |
return MB_ENOERR;
|
234 |
} |
235 |
|
236 |
case MB_INP_SERNOH:
|
237 |
*reg_buf++ = current_user[2];
|
238 |
*reg_buf++ = current_user[3];
|
239 |
n_regs--; |
240 |
if (n_regs == 0) { |
241 |
return MB_ENOERR;
|
242 |
} |
243 |
|
244 |
case MB_INP_CURRENT:
|
245 |
*reg_buf++ = (uint8_t)(current >> 8);
|
246 |
*reg_buf++ = (uint8_t)current; |
247 |
n_regs--; |
248 |
if (n_regs == 0) { |
249 |
return MB_ENOERR;
|
250 |
} |
251 |
|
252 |
default:
|
253 |
return MB_ENOREG;
|
254 |
} |
255 |
} |
256 |
|
257 |
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT n_regs, |
258 |
eMBRegisterMode mode) { |
259 |
if (mode == MB_REG_WRITE) {
|
260 |
return MB_ENOREG;
|
261 |
} else if (mode == MB_REG_READ) { |
262 |
return MB_ENOREG;
|
263 |
} else {
|
264 |
return MB_EIO;
|
265 |
} |
266 |
} |
267 |
|
268 |
int main() {
|
269 |
char rfid_ticks = 0, rfid_restart = 0; |
270 |
|
271 |
time_init(); |
272 |
led_init(); |
273 |
tool_init(); |
274 |
rfid_init(); |
275 |
current_init(); |
276 |
|
277 |
eMBInit(MB_RTU, TOOL_ADDRESS, 0, MB_BAUD, MB_PAR_NONE);
|
278 |
eMBEnable(); |
279 |
|
280 |
sei(); |
281 |
|
282 |
rfid_start_read(); |
283 |
while (1) { |
284 |
if (rfid_poll()) {
|
285 |
rfid_get_serno(latest_reading); |
286 |
rfid_restart = 1;
|
287 |
} |
288 |
if (++rfid_ticks >= RFID_PERIOD/TICK_MS && rfid_restart)
|
289 |
{ |
290 |
rfid_ticks = 0;
|
291 |
rfid_restart = 0;
|
292 |
rfid_start_read(); |
293 |
} |
294 |
current = current_read(); |
295 |
tool_tick(); |
296 |
led_tick(); |
297 |
eMBPoll(); |
298 |
time_wait(); |
299 |
} |
300 |
|
301 |
return 0; |
302 |
} |