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