X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Flcr%2Fes51919.c;h=b80a26c52868c42e559d49f8092e092693d76ad5;hb=4ba4d52a8f9c66f6b941a545e17c684e65356aff;hp=0244851a2b6c9594ab093dc4d7429523fcc9e791;hpb=7ffcf58736e510182a84eed68bc036bf00b0c85e;p=libsigrok.git diff --git a/src/lcr/es51919.c b/src/lcr/es51919.c index 0244851a..b80a26c5 100644 --- a/src/lcr/es51919.c +++ b/src/lcr/es51919.c @@ -470,15 +470,16 @@ static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel) return 0; } -static float parse_value(const uint8_t *buf) +static float parse_value(const uint8_t *buf, int *digits) { - static const float decimals[] = { - 1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7 - }; + static const int exponents[] = {0, -1, -2, -3, -4, -5, -6, -7}; + int exponent; int16_t val; + exponent = exponents[buf[3] & 7]; + *digits = -exponent; val = (buf[1] << 8) | buf[2]; - return (float)val * decimals[buf[3] & 7]; + return (float)val * powf(10, exponent); } static void parse_measurement(const uint8_t *pkt, float *floatval, @@ -487,25 +488,26 @@ static void parse_measurement(const uint8_t *pkt, float *floatval, { static const struct { int unit; - float mult; + int exponent; } units[] = { - { SR_UNIT_UNITLESS, 1 }, /* no unit */ - { SR_UNIT_OHM, 1 }, /* Ohm */ - { SR_UNIT_OHM, 1e3 }, /* kOhm */ - { SR_UNIT_OHM, 1e6 }, /* MOhm */ - { -1, 0 }, /* ??? */ - { SR_UNIT_HENRY, 1e-6 }, /* uH */ - { SR_UNIT_HENRY, 1e-3 }, /* mH */ - { SR_UNIT_HENRY, 1 }, /* H */ - { SR_UNIT_HENRY, 1e3 }, /* kH */ - { SR_UNIT_FARAD, 1e-12 }, /* pF */ - { SR_UNIT_FARAD, 1e-9 }, /* nF */ - { SR_UNIT_FARAD, 1e-6 }, /* uF */ - { SR_UNIT_FARAD, 1e-3 }, /* mF */ - { SR_UNIT_PERCENTAGE, 1 }, /* % */ - { SR_UNIT_DEGREE, 1 } /* degree */ + { SR_UNIT_UNITLESS, 0 }, /* no unit */ + { SR_UNIT_OHM, 0 }, /* Ohm */ + { SR_UNIT_OHM, 3 }, /* kOhm */ + { SR_UNIT_OHM, 6 }, /* MOhm */ + { -1, 0 }, /* ??? */ + { SR_UNIT_HENRY, -6 }, /* uH */ + { SR_UNIT_HENRY, -3 }, /* mH */ + { SR_UNIT_HENRY, 0 }, /* H */ + { SR_UNIT_HENRY, 3 }, /* kH */ + { SR_UNIT_FARAD, -12 }, /* pF */ + { SR_UNIT_FARAD, -9 }, /* nF */ + { SR_UNIT_FARAD, -6 }, /* uF */ + { SR_UNIT_FARAD, -3 }, /* mF */ + { SR_UNIT_PERCENTAGE, 0 }, /* % */ + { SR_UNIT_DEGREE, 0 }, /* degree */ }; const uint8_t *buf; + int digits, exponent; int state; buf = pkt_to_buf(pkt, is_secondary); @@ -533,7 +535,7 @@ static void parse_measurement(const uint8_t *pkt, float *floatval, analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; } - if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) < 0) + if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) == 0) return; if ((buf[3] >> 3) >= ARRAY_SIZE(units)) { @@ -544,8 +546,11 @@ static void parse_measurement(const uint8_t *pkt, float *floatval, analog->meaning->unit = units[buf[3] >> 3].unit; - *floatval = parse_value(buf); - *floatval *= (state == 0) ? units[buf[3] >> 3].mult : INFINITY; + exponent = units[buf[3] >> 3].exponent; + *floatval = parse_value(buf, &digits); + *floatval *= (state == 0) ? powf(10, exponent) : INFINITY; + analog->encoding->digits = digits - exponent; + analog->spec->spec_digits = digits - exponent; } static unsigned int parse_freq(const uint8_t *pkt) @@ -639,6 +644,7 @@ static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt) frame = FALSE; + /* Note: digits/spec_digits will be overridden later. */ sr_analog_init(&analog, &encoding, &meaning, &spec, 0); analog.num_samples = 1; @@ -735,14 +741,11 @@ static const char *const channel_names[] = { "P1", "P2" }; static int setup_channels(struct sr_dev_inst *sdi) { unsigned int i; - int ret; - - ret = SR_ERR_BUG; for (i = 0; i < ARRAY_SIZE(channel_names); i++) sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]); - return ret; + return SR_OK; } SR_PRIV void es51919_serial_clean(void *priv)