tatobari / hx711py

HX711 Python Library for Raspberry Pi.
Apache License 2.0
211 stars 153 forks source link

Accounted for 2's complement output from sensor #12

Closed n8many closed 6 years ago

n8many commented 7 years ago

The sensor outputs 2's complement data according to the data sheet p.3 and it does not look like the library currently follows that.

To account for this, the code checks if the first bit is 1 (meaning a negative number). If so, it changes the most significant byte from 0x00 to 0xFF. It then tells numpy to interpret the array as an int32 rather than a uint32 so that we don't have to do any of the 2's complement math.

This replaces the line where you flip the most significant bit of the number.

*As a note, this is going to significantly change the output of the sensor going forward, so anyone who has hardcoded offset values from prior to this pull request will have to recalculate them using this equation:

new_offset = old_offset -8388608

n8many commented 7 years ago

Just realized what you were doing with that xor operation, which achieves the same purpose but with an offset. Both work equally well when only dealing with relative values.

Take your pick on closing or merging.

tatobari commented 6 years ago

Will take a look at this during this weekend.

tatobari commented 6 years ago

@n8many I might reject this pull request because the library is supposed to be just an interface to the IC's protocol, not an implementation. That's up to each user. So, I'll think about it but I'll be glad to here any comments from you.

Sorry for the delay.

n8many commented 6 years ago

Yeah, looking back at it, I agree. Thanks for looking at it!

tatobari commented 5 years ago

@n8many Your fix was actually necessary. I used a different approach cause I only saw this closed request just now. I didn't quite understand Two's Complement back when we talked. Sorry!