]> sigrok.org Git - libsigrok.git/blob - output/output_analog.c
Merge branch 'master' of git://sigrok.git.sourceforge.net/gitroot/sigrok/sigrok
[libsigrok.git] / output / output_analog.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
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 <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sigrok.h>
24 #include <config.h>
25
26
27 #define DEFAULT_SAMPLES_PER_LINE 10
28 /* -10.25 */
29 #define VALUE_LEN 6
30
31 struct context {
32         char *header;
33         unsigned int num_enabled_probes;
34         char *probelist[MAX_NUM_PROBES];
35         int samples_per_line;
36         unsigned int unitsize;
37         int line_offset;
38         int linebuf_len;
39         char *linebuf;
40         int spl_cnt;
41         int mark_trigger;
42 };
43
44
45 static void flush_linebufs(struct context *ctx, char *outbuf)
46 {
47         static int max_probename_len = 0;
48         int len, i;
49
50         if (ctx->linebuf[0] == 0)
51                 return;
52
53         if (max_probename_len == 0) {
54                 /* First time through... */
55                 for (i = 0; ctx->probelist[i]; i++) {
56                         len = strlen(ctx->probelist[i]);
57                         if (len > max_probename_len)
58                                 max_probename_len = len;
59                 }
60         }
61
62         for (i = 0; ctx->probelist[i]; i++) {
63                 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
64                         ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
65         }
66
67         /* Mark trigger with a ^ character. */
68         if (ctx->mark_trigger != -1)
69                 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
70                         ctx->mark_trigger * (VALUE_LEN+1), "");
71
72         memset(ctx->linebuf, 0, i * ctx->linebuf_len);
73 }
74
75 static int init(struct output *o)
76 {
77         struct context *ctx;
78         struct probe *probe;
79         GSList *l;
80         uint64_t samplerate;
81         int num_probes;
82         char *samplerate_s;
83
84         if (!(ctx = calloc(1, sizeof(struct context))))
85                 return SIGROK_ERR_MALLOC;
86
87         if (!(ctx->header = malloc(512))) {
88                 free(ctx);
89                 return SIGROK_ERR_MALLOC;
90         }
91
92         o->internal = ctx;
93         ctx->num_enabled_probes = 0;
94         ctx->mark_trigger = -1;
95         if (o->param && o->param[0]) {
96                 ctx->samples_per_line = strtoul(o->param, NULL, 10);
97                 if (ctx->samples_per_line < 1)
98                         return SIGROK_ERR;
99         } else
100                 ctx->samples_per_line = DEFAULT_SAMPLES_PER_LINE;
101
102         for (l = o->device->probes; l; l = l->next) {
103                 probe = l->data;
104                 if (!probe->enabled)
105                         continue;
106                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
107         }
108         ctx->unitsize = sizeof(double) * ctx->num_enabled_probes;
109
110         snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
111         if (o->device->plugin) {
112                 num_probes = g_slist_length(o->device->probes);
113                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
114                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
115                 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
116                         free(ctx->header);
117                         free(ctx);
118                         return SIGROK_ERR;
119                 }
120                 snprintf(ctx->header + strlen(ctx->header),
121                          511 - strlen(ctx->header),
122                          "Acquisition with %d/%d probes at %s\n",
123                          ctx->num_enabled_probes, num_probes, samplerate_s);
124                 free(samplerate_s);
125         }
126
127         ctx->linebuf_len = MAX_PROBENAME_LEN + ctx->samples_per_line * VALUE_LEN
128                         + ctx->samples_per_line + 4;
129         if (!(ctx->linebuf = calloc(1, ctx->num_enabled_probes * ctx->linebuf_len))) {
130                 free(ctx->header);
131                 free(ctx);
132                 return SIGROK_ERR_MALLOC;
133         }
134
135         return 0;
136 }
137
138 static int data(struct output *o, char *data_in, uint64_t length_in,
139                 char **data_out, uint64_t *length_out)
140 {
141         struct context *ctx;
142         double probe_sample;
143         unsigned int max_linelen, outsize, offset, p;
144         char *outbuf, s[VALUE_LEN+2];
145
146         ctx = o->internal;
147         max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line * VALUE_LEN
148                         + ctx->samples_per_line / 8;
149         outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
150                         / ctx->samples_per_line * max_linelen + 512;
151         if (!(outbuf = calloc(1, outsize + 1)))
152                 return SIGROK_ERR_MALLOC;
153
154         outbuf[0] = '\0';
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;
160         }
161
162         if (length_in >= ctx->unitsize) {
163                 for (offset = 0; offset <= length_in - ctx->unitsize;
164                      offset += ctx->unitsize) {
165                         for (p = 0; p < ctx->num_enabled_probes; p++) {
166                                 memcpy(&probe_sample, data_in + offset * p, sizeof(double));
167                                 snprintf(s, VALUE_LEN+2, "% 6.2f ", probe_sample);
168                                 strcat(ctx->linebuf + p * ctx->linebuf_len + ctx->line_offset, s);
169                                 ctx->line_offset += strlen(s);
170                                 ctx->spl_cnt++;
171                         }
172
173                         /* End of line. */
174                         if (ctx->spl_cnt >= ctx->samples_per_line) {
175                                 flush_linebufs(ctx, outbuf);
176                                 ctx->line_offset = ctx->spl_cnt = 0;
177                                 ctx->mark_trigger = -1;
178                         }
179
180
181                 }
182         } else {
183                 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
184         }
185
186         *data_out = outbuf;
187         *length_out = strlen(outbuf);
188
189         return SIGROK_OK;
190 }
191
192 static int event(struct output *o, int event_type, char **data_out,
193                  uint64_t *length_out)
194 {
195         struct context *ctx;
196
197         ctx = o->internal;
198         switch (event_type) {
199         case DF_TRIGGER:
200                 ctx->mark_trigger = ctx->spl_cnt;
201                 break;
202         case DF_END:
203                 if (ctx->header)
204                         free(ctx->header);
205                 free(ctx->linebuf);
206                 free(o->internal);
207                 o->internal = NULL;
208                 break;
209         }
210
211         *data_out = NULL;
212         *length_out = 0;
213
214         return SIGROK_OK;
215 }
216
217 struct output_format output_analog = {
218         "analog",
219         "Analog data",
220         DF_ANALOG,
221         init,
222         data,
223         event,
224 };
225