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