]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds1xx2/protocol.c
rigol-ds1xx2: fix channel numbers
[libsigrok.git] / hardware / rigol-ds1xx2 / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <glib.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 SR_PRIV int rigol_ds1xx2_receive_data(int fd, int revents, void *cb_data)
32 {
33         struct sr_dev_inst *sdi;
34         struct dev_context *devc;
35         struct sr_datafeed_packet packet;
36         struct sr_datafeed_analog analog;
37         unsigned char buf[WAVEFORM_SIZE];
38         float data[WAVEFORM_SIZE];
39         int len, i;
40
41         if (!(sdi = cb_data))
42                 return TRUE;
43
44         if (!(devc = sdi->priv))
45                 return TRUE;
46
47         if (revents == G_IO_IN) {
48                 len = read(fd, buf, WAVEFORM_SIZE);
49                 sr_dbg("Received %d bytes.", len);
50                 if (len == -1)
51                         return TRUE;
52
53                 if (devc->num_frame_samples == 0) {
54                         /* Start of a new frame. */
55                         packet.type = SR_DF_FRAME_BEGIN;
56                         sr_session_send(sdi, &packet);
57                 }
58
59                 for (i = 0; i < len; i++)
60                         data[i] = devc->scale / 25.6 * (128 - buf[i]) - devc->offset;
61                 analog.probes = devc->enabled_probes;
62                 analog.num_samples = len;
63                 analog.data = data;
64                 analog.mq = SR_MQ_VOLTAGE;
65                 analog.unit = SR_UNIT_VOLT;
66                 analog.mqflags = 0;
67                 packet.type = SR_DF_ANALOG;
68                 packet.payload = &analog;
69                 sr_session_send(cb_data, &packet);
70
71                 if (len == WAVEFORM_SIZE) {
72                         /* End of the frame. */
73                         packet.type = SR_DF_FRAME_END;
74                         sr_session_send(sdi, &packet);
75
76                         if (++devc->num_frames == devc->limit_frames)
77                                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
78                         else
79                                 rigol_ds1xx2_send_data(fd, ":WAV:DATA?");
80                 }
81         }
82
83         return TRUE;
84 }
85
86 SR_PRIV int rigol_ds1xx2_send_data(int fd, const char *format, ...)
87 {
88         va_list args;
89         char buf[256];
90         int len, out, ret;
91
92         va_start(args, format);
93         len = vsprintf(buf, format, args);
94         va_end(args);
95         strcat(buf, "\n");
96         len++;
97         out = write(fd, buf, len);
98         buf[len - 1] = '\0';
99         if (out != len) {
100                 sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
101                 ret = SR_ERR;
102         } else {
103                 sr_dbg("Sent '%s'.", buf);
104                 ret = SR_OK;
105         }
106
107         return ret;
108 }