Add analog helper sr_analog_float_to_string().

This commit is contained in:
Bert Vermeulen 2014-11-18 01:15:41 +01:00
parent 1954dfa963
commit c2a25ebb8f
2 changed files with 38 additions and 0 deletions

View File

@ -30,6 +30,8 @@
SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
float *buf);
SR_API int sr_analog_float_to_string(float value, int digits, char *outbuf,
int bufsize);
/*--- backend.c -------------------------------------------------------------*/

View File

@ -17,7 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
@ -98,3 +101,36 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
return SR_OK;
}
/*
* Convert a floating point value to a string, limited to the given
* number of decimal digits.
*
* @param value The value to convert.
* @param digits Number of digits after the decimal point to print.
* @param outbuf Buffer in which the resulting string will be placed.
* @param bufsize Size of the buffer in bytes.
*
* @retval SR_OK
*
* @since 0.4.0
*/
SR_API int sr_analog_float_to_string(float value, int digits, char *outbuf,
int bufsize)
{
int cnt, i;
/* This produces at least one too many digits */
snprintf(outbuf, bufsize, "%.*f", digits, value);
for (i = 0, cnt = 0; outbuf[i] && i < bufsize; i++) {
if (isdigit(outbuf[i++]))
cnt++;
if (cnt == digits) {
outbuf[i] = 0;
break;
}
}
return SR_OK;
}