]> sigrok.org Git - libsigrok.git/blob - src/output/ascii.c
output: Finish output module API wrappers.
[libsigrok.git] / src / output / ascii.c
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
31 struct context {
32         unsigned int num_enabled_channels;
33         int samples_per_line;
34         int bit_cnt;
35         int spl_cnt;
36         int trigger;
37         uint64_t samplerate;
38         int *channel_index;
39         char **channel_names;
40         char **line_values;
41         uint8_t *prev_sample;
42         gboolean header_done;
43         GString **lines;
44         GString *header;
45 };
46
47 static int init(struct sr_output *o, GHashTable *options)
48 {
49         struct context *ctx;
50         struct sr_channel *ch;
51         GSList *l;
52         GHashTableIter iter;
53         gpointer key, value;
54         unsigned int i, j;
55         uint32_t spl;
56
57         if (!o || !o->sdi)
58                 return SR_ERR_ARG;
59
60         spl = DEFAULT_SAMPLES_PER_LINE;
61         if (options) {
62                 g_hash_table_iter_init(&iter, options);
63                 while (g_hash_table_iter_next(&iter, &key, &value)) {
64                         if (!strcmp(key, "width")) {
65                                 if (!g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
66                                         sr_err("Invalid type for 'width' option.");
67                                         return SR_ERR_ARG;
68                                 }
69                                 if (!(spl = g_variant_get_uint32(value))) {
70                                         sr_err("Invalid width.");
71                                         return SR_ERR_ARG;
72                                 }
73                         } else {
74                                 sr_err("Unknown option '%s'.", key);
75                         }
76                 }
77         }
78
79         ctx = g_malloc0(sizeof(struct context));
80         o->internal = ctx;
81         ctx->trigger = -1;
82         ctx->samples_per_line = spl;
83
84         for (l = o->sdi->channels; l; l = l->next) {
85                 ch = l->data;
86                 if (ch->type != SR_CHANNEL_LOGIC)
87                         continue;
88                 if (!ch->enabled)
89                         continue;
90                 ctx->num_enabled_channels++;
91         }
92         ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
93         ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
94         ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
95         ctx->prev_sample = g_malloc(g_slist_length(o->sdi->channels));
96
97         j = 0;
98         for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
99                 ch = l->data;
100                 if (ch->type != SR_CHANNEL_LOGIC)
101                         continue;
102                 if (!ch->enabled)
103                         continue;
104                 ctx->channel_index[j] = ch->index;
105                 ctx->channel_names[j] = ch->name;
106                 ctx->lines[j] = g_string_sized_new(80);
107                 g_string_printf(ctx->lines[j], "%s:", ch->name);
108                 j++;
109         }
110
111         return SR_OK;
112 }
113
114 static GString *gen_header(const struct sr_output *o)
115 {
116         struct context *ctx;
117         GVariant *gvar;
118         GString *header;
119         int num_channels;
120         char *samplerate_s;
121
122         ctx = o->internal;
123         if (ctx->samplerate == 0) {
124                 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
125                                 &gvar) == SR_OK) {
126                         ctx->samplerate = g_variant_get_uint64(gvar);
127                         g_variant_unref(gvar);
128                 }
129         }
130
131         header = g_string_sized_new(512);
132         g_string_printf(header, "%s\n", PACKAGE_STRING);
133         num_channels = g_slist_length(o->sdi->channels);
134         g_string_append_printf(header, "Acquisition with %d/%d channels",
135                         ctx->num_enabled_channels, num_channels);
136         if (ctx->samplerate != 0) {
137                 samplerate_s = sr_samplerate_string(ctx->samplerate);
138                 g_string_append_printf(header, " at %s", samplerate_s);
139                 g_free(samplerate_s);
140         }
141         g_string_append_printf(header, "\n");
142
143         return header;
144 }
145
146 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
147                 GString **out)
148 {
149         const struct sr_datafeed_meta *meta;
150         const struct sr_datafeed_logic *logic;
151         const struct sr_config *src;
152         GSList *l;
153         struct context *ctx;
154         int idx, offset, curbit, prevbit;
155         uint64_t i, j;
156         gchar *p, c;
157
158         *out = NULL;
159         if (!o || !o->sdi)
160                 return SR_ERR_ARG;
161         if (!(ctx = o->internal))
162                 return SR_ERR_ARG;
163
164         switch (packet->type) {
165         case SR_DF_META:
166                 meta = packet->payload;
167                 for (l = meta->config; l; l = l->next) {
168                         src = l->data;
169                         if (src->key != SR_CONF_SAMPLERATE)
170                                 continue;
171                         ctx->samplerate = g_variant_get_uint64(src->data);
172                 }
173                 break;
174         case SR_DF_TRIGGER:
175                 ctx->trigger = ctx->spl_cnt;
176                 break;
177         case SR_DF_LOGIC:
178                 if (!ctx->header_done) {
179                         *out = gen_header(o);
180                         ctx->header_done = TRUE;
181                 } else
182                         *out = g_string_sized_new(512);
183
184                 logic = packet->payload;
185                 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
186                         ctx->spl_cnt++;
187                         for (j = 0; j < ctx->num_enabled_channels; j++) {
188                                 idx = ctx->channel_index[j];
189                                 p = logic->data + i + idx / 8;
190                                 curbit = *p & (1 << (idx % 8));
191                                 prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8)));
192
193                                 c = curbit ? '"' : '.';
194                                 if (ctx->spl_cnt > 1) {
195                                         if (curbit < prevbit)
196                                                 c = '\\';
197                                         else if (curbit > prevbit)
198                                                 c = '/';
199                                 }
200                                 g_string_append_c(ctx->lines[j], c);
201
202                                 if (ctx->spl_cnt == ctx->samples_per_line) {
203                                         /* Flush line buffers. */
204                                         g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
205                                         g_string_append_c(*out, '\n');
206                                         if (j == ctx->num_enabled_channels  - 1 && ctx->trigger > -1) {
207                                                 offset = ctx->trigger + ctx->trigger / 8;
208                                                 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
209                                                 ctx->trigger = -1;
210                                         }
211                                         g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
212                                 }
213                         }
214                         if (ctx->spl_cnt == ctx->samples_per_line)
215                                 /* Line buffers were already flushed. */
216                                 ctx->spl_cnt = 0;
217                         memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
218                 }
219                 break;
220         case SR_DF_END:
221                 if (ctx->spl_cnt) {
222                         /* Line buffers need flushing. */
223                         *out = g_string_sized_new(512);
224                         for (i = 0; i < ctx->num_enabled_channels; i++) {
225                                 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
226                                 g_string_append_c(*out, '\n');
227                         }
228                 }
229                 break;
230         }
231
232         return SR_OK;
233 }
234
235 static int cleanup(struct sr_output *o)
236 {
237         struct context *ctx;
238         unsigned int i;
239
240         if (!o)
241                 return SR_ERR_ARG;
242
243         if (!(ctx = o->internal))
244                 return SR_OK;
245
246         g_free(ctx->channel_index);
247         g_free(ctx->prev_sample);
248         g_free(ctx->channel_names);
249         for (i = 0; i < ctx->num_enabled_channels; i++)
250                 g_string_free(ctx->lines[i], TRUE);
251         g_free(ctx->lines);
252         g_free(ctx);
253         o->internal = NULL;
254
255         return SR_OK;
256 }
257
258 static struct sr_option options[] = {
259         { "width", "Width", "Number of samples per line", NULL, NULL },
260         { 0 }
261 };
262
263 static struct sr_option *get_options(gboolean cached)
264 {
265         if (cached)
266                 return options;
267
268         options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE);
269         g_variant_ref_sink(options[0].def);
270
271         return options;
272 }
273
274 SR_PRIV struct sr_output_module output_ascii = {
275         .id = "ascii",
276         .name = "ASCII",
277         .desc = "ASCII art",
278         .options = get_options,
279         .init = init,
280         .receive = receive,
281         .cleanup = cleanup,
282 };
283
284