remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

commit e5eace7d2f1e8b627410765f2c2da55682d1fa47
parent f680db890d578f7ae56aa59e4771c7d257de1d04
Author: Jean Schurger <jean@schurger.org>
Date:   Sat, 14 Nov 2026 11:14:54 -0500

Device-specific parts of the dive logic for a second app: computer model name, configuration hook, ascent rate window, UDDF generator name

Open Mérou (an iPhone in a dive housing) feeds the dive logic from another
pressure sensor. What depends on the device is no longer fixed:

- DiveMonitor.computerModel / DiveTracker.computerModel / DiveRecorder:
  the name written in the recorded dives ("Open Rémora" by default).
- DiveMonitor.configure: adjusts the DiveComputer.Configuration after the
  diver's settings (depth limit warning, 38 m by default).
- DiveComputer.Configuration.ascentRateWindow (5 s by default).
- UDDF.export(generatorName:) ("Open Rémora" by default).

Nothing changes for Open Rémora: all defaults are the previous values.
Tests: the defaults, the overrides, and a 3 Hz source with drop-outs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Diffstat:
M.gitignore | 1+
MMakefile | 2+-
MREADME.md | 4++++
MSources/RemoraDeco/DiveComputer.swift | 7++++++-
MSources/RemoraDeco/DiveMonitor.swift | 12+++++++++++-
MSources/RemoraDeco/DiveRecorder.swift | 7+++++--
MSources/RemoraDeco/DiveTracker.swift | 4+++-
MSources/RemoraDeco/UDDF.swift | 5+++--
MTests/RemoraDecoTests/DiveTrackerTests.swift | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MTests/RemoraDecoTests/LogbookTests.swift | 2++
10 files changed, 123 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ .build/ .swiftpm/ *.xcodeproj +.claude/ diff --git a/Makefile b/Makefile @@ -6,7 +6,7 @@ REMOTE ?= src/perso/remora-kit .PHONY: sync test sync: - rsync -a --delete --exclude .git --exclude .build --exclude .swiftpm ./ $(CHUNGA):$(REMOTE)/ + rsync -a --delete --exclude .git --exclude .claude --exclude .build --exclude .swiftpm ./ $(CHUNGA):$(REMOTE)/ test: sync ssh $(CHUNGA) 'cd $(REMOTE) && swift test 2>&1' diff --git a/README.md b/README.md @@ -45,6 +45,10 @@ monitor.receive(.temperature(celsius)) - The monitor is `@Observable` and `@MainActor`: views read `status` (`DiveComputer.Status`), `warnings`, `isAlarming`, `activeWarnings`, `depthUnknown`, `temperature`, `sourceMessage`, `finishedDive`. - `startSimulation(_:speed:)` runs a dry demo dive from a `DemoProfile`. +- What depends on the device rather than on the diver is set on the monitor: `computerModel` (the name written + in the recorded dives, "Open Rémora" by default) and `configure`, a closure that adjusts the + `DiveComputer.Configuration` after the diver's settings (`depthLimit`, the warning depth, 38 m by default; + `ascentRateWindow`, 5 s by default). `DiveTracker` is the same logic as a plain value type, with no clock and no timer: every call takes the wall-clock time and returns the events (`diveStarted`, `cue`, `diveEnded`). The tests script dives with it. diff --git a/Sources/RemoraDeco/DiveComputer.swift b/Sources/RemoraDeco/DiveComputer.swift @@ -9,10 +9,14 @@ public struct DiveComputer: Sendable { public var deco = DecoSettings() public var ppO2Warning: Double = 1.4 public var ppO2Alarm: Double = 1.6 - /// Warning depth, before the 40 m limit of the watch. + /// Warning depth: 38 m by default, before the 40 m limit of the watch. An app with another depth + /// sensor sets its own (see `DiveMonitor.configure`). public var depthLimit: Double = 38 /// Warning when the NDL (no-stop time) drops below this value (min); 0 = off. public var lowNoDecompression: Double = 5 + /// Window over which the ascent rate is smoothed (s). 5 s suits a 1 Hz sensor; a noisier or + /// irregular source may want a longer one. + public var ascentRateWindow: Double = 5 public init() {} } @@ -67,6 +71,7 @@ public struct DiveComputer: Sendable { self.configuration = configuration self.scale = DepthScale(surfacePressure: surfacePressure, water: configuration.water) self.tissues = tissues ?? .saturated(surfacePressure: surfacePressure) + ascent.window = configuration.ascentRateWindow } /// New sample: `time` in seconds (monotonic clock), absolute `pressure` in bar. diff --git a/Sources/RemoraDeco/DiveMonitor.swift b/Sources/RemoraDeco/DiveMonitor.swift @@ -31,6 +31,9 @@ public final class DiveMonitor { public var onDiveStarted: ((_ simulation: Bool) -> Void)? /// Called when a dive ends; `saved` is nil when nothing was kept (the diver never went down). public var onDiveEnded: ((_ saved: DiveLog?, _ simulation: Bool) -> Void)? + /// Last word of the app on the computer configuration, after the diver's settings: what depends on the + /// device rather than on the diver (depth limit of its sensor, ascent rate window). Evaluated when a dive starts. + public var configure: ((inout DiveComputer.Configuration) -> Void)? private let store: any DiveLogStore private let feedback: (DiveCue) -> Void @@ -58,13 +61,20 @@ public final class DiveMonitor { get { tracker.forcedAlarm } set { tracker.forcedAlarm = newValue } } + /// Name of the computer written in the dives recorded ("Open Rémora" by default). + public var computerModel: String { + get { tracker.computerModel } + set { tracker.computerModel = newValue } + } public var tuning: DiveTracker.Tuning { get { tracker.tuning } set { tracker.tuning = newValue } } private var configuration: DiveComputer.Configuration { - (try? store.settings.configuration()) ?? DiveComputer.Configuration() + var c = (try? store.settings.configuration()) ?? DiveComputer.Configuration() + configure?(&c) + return c } /// A reading from the depth source: the dive starts by itself on submersion. diff --git a/Sources/RemoraDeco/DiveRecorder.swift b/Sources/RemoraDeco/DiveRecorder.swift @@ -6,6 +6,8 @@ public struct DiveRecorder: Sendable { public private(set) var computer: DiveComputer public let start: Date public let surfaceIntervalBefore: Double? + /// Name of the computer written in the logbook entry (the app that records the dive). + public var computerModel: String /// Minimum gap between two profile points (s). Alarms are logged at every sample. public var sampleInterval: Double = 2 @@ -21,7 +23,8 @@ public struct DiveRecorder: Sendable { /// Starts a dive. Tissues come from the logbook (repetitive dive) when it holds a recent one. public init(configuration: DiveComputer.Configuration, surfacePressure: Double, - start: Date, logbook: Logbook? = nil) { + start: Date, logbook: Logbook? = nil, computerModel: String = "Open Rémora") { + self.computerModel = computerModel let previous = logbook?.tissues(at: start, surfacePressure: surfacePressure) self.computer = DiveComputer(configuration: configuration, surfacePressure: surfacePressure, tissues: previous?.tissues) @@ -77,7 +80,7 @@ public struct DiveRecorder: Sendable { private func log(samples: [DiveSample]) -> DiveLog { let c = computer.configuration - return DiveLog(id: UUID(), source: .remora, computerModel: "Open Rémora", number: 0, start: start, surfacePressure: computer.scale.surfacePressure, + return DiveLog(id: UUID(), source: .remora, computerModel: computerModel, number: 0, start: start, surfacePressure: computer.scale.surfacePressure, surfaceIntervalBefore: surfaceIntervalBefore, gas: c.gas, gradientFactors: c.deco.gradientFactors, water: c.water, samples: samples, events: events.sorted { $0.time < $1.time }, diff --git a/Sources/RemoraDeco/DiveTracker.swift b/Sources/RemoraDeco/DiveTracker.swift @@ -70,6 +70,8 @@ public struct DiveTracker: Sendable { } public var tuning = Tuning() + /// Name of the computer written in the dives recorded (the app's name). + public var computerModel = "Open Rémora" /// Forces the alarm state (screenshots and feedback tests). public var forcedAlarm = false @@ -232,7 +234,7 @@ public struct DiveTracker: Sendable { private mutating func start(surfacePressure: Double, simulation: Bool, now: Date, configuration: DiveComputer.Configuration, logbook: Logbook?) -> [Event] { recorder = DiveRecorder(configuration: configuration, surfacePressure: surfacePressure, - start: now, logbook: logbook) + start: now, logbook: logbook, computerModel: computerModel) isSimulation = simulation isDiving = true self.surfacePressure = surfacePressure diff --git a/Sources/RemoraDeco/UDDF.swift b/Sources/RemoraDeco/UDDF.swift @@ -3,7 +3,8 @@ import Foundation /// Logbook export in UDDF 3.2 format (import into Subsurface, MacDive, etc.). SI units: m, s, K, Pa. public enum UDDF { - public static func export(_ dives: [DiveLog], generatorVersion: String = "0.1") -> String { + public static func export(_ dives: [DiveLog], generatorName: String = "Open Rémora", + generatorVersion: String = "0.1") -> String { let iso = ISO8601DateFormatter() var mixes: [Gas] = [] for d in dives where !mixes.contains(d.gas) { mixes.append(d.gas) } @@ -19,7 +20,7 @@ public enum UDDF { <?xml version="1.0" encoding="UTF-8"?> <uddf xmlns="http://www.streit.cc/uddf/3.2/" version="3.2.3"> <generator> - <name>Open Rémora</name> + <name>\(esc(generatorName))</name> <type>divecomputer</type> <version>\(generatorVersion)</version> </generator> diff --git a/Tests/RemoraDecoTests/DiveTrackerTests.swift b/Tests/RemoraDecoTests/DiveTrackerTests.swift @@ -208,6 +208,65 @@ struct DiveTrackerTests { #expect(s.tracker.isDiving) #expect(!s.tracker.isSimulation) } + + @Test func computerModelIsTheAppsName() { + var s = Script() + s.stay(at: 5, seconds: 30) + s.stay(at: 0, seconds: 62) + #expect(s.endedDive?.computerModel == "Open Rémora") // default: unchanged for the watch + + var other = Script() + other.tracker.computerModel = "Open Mérou" + other.stay(at: 5, seconds: 30) + other.stay(at: 0, seconds: 62) + #expect(other.endedDive?.computerModel == "Open Mérou") + } + + /// A source at about 3 Hz that drops out for a couple of seconds now and then (a Bluetooth sensor): + /// the ascent rate stays right and nothing is flagged. + @Test func irregularThreeHertzSourceGivesASteadyAscentRate() { + var tracker = DiveTracker() + let origin = Date(timeIntervalSinceReferenceDate: 800_000_000) + let scale = DepthScale() + var events: [DiveTracker.Event] = [] + var t = 0.0, n = 0 + func depth(at t: Double) -> Double { t < 60 ? 18 : max(5, 18 - (t - 60) * 9 / 60) } // then up at 9 m/min + var worst = 0.0 + while t < 140 { + n += 1 + // Every 40th sample is followed by a 2.5 s gap. + t += n % 40 == 0 ? 2.5 : 0.33 + let now = origin.addingTimeInterval(t) + events += tracker.handle(.pressure(time: now.timeIntervalSinceReferenceDate, surface: scale.surfacePressure, + absolute: scale.pressure(atDepth: depth(at: t)), submerged: true), + now: now, configuration: DiveComputer.Configuration(), logbook: nil) + events += tracker.tick(now: now) + if t > 70, t < 140 { worst = max(worst, abs((tracker.status?.ascentRate ?? 0) - 9)) } + } + #expect(worst < 0.5) + #expect(!tracker.depthUnknown) + #expect(!events.contains(.cue(.depthLost))) + #expect(!events.contains(.cue(.alarm))) + } + + @Test func ascentRateWindowIsConfigurable() { + var short = DiveComputer.Configuration(), long = DiveComputer.Configuration() + short.ascentRateWindow = 3 + long.ascentRateWindow = 20 + var a = DiveComputer(configuration: short, surfacePressure: standardSurfacePressure) + var b = DiveComputer(configuration: long, surfacePressure: standardSurfacePressure) + let scale = DepthScale() + // 30 s at 20 m, then 5 s of ascent at 15 m/min. + for t in 0..<36 { + let depth = t <= 30 ? 20.0 : 20 - Double(t - 30) * 0.25 + a.update(time: Double(t), pressure: scale.pressure(atDepth: depth)) + b.update(time: Double(t), pressure: scale.pressure(atDepth: depth)) + } + #expect(abs(a.status.ascentRate - 15) < 0.5) + #expect(a.status.warnings.contains(.ascentTooFast)) + #expect(b.status.ascentRate < 8) // smoothed over 20 s + #expect(DiveComputer.Configuration().ascentRateWindow == 5) + } } @MainActor @@ -252,6 +311,34 @@ struct DiveMonitorTests { #expect(monitor.finishedDive?.number == 1) #expect(monitor.finishedDive?.minTemperature == 12) #expect(cues.last == .diveSaved) + #expect(monitor.finishedDive?.computerModel == "Open Rémora") + } + + /// What depends on the device: the app names the computer and adjusts the configuration. + @Test func appSetsTheComputerModelAndTheDepthLimit() { + let store = MemoryStore() + var clock = Date(timeIntervalSinceReferenceDate: 800_000_000) + let monitor = DiveMonitor(store: store, feedback: { _ in }, now: { clock }) + monitor.computerModel = "Open Mérou" + monitor.configure = { $0.depthLimit = 12 } + + let scale = DepthScale() + func read(_ depth: Double) { + monitor.receive(.pressure(time: clock.timeIntervalSinceReferenceDate, surface: scale.surfacePressure, + absolute: scale.pressure(atDepth: depth), submerged: depth > 0)) + clock = clock.addingTimeInterval(1) + } + for _ in 0..<30 { read(10) } + #expect(monitor.warnings.isEmpty) + for _ in 0..<30 { read(12.5) } + #expect(monitor.warnings == [.depthLimit]) + for _ in 0..<62 { read(0) } + #expect(store.logbook.dives.first?.computerModel == "Open Mérou") + + // Without `configure`, the limit stays at 38 m. + let plain = DiveMonitor(store: MemoryStore(), feedback: { _ in }, now: { clock }) + plain.receive(.pressure(time: 0, surface: scale.surfacePressure, absolute: scale.pressure(atDepth: 12.5), submerged: true)) + #expect(plain.warnings.isEmpty) } } diff --git a/Tests/RemoraDecoTests/LogbookTests.swift b/Tests/RemoraDecoTests/LogbookTests.swift @@ -98,6 +98,8 @@ struct LogbookTests { book.add(dive) let xml = UDDF.export(book.dives) #expect(xml.contains("<uddf xmlns=\"http://www.streit.cc/uddf/3.2/\"")) + #expect(xml.contains("<name>Open Rémora</name>")) + #expect(UDDF.export(book.dives, generatorName: "Open Mérou").contains("<name>Open Mérou</name>")) #expect(xml.contains("<mix id=\"mix1\"><name>Air</name><o2>0.210</o2>")) #expect(xml.contains("<greatestdepth>18.00</greatestdepth>")) #expect(xml.contains("Épave &lt;test&gt; &amp; essai"))