]> sigrok.org Git - libsigrok.git/blob - output/text/text.c
don't just assume a device has a samplerate setting
[libsigrok.git] / output / text / text.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <sigrok.h>
26 #include "config.h"
27 #include "text.h"
28
29
30 void flush_linebufs(struct context *ctx, char *outbuf)
31 {
32         static int max_probename_len = 0;
33         int len, i;
34
35         if (ctx->linebuf[0] == 0)
36                 return;
37
38         if (max_probename_len == 0) {
39                 /* First time through... */
40                 for (i = 0; ctx->probelist[i]; i++) {
41                         len = strlen(ctx->probelist[i]);
42                         if (len > max_probename_len)
43                                 max_probename_len = len;
44                 }
45         }
46
47         for (i = 0; ctx->probelist[i]; i++) {
48                 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
49                         ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
50         }
51
52         /* Mark trigger with a ^ character. */
53         if (ctx->mark_trigger != -1)
54         {
55                 int space_offset = ctx->mark_trigger / 8;
56
57                 if (ctx->mode == MODE_ASCII)
58                         space_offset = 0;
59
60                 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
61                         ctx->mark_trigger + space_offset, "");
62         }
63
64         memset(ctx->linebuf, 0, i * ctx->linebuf_len);
65 }
66
67 int init(struct sr_output *o, int default_spl, enum outputmode mode)
68 {
69         struct context *ctx;
70         struct probe *probe;
71         GSList *l;
72         uint64_t samplerate;
73         int num_probes;
74         char *samplerate_s;
75
76         if (!(ctx = calloc(1, sizeof(struct context))))
77                 return SR_ERR_MALLOC;
78
79         o->internal = ctx;
80         ctx->num_enabled_probes = 0;
81
82         for (l = o->device->probes; l; l = l->next) {
83                 probe = l->data;
84                 if (!probe->enabled)
85                         continue;
86                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
87         }
88
89         ctx->probelist[ctx->num_enabled_probes] = 0;
90         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
91         ctx->line_offset = 0;
92         ctx->spl_cnt = 0;
93         ctx->mark_trigger = -1;
94         ctx->mode = mode;
95
96         if (o->param && o->param[0]) {
97                 ctx->samples_per_line = strtoul(o->param, NULL, 10);
98                 if (ctx->samples_per_line < 1)
99                         return SR_ERR;
100         } else
101                 ctx->samples_per_line = default_spl;
102
103         if (!(ctx->header = malloc(512))) {
104                 free(ctx);
105                 return SR_ERR_MALLOC;
106         }
107
108         snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
109         num_probes = g_slist_length(o->device->probes);
110         if (o->device->plugin || device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
111                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
112                                 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
113                 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
114                         free(ctx->header);
115                         free(ctx);
116                         return SR_ERR;
117                 }
118                 snprintf(ctx->header + strlen(ctx->header),
119                          511 - strlen(ctx->header),
120                          "Acquisition with %d/%d probes at %s\n",
121                          ctx->num_enabled_probes, num_probes, samplerate_s);
122                 free(samplerate_s);
123         }
124
125         ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
126         if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
127                 free(ctx->header);
128                 free(ctx);
129                 return SR_ERR_MALLOC;
130         }
131         if (!(ctx->linevalues = calloc(1, num_probes))) {
132                 free(ctx->header);
133                 free(ctx);
134                 return SR_ERR_MALLOC;
135         }
136
137         return SR_OK;
138 }
139
140 int event(struct sr_output *o, int event_type, char **data_out,
141                  uint64_t *length_out)
142 {
143         struct context *ctx;
144         int outsize;
145         char *outbuf;
146
147         ctx = o->internal;
148         switch (event_type) {
149         case SR_DF_TRIGGER:
150                 ctx->mark_trigger = ctx->spl_cnt;
151                 *data_out = NULL;
152                 *length_out = 0;
153                 break;
154         case SR_DF_END:
155                 outsize = ctx->num_enabled_probes
156                                 * (ctx->samples_per_line + 20) + 512;
157                 if (!(outbuf = calloc(1, outsize)))
158                         return SR_ERR_MALLOC;
159                 flush_linebufs(ctx, outbuf);
160                 *data_out = outbuf;
161                 *length_out = strlen(outbuf);
162                 free(o->internal);
163                 o->internal = NULL;
164                 break;
165         default:
166                 *data_out = NULL;
167                 *length_out = 0;
168                 break;
169         }
170
171         return SR_OK;
172 }
173