]>
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 | ||
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
a944a84b | 22 | #include <math.h> |
161a8a27 | 23 | #include <glib.h> |
161a8a27 BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
a944a84b | 26 | |
3544f848 | 27 | #define LOG_PREFIX "output/analog" |
161a8a27 BV |
28 | |
29 | struct context { | |
30 | int num_enabled_probes; | |
31 | GPtrArray *probelist; | |
161a8a27 BV |
32 | }; |
33 | ||
161a8a27 BV |
34 | static int init(struct sr_output *o) |
35 | { | |
36 | struct context *ctx; | |
37 | struct sr_probe *probe; | |
38 | GSList *l; | |
39 | ||
a944a84b UH |
40 | sr_spew("Initializing output module."); |
41 | ||
161a8a27 BV |
42 | if (!o || !o->sdi) |
43 | return SR_ERR_ARG; | |
44 | ||
886a52b6 | 45 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
a944a84b | 46 | sr_err("Output module context malloc failed."); |
161a8a27 | 47 | return SR_ERR_MALLOC; |
886a52b6 | 48 | } |
161a8a27 BV |
49 | o->internal = ctx; |
50 | ||
51 | /* Get the number of probes and their names. */ | |
52 | ctx->probelist = g_ptr_array_new(); | |
53 | for (l = o->sdi->probes; l; l = l->next) { | |
54 | probe = l->data; | |
55 | if (!probe || !probe->enabled) | |
56 | continue; | |
57 | g_ptr_array_add(ctx->probelist, probe->name); | |
58 | ctx->num_enabled_probes++; | |
59 | } | |
60 | ||
161a8a27 BV |
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 | { | |
161a8a27 | 94 | switch (unit) { |
06c45a66 UH |
95 | case SR_UNIT_VOLT: |
96 | si_printf(value, out, "V"); | |
97 | break; | |
98 | case SR_UNIT_AMPERE: | |
99 | si_printf(value, out, "A"); | |
100 | break; | |
101 | case SR_UNIT_OHM: | |
102 | si_printf(value, out, ""); | |
103 | g_string_append_unichar(out, 0x2126); | |
104 | break; | |
105 | case SR_UNIT_FARAD: | |
106 | si_printf(value, out, "F"); | |
107 | break; | |
108 | case SR_UNIT_KELVIN: | |
109 | si_printf(value, out, "K"); | |
110 | break; | |
111 | case SR_UNIT_CELSIUS: | |
112 | si_printf(value, out, ""); | |
113 | g_string_append_unichar(out, 0x00b0); | |
114 | g_string_append_c(out, 'C'); | |
115 | break; | |
116 | case SR_UNIT_FAHRENHEIT: | |
117 | si_printf(value, out, ""); | |
118 | g_string_append_unichar(out, 0x00b0); | |
119 | g_string_append_c(out, 'F'); | |
120 | break; | |
121 | case SR_UNIT_HERTZ: | |
122 | si_printf(value, out, "Hz"); | |
123 | break; | |
124 | case SR_UNIT_PERCENTAGE: | |
641d8f27 | 125 | g_string_append_printf(out, "%f %%", value); |
06c45a66 UH |
126 | break; |
127 | case SR_UNIT_BOOLEAN: | |
128 | if (value > 0) | |
129 | g_string_append_printf(out, "TRUE"); | |
130 | else | |
131 | g_string_append_printf(out, "FALSE"); | |
132 | break; | |
133 | case SR_UNIT_SECOND: | |
134 | si_printf(value, out, "s"); | |
135 | break; | |
136 | case SR_UNIT_SIEMENS: | |
137 | si_printf(value, out, "S"); | |
138 | break; | |
139 | case SR_UNIT_DECIBEL_MW: | |
140 | si_printf(value, out, "dBu"); | |
141 | break; | |
142 | case SR_UNIT_DECIBEL_VOLT: | |
143 | si_printf(value, out, "dBV"); | |
144 | break; | |
145 | case SR_UNIT_DECIBEL_SPL: | |
146 | if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
147 | si_printf(value, out, "dB(A)"); | |
148 | else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
149 | si_printf(value, out, "dB(C)"); | |
150 | else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z) | |
151 | si_printf(value, out, "dB(Z)"); | |
152 | else | |
153 | /* No frequency weighting, or non-standard "flat" */ | |
154 | si_printf(value, out, "dB(SPL)"); | |
155 | if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
156 | g_string_append(out, " S"); | |
157 | else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
158 | g_string_append(out, " F"); | |
159 | if (mqflags & SR_MQFLAG_SPL_LAT) | |
160 | g_string_append(out, " LAT"); | |
161 | else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM) | |
162 | /* Not a standard function for SLMs, so this is | |
163 | * a made-up notation. */ | |
164 | g_string_append(out, " %oA"); | |
165 | break; | |
166 | case SR_UNIT_CONCENTRATION: | |
167 | g_string_append_printf(out, "%f ppm", value * 1000000); | |
168 | break; | |
87532f23 AJ |
169 | case SR_UNIT_REVOLUTIONS_PER_MINUTE: |
170 | si_printf(value, out, "RPM"); | |
d69d2642 | 171 | break; |
45315d04 AJ |
172 | case SR_UNIT_VOLT_AMPERE: |
173 | si_printf(value, out, "VA"); | |
174 | break; | |
175 | case SR_UNIT_WATT: | |
176 | si_printf(value, out, "W"); | |
177 | break; | |
178 | case SR_UNIT_WATT_HOUR: | |
179 | si_printf(value, out, "Wh"); | |
87532f23 | 180 | break; |
06c45a66 UH |
181 | default: |
182 | si_printf(value, out, ""); | |
183 | break; | |
161a8a27 | 184 | } |
e6523173 UH |
185 | |
186 | if (mqflags & SR_MQFLAG_AC) | |
161a8a27 | 187 | g_string_append_printf(out, " AC"); |
e6523173 | 188 | if (mqflags & SR_MQFLAG_DC) |
161a8a27 | 189 | g_string_append_printf(out, " DC"); |
e6523173 UH |
190 | if (mqflags & SR_MQFLAG_RMS) |
191 | g_string_append_printf(out, " RMS"); | |
192 | if (mqflags & SR_MQFLAG_DIODE) | |
193 | g_string_append_printf(out, " DIODE"); | |
194 | if (mqflags & SR_MQFLAG_HOLD) | |
195 | g_string_append_printf(out, " HOLD"); | |
196 | if (mqflags & SR_MQFLAG_MAX) | |
197 | g_string_append_printf(out, " MAX"); | |
198 | if (mqflags & SR_MQFLAG_MIN) | |
199 | g_string_append_printf(out, " MIN"); | |
200 | if (mqflags & SR_MQFLAG_AUTORANGE) | |
201 | g_string_append_printf(out, " AUTO"); | |
202 | if (mqflags & SR_MQFLAG_RELATIVE) | |
203 | g_string_append_printf(out, " REL"); | |
f5027ca4 AJ |
204 | if (mqflags & SR_MQFLAG_AVG) |
205 | g_string_append_printf(out, " AVG"); | |
161a8a27 | 206 | g_string_append_c(out, '\n'); |
161a8a27 BV |
207 | } |
208 | ||
17f63de6 BV |
209 | static int receive(struct sr_output *o, const struct sr_dev_inst *sdi, |
210 | const struct sr_datafeed_packet *packet, GString **out) | |
161a8a27 | 211 | { |
69e19dd7 BV |
212 | const struct sr_datafeed_analog *analog; |
213 | struct sr_probe *probe; | |
214 | GSList *l; | |
bf53457d | 215 | const float *fdata; |
69e19dd7 | 216 | int i, p; |
161a8a27 | 217 | |
796a79eb | 218 | (void)sdi; |
64d33dc2 | 219 | |
17f63de6 | 220 | *out = NULL; |
161a8a27 | 221 | if (!o || !o->sdi) |
17f63de6 | 222 | return SR_ERR_ARG; |
161a8a27 | 223 | |
161a8a27 | 224 | switch (packet->type) { |
161a8a27 | 225 | case SR_DF_FRAME_BEGIN: |
17f63de6 | 226 | *out = g_string_new("FRAME-BEGIN\n"); |
161a8a27 BV |
227 | break; |
228 | case SR_DF_FRAME_END: | |
17f63de6 | 229 | *out = g_string_new("FRAME-END\n"); |
161a8a27 BV |
230 | break; |
231 | case SR_DF_ANALOG: | |
232 | analog = packet->payload; | |
bf53457d | 233 | fdata = (const float *)analog->data; |
17f63de6 | 234 | *out = g_string_sized_new(512); |
161a8a27 | 235 | for (i = 0; i < analog->num_samples; i++) { |
69e19dd7 BV |
236 | for (l = analog->probes, p = 0; l; l = l->next, p++) { |
237 | probe = l->data; | |
17f63de6 | 238 | g_string_append_printf(*out, "%s: ", probe->name); |
161a8a27 | 239 | fancyprint(analog->unit, analog->mqflags, |
17f63de6 | 240 | fdata[i + p], *out); |
161a8a27 BV |
241 | } |
242 | } | |
243 | break; | |
244 | } | |
245 | ||
17f63de6 | 246 | return SR_OK; |
161a8a27 BV |
247 | } |
248 | ||
249 | static int cleanup(struct sr_output *o) | |
250 | { | |
251 | struct context *ctx; | |
252 | ||
253 | if (!o || !o->sdi) | |
254 | return SR_ERR_ARG; | |
255 | ctx = o->internal; | |
256 | ||
257 | g_ptr_array_free(ctx->probelist, 1); | |
161a8a27 BV |
258 | g_free(ctx); |
259 | o->internal = NULL; | |
260 | ||
261 | return SR_OK; | |
262 | } | |
263 | ||
161a8a27 BV |
264 | SR_PRIV struct sr_output_format output_analog = { |
265 | .id = "analog", | |
266 | .description = "Analog data", | |
267 | .df_type = SR_DF_ANALOG, | |
268 | .init = init, | |
17f63de6 | 269 | .receive = receive, |
161a8a27 BV |
270 | .cleanup = cleanup |
271 | }; |