Code: Select all

```
bool parse_packet(bd_addr addr, byte[] scanData)
{
// Check for expected length
if (scanData.Length != 18) return false;
// Manufacturer specific
if (scanData[1] != 0xFF) return false;
// Length of trailing packet
if (scanData[0] != 13) return false;
// Check data in address matches mac - part of our protocol for iOS fix
if (scanData[9] != addr.Address[2]) return false;
if (scanData[10] != addr.Address[1]) return false;
if (scanData[11] != addr.Address[0]) return false;
// Check for manufacturer code
if (scanData[2] != 0x59 || scanData[3] != 0x00) return false;
// Read sensor version – set as appropriate
prod_version = scanData[4] & 0x7F;
if (prod_version != 0x03 && prod_version != 0x05) return false;
// Check that this is our "sync"ed sensor
if (addr != synced_mac) return false;
return true;
}
bool is_sync_pressed(uint8_t *scanData)
{
if (scanData[6] & 0x80) {
return true;
}
return false;
}
getAcceloY(rawByte) {
// Return value in “G’s” which can be scaled to an angle
return (char)scanData[12] / 1024;
}
getAcceloX(rawByte) {
// Return value in “G’s” which can be scaled to an angle
return (char)scanData[13] / 1024
// Returns the measured fluid height (or air height), in mm
// scanData - represents the array of raw bytes for the manufacturing data
uint32_t get_tank_height(const uint8_t* scanData)
{
// For LPG (Liquid propane) use the following coefficients
const double coef[3] = { 0.573045, -0.002822, -0.00000535 };
// Retrieve raw level from data. Only the LS 14-bits represent the level
uint16_t raw = (scanData[8] * 256) + scanData[7];
double raw_level = raw & 0x3FFF;
// add the range 16384us to 81916us with 4us resolution
if (scanData[4] & 0x80) {
raw_level = 16384.0 + raw_level * 4.0;
}
// Retrieve unscaled temperature from advert packet
double raw_t = (scanData[6] & 0x7F);
// MSbit is button state - ignored
// Apply 2nd order polynomial to compensating the raw TOF into mm of LPG
return (uint32_t)(raw_level * (coef[0] + coef[1] * raw_t + coef[2] * raw_t *
raw_t));
}
float get_battery_voltage(uint8_t *scanData)
{
// Return battery voltage – note that MSbit is reserved and should be set 0
return (float)(scanData[5] & 0x7F) / 32.0f;
}
// Arbitrary scaling of battery voltage to percent for CR2032
float get_battery_percentage(float voltage)
{
float percent = (voltage - 2.2f) / 0.65f * 100.0f;
if (percent < 0.0f) {
return 0.0f;
}
if (percent > 100.0f) {
return 100.0f;
}
return percent;
}
int get_temperature_reading(uint8_t *scanData)
{
uint8_t temp = scanData[6] & 0x7F;
return ((int)temp - 40);
}
```