]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/dmm/metex14.c
fs9922: Fix beep mode.
[libsigrok.git] / hardware / common / dmm / metex14.c
index 9c6fd62e0f6b1f672268dd6887ff3472b9ed3108..c5127252b34bd2d2b5ae34093e5010cc5edaeb1d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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>
  *
@@ -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;
@@ -75,6 +75,9 @@ static int parse_value(const uint8_t *buf, float *result)
        factor = 1000;
        for (i = 0; i < 5; i++) {
                digit = buf[4 + i];
+               /* Convert spaces to '0', so that we can parse them. */
+               if (digit == ' ')
+                       digit = '0';
                if (digit == '.') {
                        decimal_point = i;
                } else if (isdigit(digit)) {
@@ -123,6 +126,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;
@@ -132,28 +142,34 @@ 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"))
+       if (!strncmp(buf + 9, "   A", 4))
                info->is_ampere = TRUE;
-       else if (!strcmp(buf + 9, "  mA"))
+       else if (!strncmp(buf + 9, "  mA", 4))
                info->is_milli = info->is_ampere = TRUE;
-       else if (!strcmp(buf + 9, "   V"))
+       else if (!strncmp(buf + 9, "  uA", 4))
+               info->is_micro = info->is_ampere = TRUE;
+       else if (!strncmp(buf + 9, "   V", 4))
                info->is_volt = TRUE;
-       else if (!strcmp(buf + 9, "  mV"))
+       else if (!strncmp(buf + 9, "  mV", 4))
                info->is_milli = info->is_volt = TRUE;
-       else if (!strcmp(buf + 9, " Ohm"))
+       else if (!strncmp(buf + 9, " Ohm", 4))
                info->is_ohm = TRUE;
-       else if (!strcmp(buf + 9, "KOhm"))
+       else if (!strncmp(buf + 9, "KOhm", 4))
                info->is_kilo = info->is_ohm = TRUE;
-       else if (!strcmp(buf + 9, "MOhm"))
+       else if (!strncmp(buf + 9, "MOhm", 4))
                info->is_mega = info->is_ohm = TRUE;
-       else if (!strcmp(buf + 9, "  nF"))
+       else if (!strncmp(buf + 9, "  nF", 4))
                info->is_nano = info->is_farad = TRUE;
-       else if (!strcmp(buf + 9, "  uF"))
+       else if (!strncmp(buf + 9, "  uF", 4))
                info->is_micro = info->is_farad = TRUE;
-       else if (!strcmp(buf + 9, " KHz"))
+       else if (!strncmp(buf + 9, " KHz", 4))
                info->is_kilo = info->is_hertz = TRUE;
-       else if (!strcmp(buf + 9, "   C"))
+       else if (!strncmp(buf + 9, "   C", 4))
                info->is_celsius = TRUE;
+       else if (!strncmp(buf + 9, "  DB", 4))
+               info->is_decibel = TRUE;
+       else if (!strncmp(buf + 9, "    ", 4))
+               info->is_unitless = TRUE;
 
        /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */
 }
@@ -202,6 +218,14 @@ 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)
@@ -249,6 +273,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;
@@ -268,29 +301,37 @@ SR_PRIV gboolean sr_metex14_packet_valid(const uint8_t *buf)
 /**
  * Parse a protocol packet.
  *
- * @param buf Buffer containing the protocol packet.
+ * @param buf Buffer containing the protocol packet. Must not be NULL.
  * @param floatval Pointer to a float variable. That variable will be modified
- *                 in-place depending on the protocol packet.
+ *                 in-place depending on the protocol packet. Must not be NULL.
  * @param analog Pointer to a struct sr_datafeed_analog. The struct will be
  *               filled with data according to the protocol packet.
+ *               Must not be NULL.
+ * @param info Pointer to a struct metex14_info. The struct will be filled
+ *             with data according to the protocol packet. Must not be NULL.
  *
  * @return SR_OK upon success, SR_ERR upon failure. Upon errors, the
  *         'analog' variable contents are undefined and should not be used.
  */
 SR_PRIV int sr_metex14_parse(const uint8_t *buf, float *floatval,
-                            struct sr_datafeed_analog *analog,
-                            struct metex14_info *info)
+                            struct sr_datafeed_analog *analog, void *info)
 {
        int ret;
+       struct metex14_info *info_local;
+
+       info_local = (struct metex14_info *)info;
+
+       /* Don't print byte 13. That one contains the carriage return. */
+       sr_dbg("DMM packet: \"%.13s\"", buf);
 
        if ((ret = parse_value(buf, floatval)) != SR_OK) {
                sr_err("Error parsing value: %d.", ret);
                return ret;
        }
 
-       memset(info, 0x00, sizeof(struct metex14_info));
-       parse_flags((const char *)buf, info);
-       handle_flags(analog, floatval, info);
+       memset(info_local, 0x00, sizeof(struct metex14_info));
+       parse_flags((const char *)buf, info_local);
+       handle_flags(analog, floatval, info_local);
 
        return SR_OK;
 }