]> sigrok.org Git - libsigrok.git/blob - output/analog.c
sr: add new analog output module
[libsigrok.git] / output / analog.c
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>
23 #include "config.h"
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26 #include <math.h>
27
28 struct context {
29         int num_enabled_probes;
30         GPtrArray *probelist;
31         GString *out;
32 };
33
34
35 static int init(struct sr_output *o)
36 {
37         struct context *ctx;
38         struct sr_probe *probe;
39         GSList *l;
40
41         sr_spew("output/analog: initializing");
42         if (!o || !o->sdi)
43                 return SR_ERR_ARG;
44
45         if (!(ctx = g_try_malloc0(sizeof(struct context))))
46                 return SR_ERR_MALLOC;
47         o->internal = ctx;
48
49         /* Get the number of probes and their names. */
50         ctx->probelist = g_ptr_array_new();
51         for (l = o->sdi->probes; l; l = l->next) {
52                 probe = l->data;
53                 if (!probe || !probe->enabled)
54                         continue;
55                 g_ptr_array_add(ctx->probelist, probe->name);
56                 ctx->num_enabled_probes++;
57         }
58
59         ctx->out = g_string_sized_new(512);
60
61         return SR_OK;
62 }
63
64 static void si_printf(float value, GString *out, char *unitstr)
65 {
66
67         if (value > 1000000000L)
68                 g_string_append_printf(out, "%f G%s", value / 1000000000L, unitstr);
69         else if (value > 1000000)
70                 g_string_append_printf(out, "%f M%s", value / 1000000, unitstr);
71         else if (value > 1000)
72                 g_string_append_printf(out, "%f k%s", value / 1000, unitstr);
73         else if (value < 0.000000000001)
74                 g_string_append_printf(out, "%f p%s", value * 1000000000000, unitstr);
75         else if (value < 0.000000001)
76                 g_string_append_printf(out, "%f n%s", value * 1000000000, unitstr);
77         else if (value < 0.000001)
78                 g_string_append_printf(out, "%f u%s", value * 1000000, unitstr);
79         else if (value < 0.001)
80                 g_string_append_printf(out, "%f m%s", value * 1000, unitstr);
81         else
82                 g_string_append_printf(out, "%f %s", value, unitstr);
83
84 }
85
86 static void fancyprint(int unit, int mqflags, float value, GString *out)
87 {
88
89         switch (unit) {
90                 case SR_UNIT_VOLT:
91                         si_printf(value, out, "V");
92                         break;
93                 case SR_UNIT_AMPERE:
94                         si_printf(value, out, "A");
95                         break;
96                 case SR_UNIT_OHM:
97                         si_printf(value, out, "");
98                         g_string_append_unichar(out, 0x2126);
99                         break;
100                 case SR_UNIT_FARAD:
101                         si_printf(value, out, "F");
102                         break;
103                 case SR_UNIT_KELVIN:
104                         si_printf(value, out, "K");
105                         break;
106                 case SR_UNIT_CELSIUS:
107                         si_printf(value, out, "");
108                         g_string_append_unichar(out, 0x00b0);
109                         g_string_append_c(out, 'C');
110                         break;
111                 case SR_UNIT_FAHRENHEIT:
112                         si_printf(value, out, "");
113                         g_string_append_unichar(out, 0x00b0);
114                         g_string_append_c(out, 'F');
115                         break;
116                 case SR_UNIT_HERTZ:
117                         si_printf(value, out, "Hz");
118                         break;
119                 case SR_UNIT_PERCENTAGE:
120                         g_string_append_printf(out, "%f%%", value);
121                         break;
122                 case SR_UNIT_BOOLEAN:
123                         if (value > 0)
124                                 g_string_append_printf(out, "TRUE");
125                         else
126                                 g_string_append_printf(out, "FALSE");
127                         break;
128         }
129         if (mqflags & SR_MQFLAG_AC)
130                 g_string_append_printf(out, " AC");
131         else if (mqflags & SR_MQFLAG_DC)
132                 g_string_append_printf(out, " DC");
133         g_string_append_c(out, '\n');
134
135 }
136
137 static GString *recv(struct sr_output *o, const struct sr_dev_inst *sdi,
138                 struct sr_datafeed_packet *packet)
139 {
140         struct sr_datafeed_analog *analog;
141         struct context *ctx;
142         float *fdata;
143         int i, j;
144
145         if (!o || !o->sdi)
146                 return NULL;
147         ctx = o->internal;
148
149         g_string_set_size(ctx->out, 0);
150         switch (packet->type) {
151         case SR_DF_HEADER:
152                 break;
153         case SR_DF_FRAME_BEGIN:
154                 g_string_append_printf(ctx->out, "FRAME-BEGIN\n");
155                 break;
156         case SR_DF_FRAME_END:
157                 g_string_append_printf(ctx->out, "FRAME-END\n");
158                 break;
159         case SR_DF_ANALOG:
160                 analog = packet->payload;
161                 fdata = (float *)analog->data;
162                 for (i = 0; i < analog->num_samples; i++) {
163                         for (j = 0; j < ctx->num_enabled_probes; j++) {
164                                 g_string_append_printf(ctx->out, "%s: ",
165                                                 (char *)g_ptr_array_index(ctx->probelist, j));
166                                 fancyprint(analog->unit, analog->mqflags,
167                                                 fdata[i + j], ctx->out);
168                         }
169                 }
170                 break;
171         }
172
173         return ctx->out;
174 }
175
176 static int cleanup(struct sr_output *o)
177 {
178         struct context *ctx;
179
180         if (!o || !o->sdi)
181                 return SR_ERR_ARG;
182         ctx = o->internal;
183
184         g_ptr_array_free(ctx->probelist, 1);
185         g_string_free(ctx->out, 1);
186         g_free(ctx);
187         o->internal = NULL;
188
189         return SR_OK;
190 }
191
192
193 SR_PRIV struct sr_output_format output_analog = {
194         .id = "analog",
195         .description = "Analog data",
196         .df_type = SR_DF_ANALOG,
197         .init = init,
198         .recv = recv,
199         .cleanup = cleanup
200 };