X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fanalog.c;h=4eda290f1197f162f03e3e54b64330f1f1f3a5d0;hp=e2966875c3bbfb8c55212b19cd9ec2a110163347;hb=5e5fde6e2c3aabfc61bcd35a53ab0d401b2176ba;hpb=8dc423b033678508ec08d7c473d883d4b3a58178 diff --git a/src/analog.c b/src/analog.c index e2966875..4eda290f 100644 --- a/src/analog.c +++ b/src/analog.c @@ -63,8 +63,8 @@ static struct unit_mq_string unit_strings[] = { { SR_UNIT_BOOLEAN, "" }, { SR_UNIT_SECOND, "s" }, { SR_UNIT_SIEMENS, "S" }, - { SR_UNIT_DECIBEL_MW, "dBu" }, - { SR_UNIT_DECIBEL_VOLT, "dBv" }, + { SR_UNIT_DECIBEL_MW, "dBm" }, + { SR_UNIT_DECIBEL_VOLT, "dBV" }, { SR_UNIT_UNITLESS, "" }, { SR_UNIT_DECIBEL_SPL, "dB" }, { SR_UNIT_CONCENTRATION, "ppm" }, @@ -119,6 +119,7 @@ static struct unit_mq_string mq_strings[] = { ALL_ZERO }; +/** @private */ SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog, struct sr_analog_encoding *encoding, struct sr_analog_meaning *meaning, @@ -173,8 +174,7 @@ 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; if (!analog || !(analog->data) || !(analog->meaning) @@ -274,7 +274,7 @@ 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 += analog->encoding->unitsize) { for (b = 0; b < analog->encoding->unitsize; b++) { if (analog->encoding->is_bigendian == bigendian) ((uint8_t *)outbuf)[i + b] = @@ -286,7 +286,7 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog, 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); + float offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q); outbuf[i] += offset; } } @@ -306,8 +306,10 @@ 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) { -#define NEG_PREFIX_COUNT 5 /* number of prefixes below unity */ +/** @cond PRIVATE */ +#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" }; if (!value || !digits || isnan(*value)) @@ -329,6 +331,45 @@ SR_API const char *sr_analog_si_prefix(float *value, int *digits) return prefixes[prefix + NEG_PREFIX_COUNT]; } +/** + * Check if a unit "accepts" an SI prefix. + * + * E.g. SR_UNIT_VOLT is SI prefix friendly while SR_UNIT_DECIBEL_MW or + * SR_UNIT_PERCENTAGE are not. + * + * @param[in] unit The unit to check for SI prefix "friendliness". + * + * @return TRUE if the unit "accept" an SI prefix. + * + * @since 0.5.0 + */ +SR_API gboolean sr_analog_si_prefix_friendly(enum sr_unit unit) +{ + static const enum sr_unit prefix_friendly_units[] = { + SR_UNIT_VOLT, + SR_UNIT_AMPERE, + SR_UNIT_OHM, + SR_UNIT_FARAD, + SR_UNIT_KELVIN, + SR_UNIT_HERTZ, + SR_UNIT_SECOND, + SR_UNIT_SIEMENS, + SR_UNIT_VOLT_AMPERE, + SR_UNIT_WATT, + SR_UNIT_WATT_HOUR, + SR_UNIT_METER_SECOND, + SR_UNIT_HENRY, + SR_UNIT_GRAM + }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(prefix_friendly_units); i++) + if (unit == prefix_friendly_units[i]) + return TRUE; + + return FALSE; +} + /** * Convert the unit/MQ/MQ flags in the analog struct to a string. * @@ -510,8 +551,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;