]> sigrok.org Git - libsigrok.git/blame - output/hex.c
Removed ALSA driver.
[libsigrok.git] / output / hex.c
CommitLineData
8d3af2e8
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <string.h>
22#include <glib.h>
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25
26#define LOG_PREFIX "output/hex"
27
28#define DEFAULT_SAMPLES_PER_LINE 192
29
30struct context {
31 unsigned int num_enabled_channels;
32 int samples_per_line;
33 int bit_cnt;
34 int spl_cnt;
35 int trigger;
36 int *channel_index;
37 char **channel_names;
38 char **line_values;
39 uint8_t *sample_buf;
40 GString **lines;
41 GString *header;
42};
43
44static int init(struct sr_output *o)
45{
46 struct context *ctx;
47 struct sr_channel *ch;
48 GSList *l;
49 GVariant *gvar;
50 uint64_t samplerate;
51 unsigned int i, j;
52 int spl, num_channels;
53 char *samplerate_s;
54
55 if (!o || !o->sdi)
56 return SR_ERR_ARG;
57 ctx = g_malloc0(sizeof(struct context));
58 o->internal = ctx;
59 ctx->trigger = -1;
60
61 if (o->param && o->param[0]) {
62 if ((spl = strtoul(o->param, NULL, 10)) < 1)
63 return SR_ERR_ARG;
64 } else
65 spl = DEFAULT_SAMPLES_PER_LINE;
66 ctx->samples_per_line = spl;
67
68 for (l = o->sdi->channels; l; l = l->next) {
69 ch = l->data;
70 if (ch->type != SR_CHANNEL_LOGIC)
71 continue;
72 if (!ch->enabled)
73 continue;
74 ctx->num_enabled_channels++;
75 }
76 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
77 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
78 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
79 ctx->sample_buf = g_malloc(ctx->num_enabled_channels);
80
81 j = 0;
82 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
83 ch = l->data;
84 if (ch->type != SR_CHANNEL_LOGIC)
85 continue;
86 if (!ch->enabled)
87 continue;
88 ctx->channel_index[j] = ch->index;
89 ctx->channel_names[j] = ch->name;
90 ctx->lines[j] = g_string_sized_new(80);
91 ctx->sample_buf[j] = 0;
92 g_string_printf(ctx->lines[j], "%s:", ch->name);
93 j++;
94 }
95
96 ctx->header = g_string_sized_new(512);
97 g_string_printf(ctx->header, "%s\n", PACKAGE_STRING);
98 num_channels = g_slist_length(o->sdi->channels);
99 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
100 &gvar) == SR_OK) {
101 samplerate = g_variant_get_uint64(gvar);
102 samplerate_s = sr_samplerate_string(samplerate);
103 g_string_append_printf(ctx->header, "Acquisition with %d/%d channels at %s\n",
104 ctx->num_enabled_channels, num_channels, samplerate_s);
105 g_free(samplerate_s);
106 g_variant_unref(gvar);
107 }
108
109 return SR_OK;
110}
111
112static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
113 const struct sr_datafeed_packet *packet, GString **out)
114{
115 const struct sr_datafeed_logic *logic;
116 struct context *ctx;
117 int idx, pos, offset;
118 uint64_t i, j;
119 gchar *p;
120
121 (void)sdi;
122
123 *out = NULL;
124 if (!o || !o->sdi)
125 return SR_ERR_ARG;
126 if (!(ctx = o->internal))
127 return SR_ERR_ARG;
128
129 switch (packet->type) {
130 case SR_DF_TRIGGER:
131 ctx->trigger = ctx->spl_cnt;
132 break;
133 case SR_DF_LOGIC:
134 if (ctx->header) {
135 /* The header is still here, this must be the first packet. */
136 *out = ctx->header;
137 ctx->header = NULL;
138 } else
139 *out = g_string_sized_new(512);
140
141 logic = packet->payload;
142 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
143 ctx->spl_cnt++;
144 pos = ctx->spl_cnt & 7;
145 for (j = 0; j < ctx->num_enabled_channels; j++) {
146 idx = ctx->channel_index[j];
147 p = logic->data + i + idx / 8;
148 ctx->sample_buf[j] <<= 1;
149 if (*p & (1 << (idx % 8)))
150 ctx->sample_buf[j] |= 1;
151 if (ctx->spl_cnt && pos == 0) {
152 /* Buffered a byte's worth, output hex. */
153 g_string_append_printf(ctx->lines[j], "%.2x ",
154 ctx->sample_buf[j]);
155 ctx->sample_buf[j] = 0;
156 }
157
158 if (ctx->spl_cnt == ctx->samples_per_line) {
159 /* Flush line buffers. */
160 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
161 g_string_append_c(*out, '\n');
162 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
163 offset = ctx->trigger + ctx->trigger / 8;
164 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
165 ctx->trigger = -1;
166 }
167 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
168 }
169 }
170 if (ctx->spl_cnt == ctx->samples_per_line)
171 /* Line buffers were already flushed. */
172 ctx->spl_cnt = 0;
173 }
174 break;
175 case SR_DF_END:
176 if (ctx->spl_cnt) {
177 /* Line buffers need flushing. */
178 *out = g_string_sized_new(512);
179 for (i = 0; i < ctx->num_enabled_channels; i++) {
180 if (ctx->spl_cnt & 7)
181 g_string_append_printf(ctx->lines[i], "%.2x ",
182 ctx->sample_buf[i] << (8 - (ctx->spl_cnt & 7)));
183 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
184 g_string_append_c(*out, '\n');
185 }
186 }
187 break;
188 }
189
190 return SR_OK;
191}
192
193static int cleanup(struct sr_output *o)
194{
195 struct context *ctx;
196 unsigned int i;
197
198 if (!o)
199 return SR_ERR_ARG;
200
201 if (!(ctx = o->internal))
202 return SR_OK;
203
204 g_free(ctx->header);
205 g_free(ctx->channel_index);
206 g_free(ctx->sample_buf);
207 g_free(ctx->channel_names);
208 for (i = 0; i < ctx->num_enabled_channels; i++)
209 g_string_free(ctx->lines[i], TRUE);
210 g_free(ctx->lines);
211 if (ctx->header)
212 g_string_free(ctx->header, TRUE);
213 g_free(ctx);
214 o->internal = NULL;
215
216 return SR_OK;
217}
218
219SR_PRIV struct sr_output_format output_hex = {
220 .id = "hex",
221 .description = "Hexadecimal",
222 .init = init,
223 .receive = receive,
224 .cleanup = cleanup,
225};
226