]> sigrok.org Git - libsigrok.git/blame - src/analog.c
Various key lists: Add reminders of what needs updates upon changes.
[libsigrok.git] / src / analog.c
CommitLineData
fb019a0e
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 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
c2a25ebb
BV
20#include <stdio.h>
21#include <stdint.h>
fb019a0e 22#include <string.h>
c2a25ebb 23#include <ctype.h>
c1aae900 24#include <libsigrok/libsigrok.h>
fb019a0e
BV
25#include "libsigrok-internal.h"
26
e00b3f58 27/** @cond PRIVATE */
fb019a0e 28#define LOG_PREFIX "analog"
e00b3f58
UH
29/** @endcond */
30
31/**
32 * @file
33 *
34 * Handling and converting analog data.
35 */
36
37/**
38 * @defgroup grp_analog Analog data handling
39 *
40 * Handling and converting analog data.
41 *
42 * @{
43 */
fb019a0e 44
a5892391
BV
45struct unit_mq_string {
46 uint64_t value;
47 char *str;
48};
49
ca7dbb56 50/* Please use the same order as in enum sr_unit (libsigrok.h). */
a5892391
BV
51static struct unit_mq_string unit_strings[] = {
52 { SR_UNIT_VOLT, "V" },
53 { SR_UNIT_AMPERE, "A" },
54 { SR_UNIT_OHM, "\xe2\x84\xa6" },
55 { SR_UNIT_FARAD, "F" },
a5892391
BV
56 { SR_UNIT_KELVIN, "K" },
57 { SR_UNIT_CELSIUS, "\xc2\xb0""C" },
58 { SR_UNIT_FAHRENHEIT, "\xc2\xb0""F" },
59 { SR_UNIT_HERTZ, "Hz" },
60 { SR_UNIT_PERCENTAGE, "%" },
f7bcc686 61 { SR_UNIT_BOOLEAN, "" },
a5892391
BV
62 { SR_UNIT_SECOND, "s" },
63 { SR_UNIT_SIEMENS, "S" },
64 { SR_UNIT_DECIBEL_MW, "dBu" },
65 { SR_UNIT_DECIBEL_VOLT, "dBv" },
f7bcc686 66 { SR_UNIT_UNITLESS, "" },
a5892391
BV
67 { SR_UNIT_DECIBEL_SPL, "dB" },
68 { SR_UNIT_CONCENTRATION, "ppm" },
69 { SR_UNIT_REVOLUTIONS_PER_MINUTE, "RPM" },
70 { SR_UNIT_VOLT_AMPERE, "VA" },
71 { SR_UNIT_WATT, "W" },
72 { SR_UNIT_WATT_HOUR, "Wh" },
73 { SR_UNIT_METER_SECOND, "m/s" },
74 { SR_UNIT_HECTOPASCAL, "hPa" },
75 { SR_UNIT_HUMIDITY_293K, "%rF" },
76 { SR_UNIT_DEGREE, "\xc2\xb0" },
f7bcc686
UH
77 { SR_UNIT_HENRY, "H" },
78 { SR_UNIT_GRAM, "g" },
79 { SR_UNIT_CARAT, "ct" },
80 { SR_UNIT_OUNCE, "oz" },
81 { SR_UNIT_TROY_OUNCE, "oz t" },
82 { SR_UNIT_POUND, "lb" },
83 { SR_UNIT_PENNYWEIGHT, "dwt" },
84 { SR_UNIT_GRAIN, "gr" },
85 { SR_UNIT_TAEL, "tael" },
86 { SR_UNIT_MOMME, "momme" },
87 { SR_UNIT_TOLA, "tola" },
88 { SR_UNIT_PIECE, "pcs" },
a5892391
BV
89 ALL_ZERO
90};
91
ca7dbb56 92/* Please use the same order as in enum sr_mqflag (libsigrok.h). */
a5892391 93static struct unit_mq_string mq_strings[] = {
a5892391
BV
94 { SR_MQFLAG_AC, " AC" },
95 { SR_MQFLAG_DC, " DC" },
96 { SR_MQFLAG_RMS, " RMS" },
97 { SR_MQFLAG_DIODE, " DIODE" },
98 { SR_MQFLAG_HOLD, " HOLD" },
99 { SR_MQFLAG_MAX, " MAX" },
100 { SR_MQFLAG_MIN, " MIN" },
101 { SR_MQFLAG_AUTORANGE, " AUTO" },
102 { SR_MQFLAG_RELATIVE, " REL" },
f7bcc686
UH
103 { SR_MQFLAG_SPL_FREQ_WEIGHT_A, "(A)" },
104 { SR_MQFLAG_SPL_FREQ_WEIGHT_C, "(C)" },
105 { SR_MQFLAG_SPL_FREQ_WEIGHT_Z, "(Z)" },
106 { SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT, "(SPL)" },
107 { SR_MQFLAG_SPL_TIME_WEIGHT_S, " S" },
108 { SR_MQFLAG_SPL_TIME_WEIGHT_F, " F" },
109 { SR_MQFLAG_SPL_LAT, " LAT" },
110 /* Not a standard function for SLMs, so this is a made-up notation. */
111 { SR_MQFLAG_SPL_PCT_OVER_ALARM, "%oA" },
112 { SR_MQFLAG_DURATION, " DURATION" },
a5892391
BV
113 { SR_MQFLAG_AVG, " AVG" },
114 { SR_MQFLAG_REFERENCE, " REF" },
f7bcc686 115 { SR_MQFLAG_UNSTABLE, " UNSTABLE" },
a5892391
BV
116 ALL_ZERO
117};
118
41caa319
AJ
119SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog,
120 struct sr_analog_encoding *encoding,
121 struct sr_analog_meaning *meaning,
122 struct sr_analog_spec *spec,
123 int digits)
124{
125 memset(analog, 0, sizeof(*analog));
126 memset(encoding, 0, sizeof(*encoding));
127 memset(meaning, 0, sizeof(*meaning));
128 memset(spec, 0, sizeof(*spec));
129
130 analog->encoding = encoding;
131 analog->meaning = meaning;
132 analog->spec = spec;
133
134 encoding->unitsize = sizeof(float);
135 encoding->is_float = TRUE;
136#ifdef WORDS_BIGENDIAN
137 encoding->is_bigendian = TRUE;
138#else
139 encoding->is_bigendian = FALSE;
140#endif
141 encoding->digits = digits;
142 encoding->is_digits_decimal = TRUE;
143 encoding->scale.p = 1;
144 encoding->scale.q = 1;
145 encoding->offset.p = 0;
146 encoding->offset.q = 1;
147
148 spec->spec_digits = digits;
149
150 return SR_OK;
151}
152
fb019a0e 153SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
4b4fdeea 154 float *outbuf)
fb019a0e
BV
155{
156 float offset;
157 unsigned int b, i;
158 gboolean bigendian;
159
160#ifdef WORDS_BIGENDIAN
161 bigendian = TRUE;
162#else
163 bigendian = FALSE;
164#endif
165 if (!analog->encoding->is_float) {
166 /* TODO */
4b4fdeea 167 sr_err("Only floating-point encoding supported so far.");
fb019a0e
BV
168 return SR_ERR;
169 }
170
171 if (analog->encoding->unitsize == sizeof(float)
172 && analog->encoding->is_bigendian == bigendian
173 && (analog->encoding->scale.p == analog->encoding->scale.q)
4b4fdeea 174 && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) {
fb019a0e 175 /* The data is already in the right format. */
4b4fdeea 176 memcpy(outbuf, analog->data, analog->num_samples * sizeof(float));
fb019a0e
BV
177 } else {
178 for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) {
179 for (b = 0; b < analog->encoding->unitsize; b++) {
180 if (analog->encoding->is_bigendian == bigendian)
4b4fdeea 181 outbuf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e 182 else
4b4fdeea 183 outbuf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e
BV
184 }
185 if (analog->encoding->scale.p != analog->encoding->scale.q)
4b4fdeea
BV
186 outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
187 offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q);
188 outbuf[i] += offset;
fb019a0e
BV
189 }
190 }
191
192 return SR_OK;
193}
c2a25ebb
BV
194
195/*
196 * Convert a floating point value to a string, limited to the given
197 * number of decimal digits.
198 *
199 * @param value The value to convert.
200 * @param digits Number of digits after the decimal point to print.
a24da9a8
ML
201 * @param result Pointer to store result.
202 *
203 * The string is allocated by the function and must be freed by the caller
204 * after use by calling g_free().
c2a25ebb
BV
205 *
206 * @retval SR_OK
207 *
208 * @since 0.4.0
209 */
a24da9a8 210SR_API int sr_analog_float_to_string(float value, int digits, char **result)
c2a25ebb
BV
211{
212 int cnt, i;
213
214 /* This produces at least one too many digits */
a24da9a8 215 *result = g_strdup_printf("%.*f", digits, value);
a9b2283f
BV
216 for (i = 0, cnt = 0; (*result)[i]; i++) {
217 if (isdigit((*result)[i++]))
c2a25ebb
BV
218 cnt++;
219 if (cnt == digits) {
a9b2283f 220 (*result)[i] = 0;
c2a25ebb
BV
221 break;
222 }
223 }
224
225 return SR_OK;
226}
227
a5892391
BV
228/*
229 * Convert the unit/MQ/MQ flags in the analog struct to a string.
230 *
231 * @param analog Struct containing the unit, MQ and MQ flags.
a24da9a8
ML
232 * @param result Pointer to store result.
233 *
234 * The string is allocated by the function and must be freed by the caller
235 * after use by calling g_free().
a5892391
BV
236 *
237 * @retval SR_OK
238 *
239 * @since 0.4.0
240 */
241SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog2 *analog,
a24da9a8 242 char **result)
a5892391 243{
a24da9a8
ML
244 int i;
245 GString *buf = g_string_new(NULL);
a5892391 246
a5892391
BV
247 for (i = 0; unit_strings[i].value; i++) {
248 if (analog->meaning->unit == unit_strings[i].value) {
a24da9a8 249 g_string_assign(buf, unit_strings[i].str);
a5892391
BV
250 break;
251 }
252 }
253
254 /* More than one MQ flag may apply. */
a24da9a8
ML
255 for (i = 0; mq_strings[i].value; i++)
256 if (analog->meaning->mqflags & mq_strings[i].value)
257 g_string_append(buf, mq_strings[i].str);
258
259 *result = buf->str;
260 g_string_free(buf, FALSE);
a5892391
BV
261
262 return SR_OK;
263}
264
90cefe0c
BV
265/*
266 * Set sr_rational r to the given value.
267 *
268 * @param p Numerator
269 * @param q Denominator
270 */
2617c81a 271SR_API void sr_rational_set(struct sr_rational *r, uint64_t p, uint64_t q)
90cefe0c
BV
272{
273 r->p = p;
274 r->q = q;
275}
276
e00b3f58 277/** @} */