]>
Commit | Line | Data |
---|---|---|
97554432 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 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 <string.h> | |
23 | #include <glib.h> | |
45c59c8b BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
97554432 BV |
26 | #include "text.h" |
27 | ||
a944a84b UH |
28 | /* Message logging helpers with driver-specific prefix string. */ |
29 | #define DRIVER_LOG_DOMAIN "output/ascii: " | |
30 | #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args) | |
31 | #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args) | |
32 | #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args) | |
33 | #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args) | |
34 | #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) | |
35 | #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) | |
36 | ||
7c1d391c | 37 | SR_PRIV int init_ascii(struct sr_output *o) |
97554432 BV |
38 | { |
39 | return init(o, DEFAULT_BPL_ASCII, MODE_ASCII); | |
40 | } | |
41 | ||
054e6709 UH |
42 | SR_PRIV int data_ascii(struct sr_output *o, const uint8_t *data_in, |
43 | uint64_t length_in, uint8_t **data_out, | |
7c1d391c | 44 | uint64_t *length_out) |
97554432 BV |
45 | { |
46 | struct context *ctx; | |
47 | unsigned int outsize, offset, p; | |
48 | int max_linelen; | |
49 | uint64_t sample; | |
054e6709 | 50 | uint8_t *outbuf; |
97554432 BV |
51 | |
52 | ctx = o->internal; | |
9688b443 | 53 | max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
97554432 BV |
54 | + ctx->samples_per_line / 8; |
55 | /* | |
56 | * Calculate space needed for probes. Set aside 512 bytes for | |
57 | * extra output, e.g. trigger. | |
58 | */ | |
59 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
60 | * (ctx->num_enabled_probes * max_linelen); | |
61 | ||
133a37bf | 62 | if (!(outbuf = g_try_malloc0(outsize + 1))) { |
a944a84b | 63 | sr_err("%s: outbuf malloc failed", __func__); |
e46b8fb1 | 64 | return SR_ERR_MALLOC; |
133a37bf | 65 | } |
97554432 BV |
66 | |
67 | outbuf[0] = '\0'; | |
68 | if (ctx->header) { | |
69 | /* The header is still here, this must be the first packet. */ | |
054e6709 | 70 | strncpy((char *)outbuf, ctx->header, outsize); |
133a37bf | 71 | g_free(ctx->header); |
97554432 BV |
72 | ctx->header = NULL; |
73 | } | |
74 | ||
75 | if (length_in >= ctx->unitsize) { | |
76 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
77 | offset += ctx->unitsize) { | |
78 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
79 | ||
80 | char tmpval[ctx->num_enabled_probes]; | |
81 | ||
82 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
83 | uint64_t curbit = (sample & ((uint64_t) 1 << p)); | |
84 | uint64_t prevbit = (ctx->prevsample & | |
85 | ((uint64_t) 1 << p)); | |
86 | ||
87 | if (curbit < prevbit && ctx->line_offset > 0) { | |
88 | ctx->linebuf[p * ctx->linebuf_len + | |
89 | ctx->line_offset-1] = '\\'; | |
90 | } | |
91 | ||
92 | if (curbit > prevbit) { | |
93 | tmpval[p] = '/'; | |
94 | } else { | |
95 | if (curbit) | |
96 | tmpval[p] = '"'; | |
97 | else | |
98 | tmpval[p] = '.'; | |
99 | } | |
100 | } | |
101 | ||
102 | /* End of line. */ | |
103 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
104 | flush_linebufs(ctx, outbuf); | |
105 | ctx->line_offset = ctx->spl_cnt = 0; | |
106 | ctx->mark_trigger = -1; | |
107 | } | |
108 | ||
109 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
110 | ctx->linebuf[p * ctx->linebuf_len + | |
111 | ctx->line_offset] = tmpval[p]; | |
112 | } | |
113 | ||
114 | ctx->line_offset++; | |
115 | ctx->spl_cnt++; | |
116 | ||
117 | ctx->prevsample = sample; | |
118 | } | |
119 | } else { | |
a944a84b | 120 | sr_info("Short buffer (length_in=%" PRIu64 ").", length_in); |
97554432 BV |
121 | } |
122 | ||
123 | *data_out = outbuf; | |
054e6709 | 124 | *length_out = strlen((const char *)outbuf); |
97554432 | 125 | |
e46b8fb1 | 126 | return SR_OK; |
97554432 BV |
127 | } |
128 | ||
7c1d391c | 129 | SR_PRIV struct sr_output_format output_text_ascii = { |
cdb3573c | 130 | .id = "ascii", |
2e7cb004 | 131 | .description = "ASCII", |
d494a4aa UH |
132 | .df_type = SR_DF_LOGIC, |
133 | .init = init_ascii, | |
134 | .data = data_ascii, | |
135 | .event = event, | |
97554432 | 136 | }; |