]> sigrok.org Git - libsigrok.git/blame - output/ascii.c
output: Introduce output module API wrappers.
[libsigrok.git] / output / ascii.c
CommitLineData
6f64ebb2
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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 <string.h>
23#include <glib.h>
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
26
27#define LOG_PREFIX "output/hex"
28
29#define DEFAULT_SAMPLES_PER_LINE 74
30
31struct context {
32 unsigned int num_enabled_channels;
33 int samples_per_line;
34 int bit_cnt;
35 int spl_cnt;
36 int trigger;
37 int *channel_index;
38 char **channel_names;
39 char **line_values;
40 uint8_t *prev_sample;
41 GString **lines;
42 GString *header;
43};
44
45static int init(struct sr_output *o)
46{
47 struct context *ctx;
48 struct sr_channel *ch;
49 GSList *l;
50 GVariant *gvar;
dba3e682
BV
51 GHashTableIter iter;
52 gpointer key, value;
6f64ebb2
BV
53 uint64_t samplerate;
54 unsigned int i, j;
55 int spl, num_channels;
56 char *samplerate_s;
57
58 if (!o || !o->sdi)
59 return SR_ERR_ARG;
dba3e682
BV
60
61 spl = DEFAULT_SAMPLES_PER_LINE;
62 g_hash_table_iter_init(&iter, o->params);
63 while (g_hash_table_iter_next(&iter, &key, &value)) {
64 if (!strcmp(key, "width")) {
65 if ((spl = strtoul(value, NULL, 10)) < 1) {
66 sr_err("Invalid width.");
67 return SR_ERR_ARG;
68 }
69 } else {
70 sr_err("Unknown parameter '%s'.", key);
71 return SR_ERR_ARG;
72 }
73 }
74
6f64ebb2
BV
75 ctx = g_malloc0(sizeof(struct context));
76 o->internal = ctx;
77 ctx->trigger = -1;
6f64ebb2
BV
78 ctx->samples_per_line = spl;
79
80 for (l = o->sdi->channels; l; l = l->next) {
81 ch = l->data;
82 if (ch->type != SR_CHANNEL_LOGIC)
83 continue;
84 if (!ch->enabled)
85 continue;
86 ctx->num_enabled_channels++;
87 }
88 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
89 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
90 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
91 ctx->prev_sample = g_malloc(g_slist_length(o->sdi->channels));
92
93 j = 0;
94 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
95 ch = l->data;
96 if (ch->type != SR_CHANNEL_LOGIC)
97 continue;
98 if (!ch->enabled)
99 continue;
100 ctx->channel_index[j] = ch->index;
101 ctx->channel_names[j] = ch->name;
102 ctx->lines[j] = g_string_sized_new(80);
103 g_string_printf(ctx->lines[j], "%s:", ch->name);
104 j++;
105 }
106
107 ctx->header = g_string_sized_new(512);
108 g_string_printf(ctx->header, "%s\n", PACKAGE_STRING);
109 num_channels = g_slist_length(o->sdi->channels);
110 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
111 &gvar) == SR_OK) {
112 samplerate = g_variant_get_uint64(gvar);
113 samplerate_s = sr_samplerate_string(samplerate);
114 g_string_append_printf(ctx->header, "Acquisition with %d/%d channels at %s\n",
115 ctx->num_enabled_channels, num_channels, samplerate_s);
116 g_free(samplerate_s);
117 g_variant_unref(gvar);
118 }
119
120 return SR_OK;
121}
122
dba3e682
BV
123static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
124 GString **out)
6f64ebb2
BV
125{
126 const struct sr_datafeed_logic *logic;
127 struct context *ctx;
128 int idx, offset, curbit, prevbit;
129 uint64_t i, j;
130 gchar *p, c;
131
6f64ebb2
BV
132 *out = NULL;
133 if (!o || !o->sdi)
134 return SR_ERR_ARG;
135 if (!(ctx = o->internal))
136 return SR_ERR_ARG;
137
138 switch (packet->type) {
139 case SR_DF_TRIGGER:
140 ctx->trigger = ctx->spl_cnt;
141 break;
142 case SR_DF_LOGIC:
143 if (ctx->header) {
144 /* The header is still here, this must be the first packet. */
145 *out = ctx->header;
146 ctx->header = NULL;
147 } else
148 *out = g_string_sized_new(512);
149
150 logic = packet->payload;
151 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
152 ctx->spl_cnt++;
153 for (j = 0; j < ctx->num_enabled_channels; j++) {
154 idx = ctx->channel_index[j];
155 p = logic->data + i + idx / 8;
156 curbit = *p & (1 << (idx % 8));
157 prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8)));
158
159 c = curbit ? '"' : '.';
160 if (ctx->spl_cnt > 1) {
161 if (curbit < prevbit)
162 c = '\\';
163 else if (curbit > prevbit)
164 c = '/';
165 }
166 g_string_append_c(ctx->lines[j], c);
167
168 if (ctx->spl_cnt == ctx->samples_per_line) {
169 /* Flush line buffers. */
170 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
171 g_string_append_c(*out, '\n');
172 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
173 offset = ctx->trigger + ctx->trigger / 8;
174 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
175 ctx->trigger = -1;
176 }
177 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
178 }
179 }
180 if (ctx->spl_cnt == ctx->samples_per_line)
181 /* Line buffers were already flushed. */
182 ctx->spl_cnt = 0;
183 memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
184 }
185 break;
186 case SR_DF_END:
187 if (ctx->spl_cnt) {
188 /* Line buffers need flushing. */
189 *out = g_string_sized_new(512);
190 for (i = 0; i < ctx->num_enabled_channels; i++) {
191 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
192 g_string_append_c(*out, '\n');
193 }
194 }
195 break;
196 }
197
198 return SR_OK;
199}
200
201static int cleanup(struct sr_output *o)
202{
203 struct context *ctx;
204 unsigned int i;
205
206 if (!o)
207 return SR_ERR_ARG;
208
209 if (!(ctx = o->internal))
210 return SR_OK;
211
212 g_free(ctx->header);
213 g_free(ctx->channel_index);
214 g_free(ctx->prev_sample);
215 g_free(ctx->channel_names);
216 for (i = 0; i < ctx->num_enabled_channels; i++)
217 g_string_free(ctx->lines[i], TRUE);
218 g_free(ctx->lines);
219 if (ctx->header)
220 g_string_free(ctx->header, TRUE);
221 g_free(ctx);
222 o->internal = NULL;
223
224 return SR_OK;
225}
226
227SR_PRIV struct sr_output_format output_ascii = {
228 .id = "ascii",
229 .description = "ASCII",
230 .init = init,
231 .receive = receive,
232 .cleanup = cleanup,
233};
234
235