]> sigrok.org Git - libsigrok.git/blobdiff - src/dmm/vc870.c
Remove some unneeded double-spaces.
[libsigrok.git] / src / dmm / vc870.c
index d14b84152c63240f9ab29ddf7f91b43db458889c..a2b2b72a69d52da109c3bf8d61082da1ff3b08b6 100644 (file)
 
 #define LOG_PREFIX "vc870"
 
-/* Factors for the respective measurement mode (0 means "invalid"). */
-static const float factors[][8] = {
-       {1e-4,  1e-3,  1e-2,  1e-1, 0,    0,    0,    0},    /* DCV */
-       {1e-3,  1e-2,  1e-1,  1,    0,    0,    0,    0},    /* ACV */
-       {1e-5,  0,     0,     0,    0,    0,    0,    0},    /* DCmV */
-       {1e-1,  0,     0,     0,    0,    0,    0,    0},    /* Temperature (C) */
-//     {1e-2,  0,     0,     0,    0,    0,    0,    0},    /* TODO: Temperature (F) */
+/* Exponents for the respective measurement mode. */
+static const int exponents[][8] = {
+       {  -4,  -3,  -2, -1,  0,  0,  0,  0 }, /* DCV */
+       {  -3,  -2,  -1,  0,  0,  0,  0,  0 }, /* ACV */
+       {  -5,   0,   0,  0,  0,  0,  0,  0 }, /* DCmV */
+       {  -1,   0,   0,  0,  0,  0,  0,  0 }, /* Temperature (C) */
+//     {  -2,   0,   0,  0,  0,  0,  0,  0 }, /* TODO: Temperature (F) */
        /*
-        * Note: The sequence 1e-1 -> 1e1 for the resistance
+        * Note: The sequence -1 -> 1 for the resistance
         * value is correct and verified in practice!
         * Don't trust the vendor docs on this.
         */
-       {1e-2,  1e-1,  1e1,   1e2,  1e3,  1e4,  0,    0},    /* Resistance */
-       {1e-2,  0,     0,     0,    0,    0,    0,    0},    /* Continuity */
-       {1e-12, 1e-11, 1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 0},    /* Capacitance */
-       {1e-4,  0,     0,     0,    0,    0,    0,    0},    /* Diode */
-       {1e-3,  1e-2,  1e-1,  1,    1e1,  1e2,  1e3,  1e4},  /* Frequency */
-       {1e-2,  0,     0,     0,    0,    0,    0,    0},    /* Loop current */
-       {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* DCµA */
-       {1e-8,  1e-7,  0,     0,    0,    0,    0,    0},    /* ACµA */
-       {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* DCmA */
-       {1e-6,  1e-5,  0,     0,    0,    0,    0,    0},    /* ACmA */
-       {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* DCA */
-       {1e-3,  0,     0,     0,    0,    0,    0,    0},    /* ACA */
-       {1e-1,  0,     0,     0,    0,    0,    0,    0},    /* Act+apparent power */
-       {1e-1,  0,     0,     0,    0,    0,    0,    0},    /* Power factor / freq */
-       {1e-1,  0,     0,     0,    0,    0,    0,    0},    /* V eff + A eff */
+       {  -2,  -1,   1,  2,  3,  4,  0,  0 }, /* Resistance */
+       {  -2,   0,   0,  0,  0,  0,  0,  0 }, /* Continuity */
+       { -12, -11, -10, -9, -8, -7, -6,  0 }, /* Capacitance */
+       {  -4,   0,   0,  0,  0,  0,  0,  0 }, /* Diode */
+       {  -3,  -2,  -1,  0,  1,  2,  3,  4 }, /* Frequency */
+       {  -2,   0,   0,  0,  0,  0,  0,  0 }, /* Loop current */
+       /*
+        * Note: Measurements showed that AC and DC differ
+        * in the exponents used, although docs say they should
+        * be the same.
+        */
+       {  -8,  -7,   0,  0,  0,  0,  0,  0 }, /* DCµA */
+       {  -7,  -6,   0,  0,  0,  0,  0,  0 }, /* ACµA */
+       {  -6,  -5,   0,  0,  0,  0,  0,  0 }, /* DCmA */
+       {  -5,  -4,   0,  0,  0,  0,  0,  0 }, /* ACmA */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* DCA */
+       /* TODO: Verify exponent for ACA */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* ACA */
+       {  -1,   0,   0,  0,  0,  0,  0,  0 }, /* Act+apparent power */
+       {  -3,   0,   0,  0,  0,  0,  0,  0 }, /* Power exponent / freq */
+       {  -1,   0,   0,  0,  0,  0,  0,  0 }, /* V eff + A eff */
 };
 
 static int parse_value(const uint8_t *buf, struct vc870_info *info,
@@ -94,11 +100,10 @@ static int parse_value(const uint8_t *buf, struct vc870_info *info,
        return SR_OK;
 }
 
-static int parse_range(uint8_t b, float *floatval,
+static int parse_range(uint8_t b, float *floatval, int *exponent,
                        const struct vc870_info *info)
 {
        int idx, mode;
-       float factor = 0;
 
        idx = b - '0';
 
@@ -144,30 +149,25 @@ static int parse_range(uint8_t b, float *floatval,
                mode = 16; /* Act+apparent power */
        else if (info->is_power_factor_freq)
                mode = 17; /* Power factor / freq */
-       else if (info->is_v_a_eff_value)
+       else if (info->is_v_a_rms_value)
                mode = 18; /* V eff + A eff */
        else {
                sr_dbg("Invalid mode, range byte was: 0x%02x.", b);
                return SR_ERR;
        }
 
-       factor = factors[mode][idx];
-
-       if (factor == 0) {
-               sr_dbg("Invalid factor for range byte: 0x%02x (mode=%d, idx=%d).", b, mode, idx);
-               return SR_ERR;
-       }
+       *exponent = exponents[mode][idx];
 
-       /* Apply respective factor (mode-dependent) on the value. */
-       *floatval *= factor;
-       sr_dbg("Applying factor %f, new value is %f.", factor, *floatval);
+       /* Apply respective exponent (mode-dependent) on the value. */
+       *floatval *= powf(10, *exponent);
+       sr_dbg("Applying exponent %d, new value is %f.", *exponent, *floatval);
 
        return SR_OK;
 }
 
 static void parse_flags(const uint8_t *buf, struct vc870_info *info)
 {
-       /* Bytes 0/1: Function / function select  */
+       /* Bytes 0/1: Function / function select */
        /* Note: Some of these mappings are fixed up later. */
        switch (buf[0]) {
        case 0x30: /* DCV / ACV */
@@ -219,7 +219,7 @@ static void parse_flags(const uint8_t *buf, struct vc870_info *info)
                        info->is_power_factor_freq = TRUE;
                else if (buf[1] == 0x32)
                        /* Voltage effective value + current effective value */
-                       info->is_v_a_eff_value = TRUE;
+                       info->is_v_a_rms_value = TRUE;
                break;
        default:
                sr_dbg("Invalid function bytes: %02x %02x.", buf[0], buf[1]);
@@ -274,100 +274,107 @@ static void parse_flags(const uint8_t *buf, struct vc870_info *info)
        /* Byte 22: Always '\n' (newline, 0x0a, 10) */
 
        info->is_auto = !info->is_manu;
-       info->is_rms = TRUE;
 }
 
-static void handle_flags(struct sr_datafeed_analog_old *analog,
+static void handle_flags(struct sr_datafeed_analog *analog,
                         float *floatval, const struct vc870_info *info)
 {
        /*
         * Note: is_micro etc. are not used directly to multiply/divide
-        * floatval, this is handled via parse_range() and factors[][].
+        * floatval, this is handled via parse_range() and exponents[][].
         */
 
        /* Measurement modes */
        if (info->is_voltage) {
-               analog->mq = SR_MQ_VOLTAGE;
-               analog->unit = SR_UNIT_VOLT;
+               analog->meaning->mq = SR_MQ_VOLTAGE;
+               analog->meaning->unit = SR_UNIT_VOLT;
        }
        if (info->is_current) {
-               analog->mq = SR_MQ_CURRENT;
-               analog->unit = SR_UNIT_AMPERE;
+               analog->meaning->mq = SR_MQ_CURRENT;
+               analog->meaning->unit = SR_UNIT_AMPERE;
        }
        if (info->is_resistance) {
-               analog->mq = SR_MQ_RESISTANCE;
-               analog->unit = SR_UNIT_OHM;
+               analog->meaning->mq = SR_MQ_RESISTANCE;
+               analog->meaning->unit = SR_UNIT_OHM;
        }
        if (info->is_frequency) {
-               analog->mq = SR_MQ_FREQUENCY;
-               analog->unit = SR_UNIT_HERTZ;
+               analog->meaning->mq = SR_MQ_FREQUENCY;
+               analog->meaning->unit = SR_UNIT_HERTZ;
        }
        if (info->is_capacitance) {
-               analog->mq = SR_MQ_CAPACITANCE;
-               analog->unit = SR_UNIT_FARAD;
+               analog->meaning->mq = SR_MQ_CAPACITANCE;
+               analog->meaning->unit = SR_UNIT_FARAD;
        }
        if (info->is_temperature) {
-               analog->mq = SR_MQ_TEMPERATURE;
-               analog->unit = SR_UNIT_CELSIUS;
+               analog->meaning->mq = SR_MQ_TEMPERATURE;
+               analog->meaning->unit = SR_UNIT_CELSIUS;
                /* TODO: Handle Fahrenheit in auxiliary display. */
-               // analog->unit = SR_UNIT_FAHRENHEIT;
+               // analog->meaning->unit = SR_UNIT_FAHRENHEIT;
        }
        if (info->is_continuity) {
-               analog->mq = SR_MQ_CONTINUITY;
-               analog->unit = SR_UNIT_BOOLEAN;
+               analog->meaning->mq = SR_MQ_CONTINUITY;
+               analog->meaning->unit = SR_UNIT_BOOLEAN;
                /* Vendor docs: "< 20 Ohm acoustic" */
                *floatval = (*floatval < 0.0 || *floatval > 20.0) ? 0.0 : 1.0;
        }
        if (info->is_diode) {
-               analog->mq = SR_MQ_VOLTAGE;
-               analog->unit = SR_UNIT_VOLT;
+               analog->meaning->mq = SR_MQ_VOLTAGE;
+               analog->meaning->unit = SR_UNIT_VOLT;
        }
        if (info->is_loop_current) {
                /* 4mA = 0%, 20mA = 100% */
-               analog->mq = SR_MQ_CURRENT;
-               analog->unit = SR_UNIT_PERCENTAGE;
+               analog->meaning->mq = SR_MQ_CURRENT;
+               analog->meaning->unit = SR_UNIT_PERCENTAGE;
        }
        if (info->is_power) {
-               analog->mq = SR_MQ_POWER;
-               analog->unit = SR_UNIT_WATT;
-       }
-       if (info->is_power_factor_freq) {
-               /* TODO: Handle power factor. */
-               // analog->mq = SR_MQ_POWER_FACTOR;
-               // analog->unit = SR_UNIT_UNITLESS;
-               analog->mq = SR_MQ_FREQUENCY;
-               analog->unit = SR_UNIT_HERTZ;
+               analog->meaning->mq = SR_MQ_POWER;
+               analog->meaning->unit = SR_UNIT_WATT;
        }
        if (info->is_power_apparent_power) {
-               analog->mq = SR_MQ_POWER;
-               analog->unit = SR_UNIT_WATT;
+               analog->meaning->mq = SR_MQ_POWER;
+               analog->meaning->unit = SR_UNIT_WATT;
                /* TODO: Handle apparent power. */
-               // analog->mq = SR_MQ_APPARENT_POWER;
-               // analog->unit = SR_UNIT_VOLT_AMPERE;
+               // analog->meaning->mq = SR_MQ_APPARENT_POWER;
+               // analog->meaning->unit = SR_UNIT_VOLT_AMPERE;
+       }
+       if (info->is_power_factor_freq) {
+               analog->meaning->mq = SR_MQ_POWER_FACTOR;
+               analog->meaning->unit = SR_UNIT_UNITLESS;
+               /* TODO: Handle frequency. */
+               // analog->meaning->mq = SR_MQ_FREQUENCY;
+               // analog->meaning->unit = SR_UNIT_HERTZ;
+       }
+       if (info->is_v_a_rms_value) {
+               analog->meaning->mqflags |= SR_MQFLAG_RMS;
+               analog->meaning->mq = SR_MQ_VOLTAGE;
+               analog->meaning->unit = SR_UNIT_VOLT;
+               /* TODO: Handle effective current value */
+               // analog->meaning->mq = SR_MQ_CURRENT;
+               // analog->meaning->unit = SR_UNIT_AMPERE;
        }
 
        /* Measurement related flags */
        if (info->is_ac)
-               analog->mqflags |= SR_MQFLAG_AC;
+               analog->meaning->mqflags |= SR_MQFLAG_AC;
        if (info->is_dc)
-               analog->mqflags |= SR_MQFLAG_DC;
+               analog->meaning->mqflags |= SR_MQFLAG_DC;
        if (info->is_auto)
-               analog->mqflags |= SR_MQFLAG_AUTORANGE;
+               analog->meaning->mqflags |= SR_MQFLAG_AUTORANGE;
        if (info->is_diode)
-               analog->mqflags |= SR_MQFLAG_DIODE;
+               analog->meaning->mqflags |= SR_MQFLAG_DIODE;
        if (info->is_hold)
                /*
                 * Note: HOLD only affects the number displayed on the LCD,
                 * but not the value sent via the protocol! It also does not
                 * affect the bargraph on the LCD.
                 */
-               analog->mqflags |= SR_MQFLAG_HOLD;
+               analog->meaning->mqflags |= SR_MQFLAG_HOLD;
        if (info->is_max)
-               analog->mqflags |= SR_MQFLAG_MAX;
+               analog->meaning->mqflags |= SR_MQFLAG_MAX;
        if (info->is_min)
-               analog->mqflags |= SR_MQFLAG_MIN;
+               analog->meaning->mqflags |= SR_MQFLAG_MIN;
        if (info->is_rel)
-               analog->mqflags |= SR_MQFLAG_RELATIVE;
+               analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
 
        /* Other flags */
        if (info->is_batt)
@@ -399,9 +406,9 @@ SR_PRIV gboolean sr_vc870_packet_valid(const uint8_t *buf)
 }
 
 SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
-                          struct sr_datafeed_analog_old *analog, void *info)
+                          struct sr_datafeed_analog *analog, void *info)
 {
-       int ret;
+       int ret, exponent = 0;
        struct vc870_info *info_local;
 
        info_local = (struct vc870_info *)info;
@@ -417,10 +424,13 @@ SR_PRIV int sr_vc870_parse(const uint8_t *buf, float *floatval,
                return ret;
        }
 
-       if ((ret = parse_range(buf[2], floatval, info_local)) != SR_OK)
+       if ((ret = parse_range(buf[2], floatval, &exponent, info_local)) != SR_OK)
                return ret;
 
        handle_flags(analog, floatval, info_local);
 
+       analog->encoding->digits = -exponent;
+       analog->spec->spec_digits = -exponent;
+
        return SR_OK;
 }