]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/protocol.c
scpi-pps: properly set encoding digits
[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>
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
6ec6c43b 20#include <config.h>
9e45cd41 21#include <string.h>
ba464a12 22#include <strings.h>
9e45cd41 23#include <stdarg.h>
91ef511d 24#include "scpi.h"
ca1a7cb5
BV
25#include "protocol.h"
26
60475cd7
BV
27SR_PRIV int select_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch)
28{
29 struct dev_context *devc;
624503ae 30 struct pps_channel *cur_pch, *new_pch;
60475cd7
BV
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
624503ae
BV
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
91ef511d
BV
50 if ((ret = scpi_cmd(sdi, devc->device->commands, SCPI_CMD_SELECT_CHANNEL,
51 new_pch->hwname)) >= 0)
60475cd7
BV
52 devc->cur_channel = ch;
53
54 return ret;
55}
56
ca1a7cb5
BV
57SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
58{
ca1a7cb5 59 struct dev_context *devc;
9e45cd41 60 struct sr_datafeed_packet packet;
0de290a5
UH
61 struct sr_datafeed_analog analog;
62 struct sr_analog_encoding encoding;
63 struct sr_analog_meaning meaning;
64 struct sr_analog_spec spec;
9e45cd41 65 const struct sr_dev_inst *sdi;
60475cd7 66 struct sr_channel *next_channel;
9e45cd41 67 struct sr_scpi_dev_inst *scpi;
01b0257a 68 struct pps_channel *pch;
6ed709fe 69 const struct channel_spec *ch_spec;
9e45cd41 70 float f;
01b0257a 71 int cmd;
ca1a7cb5
BV
72
73 (void)fd;
9e45cd41 74 (void)revents;
ca1a7cb5
BV
75
76 if (!(sdi = cb_data))
77 return TRUE;
78
79 if (!(devc = sdi->priv))
80 return TRUE;
81
9e45cd41
BV
82 scpi = sdi->conn;
83
84 /* Retrieve requested value for this state. */
85 if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
01b0257a 86 pch = devc->cur_channel->priv;
6ed709fe 87 ch_spec = &devc->device->channels[pch->hw_output_idx];
0de290a5 88 packet.type = SR_DF_ANALOG;
9e45cd41 89 packet.payload = &analog;
0de290a5
UH
90 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
91 analog.meaning->channels = g_slist_append(NULL, devc->cur_channel);
9e45cd41 92 analog.num_samples = 1;
0de290a5 93 analog.meaning->mq = pch->mq;
6ed709fe 94 if (pch->mq == SR_MQ_VOLTAGE) {
0de290a5 95 analog.meaning->unit = SR_UNIT_VOLT;
6ed709fe
AJ
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) {
0de290a5 99 analog.meaning->unit = SR_UNIT_AMPERE;
6ed709fe
AJ
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) {
0de290a5 103 analog.meaning->unit = SR_UNIT_WATT;
6ed709fe
AJ
104 analog.encoding->digits = ch_spec->power[4];
105 analog.spec->spec_digits = ch_spec->power[3];
106 }
0de290a5 107 analog.meaning->mqflags = SR_MQFLAG_DC;
9e45cd41
BV
108 analog.data = &f;
109 sr_session_send(sdi, &packet);
0de290a5 110 g_slist_free(analog.meaning->channels);
9e45cd41
BV
111 }
112
60475cd7 113 if (g_slist_length(sdi->channels) > 1) {
9c24d16a 114 next_channel = sr_next_enabled_channel(sdi, devc->cur_channel);
624503ae 115 if (select_channel(sdi, next_channel) != SR_OK) {
60475cd7
BV
116 sr_err("Failed to select channel %s", next_channel->name);
117 return FALSE;
118 }
119 }
01b0257a
BV
120
121 pch = devc->cur_channel->priv;
122 if (pch->mq == SR_MQ_VOLTAGE)
123 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
4264f1c0
AG
124 else if (pch->mq == SR_MQ_FREQUENCY)
125 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
01b0257a
BV
126 else if (pch->mq == SR_MQ_CURRENT)
127 cmd = SCPI_CMD_GET_MEAS_CURRENT;
128 else if (pch->mq == SR_MQ_POWER)
129 cmd = SCPI_CMD_GET_MEAS_POWER;
130 else
131 return SR_ERR;
91ef511d 132 scpi_cmd(sdi, devc->device->commands, cmd);
ca1a7cb5
BV
133
134 return TRUE;
135}