Created
March 21, 2024 18:23
-
-
Save tscholze/c60f7d6521dd47ed6d516cad7f4e8f3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun readPressure(): Double { | |
val temperature = readTemperature(asFinite = true) | |
val msb = device.read(REGISTER_MSB_PRESSURE, 1U).toInt() | |
val lsb = device.read(REGISTER_LSB_PRESSURE, 1U).toInt() | |
val xlsb = device.read(REGISTER_XLSB_PRESSURE, 1U).toInt() | |
val rawValue = (msb shl 12) + (lsb shl 4) + (xlsb shr 4) | |
var part1 = temperature / 2.0 - 64000.0 | |
var part2 = part1 * part1 * calibration.pressure6 / 32768.0 | |
part2 += part1 * calibration.pressure5 * 2 | |
part2 = part2 / 4.0 + calibration.pressure4 * 65536.0 | |
part1 = (calibration.pressure3 * part1 * part1 / 524288.0 + calibration.pressure2 * part1) / 524288.0 | |
part1 = (1.0 + part1 / 32768.0) * calibration.pressure1 | |
var pressure = 1048576.0 - rawValue | |
pressure = (pressure - part2 / 4096.0) * 6250.0 / part1 | |
part1 = calibration.pressure9 * pressure * pressure / 2147483648.0 | |
part2 = pressure * calibration.pressure8 / 32768.0 | |
pressure += (part1 + part2 + calibration.pressure7) / 16.0 | |
return pressure / 100 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment