2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 SR_PRIV char *scpi_cmd_get(const struct sr_dev_inst *sdi, int command)
26 struct dev_context *devc;
32 for (i = 0; i < devc->device->num_commands; i++) {
33 if (devc->device->commands[i].command == command) {
34 cmd = devc->device->commands[i].string;
42 SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...)
44 struct sr_scpi_dev_inst *scpi;
49 if (!(cmd = scpi_cmd_get(sdi, command))) {
50 /* Device does not implement this command, that's OK. */
51 return SR_OK_CONTINUE;
55 va_start(args, command);
56 ret = sr_scpi_send_variadic(scpi, cmd, args);
62 SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, GVariant **gvar,
63 const GVariantType *gvtype, int command, ...)
65 struct sr_scpi_dev_inst *scpi;
71 if (!(cmd = scpi_cmd_get(sdi, command))) {
72 /* Device does not implement this command, that's OK. */
73 return SR_OK_CONTINUE;
77 va_start(args, command);
78 ret = sr_scpi_send_variadic(scpi, cmd, args);
83 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
84 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
86 if (!strcasecmp(s, "ON") || !strcasecmp(s, "1") || !strcasecmp(s, "YES"))
87 *gvar = g_variant_new_boolean(TRUE);
88 else if (!strcasecmp(s, "OFF") || !strcasecmp(s, "0") || !strcasecmp(s, "NO"))
89 *gvar = g_variant_new_boolean(FALSE);
92 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
93 if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK)
94 *gvar = g_variant_new_double(d);
95 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
96 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK)
97 *gvar = g_variant_new_string(s);
103 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
105 struct dev_context *devc;
106 struct sr_datafeed_packet packet;
107 struct sr_datafeed_analog analog;
108 const struct sr_dev_inst *sdi;
109 struct sr_scpi_dev_inst *scpi;
116 if (!(sdi = cb_data))
119 if (!(devc = sdi->priv))
122 if (devc->state == STATE_STOP)
127 /* Retrieve requested value for this state. */
128 if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
129 packet.type = SR_DF_ANALOG;
130 packet.payload = &analog;
131 analog.channels = g_slist_append(NULL, devc->cur_channel);
132 analog.num_samples = 1;
133 if (devc->state == STATE_VOLTAGE) {
134 analog.mq = SR_MQ_VOLTAGE;
135 analog.unit = SR_UNIT_VOLT;
137 analog.mq = SR_MQ_CURRENT;
138 analog.unit = SR_UNIT_AMPERE;
140 analog.mqflags = SR_MQFLAG_DC;
142 sr_session_send(sdi, &packet);
143 g_slist_free(analog.channels);
146 if (devc->state == STATE_VOLTAGE) {
147 /* Just got voltage, request current for this channel. */
148 devc->state = STATE_CURRENT;
149 scpi_cmd(sdi, SCPI_CMD_GET_MEAS_CURRENT, devc->cur_channel->name);
150 } else if (devc->state == STATE_CURRENT) {
152 * Done with voltage and current for this channel, switch to
153 * the next enabled channel.
156 l = g_slist_find(sdi->channels, devc->cur_channel);
158 devc->cur_channel = l->next->data;
160 devc->cur_channel = sdi->channels->data;
161 } while (!devc->cur_channel->enabled);
163 /* Request voltage. */
164 devc->state = STATE_VOLTAGE;
165 scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, devc->cur_channel->name);