libsigrok  unreleased development snapshot
sigrok hardware access and backend library
conversion.c
Go to the documentation of this file.
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 /** @cond PRIVATE */
30 #define LOG_PREFIX "conv"
31 /** @endcond */
32 
33 /**
34  * Convert analog values to logic values by using a fixed threshold.
35  *
36  * @param[in] analog The analog input values.
37  * @param[in] threshold The threshold to use.
38  * @param[out] output The converted output values; either 0 or 1. Must provide
39  * space for count bytes.
40  * @param[in] count The number of samples to process.
41  *
42  * @return SR_OK on success or SR_ERR on failure.
43  */
44 SR_API int sr_a2l_threshold(const struct sr_datafeed_analog *analog,
45  float threshold, uint8_t *output, uint64_t count)
46 {
47  float *input;
48 
49  if (!analog->encoding->is_float) {
50  input = g_try_malloc(sizeof(float) * count);
51  if (!input)
52  return SR_ERR;
53 
54  sr_analog_to_float(analog, input);
55  } else
56  input = analog->data;
57 
58  for (uint64_t i = 0; i < count; i++)
59  output[i] = (input[i] >= threshold) ? 1 : 0;
60 
61  if (!analog->encoding->is_float)
62  g_free(input);
63 
64  return SR_OK;
65 }
66 
67 /**
68  * Convert analog values to logic values by using a Schmitt-trigger algorithm.
69  *
70  * @param analog The analog input values.
71  * @param lo_thr The low threshold - result becomes 0 below it.
72  * @param hi_thr The high threshold - result becomes 1 above it.
73  * @param state The internal converter state. Must contain the state of logic
74  * sample n-1, will contain the state of logic sample n+count upon exit.
75  * @param output The converted output values; either 0 or 1. Must provide
76  * space for count bytes.
77  * @param count The number of samples to process.
78  *
79  * @return SR_OK on success or SR_ERR on failure.
80  */
82  float lo_thr, float hi_thr, uint8_t *state, uint8_t *output,
83  uint64_t count)
84 {
85  float *input;
86 
87  if (!analog->encoding->is_float) {
88  input = g_try_malloc(sizeof(float) * count);
89  if (!input)
90  return SR_ERR;
91 
92  sr_analog_to_float(analog, input);
93  } else
94  input = analog->data;
95 
96  for (uint64_t i = 0; i < count; i++) {
97  if (input[i] < lo_thr)
98  *state = 0;
99  else if (input[i] > hi_thr)
100  *state = 1;
101 
102  output[i] = *state;
103  }
104 
105  if (!analog->encoding->is_float)
106  g_free(input);
107 
108  return SR_OK;
109 }
struct sr_analog_encoding * encoding
Definition: libsigrok.h:521
int sr_analog_to_float(const struct sr_datafeed_analog *analog, float *outbuf)
Convert an analog datafeed payload to an array of floats.
Definition: analog.c:177
No error.
Definition: libsigrok.h:67
#define SR_API
Definition: libsigrok.h:128
int sr_a2l_threshold(const struct sr_datafeed_analog *analog, float threshold, uint8_t *output, uint64_t count)
Convert analog values to logic values by using a fixed threshold.
Definition: conversion.c:44
gboolean is_float
Definition: libsigrok.h:529
Analog datafeed payload for type SR_DF_ANALOG.
Definition: libsigrok.h:518
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)
Convert analog values to logic values by using a Schmitt-trigger algorithm.
Definition: conversion.c:81
Generic/unspecified error.
Definition: libsigrok.h:68
The public libsigrok header file to be used by frontends.