]> sigrok.org Git - libsigrok.git/blobdiff - src/dmm/ut372.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / dmm / ut372.c
index c5944cc47558badcad7b8a5b6603d2d9a5c99272..d2fd8bc9e8b5643d8996a87d62737b17af868836 100644 (file)
 #include <stdlib.h>
 #include <stdint.h>
 #include <math.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "ut372"
 
-uint8_t lookup[] = {
+static const uint8_t lookup[] = {
        0x7B,
        0x60,
        0x5E,
@@ -40,7 +40,7 @@ uint8_t lookup[] = {
        0x3F,
        0x70,
        0x7F,
-       0x7D
+       0x7D,
 };
 
 #define DECIMAL_POINT_MASK 0x80
@@ -48,9 +48,10 @@ uint8_t lookup[] = {
 #define FLAGS1_HOLD_MASK (1 << 2)
 
 #define FLAGS2_RPM_MASK (1 << 0)
+#define FLAGS2_COUNT_MASK (1 << 1)
 #define FLAGS2_MAX_MASK (1 << 4)
 #define FLAGS2_MIN_MASK (1 << 5)
-#define FLAGS2_AVE_MASK (1 << 6)
+#define FLAGS2_AVG_MASK (1 << 6)
 
 /* Decode a pair of characters into a byte. */
 static uint8_t decode_pair(const uint8_t *buf)
@@ -71,7 +72,18 @@ static uint8_t decode_pair(const uint8_t *buf)
 
 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
 {
-       return (buf[25] == '\r' && buf[26] == '\n');
+       uint8_t flags2;
+
+       if (!(buf[25] == '\r' && buf[26] == '\n'))
+               return FALSE;
+
+       flags2 = decode_pair(buf + 23);
+
+       if (!(flags2 & (FLAGS2_RPM_MASK | FLAGS2_COUNT_MASK)))
+               /* Device is in the setup menu - no valid data shown. */
+               return FALSE;
+
+       return TRUE;
 }
 
 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
@@ -85,10 +97,12 @@ SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
        flags1 = decode_pair(buf + 21);
        flags2 = decode_pair(buf + 23);
 
-       /* If not in RPM mode, ignore packet. */
-       if (!(flags2 & FLAGS2_RPM_MASK)) {
-               sr_dbg("Not in RPM mode. Count mode is unsupported.");
-               return SR_ERR_NA;
+       if (flags2 & FLAGS2_RPM_MASK) {
+               analog->mq = SR_MQ_FREQUENCY;
+               analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
+       } else if (flags2 & FLAGS2_COUNT_MASK) {
+               analog->mq = SR_MQ_COUNT;
+               analog->unit = SR_UNIT_UNITLESS;
        }
 
        if (flags1 & FLAGS1_HOLD_MASK)
@@ -97,14 +111,14 @@ SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
                analog->mqflags |= SR_MQFLAG_MIN;
        if (flags2 & FLAGS2_MAX_MASK)
                analog->mqflags |= SR_MQFLAG_MAX;
-       if (flags2 & FLAGS2_AVE_MASK)
+       if (flags2 & FLAGS2_AVG_MASK)
                analog->mqflags |= SR_MQFLAG_AVG;
 
        value = 0;
        divisor = 1;
 
        for (i = 0; i < 5; i++) {
-               segments = decode_pair(buf + 1 + 2*i);
+               segments = decode_pair(buf + 1 + (2 * i));
                for (j = 0; j < ARRAY_SIZE(lookup); j++) {
                        if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
                                value += j * pow(10, i);
@@ -117,8 +131,5 @@ SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
 
        *floatval = (float) value / divisor;
 
-       analog->mq = SR_MQ_FREQUENCY;
-       analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
-
        return SR_OK;
 }