]> sigrok.org Git - libsigrok.git/blame - output/text/text.c
serial-dmm: Use sr_dev_inst to store connection handle.
[libsigrok.git] / output / text / text.c
CommitLineData
97554432
BV
1/*
2 * This file is part of the sigrok project.
3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
97554432
BV
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>
545f9786 25#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
97554432
BV
28#include "text.h"
29
a944a84b
UH
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
054e6709 39SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
97554432
BV
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++) {
054e6709
UH
57 sprintf((char *)outbuf + strlen((const char *)outbuf),
58 "%*s:%s\n", max_probename_len,
97554432
BV
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
054e6709
UH
70 sprintf((char *)outbuf + strlen((const char *)outbuf),
71 "T:%*s^\n", ctx->mark_trigger + space_offset, "");
97554432
BV
72 }
73
74 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
75}
76
7c1d391c 77SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
97554432
BV
78{
79 struct context *ctx;
1afe8989 80 struct sr_probe *probe;
97554432 81 GSList *l;
ec4063b8
BV
82 GVariant *gvar;
83 uint64_t samplerate;
5c3c1241 84 int num_probes, ret;
97554432
BV
85 char *samplerate_s;
86
133a37bf 87 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 88 sr_err("%s: ctx malloc failed", __func__);
e46b8fb1 89 return SR_ERR_MALLOC;
133a37bf 90 }
97554432
BV
91
92 o->internal = ctx;
93 ctx->num_enabled_probes = 0;
94
5c3c1241 95 for (l = o->sdi->probes; l; l = l->next) {
97554432
BV
96 probe = l->data;
97 if (!probe->enabled)
98 continue;
99 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
100 }
101
102 ctx->probelist[ctx->num_enabled_probes] = 0;
103 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
104 ctx->line_offset = 0;
105 ctx->spl_cnt = 0;
106 ctx->mark_trigger = -1;
107 ctx->mode = mode;
108
aee878fa 109 ret = SR_OK;
97554432
BV
110 if (o->param && o->param[0]) {
111 ctx->samples_per_line = strtoul(o->param, NULL, 10);
5c3c1241
BV
112 if (ctx->samples_per_line < 1) {
113 ret = SR_ERR;
114 goto err;
115 }
97554432
BV
116 } else
117 ctx->samples_per_line = default_spl;
118
133a37bf 119 if (!(ctx->header = g_try_malloc0(512))) {
a944a84b 120 sr_err("%s: ctx->header malloc failed", __func__);
5c3c1241
BV
121 ret = SR_ERR_MALLOC;
122 goto err;
97554432
BV
123 }
124
125 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
5c3c1241 126 num_probes = g_slist_length(o->sdi->probes);
4d15e5c9 127 if (o->sdi->driver || sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
ec4063b8
BV
128 if ((ret = o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar,
129 o->sdi)) != SR_OK)
5c3c1241 130 goto err;
ec4063b8
BV
131 samplerate = g_variant_get_uint64(gvar);
132 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
5c3c1241 133 ret = SR_ERR;
ec4063b8 134 g_variant_unref(gvar);
5c3c1241 135 goto err;
97554432
BV
136 }
137 snprintf(ctx->header + strlen(ctx->header),
138 511 - strlen(ctx->header),
139 "Acquisition with %d/%d probes at %s\n",
140 ctx->num_enabled_probes, num_probes, samplerate_s);
133a37bf 141 g_free(samplerate_s);
ec4063b8 142 g_variant_unref(gvar);
97554432
BV
143 }
144
145 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
133a37bf 146 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
a944a84b 147 sr_err("%s: ctx->linebuf malloc failed", __func__);
5c3c1241
BV
148 ret = SR_ERR_MALLOC;
149 goto err;
97554432 150 }
5c3c1241 151
133a37bf 152 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
a944a84b 153 sr_err("%s: ctx->linevalues malloc failed", __func__);
5c3c1241
BV
154 ret = SR_ERR_MALLOC;
155 }
156
157err:
158 if (ret != SR_OK) {
133a37bf
UH
159 g_free(ctx->header);
160 g_free(ctx);
97554432
BV
161 }
162
5c3c1241 163 return ret;
97554432
BV
164}
165
054e6709 166SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
7c1d391c 167 uint64_t *length_out)
97554432
BV
168{
169 struct context *ctx;
170 int outsize;
054e6709 171 uint8_t *outbuf;
97554432
BV
172
173 ctx = o->internal;
174 switch (event_type) {
5a2326a7 175 case SR_DF_TRIGGER:
97554432
BV
176 ctx->mark_trigger = ctx->spl_cnt;
177 *data_out = NULL;
178 *length_out = 0;
179 break;
5a2326a7 180 case SR_DF_END:
97554432
BV
181 outsize = ctx->num_enabled_probes
182 * (ctx->samples_per_line + 20) + 512;
133a37bf 183 if (!(outbuf = g_try_malloc0(outsize))) {
a944a84b 184 sr_err("%s: outbuf malloc failed", __func__);
e46b8fb1 185 return SR_ERR_MALLOC;
133a37bf 186 }
97554432
BV
187 flush_linebufs(ctx, outbuf);
188 *data_out = outbuf;
054e6709 189 *length_out = strlen((const char *)outbuf);
133a37bf 190 g_free(o->internal);
97554432
BV
191 o->internal = NULL;
192 break;
193 default:
194 *data_out = NULL;
195 *length_out = 0;
196 break;
197 }
198
e46b8fb1 199 return SR_OK;
97554432 200}