DemoProfile.swift (2033B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 4 /// Simulated profiles: dry demo and screenshots. 5 public struct DemoProfile: Sendable { 6 /// Segments: target depth (m), rate to get there (m/min), then time spent there (s). 7 public var segments: [(depth: Double, rate: Double, hold: Double)] 8 9 public init(segments: [(depth: Double, rate: Double, hold: Double)]) { self.segments = segments } 10 11 public static let shallow = DemoProfile(segments: [(8, 15, 600), (5, 6, 180), (0, 8, 0)]) 12 public static let reef = DemoProfile(segments: [(22, 18, 720), (12, 16, 300), (5, 8, 180), (0, 8, 0)]) 13 public static let deep = DemoProfile(segments: [(38, 20, 780), (15, 9, 120), (6, 9, 300), (0, 9, 0)]) 14 15 /// Depth at every second, including the return to the surface. 16 public var depths: [Double] { 17 var out: [Double] = [0] 18 var d = 0.0 19 for s in segments { 20 while abs(d - s.depth) > 1e-9 { 21 let step = s.rate / 60 22 d = d < s.depth ? min(s.depth, d + step) : max(s.depth, d - step) 23 out.append(d) 24 } 25 out.append(contentsOf: Array(repeating: d, count: Int(s.hold))) 26 } 27 return out 28 } 29 30 public static func temperature(atDepth depth: Double) -> Double { 21 - min(depth, 30) * 0.4 } 31 32 /// Records the whole profile at once (demo logbook). 33 public func record(start: Date, settings: UserSettings, logbook: Logbook) -> DiveLog { 34 let configuration = (try? settings.configuration()) ?? DiveComputer.Configuration() 35 var recorder = DiveRecorder(configuration: configuration, surfacePressure: standardSurfacePressure, 36 start: start, logbook: logbook) 37 let scale = recorder.computer.scale 38 for (t, d) in depths.enumerated() { 39 recorder.record(time: Double(t), pressure: scale.pressure(atDepth: d), 40 temperature: Self.temperature(atDepth: d)) 41 } 42 return recorder.finish() 43 } 44 }