]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/protocol.c
scpi-pps: Use software sample and time limits.
[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 scpi_pps_receive_data(int fd, int revents, void *cb_data)
28 {
29         struct dev_context *devc;
30         struct sr_datafeed_packet packet;
31         struct sr_datafeed_analog analog;
32         struct sr_analog_encoding encoding;
33         struct sr_analog_meaning meaning;
34         struct sr_analog_spec spec;
35         struct sr_dev_inst *sdi;
36         int channel_group_cmd;
37         char *channel_group_name;
38         struct pps_channel *pch;
39         const struct channel_spec *ch_spec;
40         int ret;
41         float f;
42         GVariant *gvdata;
43         const GVariantType *gvtype;
44         int cmd;
45
46         (void)fd;
47         (void)revents;
48
49         if (!(sdi = cb_data))
50                 return TRUE;
51
52         if (!(devc = sdi->priv))
53                 return TRUE;
54
55         channel_group_cmd = 0;
56         channel_group_name = NULL;
57         if (g_slist_length(sdi->channel_groups) > 1) {
58                 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
59                 channel_group_name = g_strdup(devc->cur_acquisition_channel->name);
60         }
61
62         pch = devc->cur_acquisition_channel->priv;
63         if (pch->mq == SR_MQ_VOLTAGE) {
64                 gvtype = G_VARIANT_TYPE_DOUBLE;
65                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
66         } else if (pch->mq == SR_MQ_FREQUENCY) {
67                 gvtype = G_VARIANT_TYPE_DOUBLE;
68                 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
69         } else if (pch->mq == SR_MQ_CURRENT) {
70                 gvtype = G_VARIANT_TYPE_DOUBLE;
71                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
72         } else if (pch->mq == SR_MQ_POWER) {
73                 gvtype = G_VARIANT_TYPE_DOUBLE;
74                 cmd = SCPI_CMD_GET_MEAS_POWER;
75         } else {
76                 return SR_ERR;
77         }
78
79         ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
80                 channel_group_cmd, channel_group_name, &gvdata, gvtype, cmd);
81         g_free(channel_group_name);
82
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);
91         analog.meaning->channels = g_slist_append(NULL, devc->cur_acquisition_channel);
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);
109         analog.data = &f;
110         sr_session_send(sdi, &packet);
111         g_slist_free(analog.meaning->channels);
112
113         /* Next channel. */
114         if (g_slist_length(sdi->channels) > 1) {
115                 devc->cur_acquisition_channel =
116                         sr_next_enabled_channel(sdi, devc->cur_acquisition_channel);
117         }
118
119         if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL))
120                 /* First enabled channel, so each channel has been sampled */
121                 sr_sw_limits_update_samples_read(&devc->limits, 1);
122
123         /* Stop if limits have been hit. */
124         if (sr_sw_limits_check(&devc->limits))
125                 sr_dev_acquisition_stop(sdi);
126
127         return TRUE;
128 }