]> sigrok.org Git - libsigrok.git/commitdiff
Introduce A2L methods
authorSoeren Apel <redacted>
Sat, 1 Jul 2017 22:55:03 +0000 (00:55 +0200)
committerUwe Hermann <redacted>
Wed, 5 Jul 2017 22:16:24 +0000 (00:16 +0200)
The memory allocation for sr_datafeed_logic is broken, the memory
will never be freed. #994 is used to handle this issue.

Makefile.am
bindings/cxx/classes.cpp
bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp
include/libsigrok/proto.h
src/conversion.c [new file with mode: 0644]

index d7280960b799be7ba1cc745f254811c9b348b596..aec16d122fe0fa88af2b7df260942f6d83ca0787 100644 (file)
@@ -50,6 +50,7 @@ lib_LTLIBRARIES = libsigrok.la
 # Backend files
 libsigrok_la_SOURCES = \
        src/backend.c \
+       src/conversion.c \
        src/device.c \
        src/session.c \
        src/session_file.c \
index c17a2ee524efad9ebcd62e7006eeeedc41829bc5..a0b161a62a35aee683894cfe87b0b8c521d8d42a 100644 (file)
@@ -1287,6 +1287,48 @@ vector<const QuantityFlag *> Analog::mq_flags() const
        return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
 }
 
+shared_ptr<Logic> Analog::get_logic_via_threshold(float threshold,
+       uint8_t *data_ptr) const
+{
+       auto datafeed = g_new(struct sr_datafeed_logic, 1);
+       datafeed->length = num_samples();
+       datafeed->unitsize = 1;
+
+       if (data_ptr)
+               datafeed->data = data_ptr;
+       else
+               datafeed->data = g_malloc(datafeed->length);
+
+       shared_ptr<Logic> logic =
+               shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
+
+       check(sr_a2l_threshold(_structure, threshold,
+               (uint8_t*)datafeed->data, datafeed->length));
+
+       return logic;
+}
+
+shared_ptr<Logic> Analog::get_logic_via_schmitt_trigger(float lo_thr,
+       float hi_thr, uint8_t *state, uint8_t *data_ptr) const
+{
+       auto datafeed = g_new(struct sr_datafeed_logic, 1);
+       datafeed->length = num_samples();
+       datafeed->unitsize = 1;
+
+       if (data_ptr)
+               datafeed->data = data_ptr;
+       else
+               datafeed->data = g_malloc(datafeed->length);
+
+       shared_ptr<Logic> logic =
+               shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}};
+
+       check(sr_a2l_schmitt_trigger(_structure, lo_thr, hi_thr, state,
+               (uint8_t*)datafeed->data, datafeed->length));
+
+       return logic;
+}
+
 Rational::Rational(const struct sr_rational *structure) :
        _structure(structure)
 {
index f20c4dc8e86c1005d35933a74f191bb59722797f..de54630ffabe74c16f06f5b481aed1b1b07f2509 100644 (file)
@@ -758,6 +758,8 @@ private:
        const struct sr_datafeed_logic *_structure;
 
        friend class Packet;
+       friend class Analog;
+       friend struct std::default_delete<Logic>;
 };
 
 /** Payload of a datafeed packet with analog data */
@@ -803,6 +805,34 @@ public:
        const Unit *unit() const;
        /** Measurement flags associated with the samples in this packet. */
        vector<const QuantityFlag *> mq_flags() const;
+       /**
+        * Provides a Logic packet that contains a conversion of the analog
+        * data using a simple threshold.
+        *
+        * @param threshold Threshold to use.
+        * @param data_ptr Pointer to num_samples() bytes where the logic
+        *                 samples are stored. When nullptr, memory for
+        *                 logic->data_pointer() will be allocated and must
+        *                 be freed by the caller.
+        */
+       shared_ptr<Logic> get_logic_via_threshold(float threshold,
+               uint8_t *data_ptr=nullptr) const;
+       /**
+        * Provides a Logic packet that contains a conversion of the analog
+        * data using a Schmitt-Trigger.
+        *
+        * @param lo_thr Low threshold to use (anything below this is low).
+        * @param hi_thr High threshold to use (anything above this is high).
+        * @param state Points to a byte that contains the current state of the
+        *              converter. For best results, set to value of logic
+        *              sample n-1.
+        * @param data_ptr Pointer to num_samples() bytes where the logic
+        *                 samples are stored. When nullptr, memory for
+        *                 logic->data_pointer() will be allocated and must be
+        *                 freed by the caller.
+        */
+       shared_ptr<Logic> get_logic_via_schmitt_trigger(float lo_thr,
+               float hi_thr, uint8_t *state, uint8_t *data_ptr=nullptr) const;
 private:
        explicit Analog(const struct sr_datafeed_analog *structure);
        ~Analog();
