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