]> sigrok.org Git - libsigrok.git/commitdiff
ut372: Implement initial protocol parser.
authorMartin Ling <redacted>
Sun, 1 Mar 2015 22:26:21 +0000 (22:26 +0000)
committerMartin Ling <redacted>
Sun, 1 Mar 2015 22:36:52 +0000 (22:36 +0000)
For now this only works correctly if the device is in the default
state showing current RPM. The flags are not checked.

src/dmm/ut372.c

index 4e763a361f80db989327cc7e4b25350d425587f8..b5e2c703f7682eaf132888aa4fb70362951d089f 100644 (file)
  * UNI-T UT372 protocol parser.
  */
 
+#include <stdlib.h>
+#include <math.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "ut372"
 
+char lookup[] = {
+       0x7B,
+       0x60,
+       0x5E,
+       0x7C,
+       0x65,
+       0x3D,
+       0x3F,
+       0x70,
+       0x7F,
+       0x7D
+};
+
+#define DECIMAL_POINT_MASK 0x80
+
 SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf)
 {
-       return FALSE;
+       return (buf[25] == '\r' && buf[26] == '\n');
 }
 
 SR_PRIV int sr_ut372_parse(const uint8_t *buf, float *floatval,
                struct sr_datafeed_analog *analog, void *info)
 {
+       unsigned int i, j, segments, value, divisor;
+       char hex[3];
+
+       (void) info;
+
+       hex[2] = '\0';
+       value = 0;
+       divisor = 1;
+
+       for (i = 0; i < 5; i++) {
+               for (j = 0; j < 2; j++) {
+                       hex[j] = buf[2*i + 1 + j];
+                       if (hex[j] > 0x39)
+                               hex[j] += 7;
+               }
+               segments = strtol(hex, NULL, 16);
+               for (j = 0; j < ARRAY_SIZE(lookup); j++) {
+                       if (lookup[j] == (segments & ~DECIMAL_POINT_MASK)) {
+                               value += j * pow(10, i);
+                               break;
+                       }
+               }
+               if (segments & DECIMAL_POINT_MASK)
+                       divisor = pow(10, i);
+       }
+
+       *floatval = (float) value / divisor;
+
+       analog->mq = SR_MQ_FREQUENCY;
+       analog->unit = SR_UNIT_REVOLUTIONS_PER_MINUTE;
+
        return SR_OK;
 }