From: Stefan BrĂ¼ns Date: Wed, 25 Nov 2015 00:41:41 +0000 (+0100) Subject: analog: add conversion from various integer formats to float X-Git-Tag: libsigrok-0.4.0~77 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4d376e082cbc17da30cf20d18bec8f65b14b7ae0;p=libsigrok.git analog: add conversion from various integer formats to float --- diff --git a/src/analog.c b/src/analog.c index f26f7a5c..892cc6ec 100644 --- a/src/analog.c +++ b/src/analog.c @@ -187,9 +187,80 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog, bigendian = FALSE; #endif if (!analog->encoding->is_float) { - /* TODO */ - sr_err("Only floating-point encoding supported so far."); - return SR_ERR; + float offset = analog->encoding->offset.p / (float)analog->encoding->offset.q; + float scale = analog->encoding->scale.p / (float)analog->encoding->scale.q; + gboolean is_signed = analog->encoding->is_signed; + gboolean is_bigendian = analog->encoding->is_bigendian; + int8_t *data8 = (int8_t *)(analog->data); + int16_t *data16 = (int16_t *)(analog->data); + int32_t *data32 = (int32_t *)(analog->data); + + switch (analog->encoding->unitsize) { + case 1: + if (is_signed) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * data8[i]; + outbuf[i] += offset; + } + } else { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * R8(data8 + i); + outbuf[i] += offset; + } + } + break; + case 2: + if (is_signed && is_bigendian) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RB16S(&data16[i]); + outbuf[i] += offset; + } + } else if (is_bigendian) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RB16(&data16[i]); + outbuf[i] += offset; + } + } else if (is_signed) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RL16S(&data16[i]); + outbuf[i] += offset; + } + } else { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RL16(&data16[i]); + outbuf[i] += offset; + } + } + break; + case 4: + if (is_signed && is_bigendian) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RB32S(&data32[i]); + outbuf[i] += offset; + } + } else if (is_bigendian) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RB32(&data32[i]); + outbuf[i] += offset; + } + } else if (is_signed) { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RL32S(&data32[i]); + outbuf[i] += offset; + } + } else { + for (unsigned int i = 0; i < count; i++) { + outbuf[i] = scale * RL32(&data32[i]); + outbuf[i] += offset; + } + } + break; + default: + sr_err("Unsupported unit size '%d' for analog-to-float conversion.", + analog->encoding->unitsize); + return SR_ERR; + } + return SR_OK; } if (analog->encoding->unitsize == sizeof(float) diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 41754b28..3413f920 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -76,6 +76,15 @@ struct zip_stat; #define RL16(x) (((unsigned)((const uint8_t*)(x))[1] << 8) | \ (unsigned)((const uint8_t*)(x))[0]) +/** + * Read a 16 bits big endian signed integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding signed integer + */ +#define RB16S(x) ((int16_t) \ + (((unsigned)((const uint8_t*)(x))[0] << 8) | \ + (unsigned)((const uint8_t*)(x))[1])) + /** * Read a 16 bits little endian signed integer out of memory. * @param x a pointer to the input memory @@ -105,6 +114,17 @@ struct zip_stat; ((unsigned)((const uint8_t*)(x))[1] << 8) | \ (unsigned)((const uint8_t*)(x))[0]) +/** + * Read a 32 bits big endian signed integer out of memory. + * @param x a pointer to the input memory + * @return the corresponding signed integer + */ +#define RB32S(x) ((int32_t) \ + (((unsigned)((const uint8_t*)(x))[0] << 24) | \ + ((unsigned)((const uint8_t*)(x))[1] << 16) | \ + ((unsigned)((const uint8_t*)(x))[2] << 8) | \ + (unsigned)((const uint8_t*)(x))[3])) + /** * Read a 32 bits little endian signed integer out of memory. * @param x a pointer to the input memory