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.
This commit is contained in:
Soeren Apel 2017-07-02 00:55:03 +02:00 committed by Uwe Hermann
parent 895cbcdd3c
commit 6ad2fbaad2
5 changed files with 188 additions and 0 deletions

View 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 \

View 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)
{

View 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();

View 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,

107
src/conversion.c Normal file
View File

@ -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;
}