]> sigrok.org Git - libsigrok.git/blame - output/analog.c
Add compress option to input/vcd.
[libsigrok.git] / output / analog.c
CommitLineData
161a8a27
BV
1/*
2 * This file is part of the sigrok project.
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
UH
26
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)
161a8a27
BV
35
36struct context {
37 int num_enabled_probes;
38 GPtrArray *probelist;
39 GString *out;
40};
41
161a8a27
BV
42static int init(struct sr_output *o)
43{
44 struct context *ctx;
45 struct sr_probe *probe;
46 GSList *l;
47
a944a84b
UH
48 sr_spew("Initializing output module.");
49
161a8a27
BV
50 if (!o || !o->sdi)
51 return SR_ERR_ARG;
52
886a52b6 53 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 54 sr_err("Output module context malloc failed.");
161a8a27 55 return SR_ERR_MALLOC;
886a52b6 56 }
161a8a27
BV
57 o->internal = ctx;
58
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) {
62 probe = l->data;
63 if (!probe || !probe->enabled)
64 continue;
65 g_ptr_array_add(ctx->probelist, probe->name);
66 ctx->num_enabled_probes++;
67 }
68
69 ctx->out = g_string_sized_new(512);
70
71 return SR_OK;
72}
73
74static void si_printf(float value, GString *out, char *unitstr)
75{
d713e561 76 float v;
161a8a27 77
d713e561
BV
78 if (signbit(value))
79 v = -(value);
80 else
81 v = value;
82
83 if (v < 1e-12 || v > 1e+12)
84 g_string_append_printf(out, "%f %s", value, unitstr);
85 else if (v > 1e+9)
86 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
87 else if (v > 1e+6)
88 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
89 else if (v > 1e+3)
90 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
91 else if (v < 1e-9)
92 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
93 else if (v < 1e-6)
94 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
95 else if (v < 1e-3)
96 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
161a8a27
BV
97 else
98 g_string_append_printf(out, "%f %s", value, unitstr);
99
100}
101
102static void fancyprint(int unit, int mqflags, float value, GString *out)
103{
161a8a27
BV
104 switch (unit) {
105 case SR_UNIT_VOLT:
106 si_printf(value, out, "V");
107 break;
108 case SR_UNIT_AMPERE:
109 si_printf(value, out, "A");
110 break;
111 case SR_UNIT_OHM:
112 si_printf(value, out, "");
113 g_string_append_unichar(out, 0x2126);
114 break;
115 case SR_UNIT_FARAD:
116 si_printf(value, out, "F");
117 break;
118 case SR_UNIT_KELVIN:
119 si_printf(value, out, "K");
120 break;
121 case SR_UNIT_CELSIUS:
122 si_printf(value, out, "");
123 g_string_append_unichar(out, 0x00b0);
124 g_string_append_c(out, 'C');
125 break;
126 case SR_UNIT_FAHRENHEIT:
127 si_printf(value, out, "");
128 g_string_append_unichar(out, 0x00b0);
129 g_string_append_c(out, 'F');
130 break;
131 case SR_UNIT_HERTZ:
132 si_printf(value, out, "Hz");
133 break;
134 case SR_UNIT_PERCENTAGE:
135 g_string_append_printf(out, "%f%%", value);
136 break;
137 case SR_UNIT_BOOLEAN:
138 if (value > 0)
139 g_string_append_printf(out, "TRUE");
140 else
141 g_string_append_printf(out, "FALSE");
142 break;
aa839a5c
BV
143 case SR_UNIT_SECOND:
144 si_printf(value, out, "s");
145 break;
96b3b3d5
BV
146 case SR_UNIT_SIEMENS:
147 si_printf(value, out, "S");
148 break;
796a79eb
BV
149 case SR_UNIT_DECIBEL_MW:
150 si_printf(value, out, "dBu");
151 break;
152 case SR_UNIT_DECIBEL_VOLT:
153 si_printf(value, out, "dBV");
154 break;
258eeb77
BV
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)");
162 else
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");
175 break;
bde4f429
BV
176 default:
177 si_printf(value, out, "");
161a8a27 178 }
2b98e0aa
BV
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)
161a8a27
BV
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');
161a8a27
BV
186}
187
6aff0d16 188static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
161a8a27
BV
189 struct sr_datafeed_packet *packet)
190{
191 struct sr_datafeed_analog *analog;
192 struct context *ctx;
193 float *fdata;
194 int i, j;
195
796a79eb 196 (void)sdi;
64d33dc2 197
161a8a27
BV
198 if (!o || !o->sdi)
199 return NULL;
200 ctx = o->internal;
201
202 g_string_set_size(ctx->out, 0);
203 switch (packet->type) {
204 case SR_DF_HEADER:
205 break;
206 case SR_DF_FRAME_BEGIN:
207 g_string_append_printf(ctx->out, "FRAME-BEGIN\n");
208 break;
209 case SR_DF_FRAME_END:
210 g_string_append_printf(ctx->out, "FRAME-END\n");
211 break;
212 case SR_DF_ANALOG:
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);
221 }
222 }
223 break;
224 }
225
226 return ctx->out;
227}
228
229static int cleanup(struct sr_output *o)
230{
231 struct context *ctx;
232
233 if (!o || !o->sdi)
234 return SR_ERR_ARG;
235 ctx = o->internal;
236
237 g_ptr_array_free(ctx->probelist, 1);
238 g_string_free(ctx->out, 1);
239 g_free(ctx);
240 o->internal = NULL;
241
242 return SR_OK;
243}
244
161a8a27
BV
245SR_PRIV struct sr_output_format output_analog = {
246 .id = "analog",
247 .description = "Analog data",
248 .df_type = SR_DF_ANALOG,
249 .init = init,
6aff0d16 250 .recv = receive,
161a8a27
BV
251 .cleanup = cleanup
252};