]> sigrok.org Git - libsigrok.git/blob - output/text/text.c
sr: change input/output modules to use struct sr_dev_inst *
[libsigrok.git] / output / text / text.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 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 "config.h"
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "text.h"
29
30 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *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((char *)outbuf + strlen((const char *)outbuf),
49                         "%*s:%s\n", max_probename_len,
50                         ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
51         }
52
53         /* Mark trigger with a ^ character. */
54         if (ctx->mark_trigger != -1)
55         {
56                 int space_offset = ctx->mark_trigger / 8;
57
58                 if (ctx->mode == MODE_ASCII)
59                         space_offset = 0;
60
61                 sprintf((char *)outbuf + strlen((const char *)outbuf),
62                         "T:%*s^\n", ctx->mark_trigger + space_offset, "");
63         }
64
65         memset(ctx->linebuf, 0, i * ctx->linebuf_len);
66 }
67
68 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
69 {
70         struct context *ctx;
71         struct sr_probe *probe;
72         GSList *l;
73         uint64_t *samplerate;
74         int num_probes, ret;
75         char *samplerate_s;
76
77         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
78                 sr_err("text out: %s: ctx malloc failed", __func__);
79                 return SR_ERR_MALLOC;
80         }
81
82         o->internal = ctx;
83         ctx->num_enabled_probes = 0;
84
85         for (l = o->sdi->probes; l; l = l->next) {
86                 probe = l->data;
87                 if (!probe->enabled)
88                         continue;
89                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
90         }
91
92         ctx->probelist[ctx->num_enabled_probes] = 0;
93         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
94         ctx->line_offset = 0;
95         ctx->spl_cnt = 0;
96         ctx->mark_trigger = -1;
97         ctx->mode = mode;
98
99         if (o->param && o->param[0]) {
100                 ctx->samples_per_line = strtoul(o->param, NULL, 10);
101                 if (ctx->samples_per_line < 1) {
102                         ret = SR_ERR;
103                         goto err;
104                 }
105         } else
106                 ctx->samples_per_line = default_spl;
107
108         if (!(ctx->header = g_try_malloc0(512))) {
109                 sr_err("text out: %s: ctx->header malloc failed", __func__);
110                 ret = SR_ERR_MALLOC;
111                 goto err;
112         }
113
114         snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
115         num_probes = g_slist_length(o->sdi->probes);
116         if (o->sdi->driver || sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
117                 ret = o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
118                                 (const void **)&samplerate, o->sdi);
119                 if (ret != SR_OK)
120                         goto err;
121                 if (!(samplerate_s = sr_samplerate_string(*samplerate))) {
122                         ret = SR_ERR;
123                         goto err;
124                 }
125                 snprintf(ctx->header + strlen(ctx->header),
126                          511 - strlen(ctx->header),
127                          "Acquisition with %d/%d probes at %s\n",
128                          ctx->num_enabled_probes, num_probes, samplerate_s);
129                 g_free(samplerate_s);
130         }
131
132         ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
133         if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
134                 sr_err("text out: %s: ctx->linebuf malloc failed", __func__);
135                 ret = SR_ERR_MALLOC;
136                 goto err;
137         }
138
139         if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
140                 sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
141                 ret = SR_ERR_MALLOC;
142         }
143
144 err:
145         if (ret != SR_OK) {
146                 g_free(ctx->header);
147                 g_free(ctx);
148         }
149
150         return ret;
151 }
152
153 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
154                   uint64_t *length_out)
155 {
156         struct context *ctx;
157         int outsize;
158         uint8_t *outbuf;
159
160         ctx = o->internal;
161         switch (event_type) {
162         case SR_DF_TRIGGER:
163                 ctx->mark_trigger = ctx->spl_cnt;
164                 *data_out = NULL;
165                 *length_out = 0;
166                 break;
167         case SR_DF_END:
168                 outsize = ctx->num_enabled_probes
169                                 * (ctx->samples_per_line + 20) + 512;
170                 if (!(outbuf = g_try_malloc0(outsize))) {
171                         sr_err("text out: %s: outbuf malloc failed", __func__);
172                         return SR_ERR_MALLOC;
173                 }
174                 flush_linebufs(ctx, outbuf);
175                 *data_out = outbuf;
176                 *length_out = strlen((const char *)outbuf);
177                 g_free(o->internal);
178                 o->internal = NULL;
179                 break;
180         default:
181                 *data_out = NULL;
182                 *length_out = 0;
183                 break;
184         }
185
186         return SR_OK;
187 }