Meshtastic-Apple/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift

218 lines
6.4 KiB
Swift
Raw Normal View History

//
// SerialConfig.swift
// Meshtastic Apple
//
// Copyright (c) Garth Vander Houwen 6/22/22.
//
import SwiftUI
struct SerialConfig: View {
2023-03-06 10:33:18 -08:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
2023-03-06 10:33:18 -08:00
var node: NodeInfoEntity?
2023-03-06 10:33:18 -08:00
2022-06-28 20:20:02 -07:00
@State private var isPresentingSaveConfirm: Bool = false
@State var hasChanges = false
2023-03-06 10:33:18 -08:00
@State var enabled = false
@State var echo = false
@State var rxd = 0
@State var txd = 0
@State var baudRate = 0
@State var timeout = 0
2023-12-30 09:21:42 -08:00
@State var overrideConsoleSerialPort = false
@State var mode = 0
var body: some View {
VStack {
Form {
ConfigHeader(title: "Serial", config: \.serialConfig, node: node, onAppear: setSerialValues)
2022-12-13 08:47:14 -08:00
Section(header: Text("options")) {
2023-03-06 10:33:18 -08:00
Toggle(isOn: $enabled) {
2022-12-13 08:47:14 -08:00
Label("enabled", systemImage: "terminal")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2023-03-06 10:33:18 -08:00
Toggle(isOn: $echo) {
Label("echo", systemImage: "repeat")
Text("If set, any packets you send will be echoed back to your device.")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2023-03-06 10:33:18 -08:00
Picker("Baud", selection: $baudRate ) {
ForEach(SerialBaudRates.allCases) { sbr in
Text(sbr.description)
}
}
.pickerStyle(DefaultPickerStyle())
.listRowSeparator(/*@START_MENU_TOKEN@*/.visible/*@END_MENU_TOKEN@*/)
Picker("timeout", selection: $timeout ) {
ForEach(SerialTimeoutIntervals.allCases) { sti in
Text(sti.description)
}
}
.pickerStyle(DefaultPickerStyle())
.listRowSeparator(.hidden)
2022-06-28 20:20:02 -07:00
Text("The amount of time to wait before we consider your packet as done.")
.foregroundColor(.gray)
.font(.callout)
2023-03-06 10:33:18 -08:00
Picker("mode", selection: $mode ) {
ForEach(SerialModeTypes.allCases) { smt in
Text(smt.description)
}
}
.pickerStyle(DefaultPickerStyle())
}
Section(header: Text("GPIO")) {
2023-03-06 10:33:18 -08:00
Picker("Receive data (rxd) GPIO pin", selection: $rxd) {
2024-01-22 13:21:17 -08:00
ForEach(0..<49) {
if $0 == 0 {
2022-12-30 17:44:39 -08:00
Text("unset")
} else {
Text("Pin \($0)")
}
}
}
.pickerStyle(DefaultPickerStyle())
.listRowSeparator(.visible)
Picker("Transmit data (txd) GPIO pin", selection: $txd) {
2024-01-22 13:21:17 -08:00
ForEach(0..<49) {
if $0 == 0 {
2022-12-30 17:44:39 -08:00
Text("unset")
} else {
Text("Pin \($0)")
}
}
}
.pickerStyle(DefaultPickerStyle())
.listRowSeparator(.hidden)
2022-06-28 20:20:02 -07:00
Text("Set the GPIO pins for RXD and TXD.")
.foregroundColor(.gray)
.font(.callout)
2022-06-28 20:20:02 -07:00
}
}
2023-02-01 09:19:45 -08:00
.disabled(self.bleManager.connectedPeripheral == nil || node?.serialConfig == nil)
2023-03-06 10:33:18 -08:00
SaveConfigButton(node: node, hasChanges: $hasChanges) {
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
if connectedNode != nil {
var sc = ModuleConfig.SerialConfig()
sc.enabled = enabled
sc.echo = echo
sc.rxd = UInt32(rxd)
sc.txd = UInt32(txd)
sc.baud = SerialBaudRates(rawValue: baudRate)!.protoEnumValue()
sc.timeout = UInt32(timeout)
sc.overrideConsoleSerialPort = overrideConsoleSerialPort
sc.mode = SerialModeTypes(rawValue: mode)!.protoEnumValue()
let adminMessageId = bleManager.saveSerialModuleConfig(config: sc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
if adminMessageId > 0 {
// Should show a saved successfully alert once I know that to be true
// for now just disable the button after a successful save
hasChanges = false
goBack()
2022-06-28 20:20:02 -07:00
}
}
}
2022-12-12 22:33:06 -08:00
.navigationTitle("serial.config")
.navigationBarItems(trailing:
ZStack {
2023-09-02 17:37:35 -07:00
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?")
})
.onAppear {
2024-01-14 11:25:00 -08:00
if self.bleManager.context == nil {
self.bleManager.context = context
}
setSerialValues()
2023-02-01 09:19:45 -08:00
// Need to request a SerialModuleConfig from the remote node before allowing changes
if bleManager.connectedPeripheral != nil && node?.serialConfig == nil {
logger.debug("empty serial module config")
2023-02-01 09:19:45 -08:00
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
2023-03-05 14:40:07 -08:00
if node != nil && connectedNode != nil {
_ = bleManager.requestSerialModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
2023-02-01 09:19:45 -08:00
}
}
2022-10-19 14:11:36 -07:00
2022-07-02 11:28:25 -07:00
}
.onChange(of: enabled) { newEnabled in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if newEnabled != node!.serialConfig!.enabled { hasChanges = true }
}
2022-07-02 11:28:25 -07:00
}
.onChange(of: echo) { newEcho in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if newEcho != node!.serialConfig!.echo { hasChanges = true }
}
2022-07-02 11:28:25 -07:00
}
.onChange(of: rxd) { newRxd in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if newRxd != node!.serialConfig!.rxd { hasChanges = true }
}
2022-07-02 11:28:25 -07:00
}
.onChange(of: txd) { newTxd in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
if newTxd != node!.serialConfig!.txd { hasChanges = true }
}
2022-07-02 11:28:25 -07:00
}
.onChange(of: baudRate) { newBaud in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if newBaud != node!.serialConfig!.baudRate { hasChanges = true }
}
2022-07-02 11:28:25 -07:00
}
.onChange(of: timeout) { newTimeout in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if newTimeout != node!.serialConfig!.timeout { hasChanges = true }
}
2022-07-02 11:28:25 -07:00
}
2023-12-30 09:21:42 -08:00
.onChange(of: overrideConsoleSerialPort) { newOverrideConsoleSerialPort in
if node != nil && node!.serialConfig != nil {
if newOverrideConsoleSerialPort != node!.serialConfig!.overrideConsoleSerialPort { hasChanges = true }
}
}
2022-07-02 11:28:25 -07:00
.onChange(of: mode) { newMode in
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if node != nil && node!.serialConfig != nil {
2023-03-06 10:33:18 -08:00
2022-07-11 16:18:16 -07:00
if newMode != node!.serialConfig!.mode { hasChanges = true }
}
}
}
}
func setSerialValues() {
self.enabled = node?.serialConfig?.enabled ?? false
self.echo = node?.serialConfig?.echo ?? false
self.rxd = Int(node?.serialConfig?.rxd ?? 0)
self.txd = Int(node?.serialConfig?.txd ?? 0)
self.baudRate = Int(node?.serialConfig?.baudRate ?? 0)
self.timeout = Int(node?.serialConfig?.timeout ?? 0)
self.mode = Int(node?.serialConfig?.mode ?? 0)
2023-12-30 09:21:42 -08:00
self.overrideConsoleSerialPort = false // node?.serialConfig?.overrideConsoleSerialPort ?? false
self.hasChanges = false
}
}