]> sigrok.org Git - libsigrok.git/blame - hardware/agilent-dmm/sched.c
agilent-dmm: support for submodes
[libsigrok.git] / hardware / agilent-dmm / sched.c
CommitLineData
e93cdf42
BV
1/*
2 * This file is part of the sigrok 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 <glib.h>
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23#include "config.h"
24#include "agilent-dmm.h"
25#include <stdlib.h>
26#include <string.h>
27#include <errno.h>
28#include <math.h>
29
30
31static void dispatch(const struct sr_dev_inst *sdi)
32{
33 struct dev_context *devc;
34 const struct agdmm_job *jobs;
35 int64_t now;
36 int i;
37
38 devc = sdi->priv;
39 jobs = devc->profile->jobs;
40 now = g_get_monotonic_time() / 1000;
41 for (i = 0; (&jobs[i])->interval; i++) {
42 if (now - devc->jobqueue[i] > (&jobs[i])->interval) {
43 sr_spew("agilent-dmm: running job %d", i);
44 (&jobs[i])->send(sdi);
45 devc->jobqueue[i] = now;
46 }
47 }
48
49}
50
51static void receive_line(const struct sr_dev_inst *sdi)
52{
53 struct dev_context *devc;
54 const struct agdmm_recv *recvs, *recv;
55 GRegex *reg;
56 GMatchInfo *match;
57 int i;
58
59 devc = sdi->priv;
60
61 /* Strip CRLF */
62 while (devc->buflen) {
63 if (*(devc->buf + devc->buflen - 1) == '\r'
64 || *(devc->buf + devc->buflen - 1) == '\n')
65 *(devc->buf + --devc->buflen) = '\0';
66 else
67 break;
68 }
69 sr_spew("agilent-dmm: received '%s'", devc->buf);
70
71 recv = NULL;
72 recvs = devc->profile->recvs;
73 for (i = 0; (&recvs[i])->recv_regex; i++) {
74 reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL);
75 if (g_regex_match(reg, (char *)devc->buf, 0, &match)) {
76 recv = &recvs[i];
77 break;
78 }
79 g_match_info_unref(match);
80 g_regex_unref(reg);
81 }
82 if (recv) {
83 recv->recv(sdi, match);
84 g_match_info_unref(match);
85 g_regex_unref(reg);
86 }
87
88 /* Done with this. */
89 devc->buflen = 0;
90
91}
92
93SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
94{
95 const struct sr_dev_inst *sdi;
96 struct dev_context *devc;
97 int len;
98
99 if (!(sdi = cb_data))
100 return TRUE;
101
102 if (!(devc = sdi->priv))
103 return TRUE;
104
105 if (revents == G_IO_IN) {
106 /* Serial data arrived. */
107 len = AGDMM_BUFSIZE - devc->buflen - 1;
108 if (len > 0) {
109 len = serial_read(fd, devc->buf + devc->buflen, len);
110 if (len > 0) {
111 devc->buflen += len;
112 *(devc->buf + devc->buflen) = '\0';
113 if (devc->buflen > 0 && *(devc->buf + devc->buflen - 1) == '\n')
114 /* End of line */
115 receive_line(sdi);
116 }
117 }
118 }
119
120 dispatch(sdi);
121
122 if (devc->num_samples >= devc->limit_samples)
123 sdi->driver->dev_acquisition_stop(sdi, cb_data);
124
125 return TRUE;
126}
127
128static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
129{
130 struct dev_context *devc;
131 char buf[32];
132
133 devc = sdi->priv;
134 sr_spew("agilent-dmm: sending '%s'", cmd);
135 strncpy(buf, cmd, 28);
136 if (!strncmp(buf, "*IDN?", 5))
137 strncat(buf, "\r\n", 32);
138 else
139 strncat(buf, "\n\r\n", 32);
140 if (serial_write(devc->serial->fd, buf, strlen(buf)) == -1) {
141 sr_err("agilent-dmm: failed to send: %s", strerror(errno));
142 return SR_ERR;
143 }
144
145 return SR_OK;
146}
147
148static int agdmm_ident_send(const struct sr_dev_inst *sdi)
149{
150
151 return agdmm_send(sdi, "*IDN?");
152}
153
154static int agdmm_ident_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
155{
156
157 (void)sdi;
158
159 sr_spew("got ident '%s'", g_match_info_get_string(match));
160
161 return SR_OK;
162}
163
164static int agdmm_stat_send(const struct sr_dev_inst *sdi)
165{
166
167 return agdmm_send(sdi, "STAT?");
168}
169
170static int agdmm_stat_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
171{
e6b021f3
BV
172 struct dev_context *devc;
173 char *s;
174
175 devc = sdi->priv;
176 s = g_match_info_fetch(match, 1);
177 sr_spew("got stat '%s'", s);
178
179 /* Max, Min or Avg mode -- no way to tell which, so we'll
180 * set both flags to denote it's not a normal measurement. */
181 if (s[0] == '1')
182 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
183 else
184 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
e93cdf42 185
e6b021f3
BV
186 if (s[1] == '1')
187 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
188 else
189 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
190
191 /* Triggered or auto hold modes. */
192 if (s[2] == '1' || s[3] == '1')
193 devc->cur_mqflags |= SR_MQFLAG_HOLD;
194 else
195 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
196
197 /* Temp/aux mode. */
198 if (s[7] == '1')
199 devc->mode_tempaux = TRUE;
200 else
201 devc->mode_tempaux = FALSE;
202
203 /* Continuity mode. */
204 if (s[16] == '1')
205 devc->mode_continuity = TRUE;
206 else
207 devc->mode_continuity = FALSE;
208
209 g_free(s);
e93cdf42
BV
210
211 return SR_OK;
212}
213
214SR_PRIV int agdmm_fetc_send(const struct sr_dev_inst *sdi)
215{
216
217 return agdmm_send(sdi, "FETC?");
218}
219
220SR_PRIV int agdmm_fetc_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
221{
222 struct dev_context *devc;
223 struct sr_datafeed_packet packet;
224 struct sr_datafeed_analog analog;
225 float fvalue;
226 char *mstr, *eptr;
227
228 sr_spew("agilent-dmm: FETC reply '%s'", g_match_info_get_string(match));
229 devc = sdi->priv;
230
231 if (devc->cur_mq == -1)
232 /* Haven't seen configuration yet, so can't know what
233 * the fetched float means. Not really an error, we'll
234 * get metadata soon enough. */
235 return SR_OK;
236
237 if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) {
238 /* An invalid measurement shows up on the display as "O.L, but
239 * comes through like this. Since comparing 38-digit floats
240 * is rather problematic, we'll cut through this here. */
241 fvalue = NAN;
242 } else {
243 mstr = g_match_info_fetch(match, 1);
244 fvalue = strtof(mstr, &eptr);
245 g_free(mstr);
246 if (fvalue == 0.0 && eptr == mstr) {
247 sr_err("agilent-dmm: invalid float");
248 return SR_ERR;
249 }
250 if (devc->cur_divider > 0)
251 fvalue /= devc->cur_divider;
252 }
253
254 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
255 analog.mq = devc->cur_mq;
e6b021f3
BV
256 analog.unit = devc->cur_unit;
257 analog.mqflags = devc->cur_mqflags;
e93cdf42
BV
258 analog.num_samples = 1;
259 analog.data = &fvalue;
260 packet.type = SR_DF_ANALOG;
261 packet.payload = &analog;
262 sr_session_send(devc->cb_data, &packet);
263
264 devc->num_samples++;
265
266 return SR_OK;
267}
268
269SR_PRIV int agdmm_conf_send(const struct sr_dev_inst *sdi)
270{
271
272 return agdmm_send(sdi, "CONF?");
273}
274
275SR_PRIV int agdmm_conf_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
276{
277 struct dev_context *devc;
278 char *mstr;
279
280 sr_spew("got conf '%s'", g_match_info_get_string(match));
281 devc = sdi->priv;
282 mstr = g_match_info_fetch(match, 1);
283 if (!strcmp(mstr, "V")) {
284 devc->cur_mq = SR_MQ_VOLTAGE;
e6b021f3
BV
285 devc->cur_unit = SR_UNIT_VOLT;
286 devc->cur_mqflags = 0;
e93cdf42
BV
287 devc->cur_divider = 0;
288 } else if(!strcmp(mstr, "MV")) {
e6b021f3
BV
289 if (devc->mode_tempaux) {
290 devc->cur_mq = SR_MQ_TEMPERATURE;
291 /* No way to detect whether Fahrenheit or Celcius
292 * is used, so we'll just default to Celcius. */
293 devc->cur_unit = SR_UNIT_CELSIUS;
294 devc->cur_mqflags = 0;
295 devc->cur_divider = 0;
296 } else {
297 devc->cur_mq = SR_MQ_VOLTAGE;
298 devc->cur_unit = SR_UNIT_VOLT;
299 devc->cur_mqflags = 0;
300 devc->cur_divider = 1000;
301 }
e93cdf42
BV
302 } else if(!strcmp(mstr, "A")) {
303 devc->cur_mq = SR_MQ_CURRENT;
e6b021f3
BV
304 devc->cur_unit = SR_UNIT_AMPERE;
305 devc->cur_mqflags = 0;
e93cdf42
BV
306 devc->cur_divider = 0;
307 } else if(!strcmp(mstr, "UA")) {
308 devc->cur_mq = SR_MQ_CURRENT;
e6b021f3
BV
309 devc->cur_unit = SR_UNIT_AMPERE;
310 devc->cur_mqflags = 0;
e93cdf42
BV
311 devc->cur_divider = 1000000;
312 } else if(!strcmp(mstr, "FREQ")) {
313 devc->cur_mq = SR_MQ_FREQUENCY;
e6b021f3
BV
314 devc->cur_unit = SR_UNIT_HERTZ;
315 devc->cur_mqflags = 0;
e93cdf42
BV
316 devc->cur_divider = 0;
317 } else if(!strcmp(mstr, "RES")) {
e6b021f3
BV
318 if (devc->mode_continuity) {
319 devc->cur_mq = SR_MQ_CONTINUITY;
320 devc->cur_unit = SR_UNIT_BOOLEAN;
321 } else {
322 devc->cur_mq = SR_MQ_RESISTANCE;
323 devc->cur_unit = SR_UNIT_OHM;
324 }
325 devc->cur_mqflags = 0;
e93cdf42
BV
326 devc->cur_divider = 0;
327 } else if(!strcmp(mstr, "CAP")) {
328 devc->cur_mq = SR_MQ_CAPACITANCE;
e6b021f3
BV
329 devc->cur_unit = SR_UNIT_FARAD;
330 devc->cur_mqflags = 0;
e93cdf42
BV
331 devc->cur_divider = 0;
332 } else if(!strcmp(mstr, "DIOD")) {
333 devc->cur_mq = SR_MQ_VOLTAGE;
e6b021f3
BV
334 devc->cur_unit = SR_UNIT_VOLT;
335 devc->cur_mqflags = SR_MQFLAG_DIODE;
e93cdf42
BV
336 devc->cur_divider = 0;
337 } else
338 sr_dbg("agilent-dmm: unknown first argument");
339 g_free(mstr);
340
341 if (g_match_info_get_match_count(match) == 3) {
342 mstr = g_match_info_fetch(match, 1);
343 /* Third value, if present, is always AC or DC. */
344 if (!strcmp(mstr, "AC"))
e6b021f3 345 devc->cur_mqflags |= SR_MQFLAG_AC;
e93cdf42 346 else if (!strcmp(mstr, "DC"))
e6b021f3 347 devc->cur_mqflags |= SR_MQFLAG_DC;
e93cdf42
BV
348 else
349 sr_dbg("agilent-dmm: unknown third argument");
350 g_free(mstr);
e6b021f3
BV
351 } else
352 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
e93cdf42
BV
353
354 return SR_OK;
355}
356
357SR_PRIV int agdmm_switch_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
358{
359
360 (void)sdi;
361
362 sr_spew("got switch '%s'", g_match_info_get_string(match));
363
364 return SR_OK;
365}
366
367
368SR_PRIV const struct agdmm_job u123x_jobs[] = {
369 { 1000, agdmm_ident_send },
370 { 143, agdmm_stat_send },
371 { 1000, agdmm_conf_send },
372 { 143, agdmm_fetc_send },
373 { 0, NULL }
374};
375
376SR_PRIV const struct agdmm_recv u123x_recvs[] = {
377 { "^\"(\\d\\d.{18}\\d)\"$", agdmm_stat_recv },
378 { "^\\*([0-9])$", agdmm_switch_recv },
379 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", agdmm_fetc_recv },
380 { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", agdmm_conf_recv },
381 { "^\"(RES|CAP),(\\d)\"$", agdmm_conf_recv },
382 { "^\"(DIOD)\"$", agdmm_conf_recv },
383 { NULL, NULL }
384};
385
386