From d2f0d6ea615c11a5b21d95c1e68c0e4fdd216fbe Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Fri, 23 Oct 2015 17:01:02 +0900 Subject: [PATCH] print_scale_data: correctly apply exponent --- usbscale.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/usbscale.c b/usbscale.c index 8099449..376e3e0 100644 --- a/usbscale.c +++ b/usbscale.c @@ -276,16 +276,12 @@ static int print_scale_data(unsigned char* dat) { uint8_t report = dat[0]; uint8_t status = dat[1]; uint8_t unit = dat[2]; - uint8_t expt = dat[3]; - double weight = (double)(dat[4] + (dat[5] << 8)) / 10; - - if(expt != 255 && expt != 0) { - if (expt > 127) { - weight = weight * pow(10, expt-255); - } else { - weight = pow(weight, expt); - } - } + // Accoring to the docs, scaling applied to the data as a base ten exponent + int8_t expt = dat[3]; + // convert to machine order at all times + double weight = (double) le16toh(dat[5] << 8 | dat[4]); + // since the expt is signed, we do not need no trickery + weight = weight * pow(10, expt); // // The scale's first byte, its "report", is always 3.