-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathCannedMessagesConfig.swift
More file actions
310 lines (297 loc) · 10.3 KB
/
Copy pathCannedMessagesConfig.swift
File metadata and controls
310 lines (297 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// CannedMessagesConfig.swift
// Meshtastic Apple
//
// Copyright (c) Garth Vander Houwen 6/22/22.
//
import MeshtasticProtobufs
import OSLog
import SwiftUI
struct CannedMessagesConfig: View {
@Environment(\.modelContext) private var context
@EnvironmentObject var accessoryManager: AccessoryManager
@Environment(\.dismiss) private var goBack
let node: NodeInfoEntity?
@State private var isPresentingSaveConfirm: Bool = false
@State var hasChanges = false
@State var hasMessagesChanges = false
@State var configPreset = 0
/// CannedMessageModule will sends a bell character with the messages.
@State var sendBell: Bool = false
/// Enable the rotary encoder #1. This is a 'dumb' encoder sending pulses on both A and B pins while rotating.
@State var rotary1Enabled = false
/// Enable the Up/Down/Select input device. Can be RAK rotary encoder or 3 buttons. Uses the a/b/press definitions from inputbroker.
@State var updown1Enabled: Bool = false
/// GPIO pin for rotary encoder A port.
@State var inputbrokerPinA = 0
/// GPIO pin for rotary encoder B port.
@State var inputbrokerPinB = 0
/// GPIO pin for rotary encoder Press port.
@State var inputbrokerPinPress = 0
/// Generate input event on CW of this kind.
@State var inputbrokerEventCw = 0
/// Generate input event on CCW of this kind.
@State var inputbrokerEventCcw = 0
/// Generate input event on Press of this kind.
@State var inputbrokerEventPress = 0
@State var messages = ""
var body: some View {
Form {
ConfigHeader(title: "Canned messages", config: \.cannedMessageConfig, node: node, onAppear: setCannedMessagesValues)
Section(header: Text("Options")) {
Toggle(isOn: $sendBell) {
Label("Send Bell", systemImage: "bell")
}
.tint(.accentColor)
Picker("Configuration Presets", selection: $configPreset ) {
ForEach(ConfigPresets.allCases) { cp in
Text(cp.description)
}
}
.padding(.top, 10)
.padding(.bottom, 10)
}
HStack {
Label("Messages", systemImage: "message.fill")
TextField("Messages separate with |", text: $messages, axis: .vertical)
.foregroundColor(.gray)
.autocapitalization(.none)
.disableAutocorrection(true)
.onChange(of: messages) {
var totalBytes = messages.utf8.count
// Only mess with the value if it is too big
while totalBytes > 200 {
messages = String(messages.dropLast())
totalBytes = messages.utf8.count
}
if messages != node?.cannedMessageConfig?.messages ?? "" {
hasChanges = true
hasMessagesChanges = true
}
}
.foregroundColor(.gray)
}
.keyboardType(.default)
Section(header: Text("Control Type")) {
Toggle(isOn: $rotary1Enabled) {
Label("Rotary 1", systemImage: "dial.min")
}
.tint(.accentColor)
.disabled(updown1Enabled)
Toggle(isOn: $updown1Enabled) {
Label("Up Down 1", systemImage: "arrow.up.arrow.down")
}
.tint(.accentColor)
.disabled(rotary1Enabled)
}
.disabled(configPreset > 0)
Section(header: Text("Inputs")) {
VStack(alignment: .leading) {
Picker("Pin A", selection: $inputbrokerPinA) {
ForEach(0..<49) {
if $0 == 0 {
Text("Unset")
} else {
Text("Pin \($0)")
}
}
}
Text("GPIO pin for rotary encoder A port.")
.foregroundColor(.gray)
.font(.callout)
}
VStack(alignment: .leading) {
Picker("Pin B", selection: $inputbrokerPinB) {
ForEach(0..<49) {
if $0 == 0 {
Text("Unset")
} else {
Text("Pin \($0)")
}
}
}
Text("GPIO pin for rotary encoder B port.")
.foregroundColor(.gray)
.font(.callout)
}
VStack(alignment: .leading) {
Picker("Press Pin", selection: $inputbrokerPinPress) {
ForEach(0..<49) {
if $0 == 0 {
Text("Unset")
} else {
Text("Pin \($0)")
}
}
}
Text("GPIO pin for rotary encoder Press port.")
.foregroundColor(.gray)
.font(.callout)
}
}
.disabled(configPreset > 0)
Section(header: Text("Key Mapping")) {
Picker("Clockwise Rotary Event", selection: $inputbrokerEventCw ) {
ForEach(InputEventChars.allCases) { iec in
Text(iec.description)
}
}
.padding(.top, 10)
.padding(.bottom, 10)
Picker("Counter Clockwise Rotary Event", selection: $inputbrokerEventCcw ) {
ForEach(InputEventChars.allCases) { iec in
Text(iec.description)
}
}
.padding(.top, 10)
.padding(.bottom, 10)
Picker("Encoder Press Event", selection: $inputbrokerEventPress ) {
ForEach(InputEventChars.allCases) { iec in
Text(iec.description)
}
}
.padding(.top, 10)
.padding(.bottom, 10)
}
.disabled(configPreset > 0)
}
.scrollDismissesKeyboard(.immediately)
.disabled(!accessoryManager.isConnected || node?.cannedMessageConfig == nil)
.safeAreaInset(edge: .bottom, alignment: .center) {
HStack(spacing: 0) {
SaveConfigButton(node: node, hasChanges: $hasChanges) {
guard let toUser = node?.user else { return }
if hasChanges {
performConfigSave(
node: node,
context: context,
accessoryManager: accessoryManager,
hasChanges: $hasChanges,
dismiss: goBack
) { fromUser, toUser in
var cmc = ModuleConfig.CannedMessageConfig()
// enabled is deprecated with no successor and removed from active
// use — intentionally not written. (#2021)
cmc.sendBell = sendBell
cmc.rotary1Enabled = rotary1Enabled
cmc.updown1Enabled = updown1Enabled
if rotary1Enabled {
cmc.allowInputSource = "rotEnc1"
} else if updown1Enabled {
cmc.allowInputSource = "upDown1"
} else {
cmc.allowInputSource = "_any"
}
cmc.inputbrokerPinA = UInt32(inputbrokerPinA)
cmc.inputbrokerPinB = UInt32(inputbrokerPinB)
cmc.inputbrokerPinPress = UInt32(inputbrokerPinPress)
cmc.inputbrokerEventCw = InputEventChars(rawValue: inputbrokerEventCw)!.protoEnumValue()
cmc.inputbrokerEventCcw = InputEventChars(rawValue: inputbrokerEventCcw)!.protoEnumValue()
cmc.inputbrokerEventPress = InputEventChars(rawValue: inputbrokerEventPress)!.protoEnumValue()
_ = try await accessoryManager.saveCannedMessageModuleConfig(config: cmc, fromUser: fromUser, toUser: toUser)
}
}
if hasMessagesChanges {
Task {
do {
_ = try await accessoryManager.saveCannedMessageModuleMessages(messages: messages, fromUser: toUser, toUser: toUser)
hasMessagesChanges = false
if !hasChanges {
Logger.transport.debug("[CannedMessagesConfig] sending wantConfig for save cannedMessagesConfig")
goBack()
}
} catch {
Logger.mesh.error("Unable to save canned message module messages")
}
}
}
}
}
}
.navigationTitle("Canned Messages Config")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
ConnectedDevice(deviceConnected: accessoryManager.isConnected, name: accessoryManager.activeConnection?.device.shortName ?? "?")
}
}
.onFirstAppear {
requestRemoteConfig(
node: node,
context: context,
accessoryManager: accessoryManager,
configIsNil: { $0.cannedMessageConfig == nil },
request: accessoryManager.requestCannedMessagesModuleConfig
)
}
.onChange(of: configPreset) { _, newPreset in
if newPreset == 1 {
// RAK Rotary Encoder
updown1Enabled = true
rotary1Enabled = false
inputbrokerPinA = 4
inputbrokerPinB = 10
inputbrokerPinPress = 9
inputbrokerEventCw = InputEventChars.down.rawValue
inputbrokerEventCcw = InputEventChars.up.rawValue
inputbrokerEventPress = InputEventChars.select.rawValue
} else if newPreset == 2 {
// CardKB / RAK Keypad
updown1Enabled = false
rotary1Enabled = false
inputbrokerPinA = 0
inputbrokerPinB = 0
inputbrokerPinPress = 0
inputbrokerEventCw = InputEventChars.none.rawValue
inputbrokerEventCcw = InputEventChars.none.rawValue
inputbrokerEventPress = InputEventChars.none.rawValue
}
hasChanges = true
}
.onChange(of: sendBell) { _, newSendBell in
if newSendBell != node?.cannedMessageConfig?.sendBell { hasChanges = true }
}
.onChange(of: rotary1Enabled) { _, newRotary1Enabled in
if newRotary1Enabled != node?.cannedMessageConfig?.rotary1Enabled { hasChanges = true }
}
.onChange(of: updown1Enabled) { _, newUpdown1Enabled in
if newUpdown1Enabled != node?.cannedMessageConfig?.updown1Enabled { hasChanges = true }
}
.onChange(of: inputbrokerPinA) { _, newPinA in
if newPinA != node?.cannedMessageConfig?.inputbrokerPinA ?? -1 { hasChanges = true }
}
.onChange(of: inputbrokerPinB) { _, newPinB in
if newPinB != node?.cannedMessageConfig?.inputbrokerPinB ?? -1 { hasChanges = true }
}
.onChange(of: inputbrokerPinPress) { _, newPinPress in
if newPinPress != node?.cannedMessageConfig?.inputbrokerPinPress ?? -1 { hasChanges = true }
}
.onChange(of: inputbrokerEventCw) { _, newKeyA in
if newKeyA != node?.cannedMessageConfig?.inputbrokerEventCw ?? -1 { hasChanges = true }
}
.onChange(of: inputbrokerEventCcw) { _, newKeyB in
if newKeyB != node?.cannedMessageConfig?.inputbrokerEventCcw ?? -1 { hasChanges = true }
}
.onChange(of: inputbrokerEventPress) { _, newKeyPress in
if newKeyPress != node?.cannedMessageConfig?.inputbrokerEventPress ?? -1 { hasChanges = true }
}
}
func setCannedMessagesValues() {
self.sendBell = node?.cannedMessageConfig?.sendBell ?? false
self.rotary1Enabled = node?.cannedMessageConfig?.rotary1Enabled ?? false
self.updown1Enabled = node?.cannedMessageConfig?.updown1Enabled ?? false
self.inputbrokerPinA = Int(node?.cannedMessageConfig?.inputbrokerPinA ?? 0)
self.inputbrokerPinB = Int(node?.cannedMessageConfig?.inputbrokerPinB ?? 0)
self.inputbrokerPinPress = Int(node?.cannedMessageConfig?.inputbrokerPinPress ?? 0)
self.inputbrokerEventCw = Int(node?.cannedMessageConfig?.inputbrokerEventCw ?? 0)
self.inputbrokerEventCcw = Int(node?.cannedMessageConfig?.inputbrokerEventCcw ?? 0)
self.inputbrokerEventPress = Int(node?.cannedMessageConfig?.inputbrokerEventPress ?? 0)
self.messages = node?.cannedMessageConfig?.messages ?? ""
self.hasChanges = false
self.hasMessagesChanges = false
}
}
#Preview {
CannedMessagesConfig(node: nil)
.environmentObject(AccessoryManager.shared)
.modelContainer(PersistenceController.preview.container)
}