]>
Commit | Line | Data |
---|---|---|
161a8a27 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 | ||
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | #include <glib.h> | |
161a8a27 BV |
23 | #include "libsigrok.h" |
24 | #include "libsigrok-internal.h" | |
25 | #include <math.h> | |
26 | ||
27 | struct context { | |
28 | int num_enabled_probes; | |
29 | GPtrArray *probelist; | |
30 | GString *out; | |
31 | }; | |
32 | ||
161a8a27 BV |
33 | static int init(struct sr_output *o) |
34 | { | |
35 | struct context *ctx; | |
36 | struct sr_probe *probe; | |
37 | GSList *l; | |
38 | ||
39 | sr_spew("output/analog: initializing"); | |
40 | if (!o || !o->sdi) | |
41 | return SR_ERR_ARG; | |
42 | ||
886a52b6 UH |
43 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
44 | sr_err("output/analog: Context malloc failed."); | |
161a8a27 | 45 | return SR_ERR_MALLOC; |
886a52b6 | 46 | } |
161a8a27 BV |
47 | o->internal = ctx; |
48 | ||
49 | /* Get the number of probes and their names. */ | |
50 | ctx->probelist = g_ptr_array_new(); | |
51 | for (l = o->sdi->probes; l; l = l->next) { | |
52 | probe = l->data; | |
53 | if (!probe || !probe->enabled) | |
54 | continue; | |
55 | g_ptr_array_add(ctx->probelist, probe->name); | |
56 | ctx->num_enabled_probes++; | |
57 | } | |
58 | ||
59 | ctx->out = g_string_sized_new(512); | |
60 | ||
61 | return SR_OK; | |
62 | } | |
63 | ||
64 | static void si_printf(float value, GString *out, char *unitstr) | |
65 | { | |
d713e561 | 66 | float v; |
161a8a27 | 67 | |
d713e561 BV |
68 | if (signbit(value)) |
69 | v = -(value); | |
70 | else | |
71 | v = value; | |
72 | ||
73 | if (v < 1e-12 || v > 1e+12) | |
74 | g_string_append_printf(out, "%f %s", value, unitstr); | |
75 | else if (v > 1e+9) | |
76 | g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr); | |
77 | else if (v > 1e+6) | |
78 | g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr); | |
79 | else if (v > 1e+3) | |
80 | g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr); | |
81 | else if (v < 1e-9) | |
82 | g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr); | |
83 | else if (v < 1e-6) | |
84 | g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr); | |
85 | else if (v < 1e-3) | |
86 | g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr); | |
161a8a27 BV |
87 | else |
88 | g_string_append_printf(out, "%f %s", value, unitstr); | |
89 | ||
90 | } | |
91 | ||
92 | static void fancyprint(int unit, int mqflags, float value, GString *out) | |
93 | { | |
94 | ||
95 | switch (unit) { | |
96 | case SR_UNIT_VOLT: | |
97 | si_printf(value, out, "V"); | |
98 | break; | |
99 | case SR_UNIT_AMPERE: | |
100 | si_printf(value, out, "A"); | |
101 | break; | |
102 | case SR_UNIT_OHM: | |
103 | si_printf(value, out, ""); | |
104 | g_string_append_unichar(out, 0x2126); | |
105 | break; | |
106 | case SR_UNIT_FARAD: | |
107 | si_printf(value, out, "F"); | |
108 | break; | |
109 | case SR_UNIT_KELVIN: | |
110 | si_printf(value, out, "K"); | |
111 | break; | |
112 | case SR_UNIT_CELSIUS: | |
113 | si_printf(value, out, ""); | |
114 | g_string_append_unichar(out, 0x00b0); | |
115 | g_string_append_c(out, 'C'); | |
116 | break; | |
117 | case SR_UNIT_FAHRENHEIT: | |
118 | si_printf(value, out, ""); | |
119 | g_string_append_unichar(out, 0x00b0); | |
120 | g_string_append_c(out, 'F'); | |
121 | break; | |
122 | case SR_UNIT_HERTZ: | |
123 | si_printf(value, out, "Hz"); | |
124 | break; | |
125 | case SR_UNIT_PERCENTAGE: | |
126 | g_string_append_printf(out, "%f%%", value); | |
127 | break; | |
128 | case SR_UNIT_BOOLEAN: | |
129 | if (value > 0) | |
130 | g_string_append_printf(out, "TRUE"); | |
131 | else | |
132 | g_string_append_printf(out, "FALSE"); | |
133 | break; | |
aa839a5c BV |
134 | case SR_UNIT_SECOND: |
135 | si_printf(value, out, "s"); | |
136 | break; | |
96b3b3d5 BV |
137 | case SR_UNIT_SIEMENS: |
138 | si_printf(value, out, "S"); | |
139 | break; | |
796a79eb BV |
140 | case SR_UNIT_DECIBEL_MW: |
141 | si_printf(value, out, "dBu"); | |
142 | break; | |
143 | case SR_UNIT_DECIBEL_VOLT: | |
144 | si_printf(value, out, "dBV"); | |
145 | break; | |
258eeb77 BV |
146 | case SR_UNIT_DECIBEL_SPL: |
147 | if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
148 | si_printf(value, out, "dB(A)"); | |
149 | else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
150 | si_printf(value, out, "dB(C)"); | |
151 | else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z) | |
152 | si_printf(value, out, "dB(Z)"); | |
153 | else | |
154 | /* No frequency weighting, or non-standard "flat" */ | |
155 | si_printf(value, out, "dB(SPL)"); | |
156 | if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
157 | g_string_append(out, " S"); | |
158 | else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
159 | g_string_append(out, " F"); | |
160 | if (mqflags & SR_MQFLAG_SPL_LAT) | |
161 | g_string_append(out, " LAT"); | |
162 | else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM) | |
163 | /* Not a standard function for SLMs, so this is | |
164 | * a made-up notation. */ | |
165 | g_string_append(out, " %oA"); | |
166 | break; | |
bde4f429 BV |
167 | default: |
168 | si_printf(value, out, ""); | |
161a8a27 | 169 | } |
2b98e0aa BV |
170 | if ((mqflags & (SR_MQFLAG_AC | SR_MQFLAG_DC)) == (SR_MQFLAG_AC | SR_MQFLAG_DC)) |
171 | g_string_append_printf(out, " AC+DC"); | |
172 | else if (mqflags & SR_MQFLAG_AC) | |
161a8a27 BV |
173 | g_string_append_printf(out, " AC"); |
174 | else if (mqflags & SR_MQFLAG_DC) | |
175 | g_string_append_printf(out, " DC"); | |
176 | g_string_append_c(out, '\n'); | |
177 | ||
178 | } | |
179 | ||
6aff0d16 | 180 | static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi, |
161a8a27 BV |
181 | struct sr_datafeed_packet *packet) |
182 | { | |
183 | struct sr_datafeed_analog *analog; | |
184 | struct context *ctx; | |
185 | float *fdata; | |
186 | int i, j; | |
187 | ||
796a79eb | 188 | (void)sdi; |
64d33dc2 | 189 | |
161a8a27 BV |
190 | if (!o || !o->sdi) |
191 | return NULL; | |
192 | ctx = o->internal; | |
193 | ||
194 | g_string_set_size(ctx->out, 0); | |
195 | switch (packet->type) { | |
196 | case SR_DF_HEADER: | |
197 | break; | |
198 | case SR_DF_FRAME_BEGIN: | |
199 | g_string_append_printf(ctx->out, "FRAME-BEGIN\n"); | |
200 | break; | |
201 | case SR_DF_FRAME_END: | |
202 | g_string_append_printf(ctx->out, "FRAME-END\n"); | |
203 | break; | |
204 | case SR_DF_ANALOG: | |
205 | analog = packet->payload; | |
206 | fdata = (float *)analog->data; | |
207 | for (i = 0; i < analog->num_samples; i++) { | |
208 | for (j = 0; j < ctx->num_enabled_probes; j++) { | |
209 | g_string_append_printf(ctx->out, "%s: ", | |
210 | (char *)g_ptr_array_index(ctx->probelist, j)); | |
211 | fancyprint(analog->unit, analog->mqflags, | |
212 | fdata[i + j], ctx->out); | |
213 | } | |
214 | } | |
215 | break; | |
216 | } | |
217 | ||
218 | return ctx->out; | |
219 | } | |
220 | ||
221 | static int cleanup(struct sr_output *o) | |
222 | { | |
223 | struct context *ctx; | |
224 | ||
225 | if (!o || !o->sdi) | |
226 | return SR_ERR_ARG; | |
227 | ctx = o->internal; | |
228 | ||
229 | g_ptr_array_free(ctx->probelist, 1); | |
230 | g_string_free(ctx->out, 1); | |
231 | g_free(ctx); | |
232 | o->internal = NULL; | |
233 | ||
234 | return SR_OK; | |
235 | } | |
236 | ||
237 | ||
238 | SR_PRIV struct sr_output_format output_analog = { | |
239 | .id = "analog", | |
240 | .description = "Analog data", | |
241 | .df_type = SR_DF_ANALOG, | |
242 | .init = init, | |
6aff0d16 | 243 | .recv = receive, |
161a8a27 BV |
244 | .cleanup = cleanup |
245 | }; |