]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/protocol.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[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 <config.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <stdarg.h>
24 #include "scpi.h"
25 #include "protocol.h"
26
27 SR_PRIV int select_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch)
28 {
29         struct dev_context *devc;
30         struct pps_channel *cur_pch, *new_pch;
31         int ret;
32
33         if (g_slist_length(sdi->channels) == 1)
34                 return SR_OK;
35
36         devc = sdi->priv;
37         if (ch == devc->cur_channel)
38                 return SR_OK;
39
40         new_pch = ch->priv;
41         if (devc->cur_channel) {
42                 cur_pch = devc->cur_channel->priv;
43                 if (cur_pch->hw_output_idx == new_pch->hw_output_idx) {
44                         /* Same underlying output channel. */
45                         devc->cur_channel = ch;
46                         return SR_OK;
47                 }
48         }
49
50         if ((ret = scpi_cmd(sdi, devc->device->commands, SCPI_CMD_SELECT_CHANNEL,
51                         new_pch->hwname)) >= 0)
52                 devc->cur_channel = ch;
53
54         return ret;
55 }
56
57 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
58 {
59         struct dev_context *devc;
60         struct sr_datafeed_packet packet;
61         struct sr_datafeed_analog_old analog;
62         const struct sr_dev_inst *sdi;
63         struct sr_channel *next_channel;
64         struct sr_scpi_dev_inst *scpi;
65         struct pps_channel *pch;
66         float f;
67         int cmd;
68
69         (void)fd;
70         (void)revents;
71
72         if (!(sdi = cb_data))
73                 return TRUE;
74
75         if (!(devc = sdi->priv))
76                 return TRUE;
77
78         scpi = sdi->conn;
79
80         /* Retrieve requested value for this state. */
81         if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
82                 pch = devc->cur_channel->priv;
83                 packet.type = SR_DF_ANALOG_OLD;
84                 packet.payload = &analog;
85                 analog.channels = g_slist_append(NULL, devc->cur_channel);
86                 analog.num_samples = 1;
87                 analog.mq = pch->mq;
88                 if (pch->mq == SR_MQ_VOLTAGE)
89                         analog.unit = SR_UNIT_VOLT;
90                 else if (pch->mq == SR_MQ_CURRENT)
91                         analog.unit = SR_UNIT_AMPERE;
92                 else if (pch->mq == SR_MQ_POWER)
93                         analog.unit = SR_UNIT_WATT;
94                 analog.mqflags = SR_MQFLAG_DC;
95                 analog.data = &f;
96                 sr_session_send(sdi, &packet);
97                 g_slist_free(analog.channels);
98         }
99
100         if (g_slist_length(sdi->channels) > 1) {
101                 next_channel = sr_next_enabled_channel(sdi, devc->cur_channel);
102                 if (select_channel(sdi, next_channel) != SR_OK) {
103                         sr_err("Failed to select channel %s", next_channel->name);
104                         return FALSE;
105                 }
106         }
107
108         pch = devc->cur_channel->priv;
109         if (pch->mq == SR_MQ_VOLTAGE)
110                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
111         else if (pch->mq == SR_MQ_FREQUENCY)
112                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
113         else if (pch->mq == SR_MQ_CURRENT)
114                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
115         else if (pch->mq == SR_MQ_POWER)
116                 cmd = SCPI_CMD_GET_MEAS_POWER;
117         else
118                 return SR_ERR;
119         scpi_cmd(sdi, devc->device->commands, cmd);
120
121         return TRUE;
122 }