]>
Commit | Line | Data |
---|---|---|
e93cdf42 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
e93cdf42 BV |
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 | ||
6ec6c43b | 20 | #include <config.h> |
e93cdf42 | 21 | #include <glib.h> |
e93cdf42 | 22 | #include <stdlib.h> |
d822726d | 23 | #include <stdarg.h> |
e93cdf42 | 24 | #include <string.h> |
e93cdf42 | 25 | #include <math.h> |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
515ab088 | 27 | #include "libsigrok-internal.h" |
6cf1a87b | 28 | #include "protocol.h" |
e93cdf42 | 29 | |
e93cdf42 BV |
30 | static void dispatch(const struct sr_dev_inst *sdi) |
31 | { | |
32 | struct dev_context *devc; | |
33 | const struct agdmm_job *jobs; | |
34 | int64_t now; | |
35 | int i; | |
36 | ||
37 | devc = sdi->priv; | |
38 | jobs = devc->profile->jobs; | |
39 | now = g_get_monotonic_time() / 1000; | |
40 | for (i = 0; (&jobs[i])->interval; i++) { | |
41 | if (now - devc->jobqueue[i] > (&jobs[i])->interval) { | |
38d326e8 | 42 | sr_spew("Running job %d.", i); |
e93cdf42 BV |
43 | (&jobs[i])->send(sdi); |
44 | devc->jobqueue[i] = now; | |
45 | } | |
46 | } | |
e93cdf42 BV |
47 | } |
48 | ||
49 | static void receive_line(const struct sr_dev_inst *sdi) | |
50 | { | |
51 | struct dev_context *devc; | |
52 | const struct agdmm_recv *recvs, *recv; | |
53 | GRegex *reg; | |
54 | GMatchInfo *match; | |
55 | int i; | |
56 | ||
57 | devc = sdi->priv; | |
58 | ||
59 | /* Strip CRLF */ | |
60 | while (devc->buflen) { | |
61 | if (*(devc->buf + devc->buflen - 1) == '\r' | |
62 | || *(devc->buf + devc->buflen - 1) == '\n') | |
63 | *(devc->buf + --devc->buflen) = '\0'; | |
64 | else | |
65 | break; | |
66 | } | |
38d326e8 | 67 | sr_spew("Received '%s'.", devc->buf); |
e93cdf42 BV |
68 | |
69 | recv = NULL; | |
70 | recvs = devc->profile->recvs; | |
71 | for (i = 0; (&recvs[i])->recv_regex; i++) { | |
72 | reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL); | |
73 | if (g_regex_match(reg, (char *)devc->buf, 0, &match)) { | |
74 | recv = &recvs[i]; | |
75 | break; | |
76 | } | |
77 | g_match_info_unref(match); | |
78 | g_regex_unref(reg); | |
79 | } | |
80 | if (recv) { | |
81 | recv->recv(sdi, match); | |
82 | g_match_info_unref(match); | |
83 | g_regex_unref(reg); | |
f2e86bbf | 84 | } else |
38d326e8 | 85 | sr_dbg("Unknown line '%s'.", devc->buf); |
e93cdf42 BV |
86 | |
87 | /* Done with this. */ | |
88 | devc->buflen = 0; | |
e93cdf42 BV |
89 | } |
90 | ||
91 | SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data) | |
92 | { | |
642e9d62 | 93 | struct sr_dev_inst *sdi; |
e93cdf42 | 94 | struct dev_context *devc; |
fb3a1505 | 95 | struct sr_serial_dev_inst *serial; |
e93cdf42 BV |
96 | int len; |
97 | ||
109a3ba4 BV |
98 | (void)fd; |
99 | ||
e93cdf42 BV |
100 | if (!(sdi = cb_data)) |
101 | return TRUE; | |
102 | ||
103 | if (!(devc = sdi->priv)) | |
104 | return TRUE; | |
105 | ||
fb3a1505 | 106 | serial = sdi->conn; |
e93cdf42 BV |
107 | if (revents == G_IO_IN) { |
108 | /* Serial data arrived. */ | |
0c5f2abc | 109 | while (AGDMM_BUFSIZE - devc->buflen - 1 > 0) { |
e0b781a4 | 110 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); |
f2e86bbf BV |
111 | if (len < 1) |
112 | break; | |
113 | devc->buflen += len; | |
114 | *(devc->buf + devc->buflen) = '\0'; | |
115 | if (*(devc->buf + devc->buflen - 1) == '\n') { | |
116 | /* End of line */ | |
117 | receive_line(sdi); | |
118 | break; | |
e93cdf42 BV |
119 | } |
120 | } | |
121 | } | |
122 | ||
123 | dispatch(sdi); | |
124 | ||
5b6829ea | 125 | if (sr_sw_limits_check(&devc->limits)) |
695dc859 | 126 | sdi->driver->dev_acquisition_stop(sdi); |
e93cdf42 BV |
127 | |
128 | return TRUE; | |
129 | } | |
130 | ||
d822726d | 131 | static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd, ...) |
e93cdf42 | 132 | { |
fb3a1505 | 133 | struct sr_serial_dev_inst *serial; |
d822726d | 134 | va_list args; |
e93cdf42 BV |
135 | char buf[32]; |
136 | ||
fb3a1505 BV |
137 | serial = sdi->conn; |
138 | ||
d822726d AJ |
139 | va_start(args, cmd); |
140 | vsnprintf(buf, sizeof(buf)-3, cmd, args); | |
141 | va_end(args); | |
142 | sr_spew("Sending '%s'.", buf); | |
e93cdf42 | 143 | if (!strncmp(buf, "*IDN?", 5)) |
90486ba8 | 144 | strcat(buf, "\r\n"); |
e93cdf42 | 145 | else |
90486ba8 | 146 | strcat(buf, "\n\r\n"); |
95779b43 | 147 | if (serial_write_blocking(serial, buf, strlen(buf), SERIAL_WRITE_TIMEOUT_MS) < (int)strlen(buf)) { |
081c214e | 148 | sr_err("Failed to send."); |
e93cdf42 BV |
149 | return SR_ERR; |
150 | } | |
ce4d26dd | 151 | |
e93cdf42 BV |
152 | return SR_OK; |
153 | } | |
154 | ||
a4394fb3 | 155 | static int send_stat(const struct sr_dev_inst *sdi) |
e93cdf42 | 156 | { |
e93cdf42 BV |
157 | return agdmm_send(sdi, "STAT?"); |
158 | } | |
159 | ||
8c0152f2 | 160 | static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 | 161 | { |
e6b021f3 BV |
162 | struct dev_context *devc; |
163 | char *s; | |
164 | ||
165 | devc = sdi->priv; | |
166 | s = g_match_info_fetch(match, 1); | |
38d326e8 | 167 | sr_spew("STAT response '%s'.", s); |
e6b021f3 BV |
168 | |
169 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
170 | * set both flags to denote it's not a normal measurement. */ | |
171 | if (s[0] == '1') | |
d822726d | 172 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN; |
e6b021f3 | 173 | else |
d822726d | 174 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN); |
e93cdf42 | 175 | |
e6b021f3 | 176 | if (s[1] == '1') |
d822726d | 177 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; |
e6b021f3 | 178 | else |
d822726d | 179 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; |
e6b021f3 BV |
180 | |
181 | /* Triggered or auto hold modes. */ | |
182 | if (s[2] == '1' || s[3] == '1') | |
d822726d | 183 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; |
e6b021f3 | 184 | else |
d822726d | 185 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; |
e6b021f3 BV |
186 | |
187 | /* Temp/aux mode. */ | |
188 | if (s[7] == '1') | |
189 | devc->mode_tempaux = TRUE; | |
190 | else | |
191 | devc->mode_tempaux = FALSE; | |
192 | ||
38d326e8 | 193 | /* Continuity mode. */ |
e6b021f3 BV |
194 | if (s[16] == '1') |
195 | devc->mode_continuity = TRUE; | |
196 | else | |
197 | devc->mode_continuity = FALSE; | |
198 | ||
199 | g_free(s); | |
e93cdf42 BV |
200 | |
201 | return SR_OK; | |
202 | } | |
203 | ||
173378f0 BV |
204 | static int recv_stat_u124x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
205 | { | |
206 | struct dev_context *devc; | |
207 | char *s; | |
208 | ||
209 | devc = sdi->priv; | |
210 | s = g_match_info_fetch(match, 1); | |
211 | sr_spew("STAT response '%s'.", s); | |
212 | ||
213 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
214 | * set both flags to denote it's not a normal measurement. */ | |
215 | if (s[0] == '1') | |
d822726d | 216 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN; |
173378f0 | 217 | else |
d822726d | 218 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN); |
173378f0 BV |
219 | |
220 | if (s[1] == '1') | |
d822726d | 221 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; |
173378f0 | 222 | else |
d822726d | 223 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; |
173378f0 BV |
224 | |
225 | /* Hold mode. */ | |
226 | if (s[7] == '1') | |
d822726d | 227 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; |
173378f0 | 228 | else |
d822726d | 229 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; |
173378f0 BV |
230 | |
231 | g_free(s); | |
232 | ||
233 | return SR_OK; | |
234 | } | |
235 | ||
8c0152f2 BV |
236 | static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
237 | { | |
238 | struct dev_context *devc; | |
239 | char *s; | |
240 | ||
241 | devc = sdi->priv; | |
242 | s = g_match_info_fetch(match, 1); | |
38d326e8 | 243 | sr_spew("STAT response '%s'.", s); |
8c0152f2 BV |
244 | |
245 | /* Peak hold mode. */ | |
246 | if (s[4] == '1') | |
d822726d | 247 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX; |
8c0152f2 | 248 | else |
d822726d | 249 | devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX; |
8c0152f2 BV |
250 | |
251 | /* Triggered hold mode. */ | |
252 | if (s[7] == '1') | |
d822726d | 253 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; |
8c0152f2 | 254 | else |
d822726d | 255 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; |
8c0152f2 BV |
256 | |
257 | g_free(s); | |
258 | ||
259 | return SR_OK; | |
260 | } | |
261 | ||
43185ed3 AJ |
262 | static int recv_stat_u128x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
263 | { | |
264 | struct dev_context *devc; | |
265 | char *s; | |
266 | ||
267 | devc = sdi->priv; | |
268 | s = g_match_info_fetch(match, 1); | |
269 | sr_spew("STAT response '%s'.", s); | |
270 | ||
271 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
272 | * set both flags to denote it's not a normal measurement. */ | |
273 | if (s[0] == '1') | |
d822726d | 274 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG; |
43185ed3 | 275 | else |
d822726d | 276 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG); |
43185ed3 AJ |
277 | |
278 | /* Peak hold mode. */ | |
279 | if (s[4] == '4') | |
d822726d | 280 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX; |
43185ed3 | 281 | else |
d822726d | 282 | devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX; |
43185ed3 AJ |
283 | |
284 | /* Null function. */ | |
285 | if (s[1] == '1') | |
d822726d | 286 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; |
43185ed3 | 287 | else |
d822726d | 288 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; |
43185ed3 AJ |
289 | |
290 | /* Triggered or auto hold modes. */ | |
291 | if (s[7] == '1' || s[11] == '1') | |
d822726d | 292 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; |
43185ed3 | 293 | else |
d822726d | 294 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; |
43185ed3 AJ |
295 | |
296 | g_free(s); | |
297 | ||
298 | return SR_OK; | |
299 | } | |
300 | ||
a4394fb3 | 301 | static int send_fetc(const struct sr_dev_inst *sdi) |
e93cdf42 | 302 | { |
43185ed3 AJ |
303 | struct dev_context *devc; |
304 | devc = sdi->priv; | |
305 | if (devc->mode_squarewave) | |
306 | return SR_OK; | |
d822726d AJ |
307 | devc->cur_channel = sr_next_enabled_channel(sdi, devc->cur_channel); |
308 | if (devc->cur_channel->index > 0) | |
309 | return agdmm_send(sdi, "FETC? @%d", devc->cur_channel->index + 1); | |
310 | else | |
311 | return agdmm_send(sdi, "FETC?"); | |
e93cdf42 BV |
312 | } |
313 | ||
a4394fb3 | 314 | static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 BV |
315 | { |
316 | struct dev_context *devc; | |
317 | struct sr_datafeed_packet packet; | |
16aef676 UH |
318 | struct sr_datafeed_analog analog; |
319 | struct sr_analog_encoding encoding; | |
320 | struct sr_analog_meaning meaning; | |
321 | struct sr_analog_spec spec; | |
e93cdf42 | 322 | float fvalue; |
f216eb86 | 323 | const char *s; |
fe9d5abe | 324 | char *mstr; |
d822726d | 325 | int i; |
e93cdf42 | 326 | |
38d326e8 | 327 | sr_spew("FETC reply '%s'.", g_match_info_get_string(match)); |
e93cdf42 | 328 | devc = sdi->priv; |
d822726d | 329 | i = devc->cur_channel->index; |
e93cdf42 | 330 | |
d822726d | 331 | if (devc->cur_mq[i] == -1) |
e93cdf42 BV |
332 | /* Haven't seen configuration yet, so can't know what |
333 | * the fetched float means. Not really an error, we'll | |
334 | * get metadata soon enough. */ | |
335 | return SR_OK; | |
336 | ||
f216eb86 BV |
337 | s = g_match_info_get_string(match); |
338 | if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) { | |
74ac7d7f | 339 | /* An invalid measurement shows up on the display as "O.L", but |
e93cdf42 BV |
340 | * comes through like this. Since comparing 38-digit floats |
341 | * is rather problematic, we'll cut through this here. */ | |
342 | fvalue = NAN; | |
343 | } else { | |
344 | mstr = g_match_info_fetch(match, 1); | |
7c03b564 | 345 | if (sr_atof_ascii(mstr, &fvalue) != SR_OK) { |
fe9d5abe | 346 | g_free(mstr); |
51b92b7d | 347 | sr_dbg("Invalid float."); |
e93cdf42 BV |
348 | return SR_ERR; |
349 | } | |
fe9d5abe | 350 | g_free(mstr); |
d822726d AJ |
351 | if (devc->cur_exponent[i] != 0) |
352 | fvalue *= powf(10, devc->cur_exponent[i]); | |
e93cdf42 BV |
353 | } |
354 | ||
4435966e | 355 | sr_analog_init(&analog, &encoding, &meaning, &spec, |
d822726d AJ |
356 | devc->cur_digits[i] - devc->cur_exponent[i]); |
357 | analog.meaning->mq = devc->cur_mq[i]; | |
358 | analog.meaning->unit = devc->cur_unit[i]; | |
359 | analog.meaning->mqflags = devc->cur_mqflags[i]; | |
360 | analog.meaning->channels = g_slist_append(NULL, devc->cur_channel); | |
e93cdf42 BV |
361 | analog.num_samples = 1; |
362 | analog.data = &fvalue; | |
d822726d | 363 | encoding.digits = devc->cur_encoding[i] - devc->cur_exponent[i]; |
16aef676 | 364 | packet.type = SR_DF_ANALOG; |
e93cdf42 | 365 | packet.payload = &analog; |
695dc859 | 366 | sr_session_send(sdi, &packet); |
d822726d | 367 | g_slist_free(analog.meaning->channels); |
e93cdf42 | 368 | |
5b6829ea | 369 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
e93cdf42 BV |
370 | |
371 | return SR_OK; | |
372 | } | |
373 | ||
a4394fb3 | 374 | static int send_conf(const struct sr_dev_inst *sdi) |
e93cdf42 | 375 | { |
d822726d AJ |
376 | struct dev_context *devc = sdi->priv; |
377 | ||
378 | devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf); | |
379 | ||
380 | /* Do not try to send CONF? for internal temperature channel. */ | |
381 | if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1)) | |
382 | devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf); | |
383 | if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1)) | |
384 | return SR_ERR_NA; | |
385 | ||
386 | if (devc->cur_conf->index > 0) | |
387 | return agdmm_send(sdi, "CONF? @%d", devc->cur_conf->index + 1); | |
388 | else | |
389 | return agdmm_send(sdi, "CONF?"); | |
e93cdf42 BV |
390 | } |
391 | ||
a4394fb3 | 392 | static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 BV |
393 | { |
394 | struct dev_context *devc; | |
4435966e | 395 | char *mstr, *rstr; |
d822726d | 396 | int i, resolution; |
e93cdf42 | 397 | |
38d326e8 | 398 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
e93cdf42 | 399 | devc = sdi->priv; |
d822726d | 400 | i = devc->cur_conf->index; |
4435966e AJ |
401 | |
402 | rstr = g_match_info_fetch(match, 2); | |
403 | if (rstr) | |
404 | sr_atoi(rstr, &resolution); | |
405 | g_free(rstr); | |
406 | ||
e93cdf42 BV |
407 | mstr = g_match_info_fetch(match, 1); |
408 | if (!strcmp(mstr, "V")) { | |
d822726d AJ |
409 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
410 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
411 | devc->cur_mqflags[i] = 0; | |
412 | devc->cur_exponent[i] = 0; | |
413 | devc->cur_digits[i] = 4 - resolution; | |
0c5f2abc | 414 | } else if (!strcmp(mstr, "MV")) { |
e6b021f3 | 415 | if (devc->mode_tempaux) { |
d822726d | 416 | devc->cur_mq[i] = SR_MQ_TEMPERATURE; |
f3f19d11 UH |
417 | /* No way to detect whether Fahrenheit or Celsius |
418 | * is used, so we'll just default to Celsius. */ | |
d822726d AJ |
419 | devc->cur_unit[i] = SR_UNIT_CELSIUS; |
420 | devc->cur_mqflags[i] = 0; | |
421 | devc->cur_exponent[i] = 0; | |
422 | devc->cur_digits[i] = 1; | |
e6b021f3 | 423 | } else { |
d822726d AJ |
424 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
425 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
426 | devc->cur_mqflags[i] = 0; | |
427 | devc->cur_exponent[i] = -3; | |
428 | devc->cur_digits[i] = 5 - resolution; | |
e6b021f3 | 429 | } |
0c5f2abc | 430 | } else if (!strcmp(mstr, "A")) { |
d822726d AJ |
431 | devc->cur_mq[i] = SR_MQ_CURRENT; |
432 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
433 | devc->cur_mqflags[i] = 0; | |
434 | devc->cur_exponent[i] = 0; | |
435 | devc->cur_digits[i] = 3 - resolution; | |
0c5f2abc | 436 | } else if (!strcmp(mstr, "UA")) { |
d822726d AJ |
437 | devc->cur_mq[i] = SR_MQ_CURRENT; |
438 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
439 | devc->cur_mqflags[i] = 0; | |
440 | devc->cur_exponent[i] = -6; | |
441 | devc->cur_digits[i] = 8 - resolution; | |
0c5f2abc | 442 | } else if (!strcmp(mstr, "FREQ")) { |
d822726d AJ |
443 | devc->cur_mq[i] = SR_MQ_FREQUENCY; |
444 | devc->cur_unit[i] = SR_UNIT_HERTZ; | |
445 | devc->cur_mqflags[i] = 0; | |
446 | devc->cur_exponent[i] = 0; | |
447 | devc->cur_digits[i] = 2 - resolution; | |
0c5f2abc | 448 | } else if (!strcmp(mstr, "RES")) { |
e6b021f3 | 449 | if (devc->mode_continuity) { |
d822726d AJ |
450 | devc->cur_mq[i] = SR_MQ_CONTINUITY; |
451 | devc->cur_unit[i] = SR_UNIT_BOOLEAN; | |
e6b021f3 | 452 | } else { |
d822726d AJ |
453 | devc->cur_mq[i] = SR_MQ_RESISTANCE; |
454 | devc->cur_unit[i] = SR_UNIT_OHM; | |
e6b021f3 | 455 | } |
d822726d AJ |
456 | devc->cur_mqflags[i] = 0; |
457 | devc->cur_exponent[i] = 0; | |
458 | devc->cur_digits[i] = 1 - resolution; | |
873c0d11 | 459 | } else if (!strcmp(mstr, "DIOD")) { |
d822726d AJ |
460 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
461 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
462 | devc->cur_mqflags[i] = SR_MQFLAG_DIODE; | |
463 | devc->cur_exponent[i] = 0; | |
464 | devc->cur_digits[i] = 3; | |
0c5f2abc | 465 | } else if (!strcmp(mstr, "CAP")) { |
d822726d AJ |
466 | devc->cur_mq[i] = SR_MQ_CAPACITANCE; |
467 | devc->cur_unit[i] = SR_UNIT_FARAD; | |
468 | devc->cur_mqflags[i] = 0; | |
469 | devc->cur_exponent[i] = 0; | |
470 | devc->cur_digits[i] = 9 - resolution; | |
e93cdf42 | 471 | } else |
38d326e8 | 472 | sr_dbg("Unknown first argument."); |
e93cdf42 BV |
473 | g_free(mstr); |
474 | ||
4435966e | 475 | /* This is based on guess, supposing similarity with other models. */ |
d822726d | 476 | devc->cur_encoding[i] = devc->cur_digits[i] + 1; |
4435966e | 477 | |
e066c32a BV |
478 | if (g_match_info_get_match_count(match) == 4) { |
479 | mstr = g_match_info_fetch(match, 3); | |
e93cdf42 | 480 | /* Third value, if present, is always AC or DC. */ |
51b92b7d | 481 | if (!strcmp(mstr, "AC")) { |
d822726d AJ |
482 | devc->cur_mqflags[i] |= SR_MQFLAG_AC; |
483 | if (devc->cur_mq[i] == SR_MQ_VOLTAGE) | |
484 | devc->cur_mqflags[i] |= SR_MQFLAG_RMS; | |
51b92b7d | 485 | } else if (!strcmp(mstr, "DC")) { |
d822726d | 486 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
51b92b7d | 487 | } else { |
a965748a | 488 | sr_dbg("Unknown first argument '%s'.", mstr); |
51b92b7d | 489 | } |
e93cdf42 | 490 | g_free(mstr); |
e6b021f3 | 491 | } else |
d822726d | 492 | devc->cur_mqflags[i] &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC); |
e93cdf42 BV |
493 | |
494 | return SR_OK; | |
495 | } | |
496 | ||
173378f0 | 497 | static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
8c0152f2 BV |
498 | { |
499 | struct dev_context *devc; | |
4435966e | 500 | char *mstr, *rstr, *m2; |
d822726d | 501 | int i, resolution; |
8c0152f2 | 502 | |
38d326e8 | 503 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
8c0152f2 | 504 | devc = sdi->priv; |
d822726d | 505 | i = devc->cur_conf->index; |
4435966e | 506 | |
43185ed3 AJ |
507 | devc->mode_squarewave = 0; |
508 | ||
d9251a2c | 509 | rstr = g_match_info_fetch(match, 4); |
4435966e | 510 | if (rstr && sr_atoi(rstr, &resolution) == SR_OK) { |
d822726d AJ |
511 | devc->cur_digits[i] = -resolution; |
512 | devc->cur_encoding[i] = -resolution + 1; | |
4435966e AJ |
513 | } |
514 | g_free(rstr); | |
515 | ||
d9251a2c | 516 | mstr = g_match_info_fetch(match, 1); |
8c0152f2 | 517 | if (!strncmp(mstr, "VOLT", 4)) { |
d822726d AJ |
518 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
519 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
520 | devc->cur_mqflags[i] = 0; | |
521 | devc->cur_exponent[i] = 0; | |
8c0152f2 | 522 | if (mstr[4] == ':') { |
96ed8625 AJ |
523 | if (!strncmp(mstr + 5, "ACDC", 4)) { |
524 | /* AC + DC offset */ | |
d822726d | 525 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; |
96ed8625 | 526 | } else if (!strncmp(mstr + 5, "AC", 2)) { |
d822726d | 527 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS; |
ee2bcdfc | 528 | } else if (!strncmp(mstr + 5, "DC", 2)) { |
d822726d | 529 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
51b92b7d | 530 | } |
8c0152f2 | 531 | } else |
d822726d | 532 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
0fe18626 | 533 | } else if (!strncmp(mstr, "CURR", 4)) { |
d822726d AJ |
534 | devc->cur_mq[i] = SR_MQ_CURRENT; |
535 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
536 | devc->cur_mqflags[i] = 0; | |
537 | devc->cur_exponent[i] = 0; | |
0fe18626 AJ |
538 | if (mstr[4] == ':') { |
539 | if (!strncmp(mstr + 5, "ACDC", 4)) { | |
540 | /* AC + DC offset */ | |
d822726d | 541 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; |
0fe18626 | 542 | } else if (!strncmp(mstr + 5, "AC", 2)) { |
d822726d | 543 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS; |
0fe18626 | 544 | } else if (!strncmp(mstr + 5, "DC", 2)) { |
d822726d | 545 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
0fe18626 AJ |
546 | } |
547 | } else | |
d822726d | 548 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
0c5f2abc | 549 | } else if (!strcmp(mstr, "RES")) { |
d822726d AJ |
550 | devc->cur_mq[i] = SR_MQ_RESISTANCE; |
551 | devc->cur_unit[i] = SR_UNIT_OHM; | |
552 | devc->cur_mqflags[i] = 0; | |
553 | devc->cur_exponent[i] = 0; | |
43185ed3 | 554 | } else if (!strcmp(mstr, "COND")) { |
d822726d AJ |
555 | devc->cur_mq[i] = SR_MQ_CONDUCTANCE; |
556 | devc->cur_unit[i] = SR_UNIT_SIEMENS; | |
557 | devc->cur_mqflags[i] = 0; | |
558 | devc->cur_exponent[i] = 0; | |
0c5f2abc | 559 | } else if (!strcmp(mstr, "CAP")) { |
d822726d AJ |
560 | devc->cur_mq[i] = SR_MQ_CAPACITANCE; |
561 | devc->cur_unit[i] = SR_UNIT_FARAD; | |
562 | devc->cur_mqflags[i] = 0; | |
563 | devc->cur_exponent[i] = 0; | |
43185ed3 | 564 | } else if (!strncmp(mstr, "FREQ", 4) || !strncmp(mstr, "FC1", 3)) { |
d822726d AJ |
565 | devc->cur_mq[i] = SR_MQ_FREQUENCY; |
566 | devc->cur_unit[i] = SR_UNIT_HERTZ; | |
567 | devc->cur_mqflags[i] = 0; | |
568 | devc->cur_exponent[i] = 0; | |
0c5f2abc | 569 | } else if (!strcmp(mstr, "CONT")) { |
d822726d AJ |
570 | devc->cur_mq[i] = SR_MQ_CONTINUITY; |
571 | devc->cur_unit[i] = SR_UNIT_BOOLEAN; | |
572 | devc->cur_mqflags[i] = 0; | |
573 | devc->cur_exponent[i] = 0; | |
873c0d11 | 574 | } else if (!strcmp(mstr, "DIOD")) { |
d822726d AJ |
575 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
576 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
577 | devc->cur_mqflags[i] = SR_MQFLAG_DIODE; | |
578 | devc->cur_exponent[i] = 0; | |
579 | devc->cur_digits[i] = 4; | |
580 | devc->cur_encoding[i] = 5; | |
43185ed3 | 581 | } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2) || |
d9251a2c | 582 | !strncmp(mstr, "TEMP", 2)) { |
d822726d | 583 | devc->cur_mq[i] = SR_MQ_TEMPERATURE; |
a965748a | 584 | m2 = g_match_info_fetch(match, 2); |
d822726d AJ |
585 | if (!m2) |
586 | /* | |
587 | * TEMP without param is for secondary display (channel P2) | |
588 | * and is identical to channel P3, so discard it. | |
589 | */ | |
590 | devc->cur_mq[i] = -1; | |
591 | else if (!strcmp(m2, "FAR")) | |
592 | devc->cur_unit[i] = SR_UNIT_FAHRENHEIT; | |
a965748a | 593 | else |
d822726d | 594 | devc->cur_unit[i] = SR_UNIT_CELSIUS; |
a965748a | 595 | g_free(m2); |
d822726d AJ |
596 | devc->cur_mqflags[i] = 0; |
597 | devc->cur_exponent[i] = 0; | |
598 | devc->cur_digits[i] = 1; | |
599 | devc->cur_encoding[i] = 2; | |
0c5f2abc | 600 | } else if (!strcmp(mstr, "SCOU")) { |
a965748a BV |
601 | /* |
602 | * Switch counter, not supported. Not sure what values | |
603 | * come from FETC in this mode, or how they would map | |
604 | * into libsigrok. | |
605 | */ | |
0c5f2abc | 606 | } else if (!strncmp(mstr, "CPER:", 5)) { |
d822726d AJ |
607 | devc->cur_mq[i] = SR_MQ_CURRENT; |
608 | devc->cur_unit[i] = SR_UNIT_PERCENTAGE; | |
609 | devc->cur_mqflags[i] = 0; | |
610 | devc->cur_exponent[i] = 0; | |
611 | devc->cur_digits[i] = 2; | |
612 | devc->cur_encoding[i] = 3; | |
43185ed3 AJ |
613 | } else if (!strcmp(mstr, "SQU")) { |
614 | /* | |
615 | * Square wave output, not supported. FETC just return | |
616 | * an error in this mode, so don't even call it. | |
617 | */ | |
618 | devc->mode_squarewave = 1; | |
51b92b7d | 619 | } else { |
a965748a | 620 | sr_dbg("Unknown first argument '%s'.", mstr); |
51b92b7d | 621 | } |
8c0152f2 BV |
622 | g_free(mstr); |
623 | ||
8c0152f2 BV |
624 | return SR_OK; |
625 | } | |
626 | ||
81599cc5 BV |
627 | /* This comes in whenever the rotary switch is changed to a new position. |
628 | * We could use it to determine the major measurement mode, but we already | |
629 | * have the output of CONF? for that, which is more detailed. However | |
630 | * we do need to catch this here, or it'll show up in some other output. */ | |
a4394fb3 | 631 | static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 | 632 | { |
e93cdf42 BV |
633 | (void)sdi; |
634 | ||
38d326e8 | 635 | sr_spew("Switch '%s'.", g_match_info_get_string(match)); |
e93cdf42 BV |
636 | |
637 | return SR_OK; | |
638 | } | |
639 | ||
f857bd92 BV |
640 | /* Poll keys/switches and values at 7Hz, mode at 1Hz. */ |
641 | SR_PRIV const struct agdmm_job agdmm_jobs_u12xx[] = { | |
a4394fb3 BV |
642 | { 143, send_stat }, |
643 | { 1000, send_conf }, | |
644 | { 143, send_fetc }, | |
9e9dba7b | 645 | ALL_ZERO |
e93cdf42 BV |
646 | }; |
647 | ||
8c0152f2 BV |
648 | SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = { |
649 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x }, | |
a4394fb3 BV |
650 | { "^\\*([0-9])$", recv_switch }, |
651 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
652 | { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x }, | |
653 | { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x}, | |
873c0d11 | 654 | { "^\"(DIOD)\"$", recv_conf_u123x }, |
9e9dba7b | 655 | ALL_ZERO |
e93cdf42 BV |
656 | }; |
657 | ||
173378f0 BV |
658 | SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = { |
659 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x }, | |
660 | { "^\\*([0-9])$", recv_switch }, | |
661 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
4435966e AJ |
662 | { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
663 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
0fe18626 | 664 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
4435966e | 665 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
a965748a | 666 | { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, |
873c0d11 | 667 | { "^\"(DIOD)\"$", recv_conf_u124x_5x }, |
9e9dba7b | 668 | ALL_ZERO |
173378f0 BV |
669 | }; |
670 | ||
8c0152f2 BV |
671 | SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = { |
672 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x }, | |
673 | { "^\\*([0-9])$", recv_switch }, | |
674 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
4435966e AJ |
675 | { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
676 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
0fe18626 | 677 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
4435966e | 678 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
c7f5219e | 679 | { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, |
873c0d11 | 680 | { "^\"(DIOD)\"$", recv_conf_u124x_5x }, |
9e9dba7b | 681 | ALL_ZERO |
8c0152f2 | 682 | }; |
43185ed3 AJ |
683 | |
684 | SR_PRIV const struct agdmm_recv agdmm_recvs_u128x[] = { | |
685 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u128x }, | |
686 | { "^\\*([0-9])$", recv_switch }, | |
687 | { "^([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))$", recv_fetc }, | |
688 | { "^\"(VOLT|CURR|RES|COND|CAP|FREQ|FC1|FC100) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
689 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
690 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
691 | { "^\"(FREQ:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
692 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
693 | { "^\"(TEMP:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, | |
d822726d | 694 | { "^\"(DIOD|SQU|TEMP)\"$", recv_conf_u124x_5x }, |
43185ed3 AJ |
695 | ALL_ZERO |
696 | }; |