]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/dmm/metex14.c
metex14: Support DMMs with whitespace differences.
[libsigrok.git] / hardware / common / dmm / metex14.c
index 9364dcb0d0f87dddc0a218f9b2afe3a0306d984d..2ef5ecff7459d4f637964c0b794006fd123a13fb 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * This file is part of the sigrok project.
+ * This file is part of the libsigrok project.
  *
- * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
+ * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
-/* Message logging helpers with driver-specific prefix string. */
-#define DRIVER_LOG_DOMAIN "metex14: "
-#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
-#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
-#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
-#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
-#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
-#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
+/* Message logging helpers with subsystem-specific prefix string. */
+#define LOG_PREFIX "metex14: "
+#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
+#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
+#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
+#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
+#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
+#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
 
 static int parse_value(const uint8_t *buf, float *result)
 {
@@ -61,10 +61,10 @@ static int parse_value(const uint8_t *buf, float *result)
 
        /* Bytes 5-7: Over limit (various forms) */
        is_ol = 0;
-       is_ol += !strncmp((char *)&buf[5], ".OL", 3);
-       is_ol += !strncmp((char *)&buf[5], "O.L", 3);
-       is_ol += !strncmp((char *)&buf[5], "OL.", 3);
-       is_ol += !strncmp((char *)&buf[5], " OL", 3);
+       is_ol += (!strncmp((char *)&buf[5], ".OL", 3)) ? 1 : 0;
+       is_ol += (!strncmp((char *)&buf[5], "O.L", 3)) ? 1 : 0;
+       is_ol += (!strncmp((char *)&buf[5], "OL.", 3)) ? 1 : 0;
+       is_ol += (!strncmp((char *)&buf[5], " OL", 3)) ? 1 : 0;
        if (is_ol != 0) {
                sr_spew("Over limit.");
                *result = INFINITY;
@@ -117,6 +117,10 @@ static int parse_value(const uint8_t *buf, float *result)
 
 static void parse_flags(const char *buf, struct metex14_info *info)
 {
+       int i, cnt;
+       char unit[4 + 1];
+       const char *u;
+
        /* Bytes 0-1: Measurement mode */
        /* Note: Protocol doesn't distinguish "resistance" from "beep" mode. */
        info->is_ac          = !strncmp(buf, "AC", 2);
@@ -126,6 +130,13 @@ static void parse_flags(const char *buf, struct metex14_info *info)
        info->is_temperature = !strncmp(buf, "TE", 2);
        info->is_diode       = !strncmp(buf, "DI", 2);
        info->is_frequency   = !strncmp(buf, "FR", 2);
+       info->is_gain        = !strncmp(buf, "DB", 2);
+       info->is_hfe         = !strncmp(buf, "HF", 2);
+
+       /*
+        * Note: "DB" shows the logarithmic ratio of input voltage to a
+        * pre-stored (user-changeable) value in the DMM.
+        */
 
        if (info->is_dc || info->is_ac)
                info->is_volt = TRUE;
@@ -135,28 +146,40 @@ static void parse_flags(const char *buf, struct metex14_info *info)
        /* Bytes 3-8: See parse_value(). */
 
        /* Bytes 9-12: Unit */
-       if (!strcmp(buf + 9, "   A"))
+       memset(&unit, 0, 4 + 1);
+       for (i = 0, cnt = 0; i < 4; i++) {
+               if (buf[9 + i] != ' ')
+                       unit[cnt++] = buf[9 + i];
+       }
+       u = (const char *)&unit;
+       if (!strcmp(u, "A"))
                info->is_ampere = TRUE;
-       else if (!strcmp(buf + 9, "  mA"))
+       else if (!strcmp(u, "mA"))
                info->is_milli = info->is_ampere = TRUE;
-       else if (!strcmp(buf + 9, "   V"))
+       else if (!strcmp(u, "uA"))
+               info->is_micro = info->is_ampere = TRUE;
+       else if (!strcmp(u, "V"))
                info->is_volt = TRUE;
-       else if (!strcmp(buf + 9, "  mV"))
+       else if (!strcmp(u, "mV"))
                info->is_milli = info->is_volt = TRUE;
-       else if (!strcmp(buf + 9, " Ohm"))
+       else if (!strcmp(u, "Ohm"))
                info->is_ohm = TRUE;
-       else if (!strcmp(buf + 9, "KOhm"))
+       else if (!strcmp(u, "KOhm"))
                info->is_kilo = info->is_ohm = TRUE;
-       else if (!strcmp(buf + 9, "MOhm"))
+       else if (!strcmp(u, "MOhm"))
                info->is_mega = info->is_ohm = TRUE;
-       else if (!strcmp(buf + 9, "  nF"))
+       else if (!strcmp(u, "nF"))
                info->is_nano = info->is_farad = TRUE;
-       else if (!strcmp(buf + 9, "  uF"))
+       else if (!strcmp(u, "uF"))
                info->is_micro = info->is_farad = TRUE;
-       else if (!strcmp(buf + 9, " KHz"))
+       else if (!strcmp(u, "KHz"))
                info->is_kilo = info->is_hertz = TRUE;
-       else if (!strcmp(buf + 9, "   C"))
+       else if (!strcmp(u, "C"))
                info->is_celsius = TRUE;
+       else if (!strcmp(u, "DB"))
+               info->is_decibel = TRUE;
+       else if (!strcmp(u, ""))
+               info->is_unitless = TRUE;
 
        /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
 }
@@ -205,12 +228,22 @@ static void handle_flags(struct sr_datafeed_analog *analog, float *floatval,
                analog->mq = SR_MQ_VOLTAGE;
                analog->unit = SR_UNIT_VOLT;
        }
+       if (info->is_gain) {
+               analog->mq = SR_MQ_GAIN;
+               analog->unit = SR_UNIT_DECIBEL_VOLT;
+       }
+       if (info->is_hfe) {
+               analog->mq = SR_MQ_GAIN;
+               analog->unit = SR_UNIT_UNITLESS;
+       }
 
        /* Measurement related flags */
        if (info->is_ac)
                analog->mqflags |= SR_MQFLAG_AC;
        if (info->is_dc)
                analog->mqflags |= SR_MQFLAG_DC;
+       if (info->is_diode)
+               analog->mqflags |= SR_MQFLAG_DIODE;
 }
 
 static gboolean flags_valid(const struct metex14_info *info)
@@ -252,6 +285,15 @@ static gboolean flags_valid(const struct metex14_info *info)
        return TRUE;
 }
 
+SR_PRIV int sr_metex14_packet_request(struct sr_serial_dev_inst *serial)
+{
+       const uint8_t wbuf = 'D';
+
+       sr_spew("Requesting DMM packet.");
+
+       return (serial_write(serial, &wbuf, 1) == 1) ? SR_OK : SR_ERR;
+}
+
 SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
 {
        struct metex14_info info;