]> sigrok.org Git - libsigrok.git/commitdiff
Add sr_analog_to_float().
authorBert Vermeulen <redacted>
Wed, 12 Nov 2014 16:05:13 +0000 (17:05 +0100)
committerUwe Hermann <redacted>
Thu, 13 Nov 2014 21:51:39 +0000 (22:51 +0100)
Makefile.am
include/libsigrok/proto.h
src/analog.c [new file with mode: 0644]

index 7b67a9a5e4a408394427cd65aef57789e25dd384..2487b2ef8f48a17c234d7910ca4d3331959a6152 100644 (file)
@@ -35,6 +35,7 @@ libsigrok_la_SOURCES = \
        src/hwdriver.c \
        src/trigger.c \
        src/soft-trigger.c \
+       src/analog.c \
        src/strutil.c \
        src/log.c \
        src/version.c \
index f4fbb0494c86a94559fdc7000c66a6eb7bc95cc1..dfce6aa2d246392bab088506ebd7bd8442bb92bf 100644 (file)
  * Header file containing API function prototypes.
  */
 
+/*--- analog.c --------------------------------------------------------------*/
+
+SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
+               float *buf);
+
 /*--- backend.c -------------------------------------------------------------*/
 
 SR_API int sr_init(struct sr_context **ctx);
diff --git a/src/analog.c b/src/analog.c
new file mode 100644 (file)
index 0000000..0f7a307
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
+
+#define LOG_PREFIX "analog"
+
+SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
+               float *buf)
+{
+       float offset;
+       unsigned int b, i;
+       gboolean bigendian;
+
+#ifdef WORDS_BIGENDIAN
+       bigendian = TRUE;
+#else
+       bigendian = FALSE;
+#endif
+       if (!analog->encoding->is_float) {
+               /* TODO */
+               sr_err("Only floating-point analog data supported so far.");
+               return SR_ERR;
+       }
+
+       if (analog->encoding->unitsize == sizeof(float)
+                       && analog->encoding->is_bigendian == bigendian
+                       && (analog->encoding->scale.p == analog->encoding->scale.q)
+                       && analog->encoding->scale.p / (float)analog->encoding->scale.q == 0) {
+               /* The data is already in the right format. */
+               memcpy(buf, analog->data, analog->num_samples * sizeof(float));
+       } else {
+               for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) {
+                       for (b = 0; b < analog->encoding->unitsize; b++) {
+                               if (analog->encoding->is_bigendian == bigendian)
+                                       buf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
+                               else
+                                       buf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
+                       }
+                       if (analog->encoding->scale.p != analog->encoding->scale.q)
+                               buf[i] = (buf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
+                       offset = ((float)analog->encoding->scale.p / (float)analog->encoding->scale.q);
+                       buf[i] += offset;
+               }
+       }
+
+       return SR_OK;
+}