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 f680db890d578f7ae56aa59e4771c7d257de1d04
parent b87d3090f039efb42a878d5106292e7f51f61797
Author: Jean Schurger <jean@schurger.org>
Date:   Sat, 14 Nov 2026 11:00:38 -0500

Dive started by hand at the surface, marks set by the diver, entry position written into the saved dive

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7deYJa9LG8PgUSQ5UR5LY

Diffstat:
MSources/RemoraDeco/DiveMonitor.swift | 23++++++++++++++++++++++-
MSources/RemoraDeco/DiveRecorder.swift | 18+++++++++++++++++-
MSources/RemoraDeco/DiveTracker.swift | 46++++++++++++++++++++++++++++++++++++++++++++++
MSources/RemoraDeco/Logbook.swift | 16++++++++++++++++
MTests/RemoraDecoTests/DiveTrackerTests.swift | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 162 insertions(+), 2 deletions(-)

diff --git a/Sources/RemoraDeco/DiveMonitor.swift b/Sources/RemoraDeco/DiveMonitor.swift @@ -24,6 +24,9 @@ public final class DiveMonitor { public var finishedDive: DiveLog? /// Site name given to demo dives. public var simulationSite: String? + /// Position of the entry point, taken before the dive (GPS does not work underwater): written into the + /// next real dive saved, then cleared. + public var entryPosition: (latitude: Double, longitude: Double)? /// Called when a dive starts, before its first cue. public var onDiveStarted: ((_ simulation: Bool) -> Void)? /// Called when a dive ends; `saved` is nil when nothing was kept (the diver never went down). @@ -71,6 +74,18 @@ public final class DiveMonitor { apply(events) } + /// Starts a real dive by hand, before getting in the water. + public func startManually() { + guard !tracker.isDiving else { return } + let events = tracker.startManually(now: now(), configuration: configuration, logbook: store.logbook) + apply(events) + } + + /// Marks the current point of the dive. + @discardableResult + public func mark(heading: Double? = nil) -> DiveMark? { tracker.mark(heading: heading) } + public var markCount: Int { tracker.markCount } + /// Ends the dive and adds it to the logbook (nothing is kept when the diver never went down). public func end() { let events = tracker.end() @@ -110,7 +125,13 @@ public final class DiveMonitor { onDiveEnded?(nil, simulated) continue } - if simulated { dive.site = simulationSite } + if simulated { + dive.site = simulationSite + } else if let position = entryPosition { + dive.latitude = position.latitude + dive.longitude = position.longitude + entryPosition = nil + } let saved = store.add(dive) finishedDive = saved feedback(.diveSaved) diff --git a/Sources/RemoraDeco/DiveRecorder.swift b/Sources/RemoraDeco/DiveRecorder.swift @@ -17,6 +17,7 @@ public struct DiveRecorder: Sendable { /// An alarm that flickers around its threshold is logged only once per period (s). public var eventRepeatInterval: Double = 30 private var latest: DiveSample? + public private(set) var marks: [DiveMark] = [] /// Starts a dive. Tissues come from the logbook (repetitive dive) when it holds a recent one. public init(configuration: DiveComputer.Configuration, surfacePressure: Double, @@ -55,12 +56,27 @@ public struct DiveRecorder: Sendable { return status } + /// Marks the current point of the dive. Nil before the first sample. + @discardableResult + public mutating func mark(heading: Double? = nil) -> DiveMark? { + guard let latest else { return nil } + let mark = DiveMark(time: latest.time, depth: latest.depth, heading: heading) + marks.append(mark) + return mark + } + /// Ends the dive and produces the logbook entry (number assigned by `Logbook.add`). public func finish() -> DiveLog { - let c = computer.configuration // The last sample received always closes the profile (back at the surface). var samples = samples if let latest, latest.time > (samples.last?.time ?? -1) { samples.append(latest) } + var dive = log(samples: samples) + if !marks.isEmpty { dive.marks = marks } + return dive + } + + 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, surfaceIntervalBefore: surfaceIntervalBefore, gas: c.gas, gradientFactors: c.deco.gradientFactors, water: c.water, diff --git a/Sources/RemoraDeco/DiveTracker.swift b/Sources/RemoraDeco/DiveTracker.swift @@ -64,6 +64,8 @@ public struct DiveTracker: Sendable { /// Repeat periods of the feedback (s, wall clock). public var alarmRepeat = 2.0 public var warningRepeat = 15.0 + /// A dive started by hand ends by itself when the diver has still not gone under after this long (s). + public var manualStartTimeout = 1800.0 public init() {} } @@ -83,6 +85,12 @@ public struct DiveTracker: Sendable { private var recorder: DiveRecorder? private var surfacedAt: Double? + /// Dive started by hand, at the surface. + private var manualStart = false + /// Surface pressure of the last reading received out of a dive. + private var lastSurfacePressure: Double? + /// Started by hand before any reading: the surface pressure is taken from the first one. + private var surfaceAssumed = false private var wentUnder = false private var safetyStop = DiveComputer.SafetyStop.notRequired private var lastReadingAt: Date? @@ -117,10 +125,18 @@ public struct DiveTracker: Sendable { guard !(isDiving && isSimulation) else { return [] } var events: [Event] = [] if !isDiving { + lastSurfacePressure = surface guard let absolute, absolute > surface + tuning.startOverpressure else { return [] } events += start(surfacePressure: surface, simulation: false, now: now, configuration: configuration(), logbook: logbook()) } + if surfaceAssumed, !wentUnder, let r = recorder { + // Started by hand before the sensor said anything: restart the computer on the real surface pressure. + recorder = DiveRecorder(configuration: configuration(), surfacePressure: surface, + start: r.start, logbook: logbook()) + surfacePressure = surface + } + surfaceAssumed = false lastReadingAt = now if let absolute { depthUnknown = false @@ -155,6 +171,32 @@ public struct DiveTracker: Sendable { return events } + /// Starts a real dive by hand, at the surface (the diver asked for the dive screens before getting in). + /// The dive clock runs from now; `tick` moves it forward while the source is silent. + public mutating func startManually(now: Date, configuration: DiveComputer.Configuration, + logbook: Logbook?) -> [Event] { + guard !isDiving else { return [] } + let surface = lastSurfacePressure + var events = start(surfacePressure: surface ?? standardSurfacePressure, simulation: false, now: now, + configuration: configuration, logbook: logbook) + manualStart = true + surfaceAssumed = surface == nil + lastPressure = surfacePressure + events += ingest(time: now.timeIntervalSinceReferenceDate, pressure: surfacePressure, now: now) + return events + } + + /// Marks the current point of the dive (kept in the dive log). Nil when no dive is in progress. + public mutating func mark(heading: Double? = nil) -> DiveMark? { + guard isDiving, var r = recorder else { return nil } + let mark = r.mark(heading: heading) + recorder = r + return mark + } + + /// Number of points marked in the dive in progress. + public var markCount: Int { recorder?.marks.count ?? 0 } + /// Ends the dive now (it also ends by itself after a minute at the surface). public mutating func end() -> [Event] { depthUnknown = false @@ -201,6 +243,8 @@ public struct DiveTracker: Sendable { lastAlarmCue = nil surfacedAt = nil wentUnder = false + manualStart = false + surfaceAssumed = false warnings = [] safetyStop = .notRequired status = nil @@ -254,6 +298,8 @@ public struct DiveTracker: Sendable { } else { surfacedAt = nil } + // Started by hand and never went under: give up after a while. + if isDiving, manualStart, !wentUnder, s.elapsed * 60 >= tuning.manualStartTimeout { events += end() } return events } } diff --git a/Sources/RemoraDeco/Logbook.swift b/Sources/RemoraDeco/Logbook.swift @@ -18,6 +18,20 @@ public struct DiveEvent: Sendable, Hashable, Codable { public let warning: DiveComputer.Warning } +/// A point marked by the diver during the dive (Action button): something to find again on the profile. +public struct DiveMark: Sendable, Hashable, Codable { + public let time: Double // s + public let depth: Double // m + /// Magnetic heading of the device at that moment, degrees (nil when unknown). + public let heading: Double? + + public init(time: Double, depth: Double, heading: Double? = nil) { + self.time = time + self.depth = depth + self.heading = heading + } +} + /// One dive of the logbook. public struct DiveLog: Sendable, Hashable, Codable, Identifiable { /// Where the dive comes from: recorded by Open Rémora, imported from a Subsurface logbook, @@ -43,6 +57,8 @@ public struct DiveLog: Sendable, Hashable, Codable, Identifiable { public var notes: String? public var latitude: Double? public var longitude: Double? + /// Points marked by the diver; nil in logbooks written before marks existed. + public var marks: [DiveMark]? public var duration: Double { samples.last?.time ?? 0 } // s public var maxDepth: Double { samples.map(\.depth).max() ?? 0 } diff --git a/Tests/RemoraDecoTests/DiveTrackerTests.swift b/Tests/RemoraDecoTests/DiveTrackerTests.swift @@ -254,3 +254,64 @@ struct DiveMonitorTests { #expect(cues.last == .diveSaved) } } + +@Suite("Dive started by hand") +struct ManualStartTests { + @Test func manualStartShowsADiveAtTheSurfaceAndRecordsTheDiveThatFollows() { + var s = Script() + s.stay(at: 0, seconds: 3) + s.events += s.tracker.startManually(now: s.now, configuration: s.configuration, logbook: nil) + #expect(s.tracker.isDiving) + #expect(s.tracker.status?.depth == 0) + // Silent sensor at the surface: the watchdog keeps the clock running. + s.silent(seconds: 60) + #expect((s.tracker.status?.elapsed ?? 0) > 0.8) + #expect(s.tracker.isDiving) + s.move(to: 10, rate: 18) + s.stay(at: 10, seconds: 120) + let mark = s.tracker.mark(heading: 237) + #expect(mark?.depth == 10) + #expect(s.tracker.markCount == 1) + s.move(to: 0, rate: 9) + s.stay(at: 0, seconds: 62) + #expect(!s.tracker.isDiving) + #expect(s.endedDive?.marks == mark.map { [$0] }) + #expect((s.endedDive?.maxDepth ?? 0) > 9.9) + } + + @Test func manualStartWithoutADiveGivesUpAndKeepsNothing() { + var s = Script() + s.tracker.tuning.manualStartTimeout = 300 + s.events += s.tracker.startManually(now: s.now, configuration: s.configuration, logbook: nil) + s.silent(seconds: 280) + #expect(s.tracker.isDiving) + s.silent(seconds: 40) + #expect(!s.tracker.isDiving) + #expect(s.events.last == .diveEnded(nil, simulation: false)) + } + + @Test func manualStartBeforeAnyReadingTakesTheSurfacePressureOfTheFirstOne() { + var s = Script() + s.events += s.tracker.startManually(now: s.now, configuration: s.configuration, logbook: nil) + s.seconds += 1 + // Mountain lake: 0.8 bar at the surface. + let lake = DepthScale(surfacePressure: 0.8) + let reading = DiveReading.pressure(time: s.now.timeIntervalSinceReferenceDate, surface: 0.8, + absolute: lake.pressure(atDepth: 5), submerged: true) + s.events += s.tracker.handle(reading, now: s.now, configuration: s.configuration, logbook: nil) + #expect(abs((s.tracker.status?.depth ?? 0) - 5) < 0.05) + } + + @Test func aLogbookWithoutMarksStillDecodes() throws { + var s = Script() + s.move(to: 5, rate: 18) + s.stay(at: 5, seconds: 30) + s.move(to: 0, rate: 9) + s.stay(at: 0, seconds: 62) + let dive = try #require(s.endedDive) + #expect(dive.marks == nil) + let data = try JSONEncoder().encode(dive) + #expect(!String(decoding: data, as: UTF8.self).contains("marks")) + #expect(try JSONDecoder().decode(DiveLog.self, from: data) == dive) + } +}