X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fdmm%2Fmetex14.c;h=8ba4291364d2d3b2f9cf2880992ec985c4819c49;hb=0d4405ce5e9373d2dcd90a9188b79d79eaafae5d;hp=2ef5ecff7459d4f637964c0b794006fd123a13fb;hpb=71f1302b4b4b712149303d235876146e8d1d9af5;p=libsigrok.git diff --git a/hardware/common/dmm/metex14.c b/hardware/common/dmm/metex14.c index 2ef5ecff..8ba42913 100644 --- a/hardware/common/dmm/metex14.c +++ b/hardware/common/dmm/metex14.c @@ -45,72 +45,36 @@ static int parse_value(const uint8_t *buf, float *result) { - int i, sign, intval = 0, factor, decimal_point = 0, is_ol; - float floatval; - uint8_t digit; - - /* Byte 3: Sign (' ' or '-') */ - if (buf[3] == ' ') { - sign = 1; - } else if (buf[3] == '-') { - sign = -1; - } else { - sr_err("Invalid sign byte: 0x%02x.", buf[3]); - return SR_ERR; + int i, is_ol, cnt; + char valstr[7 + 1]; + + /* Strip all spaces from bytes 2-8. */ + memset(&valstr, 0, 7 + 1); + for (i = 0, cnt = 0; i < 7; i++) { + if (buf[2 + i] != ' ') + valstr[cnt++] = buf[2 + i]; } /* Bytes 5-7: Over limit (various forms) */ is_ol = 0; - 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; + is_ol += (!strcasecmp((const char *)&valstr, ".OL")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "O.L")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "OL.")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "OL")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-.OL")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-O.L")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-OL.")) ? 1 : 0; + is_ol += (!strcasecmp((const char *)&valstr, "-OL")) ? 1 : 0; if (is_ol != 0) { sr_spew("Over limit."); *result = INFINITY; return SR_OK; } - /* Bytes 4-8: Value (up to 4 digits) and decimal point */ - 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)) { - intval += (digit - '0') * factor; - factor /= 10; - } else { - sr_err("Invalid digit byte: 0x%02x.", digit); - return SR_ERR; - } - } - - floatval = (float)intval; - - /* Decimal point position */ - if (decimal_point == 0 || decimal_point == 4) { - /* TODO: Doesn't happen? */ - } else if (decimal_point == 1) { - floatval /= 1000; - } else if (decimal_point == 2) { - floatval /= 100; - } else if (decimal_point == 3) { - floatval /= 10; - } else { - sr_err("Invalid decimal point position: %d.", decimal_point); - return SR_ERR; - } - - /* Apply sign. */ - floatval *= sign; - - sr_spew("The display value is %f.", floatval); + /* Bytes 2-8: Sign, value (up to 5 digits) and decimal point */ + sscanf((const char *)&valstr, "%f", result); - *result = floatval; + sr_spew("The display value is %f.", *result); return SR_OK; } @@ -141,44 +105,46 @@ static void parse_flags(const char *buf, struct metex14_info *info) if (info->is_dc || info->is_ac) info->is_volt = TRUE; - /* Byte 2: Always space (0x20). */ + /* Bytes 2-8: See parse_value(). */ - /* Bytes 3-8: See parse_value(). */ - - /* Bytes 9-12: Unit */ + /* Strip all spaces from bytes 9-12. */ memset(&unit, 0, 4 + 1); for (i = 0, cnt = 0; i < 4; i++) { if (buf[9 + i] != ' ') unit[cnt++] = buf[9 + i]; } + + /* Bytes 9-12: Unit */ u = (const char *)&unit; - if (!strcmp(u, "A")) + if (!strcasecmp(u, "A")) info->is_ampere = TRUE; - else if (!strcmp(u, "mA")) + else if (!strcasecmp(u, "mA")) info->is_milli = info->is_ampere = TRUE; - else if (!strcmp(u, "uA")) + else if (!strcasecmp(u, "uA")) info->is_micro = info->is_ampere = TRUE; - else if (!strcmp(u, "V")) + else if (!strcasecmp(u, "V")) info->is_volt = TRUE; - else if (!strcmp(u, "mV")) + else if (!strcasecmp(u, "mV")) info->is_milli = info->is_volt = TRUE; - else if (!strcmp(u, "Ohm")) + else if (!strcasecmp(u, "Ohm")) info->is_ohm = TRUE; - else if (!strcmp(u, "KOhm")) + else if (!strcasecmp(u, "KOhm")) info->is_kilo = info->is_ohm = TRUE; - else if (!strcmp(u, "MOhm")) + else if (!strcasecmp(u, "MOhm")) info->is_mega = info->is_ohm = TRUE; - else if (!strcmp(u, "nF")) + else if (!strcasecmp(u, "pF")) + info->is_pico = info->is_farad = TRUE; + else if (!strcasecmp(u, "nF")) info->is_nano = info->is_farad = TRUE; - else if (!strcmp(u, "uF")) + else if (!strcasecmp(u, "uF")) info->is_micro = info->is_farad = TRUE; - else if (!strcmp(u, "KHz")) + else if (!strcasecmp(u, "KHz")) info->is_kilo = info->is_hertz = TRUE; - else if (!strcmp(u, "C")) + else if (!strcasecmp(u, "C")) info->is_celsius = TRUE; - else if (!strcmp(u, "DB")) + else if (!strcasecmp(u, "DB")) info->is_decibel = TRUE; - else if (!strcmp(u, "")) + else if (!strcasecmp(u, "")) info->is_unitless = TRUE; /* Byte 13: Always '\r' (carriage return, 0x0d, 13) */ @@ -188,6 +154,8 @@ static void handle_flags(struct sr_datafeed_analog *analog, float *floatval, const struct metex14_info *info) { /* Factors */ + if (info->is_pico) + *floatval /= 1000000000000ULL; if (info->is_nano) *floatval /= 1000000000; if (info->is_micro) @@ -252,6 +220,7 @@ static gboolean flags_valid(const struct metex14_info *info) /* Does the packet have more than one multiplier? */ count = 0; + count += (info->is_pico) ? 1 : 0; count += (info->is_nano) ? 1 : 0; count += (info->is_micro) ? 1 : 0; count += (info->is_milli) ? 1 : 0;