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