]>
Commit | Line | Data |
---|---|---|
e2ad47b5 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <glib.h> | |
24 | #include <sigrok.h> | |
25 | #include "config.h" | |
26 | ||
27 | struct context { | |
afc8e4de UH |
28 | unsigned int num_enabled_probes; |
29 | unsigned int unitsize; | |
e2ad47b5 UH |
30 | char *probelist[65]; |
31 | char *header; | |
32 | }; | |
33 | ||
34 | const char *gnuplot_header = "\ | |
35 | # Sample data in space-separated columns format usable by gnuplot\n\ | |
36 | #\n\ | |
7aae7462 | 37 | # Generated by: %s on %s\n%s\ |
e2ad47b5 UH |
38 | # Timescale: %d %s\n\ |
39 | # Column assignment:\n%s\n"; | |
40 | ||
7aae7462 BV |
41 | const char *gnuplot_header_comment = "\ |
42 | # Comment: Acquisition with %d/%d probes at %s\n"; | |
43 | ||
44 | ||
e2ad47b5 UH |
45 | static int init(struct output *o) |
46 | { | |
47 | /* Maximum header length */ | |
48 | #define MAX_HEADER_LEN 2048 | |
49 | ||
50 | struct context *ctx; | |
51 | struct probe *probe; | |
52 | GSList *l; | |
53 | uint64_t samplerate; | |
afc8e4de UH |
54 | unsigned int i; |
55 | int b, num_probes; | |
25e7d9b1 | 56 | char *c, *samplerate_s; |
7aae7462 | 57 | char wbuf[1000], comment[128]; |
e2ad47b5 UH |
58 | |
59 | ctx = malloc(sizeof(struct context)); | |
60 | if (ctx == NULL) | |
61 | return SIGROK_ERR_MALLOC; | |
62 | o->internal = ctx; | |
63 | ctx->num_enabled_probes = 0; | |
64 | for (l = o->device->probes; l; l = l->next) { | |
65 | probe = l->data; | |
66 | if (probe->enabled) | |
67 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
68 | } | |
69 | ||
70 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
71 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
72 | ||
73 | /* TODO: Allow for configuration via o->param. */ | |
74 | ||
75 | ctx->header = calloc(1, MAX_HEADER_LEN + 1); | |
76 | if (ctx->header == NULL) | |
77 | return SIGROK_ERR_MALLOC; | |
78 | num_probes = g_slist_length(o->device->probes); | |
79 | /* TODO: Handle num_probes == 0, too many probes, etc. */ | |
7aae7462 BV |
80 | if (o->device->plugin) { |
81 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( | |
82 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
83 | if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL) | |
84 | return SIGROK_ERR; | |
85 | snprintf(comment, 127, gnuplot_header_comment, ctx->num_enabled_probes, | |
86 | num_probes, samplerate_s); | |
87 | free(samplerate_s); | |
88 | } | |
89 | else | |
90 | comment[0] = '\0'; | |
e2ad47b5 UH |
91 | |
92 | /* Columns / channels */ | |
93 | wbuf[0] = '\0'; | |
94 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
95 | c = (char *)&wbuf + strlen((char *)&wbuf); | |
96 | sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]); | |
97 | } | |
98 | ||
99 | /* TODO: date: File or signals? Make y/n configurable. */ | |
100 | /* TODO: Timescale */ | |
101 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, | |
7aae7462 | 102 | PACKAGE_STRING, "TODO", comment, 1, "ns", (char *)&wbuf); |
e2ad47b5 UH |
103 | |
104 | /* TODO: Handle snprintf errors. */ | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
109 | static int event(struct output *o, int event_type, char **data_out, | |
110 | uint64_t *length_out) | |
111 | { | |
112 | struct context *ctx; | |
113 | char *outbuf; | |
114 | int outlen = 1; /* FIXME */ | |
115 | ||
116 | ctx = o->internal; | |
99c1fc59 | 117 | switch (event_type) { |
e2ad47b5 UH |
118 | case DF_TRIGGER: |
119 | break; | |
120 | case DF_END: | |
99c1fc59 | 121 | outbuf = calloc(1, 1); /* FIXME */ |
e2ad47b5 UH |
122 | if (outbuf == NULL) |
123 | return SIGROK_ERR_MALLOC; | |
124 | *data_out = outbuf; | |
125 | *length_out = outlen; | |
126 | free(o->internal); | |
127 | o->internal = NULL; | |
128 | break; | |
129 | } | |
130 | ||
131 | return SIGROK_OK; | |
132 | } | |
133 | ||
134 | static int data(struct output *o, char *data_in, uint64_t length_in, | |
135 | char **data_out, uint64_t *length_out) | |
136 | { | |
137 | struct context *ctx; | |
afc8e4de | 138 | unsigned int offset, outsize, p, curbit; |
e2ad47b5 UH |
139 | uint64_t sample, count = 0; |
140 | char *outbuf, *c; | |
141 | ||
142 | ctx = o->internal; | |
e273a904 HE |
143 | |
144 | outsize = 0; | |
145 | if (ctx->header) | |
146 | outsize = strlen(ctx->header); | |
99c1fc59 | 147 | outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */ |
e2ad47b5 UH |
148 | if (outbuf == NULL) |
149 | return SIGROK_ERR_MALLOC; | |
150 | if (ctx->header) { | |
151 | /* The header is still here, this must be the first packet. */ | |
152 | strncpy(outbuf, ctx->header, outsize); | |
153 | free(ctx->header); | |
154 | ctx->header = NULL; | |
155 | } else { | |
156 | outbuf[0] = 0; | |
157 | } | |
158 | ||
159 | /* TODO: Are disabled probes handled correctly? */ | |
160 | ||
161 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
99c1fc59 | 162 | offset += ctx->unitsize) { |
e2ad47b5 UH |
163 | memcpy(&sample, data_in + offset, ctx->unitsize); |
164 | ||
165 | /* The first column is a counter (needed for gnuplot). */ | |
166 | c = outbuf + strlen(outbuf); | |
99c1fc59 | 167 | sprintf(c, "%" PRIu64 "\t\t", count++); |
e2ad47b5 UH |
168 | |
169 | /* The next columns are the values of all channels. */ | |
170 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
171 | curbit = (sample & ((uint64_t) (1 << p))) != 0; | |
172 | c = outbuf + strlen(outbuf); | |
173 | sprintf(c, "%d ", curbit); | |
174 | } | |
175 | ||
176 | c = outbuf + strlen(outbuf); | |
177 | sprintf(c, "\n"); | |
178 | ||
179 | /* TODO: realloc() if strlen(outbuf) is almost "full"... */ | |
180 | } | |
181 | ||
182 | *data_out = outbuf; | |
183 | *length_out = strlen(outbuf); | |
184 | ||
185 | return SIGROK_OK; | |
186 | } | |
187 | ||
188 | struct output_format output_gnuplot = { | |
189 | "gnuplot", | |
190 | "Gnuplot", | |
191 | init, | |
192 | data, | |
193 | event, | |
194 | }; |