]> sigrok.org Git - libsigrok.git/blame - src/output/analog.c
Refactor scan options check.
[libsigrok.git] / src / output / analog.c
CommitLineData
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
29struct context {
ba7dd8bb
UH
30 int num_enabled_channels;
31 GPtrArray *channellist;
161a8a27
BV
32};
33
a755b0e1 34static int init(struct sr_output *o, GHashTable *options)
161a8a27
BV
35{
36 struct context *ctx;
ba7dd8bb 37 struct sr_channel *ch;
161a8a27
BV
38 GSList *l;
39
a755b0e1 40 (void)options;
a944a84b 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 }
d686c5ec 49 o->priv = ctx;
161a8a27 50
ba7dd8bb
UH
51 /* Get the number of channels and their names. */
52 ctx->channellist = g_ptr_array_new();
53 for (l = o->sdi->channels; l; l = l->next) {
54 ch = l->data;
55 if (!ch || !ch->enabled)
161a8a27 56 continue;
ba7dd8bb
UH
57 g_ptr_array_add(ctx->channellist, ch->name);
58 ctx->num_enabled_channels++;
161a8a27
BV
59 }
60
161a8a27
BV
61 return SR_OK;
62}
63
64static 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
92static 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;
02c7c482
JH
108 case SR_UNIT_HENRY:
109 si_printf(value, out, "H");
110 break;
06c45a66
UH
111 case SR_UNIT_KELVIN:
112 si_printf(value, out, "K");
113 break;
114 case SR_UNIT_CELSIUS:
115 si_printf(value, out, "");
116 g_string_append_unichar(out, 0x00b0);
117 g_string_append_c(out, 'C');
118 break;
119 case SR_UNIT_FAHRENHEIT:
120 si_printf(value, out, "");
121 g_string_append_unichar(out, 0x00b0);
122 g_string_append_c(out, 'F');
123 break;
124 case SR_UNIT_HERTZ:
125 si_printf(value, out, "Hz");
126 break;
127 case SR_UNIT_PERCENTAGE:
641d8f27 128 g_string_append_printf(out, "%f %%", value);
06c45a66
UH
129 break;
130 case SR_UNIT_BOOLEAN:
131 if (value > 0)
132 g_string_append_printf(out, "TRUE");
133 else
134 g_string_append_printf(out, "FALSE");
135 break;
136 case SR_UNIT_SECOND:
137 si_printf(value, out, "s");
138 break;
139 case SR_UNIT_SIEMENS:
140 si_printf(value, out, "S");
141 break;
142 case SR_UNIT_DECIBEL_MW:
143 si_printf(value, out, "dBu");
144 break;
145 case SR_UNIT_DECIBEL_VOLT:
146 si_printf(value, out, "dBV");
147 break;
148 case SR_UNIT_DECIBEL_SPL:
149 if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
150 si_printf(value, out, "dB(A)");
151 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C)
152 si_printf(value, out, "dB(C)");
153 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z)
154 si_printf(value, out, "dB(Z)");
155 else
156 /* No frequency weighting, or non-standard "flat" */
157 si_printf(value, out, "dB(SPL)");
158 if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S)
159 g_string_append(out, " S");
160 else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
161 g_string_append(out, " F");
162 if (mqflags & SR_MQFLAG_SPL_LAT)
163 g_string_append(out, " LAT");
164 else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM)
165 /* Not a standard function for SLMs, so this is
166 * a made-up notation. */
167 g_string_append(out, " %oA");
168 break;
169 case SR_UNIT_CONCENTRATION:
170 g_string_append_printf(out, "%f ppm", value * 1000000);
171 break;
87532f23
AJ
172 case SR_UNIT_REVOLUTIONS_PER_MINUTE:
173 si_printf(value, out, "RPM");
d69d2642 174 break;
45315d04
AJ
175 case SR_UNIT_VOLT_AMPERE:
176 si_printf(value, out, "VA");
177 break;
178 case SR_UNIT_WATT:
179 si_printf(value, out, "W");
180 break;
181 case SR_UNIT_WATT_HOUR:
182 si_printf(value, out, "Wh");
87532f23 183 break;
543d041d
BV
184 case SR_UNIT_METER_SECOND:
185 si_printf(value, out, "m/s");
186 break;
187 case SR_UNIT_HECTOPASCAL:
188 si_printf(value, out, "hPa");
189 break;
190 case SR_UNIT_HUMIDITY_293K:
191 si_printf(value, out, "%rF");
192 break;
02c7c482
JH
193 case SR_UNIT_DEGREE:
194 si_printf(value, out, "");
195 g_string_append_unichar(out, 0x00b0);
196 break;
06c45a66
UH
197 default:
198 si_printf(value, out, "");
199 break;
161a8a27 200 }
e6523173
UH
201
202 if (mqflags & SR_MQFLAG_AC)
161a8a27 203 g_string_append_printf(out, " AC");
e6523173 204 if (mqflags & SR_MQFLAG_DC)
161a8a27 205 g_string_append_printf(out, " DC");
e6523173
UH
206 if (mqflags & SR_MQFLAG_RMS)
207 g_string_append_printf(out, " RMS");
208 if (mqflags & SR_MQFLAG_DIODE)
209 g_string_append_printf(out, " DIODE");
210 if (mqflags & SR_MQFLAG_HOLD)
211 g_string_append_printf(out, " HOLD");
212 if (mqflags & SR_MQFLAG_MAX)
213 g_string_append_printf(out, " MAX");
214 if (mqflags & SR_MQFLAG_MIN)
215 g_string_append_printf(out, " MIN");
216 if (mqflags & SR_MQFLAG_AUTORANGE)
217 g_string_append_printf(out, " AUTO");
218 if (mqflags & SR_MQFLAG_RELATIVE)
219 g_string_append_printf(out, " REL");
f5027ca4
AJ
220 if (mqflags & SR_MQFLAG_AVG)
221 g_string_append_printf(out, " AVG");
02c7c482
JH
222 if (mqflags & SR_MQFLAG_REFERENCE)
223 g_string_append_printf(out, " REF");
161a8a27 224 g_string_append_c(out, '\n');
161a8a27
BV
225}
226
a755b0e1 227static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 228 GString **out)
161a8a27 229{
69e19dd7 230 const struct sr_datafeed_analog *analog;
e02e9e6a 231 const struct sr_datafeed_analog2 *analog2;
ba7dd8bb 232 struct sr_channel *ch;
69e19dd7 233 GSList *l;
e02e9e6a
BV
234 float *fdata;
235 unsigned int i;
236 int num_channels, c, ret, si;
161a8a27 237
17f63de6 238 *out = NULL;
161a8a27 239 if (!o || !o->sdi)
17f63de6 240 return SR_ERR_ARG;
161a8a27 241
161a8a27 242 switch (packet->type) {
161a8a27 243 case SR_DF_FRAME_BEGIN:
17f63de6 244 *out = g_string_new("FRAME-BEGIN\n");
161a8a27
BV
245 break;
246 case SR_DF_FRAME_END:
17f63de6 247 *out = g_string_new("FRAME-END\n");
161a8a27
BV
248 break;
249 case SR_DF_ANALOG:
250 analog = packet->payload;
e02e9e6a 251 fdata = (float *)analog->data;
17f63de6 252 *out = g_string_sized_new(512);
6e6babb9 253 num_channels = g_slist_length(analog->channels);
e02e9e6a 254 for (si = 0; si < analog->num_samples; si++) {
6e6babb9 255 for (l = analog->channels, c = 0; l; l = l->next, c++) {
ba7dd8bb
UH
256 ch = l->data;
257 g_string_append_printf(*out, "%s: ", ch->name);
161a8a27 258 fancyprint(analog->unit, analog->mqflags,
e02e9e6a
BV
259 fdata[si * num_channels + c], *out);
260 }
261 }
262 break;
263 case SR_DF_ANALOG2:
264 analog2 = packet->payload;
265 if (!(fdata = g_try_malloc(analog2->num_samples * sizeof(float))))
266 return SR_ERR_MALLOC;
267 if ((ret = sr_analog_to_float(analog2, fdata)) != SR_OK)
268 return ret;
269 *out = g_string_sized_new(512);
270 num_channels = g_slist_length(analog2->meaning->channels);
271 for (i = 0; i < analog2->num_samples; i++) {
272 for (l = analog2->meaning->channels, c = 0; l; l = l->next, c++) {
273 ch = l->data;
274 g_string_append_printf(*out, "%s: ", ch->name);
275 fancyprint(analog2->meaning->unit, analog2->meaning->mqflags,
6e6babb9 276 fdata[i * num_channels + c], *out);
161a8a27
BV
277 }
278 }
279 break;
280 }
281
17f63de6 282 return SR_OK;
161a8a27
BV
283}
284
285static int cleanup(struct sr_output *o)
286{
287 struct context *ctx;
288
289 if (!o || !o->sdi)
290 return SR_ERR_ARG;
d686c5ec 291 ctx = o->priv;
161a8a27 292
ba7dd8bb 293 g_ptr_array_free(ctx->channellist, 1);
161a8a27 294 g_free(ctx);
d686c5ec 295 o->priv = NULL;
161a8a27
BV
296
297 return SR_OK;
298}
299
a755b0e1 300SR_PRIV struct sr_output_module output_analog = {
161a8a27 301 .id = "analog",
a755b0e1
BV
302 .name = "Analog",
303 .desc = "Analog data and types",
304 .options = NULL,
161a8a27 305 .init = init,
17f63de6 306 .receive = receive,
161a8a27
BV
307 .cleanup = cleanup
308};