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