]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/protocol.c
scpi-pps: Add support for Owon P4000 series.
[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>
7e66bf05 5 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
ca1a7cb5
BV
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
6ec6c43b 21#include <config.h>
9e45cd41 22#include <string.h>
ba464a12 23#include <strings.h>
9e45cd41 24#include <stdarg.h>
91ef511d 25#include "scpi.h"
ca1a7cb5
BV
26#include "protocol.h"
27
28SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
29{
ca1a7cb5 30 struct dev_context *devc;
7e66bf05 31 const struct scpi_pps *device;
9e45cd41 32 struct sr_datafeed_packet packet;
0de290a5
UH
33 struct sr_datafeed_analog analog;
34 struct sr_analog_encoding encoding;
35 struct sr_analog_meaning meaning;
36 struct sr_analog_spec spec;
88e4daa9 37 struct sr_dev_inst *sdi;
17a82e83 38 int channel_group_cmd;
3d118722 39 const char *channel_group_name;
01b0257a 40 struct pps_channel *pch;
6ed709fe 41 const struct channel_spec *ch_spec;
fa2ce8c7
FS
42 int ret;
43 float f;
44 GVariant *gvdata;
45 const GVariantType *gvtype;
01b0257a 46 int cmd;
ca1a7cb5
BV
47
48 (void)fd;
9e45cd41 49 (void)revents;
ca1a7cb5
BV
50
51 if (!(sdi = cb_data))
52 return TRUE;
53
54 if (!(devc = sdi->priv))
55 return TRUE;
56
7e66bf05
FS
57 if (!(device = devc->device))
58 return TRUE;
59
3d118722
ML
60 pch = devc->cur_acquisition_channel->priv;
61
17a82e83
FS
62 channel_group_cmd = 0;
63 channel_group_name = NULL;
64 if (g_slist_length(sdi->channel_groups) > 1) {
65 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
3d118722 66 channel_group_name = pch->hwname;
17a82e83
FS
67 }
68
7e66bf05
FS
69 /*
70 * When the current channel is the first in the array, perform the device
71 * specific status update first.
72 */
73 if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL) &&
74 device->update_status) {
75 device->update_status(sdi);
76 }
77
fa2ce8c7
FS
78 if (pch->mq == SR_MQ_VOLTAGE) {
79 gvtype = G_VARIANT_TYPE_DOUBLE;
80 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
81 } else if (pch->mq == SR_MQ_FREQUENCY) {
82 gvtype = G_VARIANT_TYPE_DOUBLE;
83 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
84 } else if (pch->mq == SR_MQ_CURRENT) {
85 gvtype = G_VARIANT_TYPE_DOUBLE;
86 cmd = SCPI_CMD_GET_MEAS_CURRENT;
87 } else if (pch->mq == SR_MQ_POWER) {
88 gvtype = G_VARIANT_TYPE_DOUBLE;
89 cmd = SCPI_CMD_GET_MEAS_POWER;
90 } else {
91 return SR_ERR;
9e45cd41
BV
92 }
93
17a82e83
FS
94 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
95 channel_group_cmd, channel_group_name, &gvdata, gvtype, cmd);
17a82e83 96
fa2ce8c7
FS
97 if (ret != SR_OK)
98 return ret;
99
100 ch_spec = &devc->device->channels[pch->hw_output_idx];
101 packet.type = SR_DF_ANALOG;
102 packet.payload = &analog;
103 /* Note: digits/spec_digits will be overridden later. */
104 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
17a82e83 105 analog.meaning->channels = g_slist_append(NULL, devc->cur_acquisition_channel);
fa2ce8c7
FS
106 analog.num_samples = 1;
107 analog.meaning->mq = pch->mq;
f2bbcc33 108 analog.meaning->mqflags = pch->mqflags;
fa2ce8c7
FS
109 if (pch->mq == SR_MQ_VOLTAGE) {
110 analog.meaning->unit = SR_UNIT_VOLT;
111 analog.encoding->digits = ch_spec->voltage[4];
112 analog.spec->spec_digits = ch_spec->voltage[3];
113 } else if (pch->mq == SR_MQ_CURRENT) {
114 analog.meaning->unit = SR_UNIT_AMPERE;
115 analog.encoding->digits = ch_spec->current[4];
116 analog.spec->spec_digits = ch_spec->current[3];
117 } else if (pch->mq == SR_MQ_POWER) {
118 analog.meaning->unit = SR_UNIT_WATT;
119 analog.encoding->digits = ch_spec->power[4];
120 analog.spec->spec_digits = ch_spec->power[3];
969671a5
FS
121 } else if (pch->mq == SR_MQ_FREQUENCY) {
122 analog.meaning->unit = SR_UNIT_HERTZ;
123 analog.encoding->digits = ch_spec->frequency[4];
124 analog.spec->spec_digits = ch_spec->frequency[3];
fa2ce8c7 125 }
fa2ce8c7 126 f = (float)g_variant_get_double(gvdata);
3d118722 127 g_variant_unref(gvdata);
fa2ce8c7
FS
128 analog.data = &f;
129 sr_session_send(sdi, &packet);
130 g_slist_free(analog.meaning->channels);
131
17a82e83 132 /* Next channel. */
60475cd7 133 if (g_slist_length(sdi->channels) > 1) {
17a82e83
FS
134 devc->cur_acquisition_channel =
135 sr_next_enabled_channel(sdi, devc->cur_acquisition_channel);
60475cd7 136 }
01b0257a 137
88e4daa9
ML
138 if (devc->cur_acquisition_channel == sr_next_enabled_channel(sdi, NULL))
139 /* First enabled channel, so each channel has been sampled */
140 sr_sw_limits_update_samples_read(&devc->limits, 1);
141
142 /* Stop if limits have been hit. */
143 if (sr_sw_limits_check(&devc->limits))
144 sr_dev_acquisition_stop(sdi);
145
ca1a7cb5
BV
146 return TRUE;
147}