]>
Commit | Line | Data |
---|---|---|
161a8a27 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
161a8a27 BV |
3 | * |
4 | * Copyright (C) 2012 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 | ||
6ec6c43b | 20 | #include <config.h> |
161a8a27 BV |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
a944a84b | 23 | #include <math.h> |
161a8a27 | 24 | #include <glib.h> |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
161a8a27 | 26 | #include "libsigrok-internal.h" |
a944a84b | 27 | |
3544f848 | 28 | #define LOG_PREFIX "output/analog" |
161a8a27 BV |
29 | |
30 | struct context { | |
ba7dd8bb UH |
31 | int num_enabled_channels; |
32 | GPtrArray *channellist; | |
820c48f8 | 33 | int digits; |
2dbe445d | 34 | float *fdata; |
820c48f8 BV |
35 | }; |
36 | ||
37 | enum { | |
38 | DIGITS_ALL, | |
39 | DIGITS_SPEC, | |
161a8a27 BV |
40 | }; |
41 | ||
a755b0e1 | 42 | static int init(struct sr_output *o, GHashTable *options) |
161a8a27 BV |
43 | { |
44 | struct context *ctx; | |
ba7dd8bb | 45 | struct sr_channel *ch; |
161a8a27 | 46 | GSList *l; |
820c48f8 | 47 | const char *s; |
a944a84b | 48 | |
161a8a27 BV |
49 | if (!o || !o->sdi) |
50 | return SR_ERR_ARG; | |
51 | ||
91219afc | 52 | o->priv = ctx = g_malloc0(sizeof(struct context)); |
820c48f8 BV |
53 | s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL); |
54 | if (!strcmp(s, "all")) | |
55 | ctx->digits = DIGITS_ALL; | |
56 | else | |
57 | ctx->digits = DIGITS_SPEC; | |
161a8a27 | 58 | |
ba7dd8bb UH |
59 | /* Get the number of channels and their names. */ |
60 | ctx->channellist = g_ptr_array_new(); | |
61 | for (l = o->sdi->channels; l; l = l->next) { | |
62 | ch = l->data; | |
63 | if (!ch || !ch->enabled) | |
161a8a27 | 64 | continue; |
ba7dd8bb UH |
65 | g_ptr_array_add(ctx->channellist, ch->name); |
66 | ctx->num_enabled_channels++; | |
161a8a27 | 67 | } |
2dbe445d | 68 | ctx->fdata = NULL; |
161a8a27 | 69 | |
161a8a27 BV |
70 | return SR_OK; |
71 | } | |
72 | ||
a755b0e1 | 73 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 74 | GString **out) |
161a8a27 | 75 | { |
820c48f8 | 76 | struct context *ctx; |
edb691fc | 77 | const struct sr_datafeed_analog *analog; |
ba7dd8bb | 78 | struct sr_channel *ch; |
69e19dd7 | 79 | GSList *l; |
e02e9e6a BV |
80 | float *fdata; |
81 | unsigned int i; | |
6ca578fe | 82 | int num_channels, c, ret, digits, actual_digits; |
a24da9a8 | 83 | char *number, *suffix; |
161a8a27 | 84 | |
17f63de6 | 85 | *out = NULL; |
161a8a27 | 86 | if (!o || !o->sdi) |
17f63de6 | 87 | return SR_ERR_ARG; |
820c48f8 | 88 | ctx = o->priv; |
161a8a27 | 89 | |
161a8a27 | 90 | switch (packet->type) { |
161a8a27 | 91 | case SR_DF_FRAME_BEGIN: |
17f63de6 | 92 | *out = g_string_new("FRAME-BEGIN\n"); |
161a8a27 BV |
93 | break; |
94 | case SR_DF_FRAME_END: | |
17f63de6 | 95 | *out = g_string_new("FRAME-END\n"); |
161a8a27 | 96 | break; |
edb691fc UH |
97 | case SR_DF_ANALOG: |
98 | analog = packet->payload; | |
99 | num_channels = g_slist_length(analog->meaning->channels); | |
2dbe445d | 100 | if (!(fdata = g_try_realloc(ctx->fdata, |
edb691fc | 101 | analog->num_samples * num_channels * sizeof(float)))) |
e02e9e6a | 102 | return SR_ERR_MALLOC; |
2dbe445d | 103 | ctx->fdata = fdata; |
edb691fc | 104 | if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK) |
e02e9e6a BV |
105 | return ret; |
106 | *out = g_string_sized_new(512); | |
edb691fc | 107 | if (analog->encoding->is_digits_decimal) { |
820c48f8 | 108 | if (ctx->digits == DIGITS_ALL) |
edb691fc | 109 | digits = analog->encoding->digits; |
820c48f8 | 110 | else |
edb691fc | 111 | digits = analog->spec->spec_digits; |
820c48f8 BV |
112 | } else { |
113 | /* TODO we don't know how to print by number of bits yet. */ | |
114 | digits = 6; | |
115 | } | |
5728718b | 116 | gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit); |
edb691fc UH |
117 | sr_analog_unit_to_string(analog, &suffix); |
118 | for (i = 0; i < analog->num_samples; i++) { | |
119 | for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) { | |
962172e4 | 120 | float value = fdata[i * num_channels + c]; |
5728718b | 121 | const char *prefix = ""; |
6ca578fe | 122 | actual_digits = digits; |
5728718b | 123 | if (si_friendly) |
6ca578fe | 124 | prefix = sr_analog_si_prefix(&value, &actual_digits); |
e02e9e6a BV |
125 | ch = l->data; |
126 | g_string_append_printf(*out, "%s: ", ch->name); | |
6ca578fe | 127 | number = g_strdup_printf("%.*f", MAX(actual_digits, 0), value); |
820c48f8 | 128 | g_string_append(*out, number); |
a24da9a8 | 129 | g_free(number); |
820c48f8 | 130 | g_string_append(*out, " "); |
962172e4 | 131 | g_string_append(*out, prefix); |
820c48f8 BV |
132 | g_string_append(*out, suffix); |
133 | g_string_append(*out, "\n"); | |
161a8a27 BV |
134 | } |
135 | } | |
a24da9a8 | 136 | g_free(suffix); |
161a8a27 BV |
137 | break; |
138 | } | |
139 | ||
17f63de6 | 140 | return SR_OK; |
161a8a27 BV |
141 | } |
142 | ||
820c48f8 BV |
143 | static struct sr_option options[] = { |
144 | { "digits", "Digits", "Digits to show", NULL, NULL }, | |
145 | ALL_ZERO | |
146 | }; | |
147 | ||
148 | static const struct sr_option *get_options(void) | |
149 | { | |
150 | if (!options[0].def) { | |
151 | options[0].def = g_variant_ref_sink(g_variant_new_string("all")); | |
152 | options[0].values = g_slist_append(options[0].values, | |
153 | g_variant_ref_sink(g_variant_new_string("all"))); | |
154 | options[0].values = g_slist_append(options[0].values, | |
155 | g_variant_ref_sink(g_variant_new_string("spec"))); | |
156 | } | |
157 | ||
158 | return options; | |
159 | } | |
160 | ||
da3d141f SB |
161 | static int cleanup(struct sr_output *o) |
162 | { | |
163 | struct context *ctx; | |
164 | ||
165 | if (!o || !o->sdi) | |
166 | return SR_ERR_ARG; | |
167 | ctx = o->priv; | |
168 | ||
169 | g_ptr_array_free(ctx->channellist, 1); | |
170 | g_variant_unref(options[0].def); | |
171 | g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref); | |
172 | g_free(ctx->fdata); | |
173 | g_free(ctx); | |
174 | o->priv = NULL; | |
175 | ||
176 | return SR_OK; | |
177 | } | |
178 | ||
a755b0e1 | 179 | SR_PRIV struct sr_output_module output_analog = { |
161a8a27 | 180 | .id = "analog", |
a755b0e1 BV |
181 | .name = "Analog", |
182 | .desc = "Analog data and types", | |
8a174d23 | 183 | .exts = NULL, |
3cd4b381 | 184 | .flags = 0, |
820c48f8 | 185 | .options = get_options, |
161a8a27 | 186 | .init = init, |
17f63de6 | 187 | .receive = receive, |
161a8a27 BV |
188 | .cleanup = cleanup |
189 | }; |