]> sigrok.org Git - libsigrok.git/blame - hardware/hameg-hmo/protocol.c
hameg-hmo: Update the sample rate when the channel states or the timebase change.
[libsigrok.git] / hardware / hameg-hmo / protocol.c
CommitLineData
06a3e78a
DJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.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 "protocol.h"
21
13f2b9d7
DJ
22static const char *hameg_scpi_dialect[] = {
23 [SCPI_CMD_GET_DIG_DATA] = ":POD%d:DATA?",
24 [SCPI_CMD_GET_TIMEBASE] = ":TIM:SCAL?",
965b463d 25 [SCPI_CMD_SET_TIMEBASE] = ":TIM:SCAL %s",
13f2b9d7
DJ
26 [SCPI_CMD_GET_COUPLING] = ":CHAN%d:COUP?",
27 [SCPI_CMD_SET_COUPLING] = ":CHAN%d:COUP %s",
14a2f74d
DJ
28 [SCPI_CMD_GET_SAMPLE_RATE] = ":ACQ:SRAT?",
29 [SCPI_CMD_GET_SAMPLE_RATE_LIVE] = ":%s:DATA:POINTS?",
13f2b9d7
DJ
30 [SCPI_CMD_GET_ANALOG_DATA] = ":CHAN%d:DATA?",
31 [SCPI_CMD_GET_VERTICAL_DIV] = ":CHAN%d:SCAL?",
965b463d 32 [SCPI_CMD_SET_VERTICAL_DIV] = ":CHAN%d:SCAL %s",
13f2b9d7
DJ
33 [SCPI_CMD_GET_DIG_POD_STATE] = ":POD%d:STAT?",
34 [SCPI_CMD_SET_DIG_POD_STATE] = ":POD%d:STAT %d",
35 [SCPI_CMD_GET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP?",
36 [SCPI_CMD_SET_TRIGGER_SLOPE] = ":TRIG:A:EDGE:SLOP %s",
37 [SCPI_CMD_GET_TRIGGER_SOURCE] = ":TRIG:A:SOUR?",
38 [SCPI_CMD_SET_TRIGGER_SOURCE] = ":TRIG:A:SOUR %s",
39 [SCPI_CMD_GET_DIG_CHAN_STATE] = ":LOG%d:STAT?",
40 [SCPI_CMD_SET_DIG_CHAN_STATE] = ":LOG%d:STAT %d",
41 [SCPI_CMD_GET_VERTICAL_OFFSET] = ":CHAN%d:POS?",
42 [SCPI_CMD_GET_HORIZ_TRIGGERPOS] = ":TIM:POS?",
43 [SCPI_CMD_SET_HORIZ_TRIGGERPOS] = ":TIM:POS %E",
44 [SCPI_CMD_GET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT?",
45 [SCPI_CMD_SET_ANALOG_CHAN_STATE] = ":CHAN%d:STAT %d",
46};
47
48static const int32_t hmo_hwcaps[] = {
49 SR_CONF_OSCILLOSCOPE,
50 SR_CONF_TRIGGER_SOURCE,
51 SR_CONF_TIMEBASE,
52 SR_CONF_NUM_TIMEBASE,
53 SR_CONF_TRIGGER_SLOPE,
54 SR_CONF_HORIZ_TRIGGERPOS,
14a2f74d 55 SR_CONF_SAMPLERATE,
13f2b9d7
DJ
56};
57
58static const int32_t hmo_analog_caps[] = {
59 SR_CONF_NUM_VDIV,
60 SR_CONF_COUPLING,
61 SR_CONF_VDIV,
62};
63
64static const char *hmo_coupling_options[] = {
65 "AC",
66 "ACL",
67 "DC",
53cd1c78 68 "DCL",
13f2b9d7
DJ
69 "GND",
70 NULL,
71};
72
73static const char *scope_trigger_slopes[] = {
74 "POS",
75 "NEG",
76 NULL,
77};
78
79static const char *hmo_compact2_trigger_sources[] = {
80 "CH1",
81 "CH2",
82 "LINE",
83 "EXT",
84 "D0",
85 "D1",
86 "D2",
87 "D3",
88 "D4",
89 "D5",
90 "D6",
91 "D7",
92 NULL,
93};
94
95static const char *hmo_compact4_trigger_sources[] = {
96 "CH1",
97 "CH2",
98 "CH3",
99 "CH4",
100 "LINE",
101 "EXT",
102 "D0",
103 "D1",
104 "D2",
105 "D3",
106 "D4",
107 "D5",
108 "D6",
109 "D7",
110 NULL,
111};
112
113static const uint64_t hmo_timebases[][2] = {
114 /* nanoseconds */
115 { 2, 1000000000 },
116 { 5, 1000000000 },
117 { 10, 1000000000 },
118 { 20, 1000000000 },
119 { 50, 1000000000 },
120 { 100, 1000000000 },
121 { 200, 1000000000 },
122 { 500, 1000000000 },
123 /* microseconds */
124 { 1, 1000000 },
125 { 2, 1000000 },
126 { 5, 1000000 },
127 { 10, 1000000 },
128 { 20, 1000000 },
129 { 50, 1000000 },
130 { 100, 1000000 },
131 { 200, 1000000 },
132 { 500, 1000000 },
133 /* milliseconds */
134 { 1, 1000 },
135 { 2, 1000 },
136 { 5, 1000 },
137 { 10, 1000 },
138 { 20, 1000 },
139 { 50, 1000 },
140 { 100, 1000 },
141 { 200, 1000 },
142 { 500, 1000 },
143 /* seconds */
144 { 1, 1 },
145 { 2, 1 },
146 { 5, 1 },
147 { 10, 1 },
148 { 20, 1 },
149 { 50, 1 },
150};
151
152static const uint64_t hmo_vdivs[][2] = {
153 /* millivolts */
154 { 1, 1000 },
155 { 2, 1000 },
156 { 5, 1000 },
157 { 10, 1000 },
158 { 20, 1000 },
159 { 50, 1000 },
160 { 100, 1000 },
161 { 200, 1000 },
162 { 500, 1000 },
163 /* volts */
164 { 1, 1 },
165 { 2, 1 },
166 { 5, 1 },
167 { 10, 1 },
168};
169
170static const char *scope_analog_probe_names[] = {
171 "CH1",
172 "CH2",
173 "CH3",
174 "CH4",
175};
176
177static const char *scope_digital_probe_names[] = {
178 "D0",
179 "D1",
180 "D2",
181 "D3",
182 "D4",
183 "D5",
184 "D6",
185 "D7",
186 "D8",
187 "D9",
188 "D10",
189 "D11",
190 "D12",
191 "D13",
192 "D14",
193 "D15",
194};
195
196static struct scope_config scope_models[] = {
197 {
198 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
199 .analog_channels = 2,
200 .digital_channels = 8,
201 .digital_pods = 1,
202
203 .analog_names = &scope_analog_probe_names,
204 .digital_names = &scope_digital_probe_names,
205
206 .hw_caps = &hmo_hwcaps,
207 .num_hwcaps = ARRAY_SIZE(hmo_hwcaps),
208
209 .analog_hwcaps = &hmo_analog_caps,
210 .num_analog_hwcaps = ARRAY_SIZE(hmo_analog_caps),
211
212 .coupling_options = &hmo_coupling_options,
213 .trigger_sources = &hmo_compact2_trigger_sources,
214 .trigger_slopes = &scope_trigger_slopes,
215
216 .timebases = &hmo_timebases,
217 .num_timebases = ARRAY_SIZE(hmo_timebases),
218
219 .vdivs = &hmo_vdivs,
220 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
221
222 .num_xdivs = 12,
223 .num_ydivs = 8,
224
225 .scpi_dialect = &hameg_scpi_dialect,
226 },
227 {
228 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
229 .analog_channels = 4,
230 .digital_channels = 8,
231 .digital_pods = 1,
232
233 .analog_names = &scope_analog_probe_names,
234 .digital_names = &scope_digital_probe_names,
235
236 .hw_caps = &hmo_hwcaps,
237 .num_hwcaps = ARRAY_SIZE(hmo_hwcaps),
238
239 .analog_hwcaps = &hmo_analog_caps,
240 .num_analog_hwcaps = ARRAY_SIZE(hmo_analog_caps),
241
242 .coupling_options = &hmo_coupling_options,
243 .trigger_sources = &hmo_compact4_trigger_sources,
244 .trigger_slopes = &scope_trigger_slopes,
245
246 .timebases = &hmo_timebases,
247 .num_timebases = ARRAY_SIZE(hmo_timebases),
248
249 .vdivs = &hmo_vdivs,
250 .num_vdivs = ARRAY_SIZE(hmo_vdivs),
251
252 .num_xdivs = 12,
253 .num_ydivs = 8,
254
255 .scpi_dialect = &hameg_scpi_dialect,
256 },
257};
258
13f2b9d7
DJ
259static void scope_state_dump(struct scope_config *config,
260 struct scope_state *state)
261{
262 unsigned int i;
8de2dc3b 263 char *tmp;
13f2b9d7
DJ
264
265 for (i = 0; i < config->analog_channels; ++i) {
8de2dc3b
DJ
266 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
267 (*config->vdivs)[state->analog_channels[i].vdiv][1]);
268 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
269 i + 1, state->analog_channels[i].state ? "On" : "Off",
13f2b9d7 270 (*config->coupling_options)[state->analog_channels[i].coupling],
8de2dc3b 271 tmp, state->analog_channels[i].vertical_offset);
13f2b9d7
DJ
272 }
273
274 for (i = 0; i < config->digital_channels; ++i) {
275 sr_info("State of digital channel %d -> %s", i,
276 state->digital_channels[i] ? "On" : "Off");
277 }
278
279 for (i = 0; i < config->digital_pods; ++i) {
280 sr_info("State of digital POD %d -> %s", i,
281 state->digital_pods[i] ? "On" : "Off");
282 }
283
8de2dc3b
DJ
284 tmp = sr_period_string((*config->timebases)[state->timebase][0] *
285 (*config->timebases)[state->timebase][1]);
286 sr_info("Current timebase: %s", tmp);
287 g_free(tmp);
288
14a2f74d
DJ
289 tmp = sr_samplerate_string(state->sample_rate);
290 sr_info("Current samplerate: %s", tmp);
291 g_free(tmp);
292
8de2dc3b 293 sr_info("Current trigger: %s (source), %s (slope) %2.2e (offset)",
13f2b9d7
DJ
294 (*config->trigger_sources)[state->trigger_source],
295 (*config->trigger_slopes)[state->trigger_slope],
296 state->horiz_triggerpos);
297}
298
23f43dff 299static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
89280b1a 300 const char *command, const char *(*array)[], int *result)
13f2b9d7
DJ
301{
302 char *tmp;
303 unsigned int i;
304
23f43dff 305 if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
89280b1a 306 g_free(tmp);
13f2b9d7
DJ
307 return SR_ERR;
308 }
309
310 for (i = 0; (*array)[i]; ++i) {
311 if (!g_strcmp0(tmp, (*array)[i])) {
312 *result = i;
313 g_free(tmp);
314 tmp = NULL;
315 break;
316 }
317 }
318
319 if (tmp) {
320 g_free(tmp);
321 return SR_ERR;
322 }
323
324 return SR_OK;
325}
326
23f43dff 327static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
13f2b9d7
DJ
328 struct scope_config *config,
329 struct scope_state *state)
330{
8de2dc3b
DJ
331 unsigned int i, j;
332 float tmp_float;
13f2b9d7
DJ
333 char command[MAX_COMMAND_SIZE];
334
335 for (i = 0; i < config->analog_channels; ++i) {
336 g_snprintf(command, sizeof(command),
337 (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
338 i + 1);
339
23f43dff 340 if (sr_scpi_get_bool(scpi, command,
13f2b9d7
DJ
341 &state->analog_channels[i].state) != SR_OK)
342 return SR_ERR;
343
344 g_snprintf(command, sizeof(command),
345 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
346 i + 1);
347
8de2dc3b
DJ
348 if (sr_scpi_get_float(scpi, command, &tmp_float) != SR_OK)
349 return SR_ERR;
350 for (j = 0; j < config->num_vdivs; j++) {
351 if (tmp_float == ((float) (*config->vdivs)[j][0] /
352 (*config->vdivs)[j][1])) {
353 state->analog_channels[i].vdiv = j;
354 break;
355 }
356 }
357 if (i == config->num_vdivs)
13f2b9d7
DJ
358 return SR_ERR;
359
360 g_snprintf(command, sizeof(command),
361 (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
362 i + 1);
363
23f43dff 364 if (sr_scpi_get_float(scpi, command,
13f2b9d7
DJ
365 &state->analog_channels[i].vertical_offset) != SR_OK)
366 return SR_ERR;
367
368 g_snprintf(command, sizeof(command),
369 (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
370 i + 1);
371
23f43dff 372 if (scope_state_get_array_option(scpi, command, config->coupling_options,
13f2b9d7
DJ
373 &state->analog_channels[i].coupling) != SR_OK)
374 return SR_ERR;
375 }
376
377 return SR_OK;
378}
379
23f43dff 380static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
13f2b9d7
DJ
381 struct scope_config *config,
382 struct scope_state *state)
383{
384 unsigned int i;
385 char command[MAX_COMMAND_SIZE];
386
387 for (i = 0; i < config->digital_channels; ++i) {
388 g_snprintf(command, sizeof(command),
389 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
390 i);
391
23f43dff 392 if (sr_scpi_get_bool(scpi, command,
13f2b9d7
DJ
393 &state->digital_channels[i]) != SR_OK)
394 return SR_ERR;
395 }
396
397 for (i = 0; i < config->digital_pods; ++i) {
398 g_snprintf(command, sizeof(command),
399 (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
400 i + 1);
401
23f43dff 402 if (sr_scpi_get_bool(scpi, command,
13f2b9d7
DJ
403 &state->digital_pods[i]) != SR_OK)
404 return SR_ERR;
405 }
406
407 return SR_OK;
408}
409
14a2f74d
DJ
410SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
411{
412 struct dev_context *devc;
413 struct scope_state *state;
414 struct scope_config *config;
415
416 int tmp;
417 unsigned int i;
418 float tmp_float;
419 gboolean channel_found;
420 char tmp_str[MAX_COMMAND_SIZE];
421 char chan_name[20];
422
423 devc = sdi->priv;
424 config = devc->model_config;
425 state = devc->model_state;
426 channel_found = FALSE;
427
428 for (i = 0; i < config->analog_channels; ++i) {
429 if (state->analog_channels[i].state) {
430 g_snprintf(chan_name, sizeof(chan_name), "CHAN%d", i + 1);
431 g_snprintf(tmp_str, sizeof(tmp_str),
432 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
433 chan_name);
434 channel_found = TRUE;
435 break;
436 }
437 }
438
439 if (!channel_found) {
440 for (i = 0; i < config->digital_pods; i++) {
441 if (state->digital_pods[i]) {
442 g_snprintf(chan_name, sizeof(chan_name), "POD%d", i);
443 g_snprintf(tmp_str, sizeof(tmp_str),
444 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE_LIVE],
445 chan_name);
446 channel_found = TRUE;
447 break;
448 }
449 }
450 }
451
452 /* No channel is active, ask the instrument for the sample rate
453 * in single shot mode */
454 if (!channel_found) {
455 g_snprintf(tmp_str, sizeof(tmp_str),
456 (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE]);
457
458 if (sr_scpi_get_float(sdi->conn, tmp_str, &tmp_float) != SR_OK)
459 return SR_ERR;
460 state->sample_rate = tmp_float;
461 } else {
462 if (sr_scpi_get_int(sdi->conn, tmp_str, &tmp) != SR_OK)
463 return SR_ERR;
464 state->sample_rate = tmp / (((float) (*config->timebases)[state->timebase][0] /
465 (*config->timebases)[state->timebase][1]) *
466 config->num_xdivs);
467 }
468
469 return SR_OK;
470}
471
719eff68 472SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
13f2b9d7
DJ
473{
474 struct dev_context *devc;
475 struct scope_state *state;
476 struct scope_config *config;
8de2dc3b
DJ
477 float tmp_float;
478 unsigned int i;
13f2b9d7
DJ
479
480 devc = sdi->priv;
481 config = devc->model_config;
482 state = devc->model_state;
483
8de2dc3b
DJ
484 sr_info("Fetching scope state");
485
13f2b9d7
DJ
486 if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
487 return SR_ERR;
488
489 if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
490 return SR_ERR;
491
89280b1a
UH
492 if (sr_scpi_get_float(sdi->conn,
493 (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
8de2dc3b
DJ
494 &tmp_float) != SR_OK)
495 return SR_ERR;
496
497 for (i = 0; i < config->num_timebases; i++) {
498 if (tmp_float == ((float) (*config->timebases)[i][0] /
499 (*config->timebases)[i][1])) {
500 state->timebase = i;
501 break;
502 }
503 }
504 if (i == config->num_timebases)
13f2b9d7
DJ
505 return SR_ERR;
506
89280b1a
UH
507 if (sr_scpi_get_float(sdi->conn,
508 (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
509 &state->horiz_triggerpos) != SR_OK)
13f2b9d7
DJ
510 return SR_ERR;
511
89280b1a
UH
512 if (scope_state_get_array_option(sdi->conn,
513 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
514 config->trigger_sources, &state->trigger_source) != SR_OK)
13f2b9d7
DJ
515 return SR_ERR;
516
89280b1a
UH
517 if (scope_state_get_array_option(sdi->conn,
518 (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
519 config->trigger_slopes, &state->trigger_slope) != SR_OK)
13f2b9d7
DJ
520 return SR_ERR;
521
14a2f74d
DJ
522 if (hmo_update_sample_rate(sdi) != SR_OK)
523 return SR_ERR;
524
8de2dc3b
DJ
525 sr_info("Fetching finished.");
526
13f2b9d7
DJ
527 scope_state_dump(config, state);
528
529 return SR_OK;
530}
531
d87c1766 532static struct scope_state *scope_state_new(struct scope_config *config)
13f2b9d7
DJ
533{
534 struct scope_state *state;
535
536 if (!(state = g_try_malloc0(sizeof(struct scope_state))))
537 return NULL;
538
539 if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels,
89280b1a 540 sizeof(struct analog_channel_state))))
13f2b9d7
DJ
541 goto fail;
542
89280b1a
UH
543 if (!(state->digital_channels = g_try_malloc0_n(
544 config->digital_channels, sizeof(gboolean))))
13f2b9d7
DJ
545 goto fail;
546
547 if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods,
89280b1a 548 sizeof(gboolean))))
13f2b9d7
DJ
549 goto fail;
550
551 return state;
552
553fail:
554 if (state->analog_channels)
555 g_free(state->analog_channels);
556 if (state->digital_channels)
557 g_free(state->digital_channels);
558 if (state->digital_pods)
559 g_free(state->digital_pods);
560 g_free(state);
561
562 return NULL;
563}
564
719eff68 565SR_PRIV void hmo_scope_state_free(struct scope_state *state)
13f2b9d7
DJ
566{
567 g_free(state->analog_channels);
568 g_free(state->digital_channels);
569 g_free(state->digital_pods);
570 g_free(state);
571}
572
573SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
574{
575 char tmp[25];
576 int model_index;
89280b1a 577 unsigned int i, j;
13f2b9d7
DJ
578 struct sr_probe *probe;
579 struct dev_context *devc;
580
581 devc = sdi->priv;
582 model_index = -1;
583
89280b1a 584 /* Find the exact model. */
13f2b9d7
DJ
585 for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
586 for (j = 0; scope_models[i].name[j]; j++) {
587 if (!strcmp(sdi->model, scope_models[i].name[j])) {
588 model_index = i;
589 break;
590 }
591 }
592 if (model_index != -1)
593 break;
594 }
595
596 if (model_index == -1) {
89280b1a 597 sr_dbg("Unsupported HMO device.");
13f2b9d7
DJ
598 return SR_ERR_NA;
599 }
600
601 if (!(devc->analog_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
602 scope_models[model_index].analog_channels)))
603 return SR_ERR_MALLOC;
604
605 if (!(devc->digital_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
606 scope_models[model_index].digital_pods)))
607 return SR_ERR_MALLOC;
608
89280b1a 609 /* Add analog channels. */
13f2b9d7
DJ
610 for (i = 0; i < scope_models[model_index].analog_channels; i++) {
611 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
89280b1a 612 (*scope_models[model_index].analog_names)[i])))
13f2b9d7
DJ
613 return SR_ERR_MALLOC;
614 sdi->probes = g_slist_append(sdi->probes, probe);
615
89280b1a
UH
616 devc->analog_groups[i].name =
617 (char *)(*scope_models[model_index].analog_names)[i];
13f2b9d7
DJ
618 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
619
620 sdi->probe_groups = g_slist_append(sdi->probe_groups,
621 &devc->analog_groups[i]);
622 }
623
89280b1a 624 /* Add digital probe groups. */
13f2b9d7
DJ
625 for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
626 g_snprintf(tmp, 25, "POD%d", i);
627 devc->digital_groups[i].name = g_strdup(tmp);
628 sdi->probe_groups = g_slist_append(sdi->probe_groups,
89280b1a 629 &devc->digital_groups[i < 8 ? 0 : 1]);
13f2b9d7
DJ
630 }
631
89280b1a 632 /* Add digital channels. */
13f2b9d7
DJ
633 for (i = 0; i < scope_models[model_index].digital_channels; i++) {
634 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
89280b1a 635 (*scope_models[model_index].digital_names)[i])))
13f2b9d7
DJ
636 return SR_ERR_MALLOC;
637 sdi->probes = g_slist_append(sdi->probes, probe);
638
89280b1a
UH
639 devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(
640 devc->digital_groups[i < 8 ? 0 : 1].probes, probe);
13f2b9d7
DJ
641 }
642
643 devc->model_config = &scope_models[model_index];
644 devc->frame_limit = 0;
645
646 if (!(devc->model_state = scope_state_new(devc->model_config)))
647 return SR_ERR_MALLOC;
648
649 return SR_OK;
650}
651
719eff68 652SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
06a3e78a 653{
13f2b9d7
DJ
654 struct sr_probe *probe;
655 struct sr_dev_inst *sdi;
06a3e78a 656 struct dev_context *devc;
13f2b9d7 657 struct sr_datafeed_packet packet;
89280b1a
UH
658 GArray *data;
659 struct sr_datafeed_analog analog;
660 struct sr_datafeed_logic logic;
06a3e78a
DJ
661
662 (void)fd;
663
664 if (!(sdi = cb_data))
665 return TRUE;
666
667 if (!(devc = sdi->priv))
668 return TRUE;
669
670 if (revents == G_IO_IN) {
13f2b9d7
DJ
671 probe = devc->current_probe->data;
672
673 switch (probe->type) {
674 case SR_PROBE_ANALOG:
13f2b9d7
DJ
675 if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
676 if (data)
677 g_array_free(data, TRUE);
678
679 return TRUE;
680 }
681
682 packet.type = SR_DF_FRAME_BEGIN;
683 sr_session_send(sdi, &packet);
684
685 analog.probes = g_slist_append(NULL, probe);
686 analog.num_samples = data->len;
687 analog.data = (float *) data->data;
688 analog.mq = SR_MQ_VOLTAGE;
689 analog.unit = SR_UNIT_VOLT;
690 analog.mqflags = 0;
691 packet.type = SR_DF_ANALOG;
692 packet.payload = &analog;
693 sr_session_send(cb_data, &packet);
694 g_slist_free(analog.probes);
695 g_array_free(data, TRUE);
89280b1a 696 break;
13f2b9d7 697 case SR_PROBE_LOGIC:
13f2b9d7
DJ
698 if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
699 if (data)
700 g_free(data);
701 return TRUE;
702 }
703
704 packet.type = SR_DF_FRAME_BEGIN;
705 sr_session_send(sdi, &packet);
706
707 logic.length = data->len;
708 logic.unitsize = 1;
709 logic.data = data->data;
710 packet.type = SR_DF_LOGIC;
711 packet.payload = &logic;
712 sr_session_send(cb_data, &packet);
713 g_array_free(data, TRUE);
89280b1a 714 break;
13f2b9d7 715 default:
89280b1a 716 sr_err("Invalid probe type.");
13f2b9d7
DJ
717 break;
718 }
719
720 packet.type = SR_DF_FRAME_END;
721 sr_session_send(sdi, &packet);
722
723 if (devc->current_probe->next) {
724 devc->current_probe = devc->current_probe->next;
725 hmo_request_data(sdi);
726 } else if (++devc->num_frames == devc->frame_limit) {
13f2b9d7
DJ
727 sdi->driver->dev_acquisition_stop(sdi, cb_data);
728 } else {
729 devc->current_probe = devc->enabled_probes;
730 hmo_request_data(sdi);
731 }
06a3e78a
DJ
732 }
733
734 return TRUE;
735}