]> sigrok.org Git - libsigrok.git/blobdiff - src/dmm/metex14.c
metex14: properly set encoding digits
[libsigrok.git] / src / dmm / metex14.c
index e2d14ec34348992326c1d852983df56ff1b859f1..2677484a743608e87451fc0db7d2da266f726d50 100644 (file)
  * It does _not_ work for all Metex DMMs, some use a quite different protocol.
  */
 
+#include <config.h>
 #include <string.h>
 #include <strings.h>
 #include <ctype.h>
 #include <math.h>
 #include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "metex14"
 
 /** Parse value from buf, byte 2-8. */
 static int parse_value(const uint8_t *buf, struct metex14_info *info,
-                       float *result)
+                       float *result, int *exponent)
 {
-       int i, is_ol, cnt;
+       int i, is_ol, cnt, dot_pos;
        char valstr[7 + 1];
 
        /* Strip all spaces from bytes 2-8. */
@@ -56,14 +57,14 @@ static int parse_value(const uint8_t *buf, struct metex14_info *info,
 
        /* Bytes 5-7: Over limit (various forms) */
        is_ol = 0;
-       is_ol += (!strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "OL")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0;
-       is_ol += (!strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "OL")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0;
+       is_ol += (!g_ascii_strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0;
        if (is_ol != 0) {
                sr_spew("Over limit.");
                *result = INFINITY;
@@ -88,6 +89,12 @@ static int parse_value(const uint8_t *buf, struct metex14_info *info,
        /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */
        sscanf((const char *)&valstr, "%f", result);
 
+       dot_pos = strcspn(valstr, ".");
+       if (dot_pos < cnt)
+               *exponent = -(cnt - dot_pos - 1);
+       else
+               *exponent = 0;
+
        sr_spew("The display value is %f.", *result);
 
        return SR_OK;
@@ -114,35 +121,35 @@ static void parse_flags(const char *buf, struct metex14_info *info)
 
        /* Bytes 9-12: Unit */
        u = (const char *)&unit;
-       if (!strcasecmp(u, "A"))
+       if (!g_ascii_strcasecmp(u, "A"))
                info->is_ampere = TRUE;
-       else if (!strcasecmp(u, "mA"))
+       else if (!g_ascii_strcasecmp(u, "mA"))
                info->is_milli = info->is_ampere = TRUE;
-       else if (!strcasecmp(u, "uA"))
+       else if (!g_ascii_strcasecmp(u, "uA"))
                info->is_micro = info->is_ampere = TRUE;
-       else if (!strcasecmp(u, "V"))
+       else if (!g_ascii_strcasecmp(u, "V"))
                info->is_volt = TRUE;
-       else if (!strcasecmp(u, "mV"))
+       else if (!g_ascii_strcasecmp(u, "mV"))
                info->is_milli = info->is_volt = TRUE;
-       else if (!strcasecmp(u, "Ohm"))
+       else if (!g_ascii_strcasecmp(u, "Ohm"))
                info->is_ohm = TRUE;
-       else if (!strcasecmp(u, "KOhm"))
+       else if (!g_ascii_strcasecmp(u, "KOhm"))
                info->is_kilo = info->is_ohm = TRUE;
-       else if (!strcasecmp(u, "MOhm"))
+       else if (!g_ascii_strcasecmp(u, "MOhm"))
                info->is_mega = info->is_ohm = TRUE;
-       else if (!strcasecmp(u, "pF"))
+       else if (!g_ascii_strcasecmp(u, "pF"))
                info->is_pico = info->is_farad = TRUE;
-       else if (!strcasecmp(u, "nF"))
+       else if (!g_ascii_strcasecmp(u, "nF"))
                info->is_nano = info->is_farad = TRUE;
-       else if (!strcasecmp(u, "uF"))
+       else if (!g_ascii_strcasecmp(u, "uF"))
                info->is_micro = info->is_farad = TRUE;
-       else if (!strcasecmp(u, "KHz"))
+       else if (!g_ascii_strcasecmp(u, "KHz"))
                info->is_kilo = info->is_hertz = TRUE;
-       else if (!strcasecmp(u, "C"))
+       else if (!g_ascii_strcasecmp(u, "C"))
                info->is_celsius = TRUE;
-       else if (!strcasecmp(u, "DB"))
+       else if (!g_ascii_strcasecmp(u, "DB"))
                info->is_decibel = TRUE;
-       else if (!strcasecmp(u, ""))
+       else if (!g_ascii_strcasecmp(u, ""))
                info->is_unitless = TRUE;
 
        /* Bytes 0-1: Measurement mode, except AC/DC */
@@ -170,71 +177,74 @@ static void parse_flags(const char *buf, struct metex14_info *info)
 }
 
 static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
-                        const struct metex14_info *info)
+                        int *exponent, const struct metex14_info *info)
 {
+       int factor = 0;
        /* Factors */
        if (info->is_pico)
-               *floatval /= 1000000000000ULL;
+               factor -= 12;
        if (info->is_nano)
-               *floatval /= 1000000000;
+               factor -= 9;
        if (info->is_micro)
-               *floatval /= 1000000;
+               factor -= 6;
        if (info->is_milli)
-               *floatval /= 1000;
+               factor -= 3;
        if (info->is_kilo)
-               *floatval *= 1000;
+               factor += 3;
        if (info->is_mega)
-               *floatval *= 1000000;
+               factor += 6;
+       *floatval *= powf(10, factor);
+       *exponent += factor;
 
        /* Measurement modes */
        if (info->is_volt) {
-               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_ampere) {
-               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_ohm) {
-               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_hertz) {
-               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_farad) {
-               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_celsius) {
-               analog->mq = SR_MQ_TEMPERATURE;
-               analog->unit = SR_UNIT_CELSIUS;
+               analog->meaning->mq = SR_MQ_TEMPERATURE;
+               analog->meaning->unit = SR_UNIT_CELSIUS;
        }
        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_gain) {
-               analog->mq = SR_MQ_GAIN;
-               analog->unit = SR_UNIT_DECIBEL_VOLT;
+               analog->meaning->mq = SR_MQ_GAIN;
+               analog->meaning->unit = SR_UNIT_DECIBEL_VOLT;
        }
        if (info->is_hfe) {
-               analog->mq = SR_MQ_GAIN;
-               analog->unit = SR_UNIT_UNITLESS;
+               analog->meaning->mq = SR_MQ_GAIN;
+               analog->meaning->unit = SR_UNIT_UNITLESS;
        }
        if (info->is_logic) {
-               analog->mq = SR_MQ_GAIN;
-               analog->unit = SR_UNIT_UNITLESS;
+               analog->meaning->mq = SR_MQ_GAIN;
+               analog->meaning->unit = SR_UNIT_UNITLESS;
        }
 
        /* 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_diode)
-               analog->mqflags |= SR_MQFLAG_DIODE;
+               analog->meaning->mqflags |= SR_MQFLAG_DIODE;
 }
 
 static gboolean flags_valid(const struct metex14_info *info)
@@ -322,7 +332,7 @@ SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
                             struct sr_datafeed_analog *analog, void *info)
 {
-       int ret;
+       int ret, exponent = 0;
        struct metex14_info *info_local;
 
        info_local = (struct metex14_info *)info;
@@ -332,13 +342,16 @@ SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
 
        memset(info_local, 0x00, sizeof(struct metex14_info));
 
-       if ((ret = parse_value(buf, info_local, floatval)) != SR_OK) {
+       if ((ret = parse_value(buf, info_local, floatval, &exponent)) != SR_OK) {
                sr_dbg("Error parsing value: %d.", ret);
                return ret;
        }
 
        parse_flags((const char *)buf, info_local);
-       handle_flags(analog, floatval, info_local);
+       handle_flags(analog, floatval, &exponent, info_local);
+
+       analog->encoding->digits  = -exponent;
+       analog->spec->spec_digits = -exponent;
 
        return SR_OK;
 }