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/>.
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
28 int num_enabled_probes;
34 static int init(struct sr_output *o)
37 struct sr_probe *probe;
40 sr_spew("output/analog: initializing");
44 if (!(ctx = g_try_malloc0(sizeof(struct context))))
48 /* Get the number of probes and their names. */
49 ctx->probelist = g_ptr_array_new();
50 for (l = o->sdi->probes; l; l = l->next) {
52 if (!probe || !probe->enabled)
54 g_ptr_array_add(ctx->probelist, probe->name);
55 ctx->num_enabled_probes++;
58 ctx->out = g_string_sized_new(512);
63 static void si_printf(float value, GString *out, char *unitstr)
72 if (v < 1e-12 || v > 1e+12)
73 g_string_append_printf(out, "%f %s", value, unitstr);
75 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
77 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
79 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
81 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
83 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
85 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
87 g_string_append_printf(out, "%f %s", value, unitstr);
91 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, "K");
111 case SR_UNIT_CELSIUS:
112 si_printf(value, out, "");
113 g_string_append_unichar(out, 0x00b0);
114 g_string_append_c(out, 'C');
116 case SR_UNIT_FAHRENHEIT:
117 si_printf(value, out, "");
118 g_string_append_unichar(out, 0x00b0);
119 g_string_append_c(out, 'F');
122 si_printf(value, out, "Hz");
124 case SR_UNIT_PERCENTAGE:
125 g_string_append_printf(out, "%f%%", value);
127 case SR_UNIT_BOOLEAN:
129 g_string_append_printf(out, "TRUE");
131 g_string_append_printf(out, "FALSE");
134 si_printf(value, out, "s");
136 case SR_UNIT_SIEMENS:
137 si_printf(value, out, "S");
139 case SR_UNIT_DECIBEL_MW:
140 si_printf(value, out, "dBu");
142 case SR_UNIT_DECIBEL_VOLT:
143 si_printf(value, out, "dBV");
146 if ((mqflags & (SR_MQFLAG_AC | SR_MQFLAG_DC)) == (SR_MQFLAG_AC | SR_MQFLAG_DC))
147 g_string_append_printf(out, " AC+DC");
148 else if (mqflags & SR_MQFLAG_AC)
149 g_string_append_printf(out, " AC");
150 else if (mqflags & SR_MQFLAG_DC)
151 g_string_append_printf(out, " DC");
152 g_string_append_c(out, '\n');
156 static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
157 struct sr_datafeed_packet *packet)
159 struct sr_datafeed_analog *analog;
169 g_string_set_size(ctx->out, 0);
170 switch (packet->type) {
173 case SR_DF_FRAME_BEGIN:
174 g_string_append_printf(ctx->out, "FRAME-BEGIN\n");
176 case SR_DF_FRAME_END:
177 g_string_append_printf(ctx->out, "FRAME-END\n");
180 analog = packet->payload;
181 fdata = (float *)analog->data;
182 for (i = 0; i < analog->num_samples; i++) {
183 for (j = 0; j < ctx->num_enabled_probes; j++) {
184 g_string_append_printf(ctx->out, "%s: ",
185 (char *)g_ptr_array_index(ctx->probelist, j));
186 fancyprint(analog->unit, analog->mqflags,
187 fdata[i + j], ctx->out);
196 static int cleanup(struct sr_output *o)
204 g_ptr_array_free(ctx->probelist, 1);
205 g_string_free(ctx->out, 1);
213 SR_PRIV struct sr_output_format output_analog = {
215 .description = "Analog data",
216 .df_type = SR_DF_ANALOG,