]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/protocol.c
scpi-pps: Add support for Rigol DP832.
[libsigrok.git] / src / hardware / scpi-pps / protocol.c
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
20 #include <string.h>
21 #include <stdarg.h>
22 #include "protocol.h"
23
24 SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...)
25 {
26         va_list args;
27         struct dev_context *devc;
28         struct sr_scpi_dev_inst *scpi;
29         unsigned int i;
30         int ret;
31         char *cmd;
32
33         devc = sdi->priv;
34         cmd = NULL;
35         for (i = 0; i < devc->device->num_commands; i++) {
36                 if (devc->device->commands[i].command == command) {
37                         cmd = devc->device->commands[i].string;
38                         break;
39                 }
40         }
41         if (!cmd) {
42                 /* Device does not implement this command, that's OK. */
43                 return SR_OK_CONTINUE;
44         }
45
46         scpi = sdi->conn;
47         va_start(args, command);
48         ret = sr_scpi_send_variadic(scpi, cmd, args);
49         va_end(args);
50
51         return ret;
52 }
53
54 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
55 {
56         struct dev_context *devc;
57         struct sr_datafeed_packet packet;
58         struct sr_datafeed_analog analog;
59         const struct sr_dev_inst *sdi;
60         struct sr_scpi_dev_inst *scpi;
61         GSList *l;
62         float f;
63
64         (void)fd;
65         (void)revents;
66
67         if (!(sdi = cb_data))
68                 return TRUE;
69
70         if (!(devc = sdi->priv))
71                 return TRUE;
72
73         if (devc->state == STATE_STOP)
74                 return TRUE;
75
76         scpi = sdi->conn;
77
78         /* Retrieve requested value for this state. */
79         if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
80                 packet.type = SR_DF_ANALOG;
81                 packet.payload = &analog;
82                 analog.channels = g_slist_append(NULL, devc->cur_channel);
83                 analog.num_samples = 1;
84                 if (devc->state == STATE_VOLTAGE) {
85                         analog.mq = SR_MQ_VOLTAGE;
86                         analog.unit = SR_UNIT_VOLT;
87                 } else {
88                         analog.mq = SR_MQ_CURRENT;
89                         analog.unit = SR_UNIT_AMPERE;
90                 }
91                 analog.mqflags = SR_MQFLAG_DC;
92                 analog.data = &f;
93                 sr_session_send(sdi, &packet);
94                 g_slist_free(analog.channels);
95         }
96
97         if (devc->state == STATE_VOLTAGE) {
98                 /* Just got voltage, request current for this channel. */
99                 devc->state = STATE_CURRENT;
100                 scpi_cmd(sdi, SCPI_CMD_GET_MEAS_CURRENT, devc->cur_channel->name);
101         } else if (devc->state == STATE_CURRENT) {
102                 /*
103                  * Done with voltage and current for this channel, switch to
104                  * the next enabled channel.
105                  */
106                 do {
107                         l = g_slist_find(sdi->channels, devc->cur_channel);
108                         if (l->next)
109                                 devc->cur_channel = l->next->data;
110                         else
111                                 devc->cur_channel = sdi->channels->data;
112                 } while (!devc->cur_channel->enabled);
113
114                 /* Request voltage. */
115                 devc->state = STATE_VOLTAGE;
116                 scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, devc->cur_channel->name);
117         }
118
119         return TRUE;
120 }