Skip to content

Instantly share code, notes, and snippets.

@tscholze
Created March 21, 2024 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tscholze/380bee7a1266c632151a5fdce35baf56 to your computer and use it in GitHub Desktop.
Save tscholze/380bee7a1266c632151a5fdce35baf56 to your computer and use it in GitHub Desktop.
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