2 * This file is part of the libsigrok project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
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 2 of the License, or
9 * (at your option) any later version.
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.
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/>.
23 * Conversion helper functions.
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "conv"
32 * Convert analog values to logic values by using a fixed threshold.
34 * @param[in] analog The analog input values.
35 * @param[in] threshold The threshold to use.
36 * @param[out] output The converted output values; either 0 or 1. Must provide
37 * space for count bytes.
38 * @param[in] count The number of samples to process.
40 * @return SR_OK on success or SR_ERR on failure.
42 SR_API int sr_a2l_threshold(const struct sr_datafeed_analog *analog,
43 float threshold, uint8_t *output, uint64_t count)
47 if (!analog->encoding->is_float) {
48 input = g_try_malloc(sizeof(float) * count);
52 sr_analog_to_float(analog, input);
56 for (uint64_t i = 0; i < count; i++)
57 output[i] = (input[i] >= threshold) ? 1 : 0;
59 if (!analog->encoding->is_float)
66 * Convert analog values to logic values by using a Schmitt-trigger algorithm.
68 * @param analog The analog input values.
69 * @param lo_thr The low threshold - result becomes 0 below it.
70 * @param lo_thr The high threshold - result becomes 1 above it.
71 * @param state The internal converter state. Must contain the state of logic
72 * sample n-1, will contain the state of logic sample n+count upon exit.
73 * @param output The converted output values; either 0 or 1. Must provide
74 * space for count bytes.
75 * @param count The number of samples to process.
77 * @return SR_OK on success or SR_ERR on failure.
79 SR_API int sr_a2l_schmitt_trigger(const struct sr_datafeed_analog *analog,
80 float lo_thr, float hi_thr, uint8_t *state, uint8_t *output,
85 if (!analog->encoding->is_float) {
86 input = g_try_malloc(sizeof(float) * count);
90 sr_analog_to_float(analog, input);
94 for (uint64_t i = 0; i < count; i++) {
95 if (input[i] < lo_thr)
97 else if (input[i] > hi_thr)
103 if (!analog->encoding->is_float)