]>
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 | 244 | |
8f68f36b AJ |
245 | /* dBm/dBV modes. */ |
246 | if ((s[2] & ~0x20) == 'M') | |
247 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_MW; | |
248 | else if ((s[2] & ~0x20) == 'V') | |
249 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_VOLT; | |
250 | else | |
251 | devc->mode_dbm_dbv = 0; | |
252 | ||
8c0152f2 BV |
253 | /* Peak hold mode. */ |
254 | if (s[4] == '1') | |
d822726d | 255 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX; |
8c0152f2 | 256 | else |
d822726d | 257 | devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX; |
8c0152f2 BV |
258 | |
259 | /* Triggered hold mode. */ | |
260 | if (s[7] == '1') | |
d822726d | 261 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; |
8c0152f2 | 262 | else |
d822726d | 263 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; |
8c0152f2 BV |
264 | |
265 | g_free(s); | |
266 | ||
267 | return SR_OK; | |
268 | } | |
269 | ||
43185ed3 AJ |
270 | static int recv_stat_u128x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
271 | { | |
272 | struct dev_context *devc; | |
273 | char *s; | |
274 | ||
275 | devc = sdi->priv; | |
276 | s = g_match_info_fetch(match, 1); | |
277 | sr_spew("STAT response '%s'.", s); | |
278 | ||
279 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
280 | * set both flags to denote it's not a normal measurement. */ | |
281 | if (s[0] == '1') | |
d822726d | 282 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG; |
43185ed3 | 283 | else |
d822726d | 284 | devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG); |
43185ed3 | 285 | |
8f68f36b AJ |
286 | /* dBm/dBV modes. */ |
287 | if ((s[2] & ~0x20) == 'M') | |
288 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_MW; | |
289 | else if ((s[2] & ~0x20) == 'V') | |
290 | devc->mode_dbm_dbv = devc->cur_unit[0] = SR_UNIT_DECIBEL_VOLT; | |
291 | else | |
292 | devc->mode_dbm_dbv = 0; | |
293 | ||
43185ed3 AJ |
294 | /* Peak hold mode. */ |
295 | if (s[4] == '4') | |
d822726d | 296 | devc->cur_mqflags[0] |= SR_MQFLAG_MAX; |
43185ed3 | 297 | else |
d822726d | 298 | devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX; |
43185ed3 AJ |
299 | |
300 | /* Null function. */ | |
301 | if (s[1] == '1') | |
d822726d | 302 | devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE; |
43185ed3 | 303 | else |
d822726d | 304 | devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE; |
43185ed3 AJ |
305 | |
306 | /* Triggered or auto hold modes. */ | |
307 | if (s[7] == '1' || s[11] == '1') | |
d822726d | 308 | devc->cur_mqflags[0] |= SR_MQFLAG_HOLD; |
43185ed3 | 309 | else |
d822726d | 310 | devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD; |
43185ed3 AJ |
311 | |
312 | g_free(s); | |
313 | ||
314 | return SR_OK; | |
315 | } | |
316 | ||
a4394fb3 | 317 | static int send_fetc(const struct sr_dev_inst *sdi) |
e93cdf42 | 318 | { |
43185ed3 AJ |
319 | struct dev_context *devc; |
320 | devc = sdi->priv; | |
321 | if (devc->mode_squarewave) | |
322 | return SR_OK; | |
d822726d AJ |
323 | devc->cur_channel = sr_next_enabled_channel(sdi, devc->cur_channel); |
324 | if (devc->cur_channel->index > 0) | |
325 | return agdmm_send(sdi, "FETC? @%d", devc->cur_channel->index + 1); | |
326 | else | |
327 | return agdmm_send(sdi, "FETC?"); | |
e93cdf42 BV |
328 | } |
329 | ||
a4394fb3 | 330 | static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 BV |
331 | { |
332 | struct dev_context *devc; | |
333 | struct sr_datafeed_packet packet; | |
16aef676 UH |
334 | struct sr_datafeed_analog analog; |
335 | struct sr_analog_encoding encoding; | |
336 | struct sr_analog_meaning meaning; | |
337 | struct sr_analog_spec spec; | |
e93cdf42 | 338 | float fvalue; |
f216eb86 | 339 | const char *s; |
fe9d5abe | 340 | char *mstr; |
8f68f36b | 341 | int i, exp; |
e93cdf42 | 342 | |
38d326e8 | 343 | sr_spew("FETC reply '%s'.", g_match_info_get_string(match)); |
e93cdf42 | 344 | devc = sdi->priv; |
d822726d | 345 | i = devc->cur_channel->index; |
e93cdf42 | 346 | |
d822726d | 347 | if (devc->cur_mq[i] == -1) |
e93cdf42 BV |
348 | /* Haven't seen configuration yet, so can't know what |
349 | * the fetched float means. Not really an error, we'll | |
350 | * get metadata soon enough. */ | |
351 | return SR_OK; | |
352 | ||
f216eb86 BV |
353 | s = g_match_info_get_string(match); |
354 | if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) { | |
74ac7d7f | 355 | /* An invalid measurement shows up on the display as "O.L", but |
e93cdf42 BV |
356 | * comes through like this. Since comparing 38-digit floats |
357 | * is rather problematic, we'll cut through this here. */ | |
358 | fvalue = NAN; | |
359 | } else { | |
360 | mstr = g_match_info_fetch(match, 1); | |
7c03b564 | 361 | if (sr_atof_ascii(mstr, &fvalue) != SR_OK) { |
fe9d5abe | 362 | g_free(mstr); |
51b92b7d | 363 | sr_dbg("Invalid float."); |
e93cdf42 BV |
364 | return SR_ERR; |
365 | } | |
fe9d5abe | 366 | g_free(mstr); |
d822726d AJ |
367 | if (devc->cur_exponent[i] != 0) |
368 | fvalue *= powf(10, devc->cur_exponent[i]); | |
e93cdf42 BV |
369 | } |
370 | ||
8f68f36b AJ |
371 | if (devc->cur_unit[i] == SR_UNIT_DECIBEL_MW || |
372 | devc->cur_unit[i] == SR_UNIT_DECIBEL_VOLT || | |
373 | devc->cur_unit[i] == SR_UNIT_PERCENTAGE) { | |
374 | mstr = g_match_info_fetch(match, 2); | |
375 | if (mstr && sr_atoi(mstr, &exp) == SR_OK) { | |
376 | devc->cur_digits[i] = MIN(4 - exp, devc->cur_digits[i]); | |
377 | devc->cur_encoding[i] = MIN(5 - exp, devc->cur_encoding[i]); | |
378 | } | |
379 | g_free(mstr); | |
380 | } | |
381 | ||
4435966e | 382 | sr_analog_init(&analog, &encoding, &meaning, &spec, |
d822726d AJ |
383 | devc->cur_digits[i] - devc->cur_exponent[i]); |
384 | analog.meaning->mq = devc->cur_mq[i]; | |
385 | analog.meaning->unit = devc->cur_unit[i]; | |
386 | analog.meaning->mqflags = devc->cur_mqflags[i]; | |
387 | analog.meaning->channels = g_slist_append(NULL, devc->cur_channel); | |
e93cdf42 BV |
388 | analog.num_samples = 1; |
389 | analog.data = &fvalue; | |
d822726d | 390 | encoding.digits = devc->cur_encoding[i] - devc->cur_exponent[i]; |
16aef676 | 391 | packet.type = SR_DF_ANALOG; |
e93cdf42 | 392 | packet.payload = &analog; |
695dc859 | 393 | sr_session_send(sdi, &packet); |
d822726d | 394 | g_slist_free(analog.meaning->channels); |
e93cdf42 | 395 | |
5b6829ea | 396 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
e93cdf42 BV |
397 | |
398 | return SR_OK; | |
399 | } | |
400 | ||
a4394fb3 | 401 | static int send_conf(const struct sr_dev_inst *sdi) |
e93cdf42 | 402 | { |
d822726d AJ |
403 | struct dev_context *devc = sdi->priv; |
404 | ||
405 | devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf); | |
406 | ||
407 | /* Do not try to send CONF? for internal temperature channel. */ | |
408 | if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1)) | |
409 | devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf); | |
410 | if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1)) | |
411 | return SR_ERR_NA; | |
412 | ||
413 | if (devc->cur_conf->index > 0) | |
414 | return agdmm_send(sdi, "CONF? @%d", devc->cur_conf->index + 1); | |
415 | else | |
416 | return agdmm_send(sdi, "CONF?"); | |
e93cdf42 BV |
417 | } |
418 | ||
a4394fb3 | 419 | static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 BV |
420 | { |
421 | struct dev_context *devc; | |
4435966e | 422 | char *mstr, *rstr; |
d822726d | 423 | int i, resolution; |
e93cdf42 | 424 | |
38d326e8 | 425 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
e93cdf42 | 426 | devc = sdi->priv; |
d822726d | 427 | i = devc->cur_conf->index; |
4435966e AJ |
428 | |
429 | rstr = g_match_info_fetch(match, 2); | |
430 | if (rstr) | |
431 | sr_atoi(rstr, &resolution); | |
432 | g_free(rstr); | |
433 | ||
e93cdf42 BV |
434 | mstr = g_match_info_fetch(match, 1); |
435 | if (!strcmp(mstr, "V")) { | |
d822726d AJ |
436 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
437 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
438 | devc->cur_mqflags[i] = 0; | |
439 | devc->cur_exponent[i] = 0; | |
440 | devc->cur_digits[i] = 4 - resolution; | |
0c5f2abc | 441 | } else if (!strcmp(mstr, "MV")) { |
e6b021f3 | 442 | if (devc->mode_tempaux) { |
d822726d | 443 | devc->cur_mq[i] = SR_MQ_TEMPERATURE; |
f3f19d11 UH |
444 | /* No way to detect whether Fahrenheit or Celsius |
445 | * is used, so we'll just default to Celsius. */ | |
d822726d AJ |
446 | devc->cur_unit[i] = SR_UNIT_CELSIUS; |
447 | devc->cur_mqflags[i] = 0; | |
448 | devc->cur_exponent[i] = 0; | |
449 | devc->cur_digits[i] = 1; | |
e6b021f3 | 450 | } else { |
d822726d AJ |
451 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
452 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
453 | devc->cur_mqflags[i] = 0; | |
454 | devc->cur_exponent[i] = -3; | |
455 | devc->cur_digits[i] = 5 - resolution; | |
e6b021f3 | 456 | } |
0c5f2abc | 457 | } else if (!strcmp(mstr, "A")) { |
d822726d AJ |
458 | devc->cur_mq[i] = SR_MQ_CURRENT; |
459 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
460 | devc->cur_mqflags[i] = 0; | |
461 | devc->cur_exponent[i] = 0; | |
462 | devc->cur_digits[i] = 3 - resolution; | |
0c5f2abc | 463 | } else if (!strcmp(mstr, "UA")) { |
d822726d AJ |
464 | devc->cur_mq[i] = SR_MQ_CURRENT; |
465 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
466 | devc->cur_mqflags[i] = 0; | |
467 | devc->cur_exponent[i] = -6; | |
468 | devc->cur_digits[i] = 8 - resolution; | |
0c5f2abc | 469 | } else if (!strcmp(mstr, "FREQ")) { |
d822726d AJ |
470 | devc->cur_mq[i] = SR_MQ_FREQUENCY; |
471 | devc->cur_unit[i] = SR_UNIT_HERTZ; | |
472 | devc->cur_mqflags[i] = 0; | |
473 | devc->cur_exponent[i] = 0; | |
474 | devc->cur_digits[i] = 2 - resolution; | |
0c5f2abc | 475 | } else if (!strcmp(mstr, "RES")) { |
e6b021f3 | 476 | if (devc->mode_continuity) { |
d822726d AJ |
477 | devc->cur_mq[i] = SR_MQ_CONTINUITY; |
478 | devc->cur_unit[i] = SR_UNIT_BOOLEAN; | |
e6b021f3 | 479 | } else { |
d822726d AJ |
480 | devc->cur_mq[i] = SR_MQ_RESISTANCE; |
481 | devc->cur_unit[i] = SR_UNIT_OHM; | |
e6b021f3 | 482 | } |
d822726d AJ |
483 | devc->cur_mqflags[i] = 0; |
484 | devc->cur_exponent[i] = 0; | |
485 | devc->cur_digits[i] = 1 - resolution; | |
873c0d11 | 486 | } else if (!strcmp(mstr, "DIOD")) { |
d822726d AJ |
487 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
488 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
489 | devc->cur_mqflags[i] = SR_MQFLAG_DIODE; | |
490 | devc->cur_exponent[i] = 0; | |
491 | devc->cur_digits[i] = 3; | |
0c5f2abc | 492 | } else if (!strcmp(mstr, "CAP")) { |
d822726d AJ |
493 | devc->cur_mq[i] = SR_MQ_CAPACITANCE; |
494 | devc->cur_unit[i] = SR_UNIT_FARAD; | |
495 | devc->cur_mqflags[i] = 0; | |
496 | devc->cur_exponent[i] = 0; | |
497 | devc->cur_digits[i] = 9 - resolution; | |
e93cdf42 | 498 | } else |
38d326e8 | 499 | sr_dbg("Unknown first argument."); |
e93cdf42 BV |
500 | g_free(mstr); |
501 | ||
4435966e | 502 | /* This is based on guess, supposing similarity with other models. */ |
d822726d | 503 | devc->cur_encoding[i] = devc->cur_digits[i] + 1; |
4435966e | 504 | |
e066c32a BV |
505 | if (g_match_info_get_match_count(match) == 4) { |
506 | mstr = g_match_info_fetch(match, 3); | |
e93cdf42 | 507 | /* Third value, if present, is always AC or DC. */ |
51b92b7d | 508 | if (!strcmp(mstr, "AC")) { |
d822726d AJ |
509 | devc->cur_mqflags[i] |= SR_MQFLAG_AC; |
510 | if (devc->cur_mq[i] == SR_MQ_VOLTAGE) | |
511 | devc->cur_mqflags[i] |= SR_MQFLAG_RMS; | |
51b92b7d | 512 | } else if (!strcmp(mstr, "DC")) { |
d822726d | 513 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
51b92b7d | 514 | } else { |
a965748a | 515 | sr_dbg("Unknown first argument '%s'.", mstr); |
51b92b7d | 516 | } |
e93cdf42 | 517 | g_free(mstr); |
e6b021f3 | 518 | } else |
d822726d | 519 | devc->cur_mqflags[i] &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC); |
e93cdf42 BV |
520 | |
521 | return SR_OK; | |
522 | } | |
523 | ||
173378f0 | 524 | static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
8c0152f2 BV |
525 | { |
526 | struct dev_context *devc; | |
4435966e | 527 | char *mstr, *rstr, *m2; |
d822726d | 528 | int i, resolution; |
8c0152f2 | 529 | |
38d326e8 | 530 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
8c0152f2 | 531 | devc = sdi->priv; |
d822726d | 532 | i = devc->cur_conf->index; |
4435966e | 533 | |
43185ed3 AJ |
534 | devc->mode_squarewave = 0; |
535 | ||
d9251a2c | 536 | rstr = g_match_info_fetch(match, 4); |
4435966e | 537 | if (rstr && sr_atoi(rstr, &resolution) == SR_OK) { |
d822726d AJ |
538 | devc->cur_digits[i] = -resolution; |
539 | devc->cur_encoding[i] = -resolution + 1; | |
4435966e AJ |
540 | } |
541 | g_free(rstr); | |
542 | ||
d9251a2c | 543 | mstr = g_match_info_fetch(match, 1); |
8c0152f2 | 544 | if (!strncmp(mstr, "VOLT", 4)) { |
d822726d AJ |
545 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
546 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
547 | devc->cur_mqflags[i] = 0; | |
548 | devc->cur_exponent[i] = 0; | |
8f68f36b AJ |
549 | if (i == 0 && devc->mode_dbm_dbv) { |
550 | devc->cur_unit[i] = devc->mode_dbm_dbv; | |
551 | devc->cur_digits[i] = 3; | |
552 | devc->cur_encoding[i] = 4; | |
553 | } | |
8c0152f2 | 554 | if (mstr[4] == ':') { |
96ed8625 AJ |
555 | if (!strncmp(mstr + 5, "ACDC", 4)) { |
556 | /* AC + DC offset */ | |
d822726d | 557 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; |
96ed8625 | 558 | } else if (!strncmp(mstr + 5, "AC", 2)) { |
d822726d | 559 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS; |
ee2bcdfc | 560 | } else if (!strncmp(mstr + 5, "DC", 2)) { |
d822726d | 561 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
51b92b7d | 562 | } |
8c0152f2 | 563 | } else |
d822726d | 564 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
0fe18626 | 565 | } else if (!strncmp(mstr, "CURR", 4)) { |
d822726d AJ |
566 | devc->cur_mq[i] = SR_MQ_CURRENT; |
567 | devc->cur_unit[i] = SR_UNIT_AMPERE; | |
568 | devc->cur_mqflags[i] = 0; | |
569 | devc->cur_exponent[i] = 0; | |
0fe18626 AJ |
570 | if (mstr[4] == ':') { |
571 | if (!strncmp(mstr + 5, "ACDC", 4)) { | |
572 | /* AC + DC offset */ | |
d822726d | 573 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS; |
0fe18626 | 574 | } else if (!strncmp(mstr + 5, "AC", 2)) { |
d822726d | 575 | devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS; |
0fe18626 | 576 | } else if (!strncmp(mstr + 5, "DC", 2)) { |
d822726d | 577 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
0fe18626 AJ |
578 | } |
579 | } else | |
d822726d | 580 | devc->cur_mqflags[i] |= SR_MQFLAG_DC; |
0c5f2abc | 581 | } else if (!strcmp(mstr, "RES")) { |
d822726d AJ |
582 | devc->cur_mq[i] = SR_MQ_RESISTANCE; |
583 | devc->cur_unit[i] = SR_UNIT_OHM; | |
584 | devc->cur_mqflags[i] = 0; | |
585 | devc->cur_exponent[i] = 0; | |
43185ed3 | 586 | } else if (!strcmp(mstr, "COND")) { |
d822726d AJ |
587 | devc->cur_mq[i] = SR_MQ_CONDUCTANCE; |
588 | devc->cur_unit[i] = SR_UNIT_SIEMENS; | |
589 | devc->cur_mqflags[i] = 0; | |
590 | devc->cur_exponent[i] = 0; | |
0c5f2abc | 591 | } else if (!strcmp(mstr, "CAP")) { |
d822726d AJ |
592 | devc->cur_mq[i] = SR_MQ_CAPACITANCE; |
593 | devc->cur_unit[i] = SR_UNIT_FARAD; | |
594 | devc->cur_mqflags[i] = 0; | |
595 | devc->cur_exponent[i] = 0; | |
43185ed3 | 596 | } else if (!strncmp(mstr, "FREQ", 4) || !strncmp(mstr, "FC1", 3)) { |
d822726d AJ |
597 | devc->cur_mq[i] = SR_MQ_FREQUENCY; |
598 | devc->cur_unit[i] = SR_UNIT_HERTZ; | |
599 | devc->cur_mqflags[i] = 0; | |
600 | devc->cur_exponent[i] = 0; | |
0c5f2abc | 601 | } else if (!strcmp(mstr, "CONT")) { |
d822726d AJ |
602 | devc->cur_mq[i] = SR_MQ_CONTINUITY; |
603 | devc->cur_unit[i] = SR_UNIT_BOOLEAN; | |
604 | devc->cur_mqflags[i] = 0; | |
605 | devc->cur_exponent[i] = 0; | |
873c0d11 | 606 | } else if (!strcmp(mstr, "DIOD")) { |
d822726d AJ |
607 | devc->cur_mq[i] = SR_MQ_VOLTAGE; |
608 | devc->cur_unit[i] = SR_UNIT_VOLT; | |
609 | devc->cur_mqflags[i] = SR_MQFLAG_DIODE; | |
610 | devc->cur_exponent[i] = 0; | |
611 | devc->cur_digits[i] = 4; | |
612 | devc->cur_encoding[i] = 5; | |
43185ed3 | 613 | } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2) || |
d9251a2c | 614 | !strncmp(mstr, "TEMP", 2)) { |
d822726d | 615 | devc->cur_mq[i] = SR_MQ_TEMPERATURE; |
a965748a | 616 | m2 = g_match_info_fetch(match, 2); |
d822726d AJ |
617 | if (!m2) |
618 | /* | |
619 | * TEMP without param is for secondary display (channel P2) | |
620 | * and is identical to channel P3, so discard it. | |
621 | */ | |
622 | devc->cur_mq[i] = -1; | |
623 | else if (!strcmp(m2, "FAR")) | |
624 | devc->cur_unit[i] = SR_UNIT_FAHRENHEIT; | |
a965748a | 625 | else |
d822726d | 626 | devc->cur_unit[i] = SR_UNIT_CELSIUS; |
a965748a | 627 | g_free(m2); |
d822726d AJ |
628 | devc->cur_mqflags[i] = 0; |
629 | devc->cur_exponent[i] = 0; | |
630 | devc->cur_digits[i] = 1; | |
631 | devc->cur_encoding[i] = 2; | |
0c5f2abc | 632 | } else if (!strcmp(mstr, "SCOU")) { |
a965748a BV |
633 | /* |
634 | * Switch counter, not supported. Not sure what values | |
635 | * come from FETC in this mode, or how they would map | |
636 | * into libsigrok. | |
637 | */ | |
0c5f2abc | 638 | } else if (!strncmp(mstr, "CPER:", 5)) { |
d822726d AJ |
639 | devc->cur_mq[i] = SR_MQ_CURRENT; |
640 | devc->cur_unit[i] = SR_UNIT_PERCENTAGE; | |
641 | devc->cur_mqflags[i] = 0; | |
642 | devc->cur_exponent[i] = 0; | |
643 | devc->cur_digits[i] = 2; | |
644 | devc->cur_encoding[i] = 3; | |
43185ed3 AJ |
645 | } else if (!strcmp(mstr, "SQU")) { |
646 | /* | |
647 | * Square wave output, not supported. FETC just return | |
648 | * an error in this mode, so don't even call it. | |
649 | */ | |
650 | devc->mode_squarewave = 1; | |
51b92b7d | 651 | } else { |
a965748a | 652 | sr_dbg("Unknown first argument '%s'.", mstr); |
51b92b7d | 653 | } |
8c0152f2 BV |
654 | g_free(mstr); |
655 | ||
8c0152f2 BV |
656 | return SR_OK; |
657 | } | |
658 | ||
81599cc5 BV |
659 | /* This comes in whenever the rotary switch is changed to a new position. |
660 | * We could use it to determine the major measurement mode, but we already | |
661 | * have the output of CONF? for that, which is more detailed. However | |
662 | * we do need to catch this here, or it'll show up in some other output. */ | |
a4394fb3 | 663 | static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 | 664 | { |
e93cdf42 BV |
665 | (void)sdi; |
666 | ||
38d326e8 | 667 | sr_spew("Switch '%s'.", g_match_info_get_string(match)); |
e93cdf42 BV |
668 | |
669 | return SR_OK; | |
670 | } | |
671 | ||
f857bd92 BV |
672 | /* Poll keys/switches and values at 7Hz, mode at 1Hz. */ |
673 | SR_PRIV const struct agdmm_job agdmm_jobs_u12xx[] = { | |
a4394fb3 BV |
674 | { 143, send_stat }, |
675 | { 1000, send_conf }, | |
676 | { 143, send_fetc }, | |
9e9dba7b | 677 | ALL_ZERO |
e93cdf42 BV |
678 | }; |
679 | ||
8c0152f2 BV |
680 | SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = { |
681 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x }, | |
a4394fb3 BV |
682 | { "^\\*([0-9])$", recv_switch }, |
683 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
684 | { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x }, | |
685 | { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x}, | |
873c0d11 | 686 | { "^\"(DIOD)\"$", recv_conf_u123x }, |
9e9dba7b | 687 | ALL_ZERO |
e93cdf42 BV |
688 | }; |
689 | ||
173378f0 BV |
690 | SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = { |
691 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x }, | |
692 | { "^\\*([0-9])$", recv_switch }, | |
693 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
4435966e AJ |
694 | { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
695 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
0fe18626 | 696 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
4435966e | 697 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
a965748a | 698 | { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, |
873c0d11 | 699 | { "^\"(DIOD)\"$", recv_conf_u124x_5x }, |
9e9dba7b | 700 | ALL_ZERO |
173378f0 BV |
701 | }; |
702 | ||
8c0152f2 BV |
703 | SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = { |
704 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x }, | |
705 | { "^\\*([0-9])$", recv_switch }, | |
706 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
4435966e AJ |
707 | { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
708 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
0fe18626 | 709 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
4435966e | 710 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, |
c7f5219e | 711 | { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, |
873c0d11 | 712 | { "^\"(DIOD)\"$", recv_conf_u124x_5x }, |
9e9dba7b | 713 | ALL_ZERO |
8c0152f2 | 714 | }; |
43185ed3 AJ |
715 | |
716 | SR_PRIV const struct agdmm_recv agdmm_recvs_u128x[] = { | |
717 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u128x }, | |
718 | { "^\\*([0-9])$", recv_switch }, | |
719 | { "^([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))$", recv_fetc }, | |
720 | { "^\"(VOLT|CURR|RES|COND|CAP|FREQ|FC1|FC100) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
721 | { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
722 | { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
723 | { "^\"(FREQ:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
724 | { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x }, | |
725 | { "^\"(TEMP:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x }, | |
d822726d | 726 | { "^\"(DIOD|SQU|TEMP)\"$", recv_conf_u124x_5x }, |
43185ed3 AJ |
727 | ALL_ZERO |
728 | }; |