X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fanalog.c;h=4ec724cfde488a0ae4a37a5c2ab0f0b414264da8;hp=9fdece5df4a82a31333eb0577155b898e4bbea5d;hb=f37f11ec6b3b0bc15de0a186dd9ba424635c117e;hpb=f200d59ee21a42998752c4319d4ff48e129c95c5 diff --git a/src/analog.c b/src/analog.c index 9fdece5d..4ec724cf 100644 --- a/src/analog.c +++ b/src/analog.c @@ -88,6 +88,9 @@ static struct unit_mq_string unit_strings[] = { { SR_UNIT_MOMME, "momme" }, { SR_UNIT_TOLA, "tola" }, { SR_UNIT_PIECE, "pcs" }, + { SR_UNIT_JOULE, "J" }, + { SR_UNIT_COULOMB, "C" }, + { SR_UNIT_AMPERE_HOUR, "Ah" }, ALL_ZERO }; @@ -174,9 +177,11 @@ SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog, SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog, float *outbuf) { - float offset; - unsigned int b, i, count; + unsigned int b, count; gboolean bigendian; + uint8_t conv_buf[sizeof(double)]; + float *conv_f = (float*)conv_buf; + double *conv_d = (double*)conv_buf; if (!analog || !(analog->data) || !(analog->meaning) || !(analog->encoding) || !outbuf) @@ -275,20 +280,39 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog, /* The data is already in the right format. */ memcpy(outbuf, analog->data, count * sizeof(float)); } else { - for (i = 0; i < count; i += analog->encoding->unitsize) { + for (unsigned int i = 0; i < count; i++) { for (b = 0; b < analog->encoding->unitsize; b++) { if (analog->encoding->is_bigendian == bigendian) - ((uint8_t *)outbuf)[i + b] = + conv_buf[b] = ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b]; else - ((uint8_t *)outbuf)[i + (analog->encoding->unitsize - b)] = + conv_buf[analog->encoding->unitsize - b - 1] = ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b]; } - if (analog->encoding->scale.p != 1 - || analog->encoding->scale.q != 1) - outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q; - offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q); - outbuf[i] += offset; + + if (analog->encoding->unitsize == sizeof(float)) { + if (analog->encoding->scale.p != 1 + || analog->encoding->scale.q != 1) + *conv_f = (*conv_f * analog->encoding->scale.p) / analog->encoding->scale.q; + float offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q); + *conv_f += offset; + + outbuf[i] = *conv_f; + } + else if (analog->encoding->unitsize == sizeof(double)) { + if (analog->encoding->scale.p != 1 + || analog->encoding->scale.q != 1) + *conv_d = (*conv_d * analog->encoding->scale.p) / analog->encoding->scale.q; + double offset = ((double)analog->encoding->offset.p / (double)analog->encoding->offset.q); + *conv_d += offset; + + outbuf[i] = *conv_d; + } + else { + sr_err("Unsupported floating-point unit size '%d' for analog-to-float" + " conversion.", analog->encoding->unitsize); + return SR_ERR; + } } } @@ -308,7 +332,7 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog, SR_API const char *sr_analog_si_prefix(float *value, int *digits) { /** @cond PRIVATE */ -#define NEG_PREFIX_COUNT 5 /* number of prefixes below unity */ +#define NEG_PREFIX_COUNT 5 /* number of prefixes below unity */ #define POS_PREFIX_COUNT (int)(ARRAY_SIZE(prefixes) - NEG_PREFIX_COUNT - 1) /** @endcond */ static const char *prefixes[] = { "f", "p", "n", "µ", "m", "", "k", "M", "G", "T" }; @@ -366,12 +390,9 @@ SR_API gboolean sr_analog_si_prefix_friendly(enum sr_unit unit) for (i = 0; i < ARRAY_SIZE(prefix_friendly_units); i++) if (unit == prefix_friendly_units[i]) - break; + return TRUE; - if (unit != prefix_friendly_units[i]) - return FALSE; - - return TRUE; + return FALSE; } /** @@ -555,8 +576,8 @@ SR_API int sr_rational_mult(struct sr_rational *res, const struct sr_rational *a return SR_ERR_ARG; } - res->p = (int64_t)(p); - res->q = (uint64_t)(q); + res->p = (int64_t)p; + res->q = (uint64_t)q; return SR_OK; @@ -606,9 +627,8 @@ SR_API int sr_rational_mult(struct sr_rational *res, const struct sr_rational *a * @param[out] res Result. * * @retval SR_OK Success. - * @retval SR_ERR_ARG Division by zero. - * @retval SR_ERR_ARG Denominator of divisor too large. - * @retval SR_ERR_ARG Resulting value too large. + * @retval SR_ERR_ARG Division by zero, denominator of divisor too large, + * or resulting value too large. * * @since 0.5.0 */