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