X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fdmm%2Fut372.c;h=2f8ed1fa3c7dcd3406b5243d54bb3c9accd52afc;hb=118268a2fe27e0ec998589e228e4b9d87a760883;hp=b5e2c703f7682eaf132888aa4fb70362951d089f;hpb=472bef399078cd7e80f35940a527828a1c841c46;p=libsigrok.git diff --git a/src/dmm/ut372.c b/src/dmm/ut372.c index b5e2c703..2f8ed1fa 100644 --- a/src/dmm/ut372.c +++ b/src/dmm/ut372.c @@ -23,13 +23,14 @@ */ #include +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" #define LOG_PREFIX "ut372" -char lookup[] = { +uint8_t lookup[] = { 0x7B, 0x60, 0x5E, @@ -44,6 +45,23 @@ char lookup[] = { #define DECIMAL_POINT_MASK 0x80 +/* Decode a pair of characters into a byte. */ +static uint8_t decode_pair(const uint8_t *buf) +{ + unsigned int i; + char hex[3]; + + hex[2] = '\0'; + + for (i = 0; i < 2; i++) { + hex[i] = buf[i]; + if (hex[i] > 0x39) + hex[i] += 7; + } + + return strtol(hex, NULL, 16); +} + SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf) { return (buf[25] == '\r' && buf[26] == '\n'); @@ -52,22 +70,16 @@ SR_PRIV gboolean sr_ut372_packet_valid(const uint8_t *buf) 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]; + unsigned int i, j, value, divisor; + uint8_t segments; (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); + 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);