]> sigrok.org Git - libsigrok.git/blame - src/conversion.c
scpi-pps: Add a missing "break" in config_get().
[libsigrok.git] / src / conversion.c
CommitLineData
6ad2fbaa
SA
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20/**
21 * @file
22 *
23 * Conversion helper functions.
24 */
25
26#include <libsigrok/libsigrok.h>
27#include "libsigrok-internal.h"
28
29#define LOG_PREFIX "conv"
30
31/**
32 * Convert analog values to logic values by using a fixed threshold.
33 *
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.
39 *
40 * @return SR_OK on success or SR_ERR on failure.
41 */
42SR_API int sr_a2l_threshold(const struct sr_datafeed_analog *analog,
43 float threshold, uint8_t *output, uint64_t count)
44{
45 float *input;
46
47 if (!analog->encoding->is_float) {
48 input = g_try_malloc(sizeof(float) * count);
49 if (!input)
50 return SR_ERR;
51
52 sr_analog_to_float(analog, input);
53 } else
54 input = analog->data;
55
56 for (uint64_t i = 0; i < count; i++)
57 output[i] = (input[i] >= threshold) ? 1 : 0;
58
59 if (!analog->encoding->is_float)
60 g_free(input);
61
62 return SR_OK;
63}
64
65/**
66 * Convert analog values to logic values by using a Schmitt-trigger algorithm.
67 *
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.
76 *
77 * @return SR_OK on success or SR_ERR on failure.
78 */
79SR_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,
81 uint64_t count)
82{
83 float *input;
84
85 if (!analog->encoding->is_float) {
86 input = g_try_malloc(sizeof(float) * count);
87 if (!input)
88 return SR_ERR;
89
90 sr_analog_to_float(analog, input);
91 } else
92 input = analog->data;
93
94 for (uint64_t i = 0; i < count; i++) {
95 if (input[i] < lo_thr)
96 *state = 0;
97 else if (input[i] > hi_thr)
98 *state = 1;
99
100 output[i] = *state;
101 }
102
103 if (!analog->encoding->is_float)
104 g_free(input);
105
106 return SR_OK;
107}