weliem / bluez_inc

A C library for Bluez (BLE) that hides all DBus communication. It doesn't get easier than this. This library can also be used in C++.
MIT License
84 stars 19 forks source link

Updated parser float functions #43

Closed bojennett closed 3 months ago

bojennett commented 4 months ago

With the parser_get_float function that was there, I was not getting the correct results. I wasn't sure where the error is, but the results I was getting were way off. I have updated it to a function that I know works.

I also added a function called "parser_get_halffloat" which will convert the IEEE-754 16-bit version of float. This is the one with a sign bit, 5-bit exponent, and 10-bit significant.

Both have been tested.

weliem commented 3 months ago

In BLE we use floats as specified in IEEE 11073. See also this discussion: https://stackoverflow.com/questions/60841331/dart-convert-ieee-11073-32-bit-float-to-a-simple-double

So I think my original code is correct. I think in you usecase you are receiving floats that are encoded with a different standard and hence the unexpected results.

bojennett commented 3 months ago

I see. Yes, you are correct the 32-bit float that you had was 11073 and SFLOAT is also 11073. And bluetooth specs are about that. So I guess what is needed are parsing function for the 754 functions for 32 and 16-bit float. I replaced your float with the 754 version, so that has to be backed out. But I do think it would be good to include the 754 float formats in the parser. Bluetooth spec is one thing, but what people do is another. I know a lot of Chinese exercise devices I've use 754 32-bit float, and I've seen a couple of things that use 16-bit. If you think these would be OK to add, I can update this PR to pull out the 32-bit float change I made and add it in as a separate 754 float function.

weliem commented 3 months ago

I agree. It is fine to have additional functions for other types of floats...

bojennett commented 3 months ago

Ok, updated. I reverted back to the original parser_get_float, and have now two functions: parser_get_754half which parses the 16-bit IEEE-754 version of float, and parser_get_754float which parses the 32-bit IEEE-754 version of float.