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