Revision 208a6fb2
Added capability to read/write coils to toolbox
toolbox/main.c | ||
---|---|---|
5 | 5 |
#include "tooltron_mb.h" |
6 | 6 |
#include "rfid.h" |
7 | 7 |
|
8 |
static char coils; |
|
9 |
|
|
10 |
static inline void set_coil(char coil, char bit) { |
|
11 |
coils |= (bit << coil); |
|
12 |
} |
|
13 |
|
|
8 | 14 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils, |
9 | 15 |
eMBRegisterMode mode) { |
10 |
// TODO implement coils |
|
11 |
if (mode == MB_REG_WRITE) {
|
|
16 |
|
|
17 |
if (addr >= N_COILS || n_coils >= N_COILS-addr) {
|
|
12 | 18 |
return MB_ENOREG; |
19 |
} |
|
20 |
|
|
21 |
if (mode == MB_REG_WRITE) { |
|
22 |
|
|
23 |
switch (addr) { |
|
24 |
|
|
25 |
case MB_COIL_NEW: |
|
26 |
/* nop */ |
|
27 |
reg_buf[0] >>= 1; |
|
28 |
n_coils--; |
|
29 |
if (n_coils == 0) { |
|
30 |
return MB_ENOERR; |
|
31 |
} |
|
32 |
|
|
33 |
case MB_COIL_EN: |
|
34 |
set_coil(MB_COIL_NEW, 0); |
|
35 |
set_coil(MB_COIL_EN, reg_buf[0] & 1); |
|
36 |
reg_buf[0] >>= 1; |
|
37 |
n_coils--; |
|
38 |
if (n_coils == 0) { |
|
39 |
return MB_ENOERR; |
|
40 |
} |
|
41 |
|
|
42 |
case MB_COIL_REQ_DIS: |
|
43 |
set_coil(MB_COIL_INIT, reg_buf[0] & 1); |
|
44 |
reg_buf[0] >>= 1; |
|
45 |
n_coils--; |
|
46 |
if (n_coils == 0) { |
|
47 |
return MB_ENOERR; |
|
48 |
} |
|
49 |
|
|
50 |
case MB_COIL_INIT: |
|
51 |
set_coil(MB_COIL_INIT, reg_buf[0] & 1); |
|
52 |
reg_buf[0] >>= 1; |
|
53 |
n_coils--; |
|
54 |
if (n_coils == 0) { |
|
55 |
return MB_ENOERR; |
|
56 |
} |
|
57 |
} |
|
58 |
|
|
13 | 59 |
} else if (mode == MB_REG_READ) { |
14 |
return MB_ENOREG; |
|
15 |
} else { |
|
16 |
return MB_EIO; |
|
60 |
|
|
61 |
reg_buf[0] = (coils >> addr) & ((1 << n_coils) - 1); |
|
62 |
return MB_ENOERR; |
|
63 |
|
|
17 | 64 |
} |
65 |
|
|
66 |
return MB_EIO; |
|
18 | 67 |
} |
19 | 68 |
|
20 | 69 |
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT n_coils) { |
... | ... | |
75 | 124 |
eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE); |
76 | 125 |
eMBEnable(); |
77 | 126 |
|
127 |
/* Set pin controlling relay to output */ |
|
128 |
DDRA |= _BV(DDA1); |
|
129 |
|
|
130 |
sei(); |
|
131 |
|
|
78 | 132 |
while (1) { |
79 | 133 |
rfid_read(); |
134 |
/* TODO act on rfid input and coil state */ |
|
80 | 135 |
eMBPoll(); |
81 | 136 |
_delay_ms(200); |
82 | 137 |
} |
Also available in: Unified diff