]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <stdlib.h> | |
23 | #include <stdarg.h> | |
24 | #include <string.h> | |
25 | #include <limits.h> | |
26 | #include <math.h> | |
27 | #include <libsigrok/libsigrok.h> | |
28 | #include "libsigrok-internal.h" | |
29 | #include "protocol.h" | |
30 | ||
31 | #define JOB_TIMEOUT 300 | |
32 | ||
33 | #define INFINITE_INTERVAL INT_MAX | |
34 | #define SAMPLERATE_INTERVAL -1 | |
35 | ||
36 | static const struct agdmm_job *job_current(const struct dev_context *devc) | |
37 | { | |
38 | return &devc->profile->jobs[devc->current_job]; | |
39 | } | |
40 | ||
41 | static void job_done(struct dev_context *devc) | |
42 | { | |
43 | devc->job_running = FALSE; | |
44 | } | |
45 | ||
46 | static void job_again(struct dev_context *devc) | |
47 | { | |
48 | devc->job_again = TRUE; | |
49 | } | |
50 | ||
51 | static gboolean job_is_running(const struct dev_context *devc) | |
52 | { | |
53 | return devc->job_running; | |
54 | } | |
55 | ||
56 | static gboolean job_in_interval(const struct dev_context *devc) | |
57 | { | |
58 | int64_t job_start = devc->jobs_start[devc->current_job]; | |
59 | int64_t now = g_get_monotonic_time() / 1000; | |
60 | int interval = job_current(devc)->interval; | |
61 | if (interval == SAMPLERATE_INTERVAL) | |
62 | interval = 1000 / devc->cur_samplerate; | |
63 | return (now - job_start) < interval || interval == INFINITE_INTERVAL; | |
64 | } | |
65 | ||
66 | static gboolean job_has_timeout(const struct dev_context *devc) | |
67 | { | |
68 | int64_t job_start = devc->jobs_start[devc->current_job]; | |
69 | int64_t now = g_get_monotonic_time() / 1000; | |
70 | return job_is_running(devc) && (now - job_start) > JOB_TIMEOUT; | |
71 | } | |
72 | ||
73 | static const struct agdmm_job *job_next(struct dev_context *devc) | |
74 | { | |
75 | int current_job = devc->current_job; | |
76 | do { | |
77 | devc->current_job++; | |
78 | if (!job_current(devc)->send) | |
79 | devc->current_job = 0; | |
80 | } while(job_in_interval(devc) && devc->current_job != current_job); | |
81 | return job_current(devc); | |
82 | } | |
83 | ||
84 | static void job_run_again(const struct sr_dev_inst *sdi) | |
85 | { | |
86 | struct dev_context *devc = sdi->priv; | |
87 | devc->job_again = FALSE; | |
88 | devc->job_running = TRUE; | |
89 | if (job_current(devc)->send(sdi) == SR_ERR_NA) | |
90 | job_done(devc); | |
91 | } | |
92 | ||
93 | static void job_run(const struct sr_dev_inst *sdi) | |
94 | { | |
95 | struct dev_context *devc = sdi->priv; | |
96 | int64_t now = g_get_monotonic_time() / 1000; | |
97 | devc->jobs_start[devc->current_job] = now; | |
98 | job_run_again(sdi); | |
99 | } | |
100 | ||
101 | static void dispatch(const struct sr_dev_inst *sdi) | |
102 | { | |
103 | struct dev_context *devc = sdi->priv; | |
104 | ||
105 | if (devc->job_again) { | |
106 | job_run_again(sdi); | |
107 | return; | |
108 | } | |
109 | ||
110 | if (!job_is_running(devc)) | |
111 | job_next(devc); | |
112 | else if (job_has_timeout(devc)) | |
113 | job_done(devc); | |
114 | ||
115 | if (!job_is_running(devc) && !job_in_interval(devc)) | |
116 | job_run(sdi); | |
117 | } | |
118 | ||
119 | static void receive_line(const struct sr_dev_inst *sdi) | |
120 | { | |
121 | struct dev_context *devc; | |
122 | const struct agdmm_recv *recvs, *recv; | |
123 | GRegex *reg; | |
124 | GMatchInfo *match; | |
125 | int i; | |
126 | ||
127 | devc = sdi->priv; | |
128 | ||
129 | /* Strip CRLF */ | |
130 | while (devc->buflen) { | |
131 | if (*(devc->buf + devc->buflen - 1) == '\r' | |
132 | || *(devc->buf + devc->buflen - 1) == '\n') | |
133 | *(devc->buf + --devc->buflen) = '\0'; | |
134 | else | |
135 | break; | |
136 | } | |
137 | sr_spew("Received '%s'.", devc->buf); | |
138 | ||
139 | recv = NULL; | |
140 | recvs = devc->profile->recvs; | |
141 | for (i = 0; (&recvs[i])->recv_regex; i++) { | |
142 | reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL); | |
143 | if (g_regex_match(reg, (char *)devc->buf, 0, &match)) { | |
144 | recv = &recvs[i]; | |
145 | break; | |
146 | } | |
147 | g_match_info_unref(match); | |
148 | g_regex_unref(reg); | |
149 | } | |
150 | if (recv) { | |
151 | enum job_type type = recv->recv(sdi, match); | |
152 | if (type == job_current(devc)->type) | |
153 | job_done(devc); | |
154 | else if (type == JOB_AGAIN) | |
155 | job_again(devc); | |
156 | g_match_info_unref(match); | |
157 | g_regex_unref(reg); | |
158 | } else | |
159 | sr_dbg("Unknown line '%s'.", devc->buf); | |
160 | ||
161 | /* Done with this. */ | |
162 | devc->buflen = 0; | |
163 | } | |
164 | ||
165 | SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data) | |
166 | { | |
167 | struct sr_dev_inst *sdi; | |
168 | struct dev_context *devc; | |
169 | struct sr_serial_dev_inst *serial; | |
170 | int len; | |
171 | ||
172 | (void)fd; | |
173 | ||
174 | if (!(sdi = cb_data)) | |
175 | return TRUE; | |
176 | ||
177 | if (!(devc = sdi->priv)) | |
178 | return TRUE; | |
179 | ||
180 | serial = sdi->conn; | |
181 | if (revents == G_IO_IN) { | |
182 | /* Serial data arrived. */ | |
183 | while (AGDMM_BUFSIZE - devc->buflen - 1 > 0) { | |
184 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); | |
185 | if (len < 1) | |
186 | break; | |
187 | devc->buflen += len; | |
188 | *(devc->buf + devc->buflen) = '\0'; | |
189 | if (*(devc->buf + devc->buflen - 1) == '\n') { | |
190 | /* End of line */ | |
191 | receive_line(sdi); | |
192 | break; | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 | dispatch(sdi); | |
198 | ||
199 | if (sr_sw_limits_check(&devc->limits)) | |
200 | sdi->driver->dev_acquisition_stop(sdi); | |
201 | ||
202 | return TRUE; | |
203 | } | |
204 | ||
205 | static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd, ...) | |
206 | { | |
207 | struct sr_serial_dev_inst *serial; | |
208 | va_list args; | |
209 | char buf[32]; | |
210 | ||
211 | serial = sdi->conn; | |
212 | ||
213 | va_start(args, cmd); | |
214 | vsnprintf(buf, sizeof(buf) - 3, cmd, args); | |
215 | va_end(args); | |
216 | sr_spew("Sending '%s'.", buf); | |
217 | if (!strncmp(buf, "*IDN?", 5)) | |
218 | strcat(buf, "\r\n"); | |
219 | else | |
220 | strcat(buf, "\n\r\n"); | |
221 | if (serial_write_blocking(serial, buf, strlen(buf), SERIAL_WRITE_TIMEOUT_MS) < (int)strlen(buf)) { | |
222 | sr_err("Failed to send."); | |
223 | return SR_ERR; | |
224 | } | |
225 | ||
226 | return SR_OK; | |
227 | } | |
228 | ||
229 | static int send_stat(const struct sr_dev_inst *sdi) | |
230 | { | |
231 | return agdmm_send(sdi, "STAT?"); | |
232 | } | |
233 | ||
234 | static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
235 | { | |
236 | struct dev_context *devc; | |
237 | char *s; | |
238 | ||
239 | devc = sdi->priv; | |
240 | s = g_match_info_fetch(match, 1); | |
241 | sr_spew("STAT response '%s'.", s); | |
242 | ||
243 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
244 | * set both flags to denote it's not a normal measurement. */ | |
245 | if (s[0] == '1') | |
246 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN; | |
247 | else | |
248 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
249 | ||
250 | if (s[1] == '1') | |
251 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; | |
252 | else | |
253 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; | |
254 | ||
255 | /* Triggered or auto hold modes. */ | |
256 | if (s[2] == '1' || s[3] == '1') | |
257 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; | |
258 | else | |
259 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; | |
260 | ||
261 | /* Temp/aux mode. */ | |
262 | if (s[7] == '1') | |
263 | devc->mode_tempaux = TRUE; | |
264 | else | |
265 | devc->mode_tempaux = FALSE; | |
266 | ||
267 | /* Continuity mode. */ | |
268 | if (s[16] == '1') | |
269 | devc->mode_continuity = TRUE; | |
270 | else | |
271 | devc->mode_continuity = FALSE; | |
272 | ||
273 | g_free(s); | |
274 | ||
275 | return JOB_STAT; | |
276 | } | |
277 | ||
278 | static int recv_stat_u124x(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
279 | { | |
280 | struct dev_context *devc; | |
281 | char *s; | |
282 | ||
283 | devc = sdi->priv; | |
284 | s = g_match_info_fetch(match, 1); | |
285 | sr_spew("STAT response '%s'.", s); | |
286 | ||
287 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
288 | * set both flags to denote it's not a normal measurement. */ | |
289 | if (s[0] == '1') | |
290 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN; | |
291 | else | |
292 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
293 | ||
294 | if (s[1] == '1') | |
295 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; | |
296 | else | |
297 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; | |
298 | ||
299 | /* Hold mode. */ | |
300 | if (s[7] == '1') | |
301 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; | |
302 | else | |
303 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; | |
304 | ||
305 | g_free(s); | |
306 | ||
307 | return JOB_STAT; | |
308 | } | |
309 | ||
310 | static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
311 | { | |
312 | struct dev_context *devc; | |
313 | char *s; | |
314 | ||
315 | devc = sdi->priv; | |
316 | s = g_match_info_fetch(match, 1); | |
317 | sr_spew("STAT response '%s'.", s); | |
318 | ||
319 | /* dBm/dBV modes. */ | |
320 | if ((s[2] & ~0x20) == 'M') | |
321 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_MW; | |
322 | else if ((s[2] & ~0x20) == 'V') | |
323 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_VOLT; | |
324 | else | |
325 | devc->mode_dbm_dbv = 0; | |
326 | ||
327 | /* Peak hold mode. */ | |
328 | if (s[4] == '1') | |
329 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX; | |
330 | else | |
331 | devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX; | |
332 | ||
333 | /* Triggered hold mode. */ | |
334 | if (s[7] == '1') | |
335 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; | |
336 | else | |
337 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; | |
338 | ||
339 | g_free(s); | |
340 | ||
341 | return JOB_STAT; | |
342 | } | |
343 | ||
344 | static int recv_stat_u128x(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
345 | { | |
346 | struct dev_context *devc; | |
347 | char *s; | |
348 | ||
349 | devc = sdi->priv; | |
350 | s = g_match_info_fetch(match, 1); | |
351 | sr_spew("STAT response '%s'.", s); | |
352 | ||
353 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
354 | * set both flags to denote it's not a normal measurement. */ | |
355 | if (s[0] == '1') | |
356 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG; | |
357 | else | |
358 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG); | |
359 | ||
360 | /* dBm/dBV modes. */ | |
361 | if ((s[2] & ~0x20) == 'M') | |
362 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_MW; | |
363 | else if ((s[2] & ~0x20) == 'V') | |
364 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_VOLT; | |
365 | else | |
366 | devc->mode_dbm_dbv = 0; | |
367 | ||
368 | /* Peak hold mode. */ | |
369 | if (s[4] == '4') | |
370 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX; | |
371 | else | |
372 | devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX; | |
373 | ||
374 | /* Null function. */ | |
375 | if (s[1] == '1') | |
376 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; | |
377 | else | |
378 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; | |
379 | ||
380 | /* Triggered or auto hold modes. */ | |
381 | if (s[7] == '1' || s[11] == '1') | |
382 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; | |
383 | else | |
384 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; | |
385 | ||
386 | g_free(s); | |
387 | ||
388 | return JOB_STAT; | |
389 | } | |
390 | ||
391 | static int send_fetc(const struct sr_dev_inst *sdi) | |
392 | { | |
393 | struct dev_context *devc = sdi->priv; | |
394 | ||
395 | if (devc->mode_squarewave) | |
396 | return SR_ERR_NA; | |
397 | ||
398 | if (devc->cur_channel->index > 0) | |
399 | return agdmm_send(sdi, "FETC? @%d", devc->cur_channel->index + 1); | |
400 | else | |
401 | return agdmm_send(sdi, "FETC?"); | |
402 | } | |
403 | ||
404 | static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
405 | { | |
406 | struct dev_context *devc; | |
407 | struct sr_datafeed_packet packet; | |
408 | struct sr_datafeed_analog analog; | |
409 | struct sr_analog_encoding encoding; | |
410 | struct sr_analog_meaning meaning; | |
411 | struct sr_analog_spec spec; | |
412 | float fvalue; | |
413 | const char *s; | |
414 | char *mstr; | |
415 | int i, exp; | |
416 | ||
417 | sr_spew("FETC reply '%s'.", g_match_info_get_string(match)); | |
418 | devc = sdi->priv; | |
419 | i = devc->cur_channel->index; | |
420 | ||
421 | if (devc->cur_mq[i] == -1) | |
422 | /* This detects when channel P2 is reporting TEMP as an identical | |
423 | * copy of channel P3. In this case, we just skip P2. */ | |
424 | goto skip_value; | |
425 | ||
426 | s = g_match_info_get_string(match); | |
427 | if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) { | |
428 | /* An invalid measurement shows up on the display as "O.L", but | |
429 | * comes through like this. Since comparing 38-digit floats | |
430 | * is rather problematic, we'll cut through this here. */ | |
431 | fvalue = NAN; | |
432 | } else { | |
433 | mstr = g_match_info_fetch(match, 1); | |
434 | if (sr_atof_ascii(mstr, &fvalue) != SR_OK) { | |
435 | g_free(mstr); | |
436 | sr_dbg("Invalid float."); | |
437 | return SR_ERR; | |
438 | } | |
439 | g_free(mstr); | |
440 | if (devc->cur_exponent[i] != 0) | |
441 | fvalue *= powf(10, devc->cur_exponent[i]); | |
442 | } | |
443 | ||
444 | if (devc->cur_unit[i] == SR_UNIT_DECIBEL_MW || | |
445 | devc->cur_unit[i] == SR_UNIT_DECIBEL_VOLT || | |
446 | devc->cur_unit[i] == SR_UNIT_PERCENTAGE) { | |
447 | mstr = g_match_info_fetch(match, 2); | |
448 | if (mstr && sr_atoi(mstr, &exp) == SR_OK) { | |
449 | devc->cur_digits[i] = MIN(4 - exp, devc->cur_digits[i]); | |
450 | devc->cur_encoding[i] = MIN(5 - exp, devc->cur_encoding[i]); | |
451 | } | |
452 | g_free(mstr); | |
453 | } | |
454 | ||
455 | sr_analog_init(&analog, &encoding, &meaning, &spec, | |
456 | devc->cur_digits[i] - devc->cur_exponent[i]); | |
457 | analog.meaning->mq = devc->cur_mq[i]; | |
458 | analog.meaning->unit = devc->cur_unit[i]; | |
459 | analog.meaning->mqflags = devc->cur_mqflags[i]; | |
460 | analog.meaning->channels = g_slist_append(NULL, devc->cur_channel); | |
461 | analog.num_samples = 1; | |
462 | analog.data = &fvalue; | |
463 | encoding.digits = devc->cur_encoding[i] - devc->cur_exponent[i]; | |
464 | packet.type = SR_DF_ANALOG; | |
465 | packet.payload = &analog; | |
466 | sr_session_send(sdi, &packet); | |
467 | g_slist_free(analog.meaning->channels); | |
468 | ||
469 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
470 | ||
471 | skip_value:; | |
472 | struct sr_channel *prev_chan = devc->cur_channel; | |
473 | devc->cur_channel = sr_next_enabled_channel(sdi, devc->cur_channel); | |
474 | if (devc->cur_channel->index > prev_chan->index) | |
475 | return JOB_AGAIN; | |
476 | else | |
477 | return JOB_FETC; | |
478 | } | |
479 | ||
480 | static int send_conf(const struct sr_dev_inst *sdi) | |
481 | { | |
482 | struct dev_context *devc = sdi->priv; | |
483 | ||
484 | /* Do not try to send CONF? for internal temperature channel. */ | |
485 | if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1)) | |
486 | return SR_ERR_NA; | |
487 | ||
488 | if (devc->cur_conf->index > 0) | |
489 | return agdmm_send(sdi, "CONF? @%d", devc->cur_conf->index + 1); | |
490 | else | |
491 | return agdmm_send(sdi, "CONF?"); | |
492 | } | |
493 | ||
494 | static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
495 | { | |
496 | struct dev_context *devc; | |
497 | char *mstr, *rstr; | |
498 | int i, resolution; | |
499 | ||
500 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); | |
501 | devc = sdi->priv; | |
502 | i = devc->cur_conf->index; | |
503 | ||
504 | rstr = g_match_info_fetch(match, 2); | |
505 | if (rstr) | |
506 | sr_atoi(rstr, &resolution); | |
507 | g_free(rstr); | |
508 | ||
509 | mstr = g_match_info_fetch(match, 1); | |
510 | if (!strcmp(mstr, "V")) { | |
511 | devc->cur_mq[i] = SR_MQ_VOLTAGE; | |
512 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
513 | devc->cur_mqflags[i] = 0; | |
514 | devc->cur_exponent[i] = 0; | |
515 | devc->cur_digits[i] = 4 - resolution; | |
516 | } else if (!strcmp(mstr, "MV")) { | |
517 | if (devc->mode_tempaux) { | |
518 | devc->cur_mq[i] = SR_MQ_TEMPERATURE; | |
519 | /* No way to detect whether Fahrenheit or Celsius | |
520 | * is used, so we'll just default to Celsius. */ | |
521 | devc->cur_unit[i] = SR_UNIT_CELSIUS; | |
522 | devc->cur_mqflags[i] = 0; | |
523 | devc->cur_exponent[i] = 0; | |
524 | devc->cur_digits[i] = 1; | |
525 | } else { | |
526 | devc->cur_mq[i] = SR_MQ_VOLTAGE; | |
527 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
528 | devc->cur_mqflags[i] = 0; | |
529 | devc->cur_exponent[i] = -3; | |
530 | devc->cur_digits[i] = 5 - resolution; | |
531 | } | |
532 | } else if (!strcmp(mstr, "A")) { | |
533 | devc->cur_mq[i] = SR_MQ_CURRENT; | |
534 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
535 | devc->cur_mqflags[i] = 0; | |
536 | devc->cur_exponent[i] = 0; | |
537 | devc->cur_digits[i] = 3 - resolution; | |
538 | } else if (!strcmp(mstr, "UA")) { | |
539 | devc->cur_mq[i] = SR_MQ_CURRENT; | |
540 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
541 | devc->cur_mqflags[i] = 0; | |
542 | devc->cur_exponent[i] = -6; | |
543 | devc->cur_digits[i] = 8 - resolution; | |
544 | } else if (!strcmp(mstr, "FREQ")) { | |
545 | devc->cur_mq[i] = SR_MQ_FREQUENCY; | |
546 | devc->cur_unit[i] = SR_UNIT_HERTZ; | |
547 | devc->cur_mqflags[i] = 0; | |
548 | devc->cur_exponent[i] = 0; | |
549 | devc->cur_digits[i] = 2 - resolution; | |
550 | } else if (!strcmp(mstr, "RES")) { | |
551 | if (devc->mode_continuity) { | |
552 | devc->cur_mq[i] = SR_MQ_CONTINUITY; | |
553 | devc->cur_unit[i] = SR_UNIT_BOOLEAN; | |
554 | } else { | |
555 | devc->cur_mq[i] = SR_MQ_RESISTANCE; | |
556 | devc->cur_unit[i] = SR_UNIT_OHM; | |
557 | } | |
558 | devc->cur_mqflags[i] = 0; | |
559 | devc->cur_exponent[i] = 0; | |
560 | devc->cur_digits[i] = 1 - resolution; | |
561 | } else if (!strcmp(mstr, "DIOD")) { | |
562 | devc->cur_mq[i] = SR_MQ_VOLTAGE; | |
563 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
564 | devc->cur_mqflags[i] = SR_MQFLAG_DIODE; | |
565 | devc->cur_exponent[i] = 0; | |
566 | devc->cur_digits[i] = 3; | |
567 | } else if (!strcmp(mstr, "CAP")) { | |
568 | devc->cur_mq[i] = SR_MQ_CAPACITANCE; | |
569 | devc->cur_unit[i] = SR_UNIT_FARAD; | |
570 | devc->cur_mqflags[i] = 0; | |
571 | devc->cur_exponent[i] = 0; | |
572 | devc->cur_digits[i] = 9 - resolution; | |
573 | } else | |
574 | sr_dbg("Unknown first argument."); | |
575 | g_free(mstr); | |
576 | ||
577 | /* This is based on guess, supposing similarity with other models. */ | |
578 | devc->cur_encoding[i] = devc->cur_digits[i] + 1; | |
579 | ||
580 | if (g_match_info_get_match_count(match) == 4) { | |
581 | mstr = g_match_info_fetch(match, 3); | |
582 | /* Third value, if present, is always AC or DC. */ | |
583 | if (!strcmp(mstr, "AC")) { | |
584 | devc->cur_mqflags[i] |= SR_MQFLAG_AC; | |
585 | if (devc->cur_mq[i] == SR_MQ_VOLTAGE) | |
586 | devc->cur_mqflags[i] |= SR_MQFLAG_RMS; | |
587 | } else if (!strcmp(mstr, "DC")) { | |
588 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; | |
589 | } else { | |
590 | sr_dbg("Unknown first argument '%s'.", mstr); | |
591 | } | |
592 | g_free(mstr); | |
593 | } else | |
594 | devc->cur_mqflags[i] &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC); | |
595 | ||
596 | return JOB_CONF; | |
597 | } | |
598 | ||
599 | static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
600 | { | |
601 | struct dev_context *devc; | |
602 | char *mstr, *rstr, *m2; | |
603 | int i, resolution; | |
604 | ||
605 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); | |
606 | devc = sdi->priv; | |
607 | i = devc->cur_conf->index; | |
608 | ||
609 | devc->mode_squarewave = 0; | |
610 | ||
611 | rstr = g_match_info_fetch(match, 4); | |
612 | if (rstr && sr_atoi(rstr, &resolution) == SR_OK) { | |
613 | devc->cur_digits[i] = -resolution; | |
614 | devc->cur_encoding[i] = -resolution + 1; | |
615 | } | |
616 | g_free(rstr); | |
617 | ||
618 | mstr = g_match_info_fetch(match, 1); | |
619 | if (!strncmp(mstr, "VOLT", 4)) { | |
620 | devc->cur_mq[i] = SR_MQ_VOLTAGE; | |
621 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
622 | devc->cur_mqflags[i] = 0; | |
623 | devc->cur_exponent[i] = 0; | |
624 | if (i == 0 && devc->mode_dbm_dbv) { | |
625 | devc->cur_unit[i] = devc->mode_dbm_dbv; | |
626 | devc->cur_digits[i] = 3; | |
627 | devc->cur_encoding[i] = 4; | |
628 | } | |
629 | if (mstr[4] == ':') { | |
630 | if (!strncmp(mstr + 5, "ACDC", 4)) { | |
631 | /* AC + DC offset */ | |
632 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; | |
633 | } else if (!strncmp(mstr + 5, "AC", 2)) { | |
634 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
635 | } else if (!strncmp(mstr + 5, "DC", 2)) { | |
636 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; | |
637 | } | |
638 | } else | |
639 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; | |
640 | } else if (!strncmp(mstr, "CURR", 4)) { | |
641 | devc->cur_mq[i] = SR_MQ_CURRENT; | |
642 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
643 | devc->cur_mqflags[i] = 0; | |
644 | devc->cur_exponent[i] = 0; | |
645 | if (mstr[4] == ':') { | |
646 | if (!strncmp(mstr + 5, "ACDC", 4)) { | |
647 | /* AC + DC offset */ | |
648 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; | |
649 | } else if (!strncmp(mstr + 5, "AC", 2)) { | |
650 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS; | |
651 | } else if (!strncmp(mstr + 5, "DC", 2)) { | |
652 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; | |
653 | } | |
654 | } else | |
655 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; | |
656 | } else if (!strcmp(mstr, "RES")) { | |
657 | devc->cur_mq[i] = SR_MQ_RESISTANCE; | |
658 | devc->cur_unit[i] = SR_UNIT_OHM; | |
659 | devc->cur_mqflags[i] = 0; | |
660 | devc->cur_exponent[i] = 0; | |
661 | } else if (!strcmp(mstr, "COND")) { | |
662 | devc->cur_mq[i] = SR_MQ_CONDUCTANCE; | |
663 | devc->cur_unit[i] = SR_UNIT_SIEMENS; | |
664 | devc->cur_mqflags[i] = 0; | |
665 | devc->cur_exponent[i] = 0; | |
666 | } else if (!strcmp(mstr, "CAP")) { | |
667 | devc->cur_mq[i] = SR_MQ_CAPACITANCE; | |
668 | devc->cur_unit[i] = SR_UNIT_FARAD; | |
669 | devc->cur_mqflags[i] = 0; | |
670 | devc->cur_exponent[i] = 0; | |
671 | } else if (!strncmp(mstr, "FREQ", 4) || !strncmp(mstr, "FC1", 3)) { | |
672 | devc->cur_mq[i] = SR_MQ_FREQUENCY; | |
673 | devc->cur_unit[i] = SR_UNIT_HERTZ; | |
674 | devc->cur_mqflags[i] = 0; | |
675 | devc->cur_exponent[i] = 0; | |
676 | } else if (!strncmp(mstr, "PULS:PWID", 9)) { | |
677 | devc->cur_mq[i] = SR_MQ_PULSE_WIDTH; | |
678 | devc->cur_unit[i] = SR_UNIT_SECOND; | |
679 | devc->cur_mqflags[i] = 0; | |
680 | devc->cur_exponent[i] = 0; | |
681 | devc->cur_encoding[i] = MIN(devc->cur_encoding[i], 6); | |
682 | } else if (!strncmp(mstr, "PULS:PDUT", 9)) { | |
683 | devc->cur_mq[i] = SR_MQ_DUTY_CYCLE; | |
684 | devc->cur_unit[i] = SR_UNIT_PERCENTAGE; | |
685 | devc->cur_mqflags[i] = 0; | |
686 | devc->cur_exponent[i] = 0; | |
687 | devc->cur_digits[i] = 3; | |
688 | devc->cur_encoding[i] = 4; | |
689 | } else if (!strcmp(mstr, "CONT")) { | |
690 | devc->cur_mq[i] = SR_MQ_CONTINUITY; | |
691 | devc->cur_unit[i] = SR_UNIT_OHM; | |
692 | devc->cur_mqflags[i] = 0; | |
693 | devc->cur_exponent[i] = 0; | |
694 | } else if (!strcmp(mstr, "DIOD")) { | |
695 | devc->cur_mq[i] = SR_MQ_VOLTAGE; | |
696 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
697 | devc->cur_mqflags[i] = SR_MQFLAG_DIODE; | |
698 | devc->cur_exponent[i] = 0; | |
699 | devc->cur_digits[i] = 4; | |
700 | devc->cur_encoding[i] = 5; | |
701 | } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2) || | |
702 | !strncmp(mstr, "TEMP", 2)) { | |
703 | devc->cur_mq[i] = SR_MQ_TEMPERATURE; | |
704 | m2 = g_match_info_fetch(match, 2); | |
705 | if (!m2) | |
706 | /* | |
707 | * TEMP without param is for secondary display (channel P2) | |
708 | * and is identical to channel P3, so discard it. | |
709 | */ | |
710 | devc->cur_mq[i] = -1; | |
711 | else if (!strcmp(m2, "FAR")) | |
712 | devc->cur_unit[i] = SR_UNIT_FAHRENHEIT; | |
713 | else | |
714 | devc->cur_unit[i] = SR_UNIT_CELSIUS; | |
715 | g_free(m2); | |
716 | devc->cur_mqflags[i] = 0; | |
717 | devc->cur_exponent[i] = 0; | |
718 | devc->cur_digits[i] = 1; | |
719 | devc->cur_encoding[i] = 2; | |
720 | } else if (!strcmp(mstr, "SCOU")) { | |
721 | /* | |
722 | * Switch counter, not supported. Not sure what values | |
723 | * come from FETC in this mode, or how they would map | |
724 | * into libsigrok. | |
725 | */ | |
726 | } else if (!strncmp(mstr, "CPER:", 5)) { | |
727 | devc->cur_mq[i] = SR_MQ_CURRENT; | |
728 | devc->cur_unit[i] = SR_UNIT_PERCENTAGE; | |
729 | devc->cur_mqflags[i] = 0; | |
730 | devc->cur_exponent[i] = 0; | |
731 | devc->cur_digits[i] = 2; | |
732 | devc->cur_encoding[i] = 3; | |
733 | } else if (!strcmp(mstr, "SQU")) { | |
734 | /* | |
735 | * Square wave output, not supported. FETC just return | |
736 | * an error in this mode, so don't even call it. | |
737 | */ | |
738 | devc->mode_squarewave = 1; | |
739 | } else { | |
740 | sr_dbg("Unknown first argument '%s'.", mstr); | |
741 | } | |
742 | g_free(mstr); | |
743 | ||
744 | struct sr_channel *prev_conf = devc->cur_conf; | |
745 | devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf); | |
746 | if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1)) | |
747 | devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf); | |
748 | if (devc->cur_conf->index > prev_conf->index) | |
749 | return JOB_AGAIN; | |
750 | else | |
751 | return JOB_CONF; | |
752 | } | |
753 | ||
754 | /* This comes in whenever the rotary switch is changed to a new position. | |
755 | * We could use it to determine the major measurement mode, but we already | |
756 | * have the output of CONF? for that, which is more detailed. However | |
757 | * we do need to catch this here, or it'll show up in some other output. */ | |
758 | static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
759 | { | |
760 | struct dev_context *devc = sdi->priv; | |
761 | ||
762 | sr_spew("Switch '%s'.", g_match_info_get_string(match)); | |
763 | ||
764 | devc->current_job = 0; | |
765 | devc->job_running = FALSE; | |
766 | memset(devc->jobs_start, 0, sizeof(devc->jobs_start)); | |
767 | devc->cur_mq[0] = -1; | |
768 | if (devc->profile->nb_channels > 2) | |
769 | devc->cur_mq[1] = -1; | |
770 | ||
771 | return SR_OK; | |
772 | } | |
773 | ||
774 | /* Poll CONF/STAT at 1Hz and values at samplerate. */ | |
775 | SR_PRIV const struct agdmm_job agdmm_jobs_u12xx[] = { | |
776 | { JOB_FETC, SAMPLERATE_INTERVAL, send_fetc }, | |
777 | { JOB_CONF, 1000, send_conf }, | |
778 | { JOB_STAT, 1000, send_stat }, | |
779 | ALL_ZERO | |
780 | }; | |
781 | ||
782 | SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = { | |
783 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x }, | |
784 | { "^\\*([0-9])$", recv_switch }, | |
785 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
786 | { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x }, | |
787 | { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x}, | |
788 | { "^\"(DIOD)\"$", recv_conf_u123x }, | |
789 | ALL_ZERO | |
790 | }; | |
791 | ||
792 | SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = { | |
793 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x }, | |
794 | { "^\\*([0-9])$", recv_switch }, | |
795 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
796 | { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
797 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
798 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
799 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
800 | { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, | |
801 | { "^\"(DIOD)\"$", recv_conf_u124x_5x }, | |
802 | ALL_ZERO | |
803 | }; | |
804 | ||
805 | SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = { | |
806 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x }, | |
807 | { "^\\*([0-9])$", recv_switch }, | |
808 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
809 | { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
810 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
811 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
812 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
813 | { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, | |
814 | { "^\"(DIOD)\"$", recv_conf_u124x_5x }, | |
815 | ALL_ZERO | |
816 | }; | |
817 | ||
818 | SR_PRIV const struct agdmm_recv agdmm_recvs_u128x[] = { | |
819 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u128x }, | |
820 | { "^\\*([0-9])$", recv_switch }, | |
821 | { "^([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))$", recv_fetc }, | |
822 | { "^\"(VOLT|CURR|RES|CONT|COND|CAP|FREQ|FC1|FC100) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
823 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
824 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
825 | { "^\"(FREQ:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
826 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
827 | { "^\"(PULS:PWID|PULS:PWID:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
828 | { "^\"(TEMP:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, | |
829 | { "^\"(DIOD|SQU|PULS:PDUT|TEMP)\"$", recv_conf_u124x_5x }, | |
830 | ALL_ZERO | |
831 | }; |