From: Aurelien Jacobs Date: Tue, 16 Aug 2016 16:08:03 +0000 (+0200) Subject: brymen-bm25x: properly set encoding digits X-Git-Tag: libsigrok-0.5.0~273 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=25198a7e8912acbf2729770278e356b00a6516c0 brymen-bm25x: properly set encoding digits --- diff --git a/src/dmm/bm25x.c b/src/dmm/bm25x.c index d7468f49..4966eedb 100644 --- a/src/dmm/bm25x.c +++ b/src/dmm/bm25x.c @@ -94,36 +94,31 @@ static int decode_point(const uint8_t *buf) return p; } -static float scale_value(float val, int point, int digits) +static int decode_scale(int point, int digits) { int pos; pos = point ? point + digits - MAX_DIGITS : 0; - switch (pos) { - case 0: return val; - case 1: return val * 1e-1; - case 2: return val * 1e-2; - case 3: return val * 1e-3; + if (pos < 0 || pos > 3) { + sr_dbg("Invalid decimal point %d (%d digits).", point, digits); + return 0; } - - sr_dbg("Invalid decimal point %d (%d digits).", point, digits); - - return NAN; + return -pos; } -static float decode_prefix(const uint8_t *buf) +static int decode_prefix(const uint8_t *buf) { - if (buf[11] & 2) return 1e+6; - if (buf[11] & 1) return 1e+3; - if (buf[13] & 1) return 1e-3; - if (buf[13] & 2) return 1e-6; - if (buf[12] & 1) return 1e-9; + if (buf[11] & 2) return 6; + if (buf[11] & 1) return 3; + if (buf[13] & 1) return -3; + if (buf[13] & 2) return -6; + if (buf[12] & 1) return -9; - return 1.0f; + return 0; } -static float decode_value(const uint8_t *buf) +static float decode_value(const uint8_t *buf, int *exponent) { float val = 0.0f; int i, digit; @@ -137,7 +132,8 @@ static float decode_value(const uint8_t *buf) val = 10.0 * val + digit; } - return scale_value(val, decode_point(buf), i); + *exponent = decode_scale(decode_point(buf), i); + return val; special: if (decode_digit(1, buf) == 0 && decode_digit(2, buf) == 'L') @@ -149,6 +145,7 @@ special: SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval, struct sr_datafeed_analog *analog, void *info) { + int exponent = 0; float val; (void)info; @@ -204,12 +201,16 @@ SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval, analog->meaning->unit = SR_UNIT_FAHRENHEIT; } - val = decode_value(buf) * decode_prefix(buf); + val = decode_value(buf, &exponent); + exponent += decode_prefix(buf); + val *= powf(10, exponent); if (buf[3] & 1) val = -val; *floatval = val; + analog->encoding->digits = -exponent; + analog->spec->spec_digits = -exponent; return SR_OK; }