2 * This file is part of the sigrok 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 /* Message logging helpers with driver-specific prefix string. */
28 #define DRIVER_LOG_DOMAIN "output/analog: "
29 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
30 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
37 int num_enabled_probes;
42 static int init(struct sr_output *o)
45 struct sr_probe *probe;
48 sr_spew("Initializing output module.");
53 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
54 sr_err("Output module context malloc failed.");
59 /* Get the number of probes and their names. */
60 ctx->probelist = g_ptr_array_new();
61 for (l = o->sdi->probes; l; l = l->next) {
63 if (!probe || !probe->enabled)
65 g_ptr_array_add(ctx->probelist, probe->name);
66 ctx->num_enabled_probes++;
69 ctx->out = g_string_sized_new(512);
74 static void si_printf(float value, GString *out, char *unitstr)
83 if (v < 1e-12 || v > 1e+12)
84 g_string_append_printf(out, "%f %s", value, unitstr);
86 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
88 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
90 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
92 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
94 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
96 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
98 g_string_append_printf(out, "%f %s", value, unitstr);
102 static void fancyprint(int unit, int mqflags, float value, GString *out)
106 si_printf(value, out, "V");
109 si_printf(value, out, "A");
112 si_printf(value, out, "");
113 g_string_append_unichar(out, 0x2126);
116 si_printf(value, out, "F");
119 si_printf(value, out, "K");
121 case SR_UNIT_CELSIUS:
122 si_printf(value, out, "");
123 g_string_append_unichar(out, 0x00b0);
124 g_string_append_c(out, 'C');
126 case SR_UNIT_FAHRENHEIT:
127 si_printf(value, out, "");
128 g_string_append_unichar(out, 0x00b0);
129 g_string_append_c(out, 'F');
132 si_printf(value, out, "Hz");
134 case SR_UNIT_PERCENTAGE:
135 g_string_append_printf(out, "%f%%", value);
137 case SR_UNIT_BOOLEAN:
139 g_string_append_printf(out, "TRUE");
141 g_string_append_printf(out, "FALSE");
144 si_printf(value, out, "s");
146 case SR_UNIT_SIEMENS:
147 si_printf(value, out, "S");
149 case SR_UNIT_DECIBEL_MW:
150 si_printf(value, out, "dBu");
152 case SR_UNIT_DECIBEL_VOLT:
153 si_printf(value, out, "dBV");
155 case SR_UNIT_DECIBEL_SPL:
156 if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
157 si_printf(value, out, "dB(A)");
158 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C)
159 si_printf(value, out, "dB(C)");
160 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z)
161 si_printf(value, out, "dB(Z)");
163 /* No frequency weighting, or non-standard "flat" */
164 si_printf(value, out, "dB(SPL)");
165 if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S)
166 g_string_append(out, " S");
167 else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
168 g_string_append(out, " F");
169 if (mqflags & SR_MQFLAG_SPL_LAT)
170 g_string_append(out, " LAT");
171 else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM)
172 /* Not a standard function for SLMs, so this is
173 * a made-up notation. */
174 g_string_append(out, " %oA");
177 si_printf(value, out, "");
179 if ((mqflags & (SR_MQFLAG_AC | SR_MQFLAG_DC)) == (SR_MQFLAG_AC | SR_MQFLAG_DC))
180 g_string_append_printf(out, " AC+DC");
181 else if (mqflags & SR_MQFLAG_AC)
182 g_string_append_printf(out, " AC");
183 else if (mqflags & SR_MQFLAG_DC)
184 g_string_append_printf(out, " DC");
185 g_string_append_c(out, '\n');
188 static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
189 struct sr_datafeed_packet *packet)
191 struct sr_datafeed_analog *analog;
202 g_string_set_size(ctx->out, 0);
203 switch (packet->type) {
206 case SR_DF_FRAME_BEGIN:
207 g_string_append_printf(ctx->out, "FRAME-BEGIN\n");
209 case SR_DF_FRAME_END:
210 g_string_append_printf(ctx->out, "FRAME-END\n");
213 analog = packet->payload;
214 fdata = (float *)analog->data;
215 for (i = 0; i < analog->num_samples; i++) {
216 for (j = 0; j < ctx->num_enabled_probes; j++) {
217 g_string_append_printf(ctx->out, "%s: ",
218 (char *)g_ptr_array_index(ctx->probelist, j));
219 fancyprint(analog->unit, analog->mqflags,
220 fdata[i + j], ctx->out);
229 static int cleanup(struct sr_output *o)
237 g_ptr_array_free(ctx->probelist, 1);
238 g_string_free(ctx->out, 1);
245 SR_PRIV struct sr_output_format output_analog = {
247 .description = "Analog data",
248 .df_type = SR_DF_ANALOG,