USB-Meter / USB Meter / Model / MeterCapabilities.swift
Newer Older
119 lines | 3.004kb
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
13
    let supportsScreenSettings: Bool
14
    let supportsRecordingThreshold: Bool
15
    let supportsFahrenheit: Bool
16
    let supportsChargerDetection: Bool
17
    let chargerTypeDescriptions: [UInt16: String]
Bogdan Timofte authored 2 weeks ago
18
    let screenDescriptions: [UInt16: String]
Bogdan Timofte authored 2 weeks ago
19

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

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

            
33
extension MeterCapabilities {
34
    static let umFamily = MeterCapabilities(
35
        availableDataGroupIDs: Array(0...9),
36
        supportsDataGroupCommands: true,
37
        supportsScreenSettings: true,
38
        supportsRecordingThreshold: true,
39
        supportsFahrenheit: true,
40
        supportsChargerDetection: true,
41
        chargerTypeDescriptions: [
42
            1: "QC2",
43
            2: "QC3",
44
            3: "Apple 2.4A",
45
            4: "Apple 2.1A",
46
            5: "Apple 1.0A",
47
            6: "Apple 0.5A",
48
            7: "DCP 1.5A",
49
            8: "Samsung"
Bogdan Timofte authored 2 weeks ago
50
        ],
51
        screenDescriptions: [
52
            0: "Main Measurement",
53
            1: "Quick Charge",
54
            2: "Charging Record",
55
            3: "Cable Impedance",
56
            4: "Graphing",
57
            5: "System Settings"
Bogdan Timofte authored 2 weeks ago
58
        ]
59
    )
60

            
61
    static let tc66c = MeterCapabilities(
62
        availableDataGroupIDs: [0, 1],
63
        supportsDataGroupCommands: false,
64
        supportsScreenSettings: false,
65
        supportsRecordingThreshold: false,
66
        supportsFahrenheit: false,
67
        supportsChargerDetection: false,
Bogdan Timofte authored 2 weeks ago
68
        chargerTypeDescriptions: [:],
69
        screenDescriptions: [:]
Bogdan Timofte authored 2 weeks ago
70
    )
71
}
72

            
73
extension Model {
74
    static let byPeripheralName = Dictionary(
75
        uniqueKeysWithValues: allCases.flatMap { model in
76
            model.peripheralNames.map { ($0, model) }
77
        }
78
    )
79

            
80
    var radio: BluetoothRadio {
81
        switch self {
82
        case .UM25C, .UM34C:
83
            return .BT18
84
        case .TC66C:
85
            return .PW0316
86
        }
87
    }
88

            
89
    var peripheralNames: [String] {
90
        switch self {
91
        case .UM25C:
92
            return ["UM25C"]
93
        case .UM34C:
94
            return ["UM34C"]
95
        case .TC66C:
96
            return ["TC66C", "PW0316"]
97
        }
98
    }
99

            
100
    var color: Color {
101
        switch self {
102
        case .UM25C:
103
            return .blue
104
        case .UM34C:
105
            return .yellow
106
        case .TC66C:
107
            return .black
108
        }
109
    }
110

            
111
    var capabilities: MeterCapabilities {
112
        switch self {
113
        case .UM25C, .UM34C:
114
            return .umFamily
115
        case .TC66C:
116
            return .tc66c
117
        }
118
    }
119
}