From: Aurelien Jacobs Date: Tue, 13 Sep 2016 15:57:07 +0000 (+0200) Subject: analog: use SI prefix only with units that accept SI prefixes X-Git-Tag: libsigrok-0.5.0~200 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=5728718b66d12b82415f400d040dd9933775abb0;p=libsigrok.git analog: use SI prefix only with units that accept SI prefixes --- diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h index c799919e..05e78864 100644 --- a/include/libsigrok/proto.h +++ b/include/libsigrok/proto.h @@ -31,6 +31,7 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog, float *buf); SR_API const char *sr_analog_si_prefix(float *value, int *digits); +SR_API gboolean sr_analog_si_prefix_friendly(enum sr_unit unit); SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog *analog, char **result); SR_API void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q); diff --git a/src/analog.c b/src/analog.c index a8ca3bce..9bc88b31 100644 --- a/src/analog.c +++ b/src/analog.c @@ -329,6 +329,48 @@ 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]) + break; + + if (unit != prefix_friendly_units[i]) + return FALSE; + + return TRUE; +} + /** * Convert the unit/MQ/MQ flags in the analog struct to a string. * diff --git a/src/output/analog.c b/src/output/analog.c index 5e2b7cd5..9ff77da4 100644 --- a/src/output/analog.c +++ b/src/output/analog.c @@ -113,11 +113,14 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p /* TODO we don't know how to print by number of bits yet. */ digits = 6; } + gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit); sr_analog_unit_to_string(analog, &suffix); for (i = 0; i < analog->num_samples; i++) { for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) { float value = fdata[i * num_channels + c]; - const char *prefix = sr_analog_si_prefix(&value, &digits); + const char *prefix = ""; + if (si_friendly) + prefix = sr_analog_si_prefix(&value, &digits); ch = l->data; g_string_append_printf(*out, "%s: ", ch->name); number = g_strdup_printf("%.*f", MAX(digits, 0), value);