]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/protocol.c
scpi-pps: Add config keys SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE,
[libsigrok.git] / src / hardware / scpi-pps / protocol.c
CommitLineData
ca1a7cb5
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 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>
9e45cd41 21#include <string.h>
ba464a12 22#include <strings.h>
9e45cd41 23#include <stdarg.h>
91ef511d 24#include "scpi.h"
ca1a7cb5
BV
25#include "protocol.h"
26
27SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
28{
ca1a7cb5 29 struct dev_context *devc;
9e45cd41 30 struct sr_datafeed_packet packet;
0de290a5
UH
31 struct sr_datafeed_analog analog;
32 struct sr_analog_encoding encoding;
33 struct sr_analog_meaning meaning;
34 struct sr_analog_spec spec;
88e4daa9 35 struct sr_dev_inst *sdi;
17a82e83 36 int channel_group_cmd;
3d118722 37 const char *channel_group_name;
01b0257a 38 struct pps_channel *pch;
6ed709fe 39 const struct channel_spec *ch_spec;
fa2ce8c7
FS
40 int ret;
41 float f;
42 GVariant *gvdata;
43 const GVariantType *gvtype;
01b0257a 44 int cmd;
ca1a7cb5
BV
45
46 (void)fd;
9e45cd41 47 (void)revents;
ca1a7cb5
BV
48
49 if (!(sdi = cb_data))
50 return TRUE;
51
52 if (!(devc = sdi->priv))
53 return TRUE;
54
3d118722
ML
55 pch = devc->cur_acquisition_channel->priv;
56
17a82e83
FS
57 channel_group_cmd = 0;
58 channel_group_name = NULL;
59 if (g_slist_length(sdi->channel_groups) > 1) {
60 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
3d118722 61 channel_group_name = pch->hwname;
17a82e83
FS
62 }
63
fa2ce8c7
FS
64 if (pch->mq == SR_MQ_VOLTAGE) {
65 gvtype = G_VARIANT_TYPE_DOUBLE;
66 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
67 } else if (pch->mq == SR_MQ_FREQUENCY) {
68 gvtype = G_VARIANT_TYPE_DOUBLE;
69 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
70 } else if (pch->mq == SR_MQ_CURRENT) {
71 gvtype = G_VARIANT_TYPE_DOUBLE;
72 cmd = SCPI_CMD_GET_MEAS_CURRENT;
73 } else if (pch->mq == SR_MQ_POWER) {
74 gvtype = G_VARIANT_TYPE_DOUBLE;
75 cmd = SCPI_CMD_GET_MEAS_POWER;
76 } else {
77 return SR_ERR;
9e45cd41
BV
78 }
79
17a82e83
FS
80 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
81 channel_group_cmd, channel_group_name, &gvdata, gvtype, cmd);
17a82e83 82
fa2ce8c7
FS
83 if (ret != SR_OK)
84 return ret;
85
86 ch_spec = &devc->device->channels[pch->hw_output_idx];
87 packet.type = SR_DF_ANALOG;
88 packet.payload = &analog;
89 /* Note: digits/spec_digits will be overridden later. */
90 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
17a82e83 91 analog.meaning->channels = g_slist_append(NULL, devc->cur_acquisition_channel);
fa2ce8c7
FS
92 analog.num_samples = 1;
93 analog.meaning->mq = pch->mq;
94 if (pch->mq == SR_MQ_VOLTAGE) {
95 analog.meaning->unit = SR_UNIT_VOLT;
96 analog.encoding->digits = ch_spec->voltage[4];
97 analog.spec->spec_digits = ch_spec->voltage[3];
98 } else if (pch->mq == SR_MQ_CURRENT) {
99 analog.meaning->unit = SR_UNIT_AMPERE;
100 analog.encoding->digits = ch_spec->current[4];
101 analog.spec->spec_digits = ch_spec->current[3];
102 } else if (pch->mq == SR_MQ_POWER) {
103 analog.meaning->unit = SR_UNIT_WATT;
104 analog.encoding->digits = ch_spec->power[4];
105 analog.spec->spec_digits = ch_spec->power[3];
106 }
107 analog.meaning->mqflags = SR_MQFLAG_DC;
108 f = (float)g_variant_get_double(gvdata);
3d118722 109 g_variant_unref(gvdata);
fa2ce8c7
FS
110 analog.data = &f;
111 sr_session_send(sdi, &packet);
112 g_slist_free(analog.meaning->channels);
113
17a82e83 114 /* Next channel. */
60475cd7 115 if (g_slist_length(sdi->channels) > 1) {
17a82e83
FS
116 devc->cur_acquisition_channel =
117 sr_next_enabled_channel(sdi, devc->cur_acquisition_channel);
60475cd7 118 }
01b0257a 119
88e4daa9
ML
120 if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL))
121 /* First enabled channel, so each channel has been sampled */
122 sr_sw_limits_update_samples_read(&devc->limits, 1);
123
124 /* Stop if limits have been hit. */
125 if (sr_sw_limits_check(&devc->limits))
126 sr_dev_acquisition_stop(sdi);
127
ca1a7cb5
BV
128 return TRUE;
129}