]>
Commit | Line | Data |
---|---|---|
97554432 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
97554432 | 3 | * |
13d8e03c | 4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
97554432 BV |
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> | |
545f9786 | 25 | #include "config.h" /* Needed for PACKAGE_STRING and others. */ |
45c59c8b BV |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
97554432 BV |
28 | #include "text.h" |
29 | ||
a944a84b UH |
30 | /* Message logging helpers with driver-specific prefix string. */ |
31 | #define DRIVER_LOG_DOMAIN "output/text: " | |
32 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
33 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
34 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
35 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
36 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
37 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
38 | ||
054e6709 | 39 | SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf) |
97554432 BV |
40 | { |
41 | static int max_probename_len = 0; | |
42 | int len, i; | |
d53e4e8d ML |
43 | GSList *l; |
44 | char *probe_name; | |
97554432 BV |
45 | |
46 | if (ctx->linebuf[0] == 0) | |
47 | return; | |
48 | ||
49 | if (max_probename_len == 0) { | |
50 | /* First time through... */ | |
d53e4e8d ML |
51 | for (l = ctx->probenames; l; l = l->next) { |
52 | probe_name = l->data; | |
53 | len = strlen(probe_name); | |
97554432 BV |
54 | if (len > max_probename_len) |
55 | max_probename_len = len; | |
56 | } | |
57 | } | |
58 | ||
d53e4e8d ML |
59 | for (i = 0, l = ctx->probenames; l; l = l->next, i++) { |
60 | probe_name = l->data; | |
054e6709 UH |
61 | sprintf((char *)outbuf + strlen((const char *)outbuf), |
62 | "%*s:%s\n", max_probename_len, | |
d53e4e8d | 63 | probe_name, ctx->linebuf + i * ctx->linebuf_len); |
97554432 BV |
64 | } |
65 | ||
66 | /* Mark trigger with a ^ character. */ | |
67 | if (ctx->mark_trigger != -1) | |
68 | { | |
69 | int space_offset = ctx->mark_trigger / 8; | |
70 | ||
71 | if (ctx->mode == MODE_ASCII) | |
72 | space_offset = 0; | |
73 | ||
054e6709 UH |
74 | sprintf((char *)outbuf + strlen((const char *)outbuf), |
75 | "T:%*s^\n", ctx->mark_trigger + space_offset, ""); | |
97554432 BV |
76 | } |
77 | ||
78 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); | |
79 | } | |
80 | ||
7c1d391c | 81 | SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode) |
97554432 BV |
82 | { |
83 | struct context *ctx; | |
1afe8989 | 84 | struct sr_probe *probe; |
97554432 | 85 | GSList *l; |
ec4063b8 BV |
86 | GVariant *gvar; |
87 | uint64_t samplerate; | |
5c3c1241 | 88 | int num_probes, ret; |
97554432 BV |
89 | char *samplerate_s; |
90 | ||
133a37bf | 91 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
a944a84b | 92 | sr_err("%s: ctx malloc failed", __func__); |
e46b8fb1 | 93 | return SR_ERR_MALLOC; |
133a37bf | 94 | } |
97554432 BV |
95 | |
96 | o->internal = ctx; | |
97 | ctx->num_enabled_probes = 0; | |
d53e4e8d | 98 | ctx->probenames = NULL; |
97554432 | 99 | |
5c3c1241 | 100 | for (l = o->sdi->probes; l; l = l->next) { |
97554432 BV |
101 | probe = l->data; |
102 | if (!probe->enabled) | |
103 | continue; | |
d53e4e8d ML |
104 | ctx->probenames = g_slist_append(ctx->probenames, probe->name); |
105 | ctx->num_enabled_probes++; | |
97554432 BV |
106 | } |
107 | ||
97554432 BV |
108 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; |
109 | ctx->line_offset = 0; | |
110 | ctx->spl_cnt = 0; | |
111 | ctx->mark_trigger = -1; | |
112 | ctx->mode = mode; | |
113 | ||
aee878fa | 114 | ret = SR_OK; |
97554432 BV |
115 | if (o->param && o->param[0]) { |
116 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
5c3c1241 BV |
117 | if (ctx->samples_per_line < 1) { |
118 | ret = SR_ERR; | |
119 | goto err; | |
120 | } | |
97554432 BV |
121 | } else |
122 | ctx->samples_per_line = default_spl; | |
123 | ||
133a37bf | 124 | if (!(ctx->header = g_try_malloc0(512))) { |
a944a84b | 125 | sr_err("%s: ctx->header malloc failed", __func__); |
5c3c1241 BV |
126 | ret = SR_ERR_MALLOC; |
127 | goto err; | |
97554432 BV |
128 | } |
129 | ||
130 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); | |
5c3c1241 | 131 | num_probes = g_slist_length(o->sdi->probes); |
af51a771 BV |
132 | if (sr_config_get(o->sdi->driver, SR_CONF_SAMPLERATE, &gvar, |
133 | o->sdi) == SR_OK) { | |
ec4063b8 | 134 | samplerate = g_variant_get_uint64(gvar); |
af51a771 | 135 | g_variant_unref(gvar); |
ec4063b8 | 136 | if (!(samplerate_s = sr_samplerate_string(samplerate))) { |
5c3c1241 BV |
137 | ret = SR_ERR; |
138 | goto err; | |
97554432 BV |
139 | } |
140 | snprintf(ctx->header + strlen(ctx->header), | |
141 | 511 - strlen(ctx->header), | |
142 | "Acquisition with %d/%d probes at %s\n", | |
143 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
133a37bf | 144 | g_free(samplerate_s); |
97554432 BV |
145 | } |
146 | ||
147 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; | |
133a37bf | 148 | if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) { |
a944a84b | 149 | sr_err("%s: ctx->linebuf malloc failed", __func__); |
5c3c1241 BV |
150 | ret = SR_ERR_MALLOC; |
151 | goto err; | |
97554432 | 152 | } |
5c3c1241 | 153 | |
133a37bf | 154 | if (!(ctx->linevalues = g_try_malloc0(num_probes))) { |
a944a84b | 155 | sr_err("%s: ctx->linevalues malloc failed", __func__); |
5c3c1241 BV |
156 | ret = SR_ERR_MALLOC; |
157 | } | |
158 | ||
3a581560 ML |
159 | if (mode == MODE_ASCII && |
160 | !(ctx->prevsample = g_try_malloc0(num_probes / 8))) { | |
161 | sr_err("%s: ctx->prevsample malloc failed", __func__); | |
162 | ret = SR_ERR_MALLOC; | |
163 | } | |
164 | ||
5c3c1241 BV |
165 | err: |
166 | if (ret != SR_OK) { | |
133a37bf UH |
167 | g_free(ctx->header); |
168 | g_free(ctx); | |
97554432 BV |
169 | } |
170 | ||
5c3c1241 | 171 | return ret; |
97554432 BV |
172 | } |
173 | ||
054e6709 | 174 | SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out, |
7c1d391c | 175 | uint64_t *length_out) |
97554432 BV |
176 | { |
177 | struct context *ctx; | |
178 | int outsize; | |
054e6709 | 179 | uint8_t *outbuf; |
97554432 BV |
180 | |
181 | ctx = o->internal; | |
182 | switch (event_type) { | |
5a2326a7 | 183 | case SR_DF_TRIGGER: |
97554432 BV |
184 | ctx->mark_trigger = ctx->spl_cnt; |
185 | *data_out = NULL; | |
186 | *length_out = 0; | |
187 | break; | |
5a2326a7 | 188 | case SR_DF_END: |
97554432 BV |
189 | outsize = ctx->num_enabled_probes |
190 | * (ctx->samples_per_line + 20) + 512; | |
133a37bf | 191 | if (!(outbuf = g_try_malloc0(outsize))) { |
a944a84b | 192 | sr_err("%s: outbuf malloc failed", __func__); |
e46b8fb1 | 193 | return SR_ERR_MALLOC; |
133a37bf | 194 | } |
97554432 BV |
195 | flush_linebufs(ctx, outbuf); |
196 | *data_out = outbuf; | |
054e6709 | 197 | *length_out = strlen((const char *)outbuf); |
133a37bf | 198 | g_free(o->internal); |
97554432 BV |
199 | o->internal = NULL; |
200 | break; | |
201 | default: | |
202 | *data_out = NULL; | |
203 | *length_out = 0; | |
204 | break; | |
205 | } | |
206 | ||
e46b8fb1 | 207 | return SR_OK; |
97554432 | 208 | } |