HousingDepthSource.swift (4161B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 // Turns the external pressure of the housing (characteristic 1625) into what the dive logic of RemoraKit 3 // expects: `DiveReading` values with the absolute pressure, the surface pressure captured before the 4 // dive and a "submerged" decision. Pure logic (no Bluetooth, no clock), so that it can be tested. 5 // 6 // 1625 is still a HYPOTHESIS (external absolute pressure, never seen underwater): every value is checked 7 // for plausibility before it is used, and nothing is emitted for a value that cannot be a pressure. 8 import Foundation 9 import RemoraDeco 10 11 struct HousingDepthSource: Equatable, Sendable { 12 /// Absolute pressures the sensor can plausibly report (Pa): from a high mountain lake to about 100 m. 13 static let plausiblePa = 50_000.0...1_100_000.0 14 /// Pressures that can be a surface pressure (Pa): up to about 4000 m of altitude. 15 static let plausibleSurfacePa = 60_000.0...110_000.0 16 /// Above the surface pressure by this much (Pa; 3 kPa ≈ 30 cm of water), the housing is underwater. 17 static let submergedOverPa = 3_000.0 18 /// Link lost deeper than this (Pa above the surface; 15 kPa ≈ 1.5 m): still underwater, depth unknown. 19 /// Shallower, the housing is considered out of the water (same rule as the watchdog of RemoraKit). 20 static let shallowOverPa = 15_000.0 21 22 /// Surface pressure: the lowest plausible pressure seen since the housing session started. 23 private(set) var surfacePa: Double? 24 /// True when the first values were already above any surface pressure (app started underwater): 25 /// the standard atmosphere is assumed. 26 private(set) var surfaceAssumed = false 27 private(set) var lastPa: Double? 28 /// The last value received cannot be a pressure. 29 private(set) var implausible = false 30 31 /// Basic dive information, shown whether the dive computer is on or off: estimated depth (m, fresh 32 /// water) and the time the housing went below 0.5 m (nil at the surface). 33 private(set) var depth: Double? 34 private(set) var diveStart: Date? 35 36 var submerged: Bool { 37 guard let surfacePa, let lastPa else { return false } 38 return lastPa - surfacePa > Self.submergedOverPa 39 } 40 41 /// One sample of the external pressure, in Pa. `time` is the clock of the source in seconds (only 42 /// differences matter), `now` the wall clock. Returns nil when the value is not usable. 43 mutating func pressure(_ pa: Double, time: Double, now: Date) -> DiveReading? { 44 guard Self.plausiblePa.contains(pa) else { 45 implausible = true 46 return nil 47 } 48 implausible = false 49 if Self.plausibleSurfacePa.contains(pa) { 50 if surfaceAssumed { 51 // Back in the range of a surface pressure after starting underwater: a real measurement at last. 52 if pa < (surfacePa ?? pa) { surfacePa = pa; surfaceAssumed = false } 53 } else { 54 surfacePa = min(surfacePa ?? pa, pa) 55 } 56 } else if surfacePa == nil { 57 surfacePa = standardSurfacePressure * 100_000 58 surfaceAssumed = true 59 } 60 guard let surfacePa else { return nil } 61 lastPa = pa 62 let over = pa - surfacePa 63 let metres = max(0, over / 9_806.65) 64 depth = metres 65 if metres > 0.5, diveStart == nil { diveStart = now } 66 if metres < 0.2, diveStart != nil { diveStart = nil } 67 return .pressure(time: time, surface: surfacePa / 100_000, absolute: pa / 100_000, 68 submerged: over > Self.submergedOverPa) 69 } 70 71 /// The Bluetooth link to the housing was lost. Underwater, the reading says so: no measurement, still 72 /// submerged (the dive logic then holds the last depth and flags it as unknown). Near the surface, nothing 73 /// is emitted: the silence is handled by the watchdog of the dive logic. 74 func linkLost(time: Double) -> DiveReading? { 75 guard let surfacePa, let lastPa, lastPa - surfacePa > Self.shallowOverPa else { return nil } 76 return .pressure(time: time, surface: surfacePa / 100_000, absolute: nil, submerged: true) 77 } 78 }