]> sigrok.org Git - libsigrok.git/blame - output/analog.c
Factor out serial_readline() to serial.c.
[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>
22#include <glib.h>
161a8a27
BV
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25#include <math.h>
26
27struct context {
28 int num_enabled_probes;
29 GPtrArray *probelist;
30 GString *out;
31};
32
33
34static int init(struct sr_output *o)
35{
36 struct context *ctx;
37 struct sr_probe *probe;
38 GSList *l;
39
40 sr_spew("output/analog: initializing");
41 if (!o || !o->sdi)
42 return SR_ERR_ARG;
43
44 if (!(ctx = g_try_malloc0(sizeof(struct context))))
45 return SR_ERR_MALLOC;
46 o->internal = ctx;
47
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) {
51 probe = l->data;
52 if (!probe || !probe->enabled)
53 continue;
54 g_ptr_array_add(ctx->probelist, probe->name);
55 ctx->num_enabled_probes++;
56 }
57
58 ctx->out = g_string_sized_new(512);
59
60 return SR_OK;
61}
62
63static void si_printf(float value, GString *out, char *unitstr)
64{
d713e561 65 float v;
161a8a27 66
d713e561
BV
67 if (signbit(value))
68 v = -(value);
69 else
70 v = value;
71
72 if (v < 1e-12 || v > 1e+12)
73 g_string_append_printf(out, "%f %s", value, unitstr);
74 else if (v > 1e+9)
75 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
76 else if (v > 1e+6)
77 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
78 else if (v > 1e+3)
79 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
80 else if (v < 1e-9)
81 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
82 else if (v < 1e-6)
83 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
84 else if (v < 1e-3)
85 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
161a8a27
BV
86 else
87 g_string_append_printf(out, "%f %s", value, unitstr);
88
89}
90
91static void fancyprint(int unit, int mqflags, float value, GString *out)
92{
93
94 switch (unit) {
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;
108 case SR_UNIT_KELVIN:
109 si_printf(value, out, "K");
110 break;
111 case SR_UNIT_CELSIUS:
112 si_printf(value, out, "");
113 g_string_append_unichar(out, 0x00b0);
114 g_string_append_c(out, 'C');
115 break;
116 case SR_UNIT_FAHRENHEIT:
117 si_printf(value, out, "");
118 g_string_append_unichar(out, 0x00b0);
119 g_string_append_c(out, 'F');
120 break;
121 case SR_UNIT_HERTZ:
122 si_printf(value, out, "Hz");
123 break;
124 case SR_UNIT_PERCENTAGE:
125 g_string_append_printf(out, "%f%%", value);
126 break;
127 case SR_UNIT_BOOLEAN:
128 if (value > 0)
129 g_string_append_printf(out, "TRUE");
130 else
131 g_string_append_printf(out, "FALSE");
132 break;
aa839a5c
BV
133 case SR_UNIT_SECOND:
134 si_printf(value, out, "s");
135 break;
96b3b3d5
BV
136 case SR_UNIT_SIEMENS:
137 si_printf(value, out, "S");
138 break;
796a79eb
BV
139 case SR_UNIT_DECIBEL_MW:
140 si_printf(value, out, "dBu");
141 break;
142 case SR_UNIT_DECIBEL_VOLT:
143 si_printf(value, out, "dBV");
144 break;
161a8a27 145 }
2b98e0aa
BV
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)
161a8a27
BV
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');
153
154}
155
6aff0d16 156static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi,
161a8a27
BV
157 struct sr_datafeed_packet *packet)
158{
159 struct sr_datafeed_analog *analog;
160 struct context *ctx;
161 float *fdata;
162 int i, j;
163
796a79eb 164 (void)sdi;
161a8a27
BV
165 if (!o || !o->sdi)
166 return NULL;
167 ctx = o->internal;
168
169 g_string_set_size(ctx->out, 0);
170 switch (packet->type) {
171 case SR_DF_HEADER:
172 break;
173 case SR_DF_FRAME_BEGIN:
174 g_string_append_printf(ctx->out, "FRAME-BEGIN\n");
175 break;
176 case SR_DF_FRAME_END:
177 g_string_append_printf(ctx->out, "FRAME-END\n");
178 break;
179 case SR_DF_ANALOG:
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);
188 }
189 }
190 break;
191 }
192
193 return ctx->out;
194}
195
196static int cleanup(struct sr_output *o)
197{
198 struct context *ctx;
199
200 if (!o || !o->sdi)
201 return SR_ERR_ARG;
202 ctx = o->internal;
203
204 g_ptr_array_free(ctx->probelist, 1);
205 g_string_free(ctx->out, 1);
206 g_free(ctx);
207 o->internal = NULL;
208
209 return SR_OK;
210}
211
212
213SR_PRIV struct sr_output_format output_analog = {
214 .id = "analog",
215 .description = "Analog data",
216 .df_type = SR_DF_ANALOG,
217 .init = init,
6aff0d16 218 .recv = receive,
161a8a27
BV
219 .cleanup = cleanup
220};