USB-Meter / USB Meter / Model / MeterCapabilities.swift
Newer Older
128 lines | 3.449kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  MeterCapabilities.swift
3
//  USB Meter
4
//
5
//  Created by Codex on 23/03/2026.
6
//
7

            
8
import SwiftUI
9

            
10
struct MeterCapabilities {
11
    let availableDataGroupIDs: [UInt8]
12
    let supportsDataGroupCommands: Bool
Bogdan Timofte authored 2 weeks ago
13
    let supportsRecordingView: Bool
Bogdan Timofte authored 2 weeks ago
14
    let supportsScreenSettings: Bool
15
    let supportsRecordingThreshold: Bool
16
    let supportsFahrenheit: Bool
17
    let supportsChargerDetection: Bool
18
    let chargerTypeDescriptions: [UInt16: String]
Bogdan Timofte authored 2 weeks ago
19
    let screenDescriptions: [UInt16: String]
Bogdan Timofte authored 2 weeks ago
20
    let dataGroupsHint: String?
21
    let recordingThresholdHint: String?
Bogdan Timofte authored 2 weeks ago
22

            
23
    func chargerTypeDescription(for index: UInt16) -> String {
24
        guard supportsChargerDetection else { return "-" }
25
        if let label = chargerTypeDescriptions[index] {
26
            return label
27
        }
28
        return index == 0 ? "Unknown" : "Unknown (\(index))"
29
    }
Bogdan Timofte authored 2 weeks ago
30

            
31
    func screenDescription(for index: UInt16) -> String? {
32
        screenDescriptions[index]
33
    }
Bogdan Timofte authored 2 weeks ago
34
}
35

            
36
extension MeterCapabilities {
37
    static let umFamily = MeterCapabilities(
38
        availableDataGroupIDs: Array(0...9),
39
        supportsDataGroupCommands: true,
Bogdan Timofte authored 2 weeks ago
40
        supportsRecordingView: true,
Bogdan Timofte authored 2 weeks ago
41
        supportsScreenSettings: true,
42
        supportsRecordingThreshold: true,
43
        supportsFahrenheit: true,
44
        supportsChargerDetection: true,
45
        chargerTypeDescriptions: [
46
            1: "QC2",
47
            2: "QC3",
48
            3: "Apple 2.4A",
49
            4: "Apple 2.1A",
50
            5: "Apple 1.0A",
51
            6: "Apple 0.5A",
52
            7: "DCP 1.5A",
53
            8: "Samsung"
Bogdan Timofte authored 2 weeks ago
54
        ],
55
        screenDescriptions: [
56
            0: "Main Measurement",
57
            1: "Quick Charge",
58
            2: "Charging Record",
59
            3: "Cable Impedance",
60
            4: "Graphing",
61
            5: "System Settings"
Bogdan Timofte authored 2 weeks ago
62
        ],
63
        dataGroupsHint: "Group 0 is temporary. Groups 1-9 persist across power cycles.",
64
        recordingThresholdHint: "Recording starts automatically when current rises above this threshold."
Bogdan Timofte authored 2 weeks ago
65
    )
66

            
67
    static let tc66c = MeterCapabilities(
68
        availableDataGroupIDs: [0, 1],
69
        supportsDataGroupCommands: false,
Bogdan Timofte authored 2 weeks ago
70
        supportsRecordingView: false,
Bogdan Timofte authored 2 weeks ago
71
        supportsScreenSettings: false,
72
        supportsRecordingThreshold: false,
73
        supportsFahrenheit: false,
74
        supportsChargerDetection: false,
Bogdan Timofte authored 2 weeks ago
75
        chargerTypeDescriptions: [:],
Bogdan Timofte authored 2 weeks ago
76
        screenDescriptions: [:],
77
        dataGroupsHint: nil,
78
        recordingThresholdHint: nil
Bogdan Timofte authored 2 weeks ago
79
    )
80
}
81

            
82
extension Model {
83
    static let byPeripheralName = Dictionary(
84
        uniqueKeysWithValues: allCases.flatMap { model in
85
            model.peripheralNames.map { ($0, model) }
86
        }
87
    )
88

            
89
    var radio: BluetoothRadio {
90
        switch self {
91
        case .UM25C, .UM34C:
92
            return .BT18
93
        case .TC66C:
94
            return .PW0316
95
        }
96
    }
97

            
98
    var peripheralNames: [String] {
99
        switch self {
100
        case .UM25C:
101
            return ["UM25C"]
102
        case .UM34C:
103
            return ["UM34C"]
104
        case .TC66C:
105
            return ["TC66C", "PW0316"]
106
        }
107
    }
108

            
109
    var color: Color {
110
        switch self {
111
        case .UM25C:
112
            return .blue
113
        case .UM34C:
114
            return .yellow
115
        case .TC66C:
116
            return .black
117
        }
118
    }
119

            
120
    var capabilities: MeterCapabilities {
121
        switch self {
122
        case .UM25C, .UM34C:
123
            return .umFamily
124
        case .TC66C:
125
            return .tc66c
126
        }
127
    }
128
}