implement function to set integer position precision in bits

This commit is contained in:
liamcottle
2024-10-03 09:41:57 +13:00
parent 98a58eb3e3
commit 14c7339ed0
2 changed files with 51 additions and 0 deletions

View File

@ -1,5 +1,27 @@
class PositionUtil {
/**
* Obfuscates the provided latitude or longitude down to the provided precision in bits.
* This is based on the same logic in the official meshtastic firmware.
* https://github.com/meshtastic/firmware/blob/0a93261c0646f93aea518cc0599e547e9dc0e997/src/modules/PositionModule.cpp#L187
*/
static setPositionPrecision(latitudeOrLongitudeInteger, precision) {
// check if we should use the provided precision
if(precision > 0 && precision < 32){
// apply bitmask to reduce precision of position to wanted bits
latitudeOrLongitudeInteger = latitudeOrLongitudeInteger & (0xFFFFFFFF << (32 - precision));
// we want the imprecise position to be the middle of the possible location
latitudeOrLongitudeInteger += (1 << (31 - precision));
}
return latitudeOrLongitudeInteger;
}
/**
* Truncates the provided latitude or longitude to a maximum of x decimal places
* e.g: 12.3456789 with 2 decimal places would be 12.34