print_scale_data: correctly apply exponent

This commit is contained in:
Alexey Kopytko 2015-10-23 17:01:02 +09:00
parent c0f3c1ea05
commit d2f0d6ea61

View File

@ -276,16 +276,12 @@ static int print_scale_data(unsigned char* dat) {
uint8_t report = dat[0]; uint8_t report = dat[0];
uint8_t status = dat[1]; uint8_t status = dat[1];
uint8_t unit = dat[2]; uint8_t unit = dat[2];
uint8_t expt = dat[3]; // Accoring to the docs, scaling applied to the data as a base ten exponent
double weight = (double)(dat[4] + (dat[5] << 8)) / 10; int8_t expt = dat[3];
// convert to machine order at all times
if(expt != 255 && expt != 0) { double weight = (double) le16toh(dat[5] << 8 | dat[4]);
if (expt > 127) { // since the expt is signed, we do not need no trickery
weight = weight * pow(10, expt-255); weight = weight * pow(10, expt);
} else {
weight = pow(weight, expt);
}
}
// //
// The scale's first byte, its "report", is always 3. // The scale's first byte, its "report", is always 3.