index 48c12c63bcf52f421f42429952e4bab8fe1a5a84..7f3129379415576cbe0e35c46078619472c8ad87 100644 (file)
@@ -50,6 +50,14 @@ SR_API GSList *sr_buildinfo_libs_get(void);
 SR_API char *sr_buildinfo_host_get(void);
 SR_API char *sr_buildinfo_scpi_backends_get(void);
 
+/*--- conversion.c ----------------------------------------------------------*/
+
+SR_API int sr_a2l_threshold(const struct sr_datafeed_analog *analog,
+               float threshold, uint8_t *output, uint64_t count);
+SR_API int sr_a2l_schmitt_trigger(const struct sr_datafeed_analog *analog,
+               float lo_thr, float hi_thr, uint8_t *state, uint8_t *output,
+               uint64_t count);
+
 /*--- log.c -----------------------------------------------------------------*/
 
 typedef int (*sr_log_callback)(void *cb_data, int loglevel,
diff --git a/src/conversion.c b/src/conversion.c
new file mode 100644 (file)
index 0000000..3d96930
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
+ *
+ * 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 2 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/>.
+ */
+
+/**
+ * @file
+ *
+ * Conversion helper functions.
+ */
+
+#include <libsigrok/libsigrok.h>
+#include "libsigrok-internal.h"
+
+#define LOG_PREFIX "conv"
+
+/**
+ * Convert analog values to logic values by using a fixed threshold.
+ *
+ * @param[in] analog The analog input values.
+ * @param[in] threshold The threshold to use.
+ * @param[out] output The converted output values; either 0 or 1. Must provide
+ *                    space for count bytes.
+ * @param[in] count The number of samples to process.
+ *
+ * @return SR_OK on success or SR_ERR on failure.
+ */
+SR_API int sr_a2l_threshold(const struct sr_datafeed_analog *analog,
+               float threshold, uint8_t *output, uint64_t count)
+{
+       float *input;
+
+       if (!analog->encoding->is_float) {
+               input = g_try_malloc(sizeof(float) * count);
+               if (!input)
+                       return SR_ERR;
+
+               sr_analog_to_float(analog, input);
+       } else
+               input = analog->data;
+
+       for (uint64_t i = 0; i < count; i++)
+               output[i] = (input[i] >= threshold) ? 1 : 0;
+
+       if (!analog->encoding->is_float)
+               g_free(input);
+
+       return SR_OK;
+}
+
+/**
+ * Convert analog values to logic values by using a Schmitt-trigger algorithm.
+ *
+ * @param analog The analog input values.
+ * @param lo_thr The low threshold - result becomes 0 below it.
+ * @param lo_thr The high threshold - result becomes 1 above it.
+ * @param state The internal converter state. Must contain the state of logic
+ *        sample n-1, will contain the state of logic sample n+count upon exit.
+ * @param output The converted output values; either 0 or 1. Must provide
+ *        space for count bytes.
+ * @param count The number of samples to process.
+ *
+ * @return SR_OK on success or SR_ERR on failure.
+ */
+SR_API int sr_a2l_schmitt_trigger(const struct sr_datafeed_analog *analog,
+               float lo_thr, float hi_thr, uint8_t *state, uint8_t *output,
+               uint64_t count)
+{
+       float *input;
+
+       if (!analog->encoding->is_float) {
+               input = g_try_malloc(sizeof(float) * count);
+               if (!input)
+                       return SR_ERR;
+
+               sr_analog_to_float(analog, input);
+       } else
+               input = analog->data;
+
+       for (uint64_t i = 0; i < count; i++) {
+               if (input[i] < lo_thr)
+                       *state = 0;
+               else if (input[i] > hi_thr)
+                       *state = 1;
+
+               output[i] = *state;
+       }
+
+       if (!analog->encoding->is_float)
+               g_free(input);
+
+       return SR_OK;
+}