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