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