]>
Commit | Line | Data |
---|---|---|
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 <math.h> | |
23 | #include <glib.h> | |
24 | #include "libsigrok.h" | |
25 | #include "libsigrok-internal.h" | |
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) | |
35 | ||
36 | struct context { | |
37 | int num_enabled_probes; | |
38 | GPtrArray *probelist; | |
39 | GString *out; | |
40 | }; | |
41 | ||
42 | static int init(struct sr_output *o) | |
43 | { | |
44 | struct context *ctx; | |
45 | struct sr_probe *probe; | |
46 | GSList *l; | |
47 | ||
48 | sr_spew("Initializing output module."); | |
49 | ||
50 | if (!o || !o->sdi) | |
51 | return SR_ERR_ARG; | |
52 | ||
53 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { | |
54 | sr_err("Output module context malloc failed."); | |
55 | return SR_ERR_MALLOC; | |
56 | } | |
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 | ||
74 | static void si_printf(float value, GString *out, char *unitstr) | |
75 | { | |
76 | float v; | |
77 | ||
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); | |
97 | else | |
98 | g_string_append_printf(out, "%f %s", value, unitstr); | |
99 | ||
100 | } | |
101 | ||
102 | static void fancyprint(int unit, int mqflags, float value, GString *out) | |
103 | { | |
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; | |
143 | case SR_UNIT_SECOND: | |
144 | si_printf(value, out, "s"); | |
145 | break; | |
146 | case SR_UNIT_SIEMENS: | |
147 | si_printf(value, out, "S"); | |
148 | break; | |
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; | |
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; | |
176 | case SR_UNIT_CONCENTRATION: | |
177 | g_string_append_printf(out, "%f ppm", value * 1000000); | |
178 | break; | |
179 | default: | |
180 | si_printf(value, out, ""); | |
181 | } | |
182 | if ((mqflags & (SR_MQFLAG_AC | SR_MQFLAG_DC)) == (SR_MQFLAG_AC | SR_MQFLAG_DC)) | |
183 | g_string_append_printf(out, " AC+DC"); | |
184 | else if (mqflags & SR_MQFLAG_AC) | |
185 | g_string_append_printf(out, " AC"); | |
186 | else if (mqflags & SR_MQFLAG_DC) | |
187 | g_string_append_printf(out, " DC"); | |
188 | g_string_append_c(out, '\n'); | |
189 | } | |
190 | ||
191 | static GString *receive(struct sr_output *o, const struct sr_dev_inst *sdi, | |
192 | const struct sr_datafeed_packet *packet) | |
193 | { | |
194 | struct context *ctx; | |
195 | const struct sr_datafeed_analog *analog; | |
196 | struct sr_probe *probe; | |
197 | GSList *l; | |
198 | const float *fdata; | |
199 | int i, p; | |
200 | ||
201 | (void)sdi; | |
202 | ||
203 | if (!o || !o->sdi) | |
204 | return NULL; | |
205 | ctx = o->internal; | |
206 | ||
207 | g_string_set_size(ctx->out, 0); | |
208 | switch (packet->type) { | |
209 | case SR_DF_HEADER: | |
210 | break; | |
211 | case SR_DF_FRAME_BEGIN: | |
212 | g_string_append_printf(ctx->out, "FRAME-BEGIN\n"); | |
213 | break; | |
214 | case SR_DF_FRAME_END: | |
215 | g_string_append_printf(ctx->out, "FRAME-END\n"); | |
216 | break; | |
217 | case SR_DF_ANALOG: | |
218 | analog = packet->payload; | |
219 | fdata = (const float *)analog->data; | |
220 | for (i = 0; i < analog->num_samples; i++) { | |
221 | for (l = analog->probes, p = 0; l; l = l->next, p++) { | |
222 | probe = l->data; | |
223 | g_string_append_printf(ctx->out, "%s: ", probe->name); | |
224 | fancyprint(analog->unit, analog->mqflags, | |
225 | fdata[i + p], ctx->out); | |
226 | } | |
227 | } | |
228 | break; | |
229 | } | |
230 | ||
231 | return ctx->out; | |
232 | } | |
233 | ||
234 | static int cleanup(struct sr_output *o) | |
235 | { | |
236 | struct context *ctx; | |
237 | ||
238 | if (!o || !o->sdi) | |
239 | return SR_ERR_ARG; | |
240 | ctx = o->internal; | |
241 | ||
242 | g_ptr_array_free(ctx->probelist, 1); | |
243 | g_string_free(ctx->out, 1); | |
244 | g_free(ctx); | |
245 | o->internal = NULL; | |
246 | ||
247 | return SR_OK; | |
248 | } | |
249 | ||
250 | SR_PRIV struct sr_output_format output_analog = { | |
251 | .id = "analog", | |
252 | .description = "Analog data", | |
253 | .df_type = SR_DF_ANALOG, | |
254 | .init = init, | |
255 | .recv = receive, | |
256 | .cleanup = cleanup | |
257 | }; |