]> sigrok.org Git - libsigrok.git/commitdiff
analog: improve output readability by using SI prefix
authorAurelien Jacobs <redacted>
Sun, 28 Aug 2016 20:11:20 +0000 (22:11 +0200)
committerUwe Hermann <redacted>
Sun, 28 Aug 2016 22:25:19 +0000 (00:25 +0200)
include/libsigrok/proto.h
src/analog.c
src/output/analog.c
tests/analog.c

index 1a3fba8c048fd9f2a6ae6989a3bad3babcf04d51..c799919e267a710dfed69de94db5a303dec8edd9 100644 (file)
@@ -30,6 +30,7 @@
 
 SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog,
                float *buf);
 
 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 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);
 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 7343b481e610d409d51bb45dd5f2be1a31138c77..634e9a674ba8ada7817bd489d68bedbe7853ed59 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdint.h>
 #include <string.h>
 #include <ctype.h>
+#include <math.h>
 #include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
@@ -292,6 +293,37 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog,
        return SR_OK;
 }
 
        return SR_OK;
 }
 
+/**
+ * Scale a float value to the appropriate SI prefix.
+ *
+ * @param[in,out] value The float value to convert to appropriate SI prefix.
+ * @param[in,out] digits The number of significant decimal digits in value.
+ *
+ * @return The SI prefix to which value was scaled, as a printable string.
+ *
+ * @since 0.5.0
+ */
+SR_API const char *sr_analog_si_prefix(float *value, int *digits)
+{
+#define NEG_PREFIX_COUNT 5  /* number of prefixes below unity */
+#define POS_PREFIX_COUNT (int)(ARRAY_SIZE(prefixes) - NEG_PREFIX_COUNT - 1)
+       static const char *prefixes[] = { "f","p","n","µ","m","","k","M","G","T" };
+
+       if (value == NULL || digits == NULL || isnan(*value))
+               return prefixes[NEG_PREFIX_COUNT];
+
+       float logval = log10f(fabsf(*value));
+       int prefix = (logval / 3) - (logval < 1);
+
+       if (prefix < -NEG_PREFIX_COUNT) prefix = -NEG_PREFIX_COUNT;
+       if (3 * prefix < -*digits)      prefix = (-*digits + 2 * (*digits < 0)) / 3;
+       if (prefix > POS_PREFIX_COUNT)  prefix = POS_PREFIX_COUNT;
+
+       *value *= powf(10, -3 * prefix);
+       *digits += 3 * prefix;
+       return prefixes[prefix + NEG_PREFIX_COUNT];
+}
+
 /**
  * Convert the unit/MQ/MQ flags in the analog struct to a string.
  *
 /**
  * Convert the unit/MQ/MQ flags in the analog struct to a string.
  *
index 5f77a59c2c3393c1160ea610a116e893f50cf512..5e2b7cd527b6ce0c60c7b1f3bafbb44e60d862ed 100644 (file)
@@ -116,13 +116,15 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
                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++) {
                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);
                                ch = l->data;
                                g_string_append_printf(*out, "%s: ", ch->name);
                                ch = l->data;
                                g_string_append_printf(*out, "%s: ", ch->name);
-                               number = g_strdup_printf("%.*f", MAX(digits, 0),
-                                               fdata[i * num_channels + c]);
+                               number = g_strdup_printf("%.*f", MAX(digits, 0), value);
                                g_string_append(*out, number);
                                g_free(number);
                                g_string_append(*out, " ");
                                g_string_append(*out, number);
                                g_free(number);
                                g_string_append(*out, " ");
+                               g_string_append(*out, prefix);
                                g_string_append(*out, suffix);
                                g_string_append(*out, "\n");
                        }
                                g_string_append(*out, suffix);
                                g_string_append(*out, "\n");
                        }
index 3e8fd494c2c80616b012e99aed9c4a81c40aba18..6dd8299969e4e7425ef2e1d1cb3fcfde1a339078 100644 (file)
@@ -124,6 +124,71 @@ START_TEST(test_analog_to_float_null)
 }
 END_TEST
 
 }
 END_TEST
 
+START_TEST(test_analog_si_prefix)
+{
+       struct {
+               float input_value;
+               int input_digits;
+               float output_value;
+               int output_digits;
+               const char *output_si_prefix;
+       } v[] = {
+               {   12.0     ,  0,  12.0  ,    0, ""  },
+               {   12.0     ,  1,  12.0  ,    1, ""  },
+               {   12.0     , -1,   0.012,    2, "k" },
+               { 1024.0     ,  0,   1.024,    3, "k" },
+               { 1024.0     , -1,   1.024,    2, "k" },
+               { 1024.0     , -3,   1.024,    0, "k" },
+               {   12.0e5   ,  0,   1.2,      6, "M" },
+               {    0.123456,  0,   0.123456, 0, ""  },
+               {    0.123456,  1,   0.123456, 1, ""  },
+               {    0.123456,  2,   0.123456, 2, ""  },
+               {    0.123456,  3, 123.456,    0, "m" },
+               {    0.123456,  4, 123.456,    1, "m" },
+               {    0.123456,  5, 123.456,    2, "m" },
+               {    0.123456,  6, 123.456,    3, "m" },
+               {    0.123456,  7, 123.456,    4, "m" },
+               {    0.0123  ,  4,  12.3,      1, "m" },
+               {    0.00123 ,  5,   1.23,     2, "m" },
+               {    0.000123,  4,   0.123,    1, "m" },
+               {    0.000123,  5,   0.123,    2, "m" },
+               {    0.000123,  6, 123.0,      0, "µ" },
+               {    0.000123,  7, 123.0,      1, "µ" },
+       };
+
+       for (unsigned int i = 0; i < ARRAY_SIZE(v); i++) {
+               float value = v[i].input_value;
+               int digits = v[i].input_digits;
+               const char *si_prefix = sr_analog_si_prefix(&value, &digits);
+
+               fail_unless(fabs(value - v[i].output_value) <= 0.00001,
+                       "sr_analog_si_prefix() unexpected output value %f (i=%d).",
+                       value , i);
+               fail_unless(digits == v[i].output_digits,
+                       "sr_analog_si_prefix() unexpected output digits %d (i=%d).",
+                       digits, i);
+               fail_unless(!strcmp(si_prefix, v[i].output_si_prefix),
+                       "sr_analog_si_prefix() unexpected output prefix \"%s\" (i=%d).",
+                       si_prefix, i);
+       }
+}
+END_TEST
+
+START_TEST(test_analog_si_prefix_null)
+{
+       float value = 1.23;
+       int digits = 1;
+       const char *si_prefix;
+
+       si_prefix = sr_analog_si_prefix(NULL, &digits);
+       fail_unless(!strcmp(si_prefix, ""));
+       si_prefix = sr_analog_si_prefix(&value, NULL);
+       fail_unless(!strcmp(si_prefix, ""));
+       si_prefix = sr_analog_si_prefix(NULL, NULL);
+       fail_unless(!strcmp(si_prefix, ""));
+}
+END_TEST
+
 START_TEST(test_analog_unit_to_string)
 {
        int ret;
 START_TEST(test_analog_unit_to_string)
 {
        int ret;
@@ -320,6 +385,8 @@ Suite *suite_analog(void)
        tc = tcase_create("analog_to_float");
        tcase_add_test(tc, test_analog_to_float);
        tcase_add_test(tc, test_analog_to_float_null);
        tc = tcase_create("analog_to_float");
        tcase_add_test(tc, test_analog_to_float);
        tcase_add_test(tc, test_analog_to_float_null);
+       tcase_add_test(tc, test_analog_si_prefix);
+       tcase_add_test(tc, test_analog_si_prefix_null);
        tcase_add_test(tc, test_analog_unit_to_string);
        tcase_add_test(tc, test_analog_unit_to_string_null);
        tcase_add_test(tc, test_set_rational);
        tcase_add_test(tc, test_analog_unit_to_string);
        tcase_add_test(tc, test_analog_unit_to_string_null);
        tcase_add_test(tc, test_set_rational);