libsigrok  0.4.0
sigrok hardware access and backend library
analog.c
Go to the documentation of this file.
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 <config.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27 
28 /** @cond PRIVATE */
29 #define LOG_PREFIX "analog"
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  */
45 
46 struct unit_mq_string {
47  uint64_t value;
48  const char *str;
49 };
50 
51 /* Please use the same order as in enum sr_unit (libsigrok.h). */
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" },
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, "%" },
62  { SR_UNIT_BOOLEAN, "" },
63  { SR_UNIT_SECOND, "s" },
64  { SR_UNIT_SIEMENS, "S" },
65  { SR_UNIT_DECIBEL_MW, "dBu" },
66  { SR_UNIT_DECIBEL_VOLT, "dBv" },
67  { SR_UNIT_UNITLESS, "" },
68  { SR_UNIT_DECIBEL_SPL, "dB" },
69  { SR_UNIT_CONCENTRATION, "ppm" },
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" },
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" },
90  ALL_ZERO
91 };
92 
93 /* Please use the same order as in enum sr_mqflag (libsigrok.h). */
94 static struct unit_mq_string mq_strings[] = {
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" },
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" },
114  { SR_MQFLAG_AVG, " AVG" },
115  { SR_MQFLAG_REFERENCE, " REF" },
116  { SR_MQFLAG_UNSTABLE, " UNSTABLE" },
117  ALL_ZERO
118 };
119 
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 
154 /**
155  * Convert an analog datafeed payload to an array of floats.
156  *
157  * @param[in] analog The analog payload to convert. Must not be NULL.
158  * analog->data, analog->meaning, and analog->encoding
159  * must not be NULL.
160  * @param[out] outbuf Memory where to store the result. Must not be NULL.
161  *
162  * Sufficient memory for outbuf must have been pre-allocated by the caller,
163  * who is also responsible for freeing it when no longer needed.
164  *
165  * @retval SR_OK Success.
166  * @retval SR_ERR Unsupported encoding.
167  * @retval SR_ERR_ARG Invalid argument.
168  *
169  * @since 0.4.0
170  */
172  float *outbuf)
173 {
174  float offset;
175  unsigned int b, i, count;
176  gboolean bigendian;
177 
178  if (!analog || !(analog->data) || !(analog->meaning)
179  || !(analog->encoding) || !outbuf)
180  return SR_ERR_ARG;
181 
182  count = analog->num_samples * g_slist_length(analog->meaning->channels);
183 
184 #ifdef WORDS_BIGENDIAN
185  bigendian = TRUE;
186 #else
187  bigendian = FALSE;
188 #endif
189  if (!analog->encoding->is_float) {
190  float offset = analog->encoding->offset.p / (float)analog->encoding->offset.q;
191  float scale = analog->encoding->scale.p / (float)analog->encoding->scale.q;
192  gboolean is_signed = analog->encoding->is_signed;
193  gboolean is_bigendian = analog->encoding->is_bigendian;
194  int8_t *data8 = (int8_t *)(analog->data);
195  int16_t *data16 = (int16_t *)(analog->data);
196  int32_t *data32 = (int32_t *)(analog->data);
197 
198  switch (analog->encoding->unitsize) {
199  case 1:
200  if (is_signed) {
201  for (unsigned int i = 0; i < count; i++) {
202  outbuf[i] = scale * data8[i];
203  outbuf[i] += offset;
204  }
205  } else {
206  for (unsigned int i = 0; i < count; i++) {
207  outbuf[i] = scale * R8(data8 + i);
208  outbuf[i] += offset;
209  }
210  }
211  break;
212  case 2:
213  if (is_signed && is_bigendian) {
214  for (unsigned int i = 0; i < count; i++) {
215  outbuf[i] = scale * RB16S(&data16[i]);
216  outbuf[i] += offset;
217  }
218  } else if (is_bigendian) {
219  for (unsigned int i = 0; i < count; i++) {
220  outbuf[i] = scale * RB16(&data16[i]);
221  outbuf[i] += offset;
222  }
223  } else if (is_signed) {
224  for (unsigned int i = 0; i < count; i++) {
225  outbuf[i] = scale * RL16S(&data16[i]);
226  outbuf[i] += offset;
227  }
228  } else {
229  for (unsigned int i = 0; i < count; i++) {
230  outbuf[i] = scale * RL16(&data16[i]);
231  outbuf[i] += offset;
232  }
233  }
234  break;
235  case 4:
236  if (is_signed && is_bigendian) {
237  for (unsigned int i = 0; i < count; i++) {
238  outbuf[i] = scale * RB32S(&data32[i]);
239  outbuf[i] += offset;
240  }
241  } else if (is_bigendian) {
242  for (unsigned int i = 0; i < count; i++) {
243  outbuf[i] = scale * RB32(&data32[i]);
244  outbuf[i] += offset;
245  }
246  } else if (is_signed) {
247  for (unsigned int i = 0; i < count; i++) {
248  outbuf[i] = scale * RL32S(&data32[i]);
249  outbuf[i] += offset;
250  }
251  } else {
252  for (unsigned int i = 0; i < count; i++) {
253  outbuf[i] = scale * RL32(&data32[i]);
254  outbuf[i] += offset;
255  }
256  }
257  break;
258  default:
259  sr_err("Unsupported unit size '%d' for analog-to-float conversion.",
260  analog->encoding->unitsize);
261  return SR_ERR;
262  }
263  return SR_OK;
264  }
265 
266  if (analog->encoding->unitsize == sizeof(float)
267  && analog->encoding->is_bigendian == bigendian
268  && analog->encoding->scale.p == 1
269  && analog->encoding->scale.q == 1
270  && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) {
271  /* The data is already in the right format. */
272  memcpy(outbuf, analog->data, count * sizeof(float));
273  } else {
274  for (i = 0; i < count; i += analog->encoding->unitsize) {
275  for (b = 0; b < analog->encoding->unitsize; b++) {
276  if (analog->encoding->is_bigendian == bigendian)
277  ((uint8_t *)outbuf)[i + b] =
278  ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
279  else
280  ((uint8_t *)outbuf)[i + (analog->encoding->unitsize - b)] =
281  ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
282  }
283  if (analog->encoding->scale.p != 1
284  || analog->encoding->scale.q != 1)
285  outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
286  offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q);
287  outbuf[i] += offset;
288  }
289  }
290 
291  return SR_OK;
292 }
293 
294 /**
295  * Convert the unit/MQ/MQ flags in the analog struct to a string.
296  *
297  * @param[in] analog Struct containing the unit, MQ and MQ flags.
298  * Must not be NULL. analog->meaning must not be NULL.
299  * @param[out] result Pointer to store result. Must not be NULL.
300  *
301  * The string is allocated by the function and must be freed by the caller
302  * after use by calling g_free().
303  *
304  * @retval SR_OK Success.
305  * @retval SR_ERR_ARG Invalid argument.
306  *
307  * @since 0.4.0
308  */
310  char **result)
311 {
312  int i;
313  GString *buf;
314 
315  if (!analog || !(analog->meaning) || !result)
316  return SR_ERR_ARG;
317 
318  buf = g_string_new(NULL);
319 
320  for (i = 0; unit_strings[i].value; i++) {
321  if (analog->meaning->unit == unit_strings[i].value) {
322  g_string_assign(buf, unit_strings[i].str);
323  break;
324  }
325  }
326 
327  /* More than one MQ flag may apply. */
328  for (i = 0; mq_strings[i].value; i++)
329  if (analog->meaning->mqflags & mq_strings[i].value)
330  g_string_append(buf, mq_strings[i].str);
331 
332  *result = buf->str;
333  g_string_free(buf, FALSE);
334 
335  return SR_OK;
336 }
337 
338 /**
339  * Set sr_rational r to the given value.
340  *
341  * @param[out] r Rational number struct to set. Must not be NULL.
342  * @param[in] p Numerator.
343  * @param[in] q Denominator.
344  *
345  * @since 0.4.0
346  */
347 SR_API void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q)
348 {
349  if (!r)
350  return;
351 
352  r->p = p;
353  r->q = q;
354 }
355 
356 /** @} */
Sound pressure level is Z-weighted (i.e.
Definition: libsigrok.h:362
An absolute measurement of power, in decibels, referenced to 1 milliwatt (dBu).
Definition: libsigrok.h:270
Device is in autoranging mode.
Definition: libsigrok.h:351
SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog, struct sr_analog_encoding *encoding, struct sr_analog_meaning *meaning, struct sr_analog_spec *spec, int digits)
Definition: analog.c:120
struct sr_analog_encoding * encoding
Definition: libsigrok.h:519
Pieces (number of items).
Definition: libsigrok.h:326
Value is voltage drop across a diode, or NAN.
Definition: libsigrok.h:343
The public libsigrok header file to be used by frontends.
Mass in tael (variants: Hong Kong, Singapore/Malaysia, Taiwan)
Definition: libsigrok.h:320
Mass in pennyweight [dwt].
Definition: libsigrok.h:316
Time in seconds.
Definition: libsigrok.h:263
Consumption [Wh].
Definition: libsigrok.h:294
Wind speed in meters per second.
Definition: libsigrok.h:296
Voltage measurement is alternating current (AC).
Definition: libsigrok.h:337
uint32_t num_samples
Definition: libsigrok.h:518
enum sr_mqflag mqflags
Definition: libsigrok.h:538
Farad (capacity).
Definition: libsigrok.h:249
Mass in ounce [oz].
Definition: libsigrok.h:310
gboolean is_bigendian
Definition: libsigrok.h:528
Unstable value (hasn't settled yet).
Definition: libsigrok.h:385
void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q)
Set sr_rational r to the given value.
Definition: analog.c:347
Mass in gram [g].
Definition: libsigrok.h:306
Generic/unspecified error.
Definition: libsigrok.h:68
Device is in "hold" mode (repeating the last measurement).
Definition: libsigrok.h:345
Time is duration (as opposed to epoch, ...).
Definition: libsigrok.h:379
Sound pressure level is A-weighted in the frequency domain, according to IEC 61672:2003.
Definition: libsigrok.h:356
GSList * channels
Definition: libsigrok.h:539
Degrees Fahrenheit (temperature).
Definition: libsigrok.h:255
This is a true RMS measurement.
Definition: libsigrok.h:341
Percent value.
Definition: libsigrok.h:259
Ampere (current).
Definition: libsigrok.h:245
Unit of conductance, the inverse of resistance.
Definition: libsigrok.h:265
Sound pressure level measurement is F-weighted (125ms) in the time domain.
Definition: libsigrok.h:371
Hertz (frequency, 1/s, [Hz]).
Definition: libsigrok.h:257
#define SR_API
Definition: libsigrok.h:121
No error.
Definition: libsigrok.h:67
Degrees Celsius (temperature).
Definition: libsigrok.h:253
uint8_t spec_digits
Definition: libsigrok.h:543
struct sr_rational offset
Definition: libsigrok.h:532
Kelvin (temperature).
Definition: libsigrok.h:251
Mass in pound [lb].
Definition: libsigrok.h:314
Mass in momme.
Definition: libsigrok.h:322
gboolean is_digits_decimal
Definition: libsigrok.h:530
Analog datafeed payload for type SR_DF_ANALOG.
Definition: libsigrok.h:516
Sound pressure level is time-averaged (LAT), also known as Equivalent Continuous A-weighted Sound Lev...
Definition: libsigrok.h:374
int sr_analog_to_float(const struct sr_datafeed_analog *analog, float *outbuf)
Convert an analog datafeed payload to an array of floats.
Definition: analog.c:171
Volt.
Definition: libsigrok.h:243
Sound pressure level is C-weighted in the frequency domain, according to IEC 61672:2003.
Definition: libsigrok.h:359
Voltage measurement is direct current (DC).
Definition: libsigrok.h:339
Pressure in hectopascal.
Definition: libsigrok.h:298
struct sr_analog_meaning * meaning
Definition: libsigrok.h:520
Device is in "min" mode, only updating upon a new min value.
Definition: libsigrok.h:349
Sound pressure level represented as a percentage of measurements that were over a preset alarm level...
Definition: libsigrok.h:377
uint64_t q
Denominator of the rational number.
Definition: libsigrok.h:470
Mass in tola.
Definition: libsigrok.h:324
gboolean is_float
Definition: libsigrok.h:527
int64_t p
Numerator of the rational number.
Definition: libsigrok.h:468
Voltage in decibel, referenced to 1 volt (dBV).
Definition: libsigrok.h:272
Ohm (resistance).
Definition: libsigrok.h:247
struct sr_analog_spec * spec
Definition: libsigrok.h:521
Real power [W].
Definition: libsigrok.h:292
Relative humidity assuming air temperature of 293 Kelvin (rF).
Definition: libsigrok.h:300
struct sr_rational scale
Definition: libsigrok.h:531
Sound pressure level, in decibels, relative to 20 micropascals.
Definition: libsigrok.h:280
Device is in "max" mode, only updating upon a new max value.
Definition: libsigrok.h:347
Device is in relative mode.
Definition: libsigrok.h:353
enum sr_unit unit
Definition: libsigrok.h:537
Boolean value.
Definition: libsigrok.h:261
Measurements that intrinsically do not have units attached, such as ratios, gains, etc.
Definition: libsigrok.h:278
Mass in carat [ct].
Definition: libsigrok.h:308
Reference value shown.
Definition: libsigrok.h:383
Sound pressure level is not weighted in the frequency domain, albeit without standards-defined low an...
Definition: libsigrok.h:365
Device is in "avg" mode, averaging upon each new value.
Definition: libsigrok.h:381
Revolutions per minute.
Definition: libsigrok.h:288
Mass in troy ounce [oz t].
Definition: libsigrok.h:312
Apparent power [VA].
Definition: libsigrok.h:290
Mass in grain [gr].
Definition: libsigrok.h:318
Normalized (0 to 1) concentration of a substance or compound with 0 representing a concentration of 0...
Definition: libsigrok.h:286
Plane angle in 1/360th of a full circle.
Definition: libsigrok.h:302
Sound pressure level measurement is S-weighted (1s) in the time domain.
Definition: libsigrok.h:368
Henry (inductance).
Definition: libsigrok.h:304
#define SR_PRIV
Definition: libsigrok.h:128
Function argument error.
Definition: libsigrok.h:70
int sr_analog_unit_to_string(const struct sr_datafeed_analog *analog, char **result)
Convert the unit/MQ/MQ flags in the analog struct to a string.
Definition: analog.c:309
gboolean is_signed
Definition: libsigrok.h:526