2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "output/analog"
30 int num_enabled_channels;
31 GPtrArray *channellist;
34 static int init(struct sr_output *o, GHashTable *options)
37 struct sr_channel *ch;
45 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
46 sr_err("Output module context malloc failed.");
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) {
55 if (!ch || !ch->enabled)
57 g_ptr_array_add(ctx->channellist, ch->name);
58 ctx->num_enabled_channels++;
64 static void si_printf(float value, GString *out, char *unitstr)
73 if (v < 1e-12 || v > 1e+12)
74 g_string_append_printf(out, "%f %s", value, unitstr);
76 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
78 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
80 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
82 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
84 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
86 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
88 g_string_append_printf(out, "%f %s", value, unitstr);
92 static void fancyprint(int unit, int mqflags, float value, GString *out)
96 si_printf(value, out, "V");
99 si_printf(value, out, "A");
102 si_printf(value, out, "");
103 g_string_append_unichar(out, 0x2126);
106 si_printf(value, out, "F");
109 si_printf(value, out, "H");
112 si_printf(value, out, "K");
114 case SR_UNIT_CELSIUS:
115 si_printf(value, out, "");
116 g_string_append_unichar(out, 0x00b0);
117 g_string_append_c(out, 'C');
119 case SR_UNIT_FAHRENHEIT:
120 si_printf(value, out, "");
121 g_string_append_unichar(out, 0x00b0);
122 g_string_append_c(out, 'F');
125 si_printf(value, out, "Hz");
127 case SR_UNIT_PERCENTAGE:
128 g_string_append_printf(out, "%f %%", value);
130 case SR_UNIT_BOOLEAN:
132 g_string_append_printf(out, "TRUE");
134 g_string_append_printf(out, "FALSE");
137 si_printf(value, out, "s");
139 case SR_UNIT_SIEMENS:
140 si_printf(value, out, "S");
142 case SR_UNIT_DECIBEL_MW:
143 si_printf(value, out, "dBu");
145 case SR_UNIT_DECIBEL_VOLT:
146 si_printf(value, out, "dBV");
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)");
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");
169 case SR_UNIT_CONCENTRATION:
170 g_string_append_printf(out, "%f ppm", value * 1000000);
172 case SR_UNIT_REVOLUTIONS_PER_MINUTE:
173 si_printf(value, out, "RPM");
175 case SR_UNIT_VOLT_AMPERE:
176 si_printf(value, out, "VA");
179 si_printf(value, out, "W");
181 case SR_UNIT_WATT_HOUR:
182 si_printf(value, out, "Wh");
184 case SR_UNIT_METER_SECOND:
185 si_printf(value, out, "m/s");
187 case SR_UNIT_HECTOPASCAL:
188 si_printf(value, out, "hPa");
190 case SR_UNIT_HUMIDITY_293K:
191 si_printf(value, out, "%rF");
194 si_printf(value, out, "");
195 g_string_append_unichar(out, 0x00b0);
198 si_printf(value, out, "");
202 if (mqflags & SR_MQFLAG_AC)
203 g_string_append_printf(out, " AC");
204 if (mqflags & SR_MQFLAG_DC)
205 g_string_append_printf(out, " DC");
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");
220 if (mqflags & SR_MQFLAG_AVG)
221 g_string_append_printf(out, " AVG");
222 if (mqflags & SR_MQFLAG_REFERENCE)
223 g_string_append_printf(out, " REF");
224 g_string_append_c(out, '\n');
227 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
230 const struct sr_datafeed_analog *analog;
231 struct sr_channel *ch;
234 int num_channels, i, c;
240 switch (packet->type) {
241 case SR_DF_FRAME_BEGIN:
242 *out = g_string_new("FRAME-BEGIN\n");
244 case SR_DF_FRAME_END:
245 *out = g_string_new("FRAME-END\n");
248 analog = packet->payload;
249 fdata = (const float *)analog->data;
250 *out = g_string_sized_new(512);
251 num_channels = g_slist_length(analog->channels);
252 for (i = 0; i < analog->num_samples; i++) {
253 for (l = analog->channels, c = 0; l; l = l->next, c++) {
255 g_string_append_printf(*out, "%s: ", ch->name);
256 fancyprint(analog->unit, analog->mqflags,
257 fdata[i * num_channels + c], *out);
266 static int cleanup(struct sr_output *o)
274 g_ptr_array_free(ctx->channellist, 1);
281 SR_PRIV struct sr_output_module output_analog = {
284 .desc = "Analog data and types",