analog: use SI prefix only with units that accept SI prefixes

This commit is contained in:
Aurelien Jacobs 2016-09-13 17:57:07 +02:00 committed by Uwe Hermann
parent d2f6abf6de
commit 5728718b66
3 changed files with 47 additions and 1 deletions

View File

@ -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);

View File

@ -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.
*

View File

@ -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);