mirror of
https://github.com/erjiang/usbscale.git
synced 2025-06-29 09:30:59 -06:00
print_scale_data: correctly apply exponent
This commit is contained in:
parent
c0f3c1ea05
commit
d2f0d6ea61
16
usbscale.c
16
usbscale.c
@ -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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user