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 9390d4d2066bf3d5245ec15208b410b3512b58cb
parent c2045844edbdf40a9cae4e08622153d0a43db2f8
Author: Jean Schurger <jean@schurger.org>
Date:   Fri, 13 Nov 2026 13:46:43 -0500

Diver settings: O2 percentage (nitrox 21-40%), conservatism, advanced gradient factors, ppO2, water type

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7deYGcjJYQu2t1EUEKgbs

Diffstat:
MSources/RemoraDeco/Physics.swift | 4++--
MSources/RemoraDeco/Tissues.swift | 2+-
ASources/RemoraDeco/UserSettings.swift | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ATests/RemoraDecoTests/SettingsTests.swift | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 151 insertions(+), 3 deletions(-)

diff --git a/Sources/RemoraDeco/Physics.swift b/Sources/RemoraDeco/Physics.swift @@ -2,7 +2,7 @@ import Foundation /// Unités du moteur : pressions en bar (absolues), profondeurs en mètres, durées en minutes. /// La montre fournit directement la pression ambiante : la profondeur n'est qu'un affichage. -public enum Water: Sendable, Hashable { +public enum Water: String, Sendable, Hashable, Codable, CaseIterable { case fresh // 1000 kg/m³ case en13319 // 1020 kg/m³, convention des ordinateurs de plongée (EN 13319) case salt // 1025 kg/m³ @@ -46,7 +46,7 @@ public struct DepthScale: Sendable, Hashable { } /// Mélange respiré. L'hélium n'est pas géré (plongée loisir à l'air ou au nitrox, 40 m au plus). -public struct Gas: Sendable, Hashable, CustomStringConvertible { +public struct Gas: Sendable, Hashable, Codable, CustomStringConvertible { public let o2: Double public var n2: Double { 1 - o2 } diff --git a/Sources/RemoraDeco/Tissues.swift b/Sources/RemoraDeco/Tissues.swift @@ -32,7 +32,7 @@ public enum ZHL16C { } /// Facteurs de gradient (Baker) : `low` fixe le premier palier, `high` la sortie en surface. -public struct GradientFactors: Sendable, Hashable, CustomStringConvertible { +public struct GradientFactors: Sendable, Hashable, Codable, CustomStringConvertible { public let low: Double public let high: Double diff --git a/Sources/RemoraDeco/UserSettings.swift b/Sources/RemoraDeco/UserSettings.swift @@ -0,0 +1,84 @@ +import Foundation + +/// Réglages du plongeur, tels que l'app les enregistre et les affiche. +/// Écran principal : mélange (pourcentage d'O2) et prudence. Réglages avancés : facteurs de gradient libres, ppO2, eau. +public struct UserSettings: Sendable, Hashable, Codable { + /// Niveaux de prudence proposés à l'écran principal (valeurs propres à Open Rémora). + public enum Conservatism: String, Sendable, Hashable, Codable, CaseIterable { + case high // GF 30/70, défaut + case medium // GF 40/80 + case low // GF 50/85 + case custom // réglages avancés + + public var gradientFactors: GradientFactors? { + switch self { + case .high: GradientFactors(low: 0.30, high: 0.70) + case .medium: GradientFactors(low: 0.40, high: 0.80) + case .low: GradientFactors(low: 0.50, high: 0.85) + case .custom: nil + } + } + } + + /// Bornes des réglages (l'interface ne propose rien en dehors). + public enum Limits { + public static let oxygenPercent = 21...40 // air à EAN40 : un seul mélange, 40 m au plus + public static let gfLowPercent = 10...100 + public static let gfHighPercent = 50...100 + public static let ppO2Max = 1.2...1.6 + } + + public var oxygenPercent: Int = 21 + public var conservatism: Conservatism = .high + /// Facteurs de gradient des réglages avancés (en %), utilisés quand `conservatism == .custom`. + public var customGFLowPercent: Int = 30 + public var customGFHighPercent: Int = 70 + public var ppO2Max: Double = 1.4 + public var water: Water = .en13319 + + public init() {} + + public enum Problem: Error, Hashable, Sendable { + case oxygenOutOfRange + case gfLowOutOfRange, gfHighOutOfRange, gfLowAboveHigh + case ppO2OutOfRange + } + + /// Ce qui empêche d'appliquer les réglages (vide = valides). + public var problems: [Problem] { + var p: [Problem] = [] + if !Limits.oxygenPercent.contains(oxygenPercent) { p.append(.oxygenOutOfRange) } + if conservatism == .custom { + if !Limits.gfLowPercent.contains(customGFLowPercent) { p.append(.gfLowOutOfRange) } + if !Limits.gfHighPercent.contains(customGFHighPercent) { p.append(.gfHighOutOfRange) } + if customGFLowPercent > customGFHighPercent { p.append(.gfLowAboveHigh) } + } + if !Limits.ppO2Max.contains(ppO2Max) { p.append(.ppO2OutOfRange) } + return p + } + + public var gas: Gas { Gas(o2: Double(oxygenPercent) / 100) } + + public var gradientFactors: GradientFactors { + conservatism.gradientFactors + ?? GradientFactors(low: Double(customGFLowPercent) / 100, high: Double(customGFHighPercent) / 100) + } + + /// Profondeur maximale d'utilisation du mélange choisi (affichée à côté du pourcentage). + public func maximumOperatingDepth(surfacePressure: Double = standardSurfacePressure) -> Double { + gas.maximumOperatingDepth(ppO2Limit: ppO2Max, + scale: DepthScale(surfacePressure: surfacePressure, water: water)) + } + + /// Configuration de l'ordinateur ; refusée si les réglages ne sont pas valides. + public func configuration() throws -> DiveComputer.Configuration { + if let first = problems.first { throw first } + var c = DiveComputer.Configuration() + c.gas = gas + c.water = water + c.deco.gradientFactors = gradientFactors + c.ppO2Warning = ppO2Max + c.ppO2Alarm = max(1.6, ppO2Max) + return c + } +} diff --git a/Tests/RemoraDecoTests/SettingsTests.swift b/Tests/RemoraDecoTests/SettingsTests.swift @@ -0,0 +1,64 @@ +import Foundation +import Testing +@testable import RemoraDeco + +@Suite("Réglages") +struct SettingsTests { + @Test func defaultsAreAirAndConservative() throws { + let c = try UserSettings().configuration() + #expect(c.gas == .air) + #expect(c.deco.gradientFactors == GradientFactors(low: 0.30, high: 0.70)) + #expect(c.ppO2Warning == 1.4) + } + + @Test func nitroxPercentSetsGasAndMOD() throws { + var s = UserSettings() + s.oxygenPercent = 32 + s.water = .salt + #expect(try s.configuration().gas == .nitrox(32)) + #expect(abs(s.maximumOperatingDepth() - 33.4) < 0.3) + } + + @Test func customGradientFactorsFromAdvancedSettings() throws { + var s = UserSettings() + s.conservatism = .custom + s.customGFLowPercent = 45 + s.customGFHighPercent = 90 + #expect(try s.configuration().deco.gradientFactors == GradientFactors(low: 0.45, high: 0.90)) + s.conservatism = .medium // les valeurs avancées restent en mémoire mais ne s'appliquent plus + #expect(try s.configuration().deco.gradientFactors == GradientFactors(low: 0.40, high: 0.80)) + } + + @Test func invalidSettingsAreRefused() { + var s = UserSettings() + s.oxygenPercent = 50 + s.conservatism = .custom + s.customGFLowPercent = 80 + s.customGFHighPercent = 60 + s.ppO2Max = 1.8 + #expect(Set(s.problems) == [.oxygenOutOfRange, .gfLowAboveHigh, .ppO2OutOfRange]) + #expect(throws: UserSettings.Problem.self) { try s.configuration() } + } + + @Test func roundTripsThroughJSON() throws { + var s = UserSettings() + s.oxygenPercent = 36 + s.conservatism = .custom + s.customGFLowPercent = 35 + let data = try JSONEncoder().encode(s) + #expect(try JSONDecoder().decode(UserSettings.self, from: data) == s) + } + + @Test func nitroxShortensDecoAtSameProfile() throws { + func tts(_ o2: Int) throws -> Double { + var s = UserSettings(); s.oxygenPercent = o2 + let c = try s.configuration() + let scale = DepthScale(water: c.water) + var t = TissueState.saturated() + t.stay(atPressure: scale.pressure(atDepth: 28), gas: c.gas, minutes: 40) + return AscentPlanner.plan(tissues: t, pressure: scale.pressure(atDepth: 28), gas: c.gas, + scale: scale, settings: c.deco).timeToSurface + } + #expect(try tts(32) < tts(21)) + } +}