From: Soeren Apel Date: Sat, 1 Jul 2017 22:55:03 +0000 (+0200) Subject: Introduce A2L methods X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=6ad2fbaad2722f0aa18c3600cdbdddf812784b2d;p=libsigrok.git Introduce A2L methods The memory allocation for sr_datafeed_logic is broken, the memory will never be freed. #994 is used to handle this issue. --- diff --git a/Makefile.am b/Makefile.am index d7280960..aec16d12 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index c17a2ee5..a0b161a6 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -1287,6 +1287,48 @@ vector Analog::mq_flags() const return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); } +shared_ptr 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 = + shared_ptr{new Logic{datafeed}, default_delete{}}; + + check(sr_a2l_threshold(_structure, threshold, + (uint8_t*)datafeed->data, datafeed->length)); + + return logic; +} + +shared_ptr 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 = + shared_ptr{new Logic{datafeed}, default_delete{}}; + + 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) { diff --git a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp index f20c4dc8..de54630f 100644 --- a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp +++ b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp @@ -758,6 +758,8 @@ private: const struct sr_datafeed_logic *_structure; friend class Packet; + friend class Analog; + friend struct std::default_delete; }; /** 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 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 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 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(); diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h index 48c12c63..7f312937 100644 --- a/include/libsigrok/proto.h +++ b/include/libsigrok/proto.h @@ -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 index 00000000..3d969308 --- /dev/null +++ b/src/conversion.c @@ -0,0 +1,107 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2017 Soeren Apel + * + * 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 . + */ + +/** + * @file + * + * Conversion helper functions. + */ + +#include +#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; +}