-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensors_poll.tc
407 lines (340 loc) · 7.27 KB
/
sensors_poll.tc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "global.th"
#include "1wire.th"
#include "ssi.th"
#include "sensors_db.th"
#include "device_funcs.th"
#include "utils.th"
#include "tgs.th"
#include "modbus.th"
/* I/O program commands
*/
enum pcmd {
CMD_WRITE = 'W',
CMD_READ = 'R',
CMD_DELAY = 'D',
CMD_END = '.',
};
struct pchunk {
pcmd cmd;
byte cb;
byte data[1];
};
static string
_read(sensor *sens, byte cb, bool do_stop)
{
string res = "";
#ifdef _TRACE_IO
string out = "";
#endif
byte r, rl;
byte i2ca;
switch(sens->bus) {
case IO_I2C:
i2ca = (sens->i2c_addr << 1) | 0x01;
i2c_start(sens->port);
i2c_write(sens->port, i2ca);
#ifdef _TRACE_IO
out = "I2C_START R " + tohex(i2ca) + " LEN=" + str(cb) + " [";
#endif
for(; cb != 0; --cb) {
r = i2c_read(sens->port, cb != 1);
res += chr(r);
#ifdef _TRACE_IO
out += tohex(r);
if(cb != 1)
out += " ";
#endif
}
i2c_stop(sens->port);
#ifdef _TRACE_IO
out += "] I2C_STOP";
#endif
break;
case IO_OW:
#ifdef _TRACE_IO
out = "";
#endif
if(do_stop) {
if(!ow_start(sens->port, sens->chnl, OW_MATCH_ROM, &sens->ow_addr))
break;
#ifdef _TRACE_IO
else out += "1W_START R "+address_format(IO_OW, &sens->ow_addr);
} else {
out += "R ";
#endif
}
#ifdef _TRACE_IO
out += "LEN="+str(cb)+" [";
#endif
res = ow_recvstr(sens->port, cb);
#ifdef _TRACE_IO
rl = len(res);
for(byte i=0; i<rl; ++i) {
out += tohex(res[i]);
if(i < rl-1)
out += " ";
}
out += "] 1W_STOP";
#endif
ow_stop(sens->port);
break;
case IO_RS232:
case IO_RS422:
case IO_RS485:
#if 0
/* Code moved out of this function because it requires 'doevents'
*/
if(SR_MODBUS == serial_get_role(sens->port)) {
if(!modbus_process_result(sens))
ERR("ModBus request timeout");
}
#endif
break;
}
#ifdef _TRACE_IO
if(len(out) > 0)
DBG(out);
#endif
return res;
}
static void
_write(sensor *sens, byte *cmd, byte cmdlen, bool do_stop)
{
byte i2ca;
#ifdef _TRACE_IO
string out = "";
#endif
word *regno;
switch(sens->bus) {
case IO_I2C:
i2ca = (sens->i2c_addr << 1) | 0x00;
i2c_start(sens->port);
i2c_write(sens->port, i2ca);
#ifdef _TRACE_IO
out = "I2C_START W " + tohex(i2ca) + " LEN="+str(cmdlen) + " [";
#endif
for(byte i=0; i<cmdlen; ++i) {
#ifdef _TRACE_IO
out += tohex(cmd[i]);
if(i != cmdlen-1)
out += " ";
#endif
i2c_write(sens->port, cmd[i]);
}
#ifdef _TRACE_IO
out += "] ";
#endif
if(do_stop) {
#ifdef _TRACE_IO
out += "I2C_STOP";
#endif
i2c_stop(sens->port);
}
break;
case IO_OW:
if(ow_start(sens->port, sens->chnl, OW_MATCH_ROM, &sens->ow_addr)) {
#ifdef _TRACE_IO
out = "1W_START W " + address_format(IO_OW, &sens->ow_addr) + " [";
for(byte i=0; i<cmdlen; ++i) {
out += tohex(cmd[i]);
if(i < cmdlen-1)
out += " ";
}
out += "] ";
#endif
ow_send(sens->port, cmd, cmdlen);
if(do_stop) {
#ifdef _TRACE_IO
out += "1W_STOP";
#endif
ow_stop(sens->port);
}
}
break;
case IO_RS232:
case IO_RS422:
case IO_RS485:
if(SR_MODBUS == serial_get_role(sens->port)) {
if(cmdlen == sizeof(word)) {
regno = (word *)&cmd[0];
modbus_request(sens->port, sens->modbus_addr, READ_INPUT_REGISTERS, *regno, 1);
}
}
break;
} // switch
#ifdef _TRACE_IO
if(len(out) > 0)
DBG(out);
#endif
}
static string
_exec_io_program(sensor *sens, byte *prog, byte plen)
{
pchunk *ck = (pchunk *)prog;
pchunk *next;
byte i, l;
string res;
bool do_stop;
res = "";
do_stop = true;
while(plen != 0) {
if(ck->cb > plen) // Something went wrong: chunk longer than remaining part of program!
break;
next = (plen == ck->cb)? NULL: (pchunk *)&ck->data[ck->cb];
switch(ck->cmd) {
case CMD_READ:
res += _read(sens, ck->data[0], do_stop);
do_stop = true;
break;
case CMD_WRITE:
do_stop = (NULL != next)? next->cmd != CMD_READ: true; // special handling for R->W pairs
_write(sens, ck->data, ck->cb, do_stop);
break;
case CMD_DELAY:
delay(ck->data[0]);
break;
case CMD_END:
return res;
default: // Something went wrong: unknown command opcode!
return res;
}
plen -= ck->cb;
ck = next;
}
return res;
}
static void
_run(sensor *addr, string& cmd, string& tmp)
{
byte *ptr, i, np, nbf, pl;
// First byte is the subprogram counter
ptr = (byte *)&cmd[0];
for(np = *ptr++; np != 0; --np) {
for(nbf = *ptr++; nbf != 0; --nbf) {
pl = *ptr++; // Command length byte
for(i=0; i<NUM_SENSOR_PARAMS; ++i)
addr->prev[i] = addr->res[i];
exec_program(addr, tmp, ptr, pl);
ptr += pl;
}
}
}
static void
_sensor_read_tibbo(sensor *s)
{
tgs_addr a = chr(s->mac[0]) + chr(s->mac[1]) + chr(s->mac[2]) + chr(s->mac[3]);
word rqid;
dword tmo;
tibbo_sensors_poll();
while(0 == (rqid = tibbo_query_sensor(s->port, a, 0, s))) {
/* wait until queue has some available space */
doevents;
tibbo_sensors_poll();
}
tibbo_sensors_poll();
while(!tibbo_task_finished(rqid)) {
doevents;
tibbo_sensors_poll();
}
tibbo_task_done(rqid);
}
static void
_sensor_read_modbus(sensor *addr)
{
string<16> tmp;
string cmd;
dword tmo;
if(!sensor_db_open())
return;
cmd = sensor_db_get_read_command(addr->drvid);
if(len(cmd) != 0) {
#ifdef _TRACE_IO
DBG("TYPE="+tohex(addr->drvid));
#endif
tmp = _exec_io_program(addr, (byte *)&cmd[0], len(cmd));
cmd = sensor_db_get_read_program(addr->drvid);
if(len(cmd) > 0) {
_run(addr, cmd, tmp);
tmo = sys.timercountms + MODBUS_TIMEOUT;
while(sys.timercountms < tmo) {
if(modbus_process_result(addr))
break;
// allow OS to handle all internal jobs
doevents;
}
}
}
}
static void
_sensor_read(sensor *addr)
{
string cmd;
string<16> tmp;
if(!sensor_db_open())
return;
cmd = sensor_db_get_read_command(addr->drvid);
if(len(cmd) != 0) {
#ifdef _TRACE_IO
DBG("-- READ_SENSOR TYPE="+tohex(addr->drvid)+" --");
#endif
tmp = _exec_io_program(addr, (byte *)&cmd[0], len(cmd));
cmd = sensor_db_get_read_program(addr->drvid);
if(len(cmd) > 0)
_run(addr, cmd, tmp);
}
}
/*! \brief Read sensor value
*/
void
sensor_read(sensor *addr)
{
serial_role sr;
switch(ioport_type(addr->port)) {
case IO_RS232:
case IO_RS422:
case IO_RS485:
sr = serial_get_role(addr->port);
if(SR_TIBBO == sr)
_sensor_read_tibbo(addr);
else if(SR_MODBUS == sr)
_sensor_read_modbus(addr);
else if(SR_MODEM != sr)
_sensor_read(addr);
break;
case IO_DC:
case IO_WC:
case IO_GPIO4:
/* The dry contact lines are polled elsewhere
*/
break;
default:
_sensor_read(addr);
break;
}
sensor_alarm_check(addr);
}
/*! \brief Issue sensor initialization commands
*/
void
sensor_init(sensor *addr)
{
string cmd;
string<16> tmp;
/* Dry contacts doesn't have any initialization or configuration
* commands
*/
if(IO_DC == ioport_type(addr->port))
return;
if(!sensor_db_open())
return;
cmd = sensor_db_get_init_command(addr->drvid);
if(len(cmd) == 0)
return;
#ifdef _TRACE_IO
DBG("-- INIT_SENSOR TYPE="+tohex(addr->drvid)+" --");
#endif
tmp = _exec_io_program(addr, (byte *)&cmd[0], len(cmd));
cmd = sensor_db_get_init_program(addr->drvid);
if(len(cmd) > 0)
_run(addr, cmd, tmp);
}