]>
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\ | |
5cca9adb | 37 | # Generated by: %s on %s%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 | ||
e2ad47b5 UH |
44 | static int init(struct output *o) |
45 | { | |
46 | /* Maximum header length */ | |
47 | #define MAX_HEADER_LEN 2048 | |
48 | ||
49 | struct context *ctx; | |
50 | struct probe *probe; | |
51 | GSList *l; | |
52 | uint64_t samplerate; | |
afc8e4de UH |
53 | unsigned int i; |
54 | int b, num_probes; | |
25e7d9b1 | 55 | char *c, *samplerate_s; |
7aae7462 | 56 | char wbuf[1000], comment[128]; |
5cca9adb | 57 | time_t t; |
e2ad47b5 | 58 | |
a821069b | 59 | if (!(ctx = calloc(1, sizeof(struct context)))) |
e2ad47b5 | 60 | return SIGROK_ERR_MALLOC; |
a821069b | 61 | |
e2ad47b5 UH |
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 | ||
a821069b UH |
75 | if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { |
76 | free(ctx); | |
e2ad47b5 | 77 | return SIGROK_ERR_MALLOC; |
a821069b UH |
78 | } |
79 | ||
e2ad47b5 UH |
80 | num_probes = g_slist_length(o->device->probes); |
81 | /* TODO: Handle num_probes == 0, too many probes, etc. */ | |
a821069b UH |
82 | |
83 | comment[0] = '\0'; | |
7aae7462 BV |
84 | if (o->device->plugin) { |
85 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( | |
86 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
a821069b UH |
87 | if (!(samplerate_s = sigrok_samplerate_string(samplerate))) { |
88 | free(ctx->header); | |
89 | free(ctx); | |
7aae7462 | 90 | return SIGROK_ERR; |
a821069b UH |
91 | } |
92 | snprintf(comment, 127, gnuplot_header_comment, | |
93 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
7aae7462 BV |
94 | free(samplerate_s); |
95 | } | |
e2ad47b5 UH |
96 | |
97 | /* Columns / channels */ | |
98 | wbuf[0] = '\0'; | |
99 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
100 | c = (char *)&wbuf + strlen((char *)&wbuf); | |
101 | sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]); | |
102 | } | |
103 | ||
104 | /* TODO: date: File or signals? Make y/n configurable. */ | |
105 | /* TODO: Timescale */ | |
5cca9adb | 106 | t = time(NULL); |
e2ad47b5 | 107 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, |
5cca9adb UH |
108 | PACKAGE_STRING, ctime(&t), comment, 1, "ns", |
109 | (char *)&wbuf); | |
e2ad47b5 UH |
110 | |
111 | /* TODO: Handle snprintf errors. */ | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | static int event(struct output *o, int event_type, char **data_out, | |
117 | uint64_t *length_out) | |
118 | { | |
119 | struct context *ctx; | |
e2ad47b5 UH |
120 | |
121 | ctx = o->internal; | |
99c1fc59 | 122 | switch (event_type) { |
e2ad47b5 | 123 | case DF_TRIGGER: |
a821069b | 124 | /* TODO */ |
e2ad47b5 UH |
125 | break; |
126 | case DF_END: | |
caf62e22 UH |
127 | *data_out = NULL; |
128 | *length_out = 0; | |
e2ad47b5 UH |
129 | free(o->internal); |
130 | o->internal = NULL; | |
131 | break; | |
132 | } | |
133 | ||
134 | return SIGROK_OK; | |
135 | } | |
136 | ||
137 | static int data(struct output *o, char *data_in, uint64_t length_in, | |
138 | char **data_out, uint64_t *length_out) | |
139 | { | |
140 | struct context *ctx; | |
607b58de | 141 | unsigned int i, outsize, p, curbit; |
e2ad47b5 UH |
142 | uint64_t sample, count = 0; |
143 | char *outbuf, *c; | |
144 | ||
145 | ctx = o->internal; | |
e273a904 HE |
146 | outsize = 0; |
147 | if (ctx->header) | |
148 | outsize = strlen(ctx->header); | |
a821069b UH |
149 | |
150 | /* FIXME: Use realloc(). */ | |
1e32053c | 151 | if (!(outbuf = calloc(1, outsize + 1 + 1000000))) |
a821069b UH |
152 | return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */ |
153 | ||
154 | outbuf[0] = '\0'; | |
e2ad47b5 UH |
155 | if (ctx->header) { |
156 | /* The header is still here, this must be the first packet. */ | |
157 | strncpy(outbuf, ctx->header, outsize); | |
158 | free(ctx->header); | |
159 | ctx->header = NULL; | |
e2ad47b5 UH |
160 | } |
161 | ||
162 | /* TODO: Are disabled probes handled correctly? */ | |
163 | ||
607b58de UH |
164 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
165 | memcpy(&sample, data_in + i, ctx->unitsize); | |
e2ad47b5 UH |
166 | |
167 | /* The first column is a counter (needed for gnuplot). */ | |
168 | c = outbuf + strlen(outbuf); | |
99c1fc59 | 169 | sprintf(c, "%" PRIu64 "\t\t", count++); |
e2ad47b5 UH |
170 | |
171 | /* The next columns are the values of all channels. */ | |
172 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
173 | curbit = (sample & ((uint64_t) (1 << p))) != 0; | |
174 | c = outbuf + strlen(outbuf); | |
175 | sprintf(c, "%d ", curbit); | |
176 | } | |
177 | ||
178 | c = outbuf + strlen(outbuf); | |
179 | sprintf(c, "\n"); | |
180 | ||
181 | /* TODO: realloc() if strlen(outbuf) is almost "full"... */ | |
182 | } | |
183 | ||
184 | *data_out = outbuf; | |
185 | *length_out = strlen(outbuf); | |
186 | ||
187 | return SIGROK_OK; | |
188 | } | |
189 | ||
190 | struct output_format output_gnuplot = { | |
191 | "gnuplot", | |
192 | "Gnuplot", | |
193 | init, | |
194 | data, | |
195 | event, | |
196 | }; |