]> sigrok.org Git - libsigrok.git/blame - output/text/text.c
sr: Mark API functions with SR_API/SR_PRIV.
[libsigrok.git] / output / text / text.c
CommitLineData
97554432
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 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>
97554432 25#include "config.h"
1a081ca6 26#include "sigrok.h"
97554432
BV
27#include "text.h"
28
97554432
BV
29void flush_linebufs(struct context *ctx, char *outbuf)
30{
31 static int max_probename_len = 0;
32 int len, i;
33
34 if (ctx->linebuf[0] == 0)
35 return;
36
37 if (max_probename_len == 0) {
38 /* First time through... */
39 for (i = 0; ctx->probelist[i]; i++) {
40 len = strlen(ctx->probelist[i]);
41 if (len > max_probename_len)
42 max_probename_len = len;
43 }
44 }
45
46 for (i = 0; ctx->probelist[i]; i++) {
47 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
48 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
49 }
50
51 /* Mark trigger with a ^ character. */
52 if (ctx->mark_trigger != -1)
53 {
54 int space_offset = ctx->mark_trigger / 8;
55
56 if (ctx->mode == MODE_ASCII)
57 space_offset = 0;
58
59 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
60 ctx->mark_trigger + space_offset, "");
61 }
62
63 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
64}
65
f50f3f40 66int init(struct sr_output *o, int default_spl, enum outputmode mode)
97554432
BV
67{
68 struct context *ctx;
1afe8989 69 struct sr_probe *probe;
97554432
BV
70 GSList *l;
71 uint64_t samplerate;
72 int num_probes;
73 char *samplerate_s;
74
75 if (!(ctx = calloc(1, sizeof(struct context))))
e46b8fb1 76 return SR_ERR_MALLOC;
97554432
BV
77
78 o->internal = ctx;
79 ctx->num_enabled_probes = 0;
80
81 for (l = o->device->probes; l; l = l->next) {
82 probe = l->data;
83 if (!probe->enabled)
84 continue;
85 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
86 }
87
88 ctx->probelist[ctx->num_enabled_probes] = 0;
89 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
90 ctx->line_offset = 0;
91 ctx->spl_cnt = 0;
92 ctx->mark_trigger = -1;
93 ctx->mode = mode;
94
95 if (o->param && o->param[0]) {
96 ctx->samples_per_line = strtoul(o->param, NULL, 10);
97 if (ctx->samples_per_line < 1)
e46b8fb1 98 return SR_ERR;
97554432
BV
99 } else
100 ctx->samples_per_line = default_spl;
101
102 if (!(ctx->header = malloc(512))) {
103 free(ctx);
e46b8fb1 104 return SR_ERR_MALLOC;
97554432
BV
105 }
106
107 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
108 num_probes = g_slist_length(o->device->probes);
2bf4aca6 109 if (o->device->plugin || sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
97554432 110 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
5a2326a7 111 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
a00ba012 112 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
97554432
BV
113 free(ctx->header);
114 free(ctx);
e46b8fb1 115 return SR_ERR;
97554432
BV
116 }
117 snprintf(ctx->header + strlen(ctx->header),
118 511 - strlen(ctx->header),
119 "Acquisition with %d/%d probes at %s\n",
120 ctx->num_enabled_probes, num_probes, samplerate_s);
121 free(samplerate_s);
122 }
123
124 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
125 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
126 free(ctx->header);
127 free(ctx);
e46b8fb1 128 return SR_ERR_MALLOC;
97554432
BV
129 }
130 if (!(ctx->linevalues = calloc(1, num_probes))) {
131 free(ctx->header);
132 free(ctx);
e46b8fb1 133 return SR_ERR_MALLOC;
97554432
BV
134 }
135
e46b8fb1 136 return SR_OK;
97554432
BV
137}
138
f50f3f40 139int event(struct sr_output *o, int event_type, char **data_out,
97554432
BV
140 uint64_t *length_out)
141{
142 struct context *ctx;
143 int outsize;
144 char *outbuf;
145
146 ctx = o->internal;
147 switch (event_type) {
5a2326a7 148 case SR_DF_TRIGGER:
97554432
BV
149 ctx->mark_trigger = ctx->spl_cnt;
150 *data_out = NULL;
151 *length_out = 0;
152 break;
5a2326a7 153 case SR_DF_END:
97554432
BV
154 outsize = ctx->num_enabled_probes
155 * (ctx->samples_per_line + 20) + 512;
156 if (!(outbuf = calloc(1, outsize)))
e46b8fb1 157 return SR_ERR_MALLOC;
97554432
BV
158 flush_linebufs(ctx, outbuf);
159 *data_out = outbuf;
160 *length_out = strlen(outbuf);
161 free(o->internal);
162 o->internal = NULL;
163 break;
164 default:
165 *data_out = NULL;
166 *length_out = 0;
167 break;
168 }
169
e46b8fb1 170 return SR_OK;
97554432 171}