]> sigrok.org Git - libsigrok.git/blame - src/analog.c
Build: Set local include directories in Makefile.am
[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
50static struct unit_mq_string unit_strings[] = {
51 { SR_UNIT_VOLT, "V" },
52 { SR_UNIT_AMPERE, "A" },
53 { SR_UNIT_OHM, "\xe2\x84\xa6" },
54 { SR_UNIT_FARAD, "F" },
55 { SR_UNIT_HENRY, "H" },
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, "%" },
61 { SR_UNIT_SECOND, "s" },
62 { SR_UNIT_SIEMENS, "S" },
63 { SR_UNIT_DECIBEL_MW, "dBu" },
64 { SR_UNIT_DECIBEL_VOLT, "dBv" },
65 { SR_UNIT_DECIBEL_SPL, "dB" },
66 { SR_UNIT_CONCENTRATION, "ppm" },
67 { SR_UNIT_REVOLUTIONS_PER_MINUTE, "RPM" },
68 { SR_UNIT_VOLT_AMPERE, "VA" },
69 { SR_UNIT_WATT, "W" },
70 { SR_UNIT_WATT_HOUR, "Wh" },
71 { SR_UNIT_METER_SECOND, "m/s" },
72 { SR_UNIT_HECTOPASCAL, "hPa" },
73 { SR_UNIT_HUMIDITY_293K, "%rF" },
74 { SR_UNIT_DEGREE, "\xc2\xb0" },
75 ALL_ZERO
76};
77
78static struct unit_mq_string mq_strings[] = {
79 { SR_MQFLAG_SPL_FREQ_WEIGHT_A, "(A)" },
80 { SR_MQFLAG_SPL_FREQ_WEIGHT_C, "(C)" },
81 { SR_MQFLAG_SPL_FREQ_WEIGHT_Z, "(Z)" },
82 { SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT, "(SPL)" },
83 { SR_MQFLAG_SPL_TIME_WEIGHT_S, " S" },
84 { SR_MQFLAG_SPL_TIME_WEIGHT_F, " F" },
85 { SR_MQFLAG_SPL_LAT, " LAT" },
86 /* Not a standard function for SLMs, so this is a made-up notation. */
87 { SR_MQFLAG_SPL_PCT_OVER_ALARM, "%oA" },
88 { SR_MQFLAG_AC, " AC" },
89 { SR_MQFLAG_DC, " DC" },
90 { SR_MQFLAG_RMS, " RMS" },
91 { SR_MQFLAG_DIODE, " DIODE" },
92 { SR_MQFLAG_HOLD, " HOLD" },
93 { SR_MQFLAG_MAX, " MAX" },
94 { SR_MQFLAG_MIN, " MIN" },
95 { SR_MQFLAG_AUTORANGE, " AUTO" },
96 { SR_MQFLAG_RELATIVE, " REL" },
97 { SR_MQFLAG_AVG, " AVG" },
98 { SR_MQFLAG_REFERENCE, " REF" },
99 ALL_ZERO
100};
101
41caa319
AJ
102SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog,
103 struct sr_analog_encoding *encoding,
104 struct sr_analog_meaning *meaning,
105 struct sr_analog_spec *spec,
106 int digits)
107{
108 memset(analog, 0, sizeof(*analog));
109 memset(encoding, 0, sizeof(*encoding));
110 memset(meaning, 0, sizeof(*meaning));
111 memset(spec, 0, sizeof(*spec));
112
113 analog->encoding = encoding;
114 analog->meaning = meaning;
115 analog->spec = spec;
116
117 encoding->unitsize = sizeof(float);
118 encoding->is_float = TRUE;
119#ifdef WORDS_BIGENDIAN
120 encoding->is_bigendian = TRUE;
121#else
122 encoding->is_bigendian = FALSE;
123#endif
124 encoding->digits = digits;
125 encoding->is_digits_decimal = TRUE;
126 encoding->scale.p = 1;
127 encoding->scale.q = 1;
128 encoding->offset.p = 0;
129 encoding->offset.q = 1;
130
131 spec->spec_digits = digits;
132
133 return SR_OK;
134}
135
fb019a0e 136SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
4b4fdeea 137 float *outbuf)
fb019a0e
BV
138{
139 float offset;
140 unsigned int b, i;
141 gboolean bigendian;
142
143#ifdef WORDS_BIGENDIAN
144 bigendian = TRUE;
145#else
146 bigendian = FALSE;
147#endif
148 if (!analog->encoding->is_float) {
149 /* TODO */
4b4fdeea 150 sr_err("Only floating-point encoding supported so far.");
fb019a0e
BV
151 return SR_ERR;
152 }
153
154 if (analog->encoding->unitsize == sizeof(float)
155 && analog->encoding->is_bigendian == bigendian
156 && (analog->encoding->scale.p == analog->encoding->scale.q)
4b4fdeea 157 && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) {
fb019a0e 158 /* The data is already in the right format. */
4b4fdeea 159 memcpy(outbuf, analog->data, analog->num_samples * sizeof(float));
fb019a0e
BV
160 } else {
161 for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) {
162 for (b = 0; b < analog->encoding->unitsize; b++) {
163 if (analog->encoding->is_bigendian == bigendian)
4b4fdeea 164 outbuf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e 165 else
4b4fdeea 166 outbuf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
fb019a0e
BV
167 }
168 if (analog->encoding->scale.p != analog->encoding->scale.q)
4b4fdeea
BV
169 outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
170 offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q);
171 outbuf[i] += offset;
fb019a0e
BV
172 }
173 }
174
175 return SR_OK;
176}
c2a25ebb
BV
177
178/*
179 * Convert a floating point value to a string, limited to the given
180 * number of decimal digits.
181 *
182 * @param value The value to convert.
183 * @param digits Number of digits after the decimal point to print.
a24da9a8
ML
184 * @param result Pointer to store result.
185 *
186 * The string is allocated by the function and must be freed by the caller
187 * after use by calling g_free().
c2a25ebb
BV
188 *
189 * @retval SR_OK
190 *
191 * @since 0.4.0
192 */
a24da9a8 193SR_API int sr_analog_float_to_string(float value, int digits, char **result)
c2a25ebb
BV
194{
195 int cnt, i;
196
197 /* This produces at least one too many digits */
a24da9a8 198 *result = g_strdup_printf("%.*f", digits, value);
a9b2283f
BV
199 for (i = 0, cnt = 0; (*result)[i]; i++) {
200 if (isdigit((*result)[i++]))
c2a25ebb
BV
201 cnt++;
202 if (cnt == digits) {
a9b2283f 203 (*result)[i] = 0;
c2a25ebb
BV
204 break;
205 }
206 }
207
208 return SR_OK;
209}
210
a5892391
BV
211/*
212 * Convert the unit/MQ/MQ flags in the analog struct to a string.
213 *
214 * @param analog Struct containing the unit, MQ and MQ flags.
a24da9a8
ML
215 * @param result Pointer to store result.
216 *
217 * The string is allocated by the function and must be freed by the caller
218 * after use by calling g_free().
a5892391
BV
219 *
220 * @retval SR_OK
221 *
222 * @since 0.4.0
223 */
224SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog2 *analog,
a24da9a8 225 char **result)
a5892391 226{
a24da9a8
ML
227 int i;
228 GString *buf = g_string_new(NULL);
a5892391 229
a5892391
BV
230 for (i = 0; unit_strings[i].value; i++) {
231 if (analog->meaning->unit == unit_strings[i].value) {
a24da9a8 232 g_string_assign(buf, unit_strings[i].str);
a5892391
BV
233 break;
234 }
235 }
236
237 /* More than one MQ flag may apply. */
a24da9a8
ML
238 for (i = 0; mq_strings[i].value; i++)
239 if (analog->meaning->mqflags & mq_strings[i].value)
240 g_string_append(buf, mq_strings[i].str);
241
242 *result = buf->str;
243 g_string_free(buf, FALSE);
a5892391
BV
244
245 return SR_OK;
246}
247
90cefe0c
BV
248/*
249 * Set sr_rational r to the given value.
250 *
251 * @param p Numerator
252 * @param q Denominator
253 */
2617c81a 254SR_API void sr_rational_set(struct sr_rational *r, uint64_t p, uint64_t q)
90cefe0c
BV
255{
256 r->p = p;
257 r->q = q;
258}
259
e00b3f58 260/** @} */