]> sigrok.org Git - libsigrok.git/commitdiff
analog: use SI prefix only with units that accept SI prefixes
authorAurelien Jacobs <redacted>
Tue, 13 Sep 2016 15:57:07 +0000 (17:57 +0200)
committerUwe Hermann <redacted>
Mon, 17 Oct 2016 00:24:53 +0000 (02:24 +0200)
include/libsigrok/proto.h
src/analog.c
src/output/analog.c

index c799919e267a710dfed69de94db5a303dec8edd9..05e788646d640482f5a656b0f4a3c0c619d4cf9d 100644 (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);
index a8ca3bce650424165227546b534924081dfbef3c..9bc88b310efd8aeb42333575562ce557d79c1fae 100644 (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.
  *
index 5e2b7cd527b6ce0c60c7b1f3bafbb44e60d862ed..9ff77da4b765cf7ae7d8ea4af9b42dd66b3fdf71 100644 (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);