2022-06-22 15:52:51 -07:00
|
|
|
//
|
|
|
|
|
// SerialConfig.swift
|
|
|
|
|
// Meshtastic Apple
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) Garth Vander Houwen 6/22/22.
|
|
|
|
|
//
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct SerialConfig: View {
|
|
|
|
|
|
|
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
|
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
var node: NodeInfoEntity?
|
2022-07-02 11:28:25 -07:00
|
|
|
|
2022-06-28 20:20:02 -07:00
|
|
|
@State private var isPresentingSaveConfirm: Bool = false
|
|
|
|
|
@State var initialLoad: Bool = true
|
|
|
|
|
@State var hasChanges = false
|
|
|
|
|
|
2022-06-22 15:52:51 -07: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
|
|
|
|
|
@State var mode = 0
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
|
|
|
|
|
VStack {
|
|
|
|
|
|
|
|
|
|
Form {
|
|
|
|
|
|
|
|
|
|
Section(header: Text("Options")) {
|
|
|
|
|
|
|
|
|
|
Toggle(isOn: $enabled) {
|
|
|
|
|
|
|
|
|
|
Label("Enabled", systemImage: "terminal")
|
|
|
|
|
}
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
|
|
|
|
|
|
|
|
|
Toggle(isOn: $echo) {
|
|
|
|
|
|
|
|
|
|
Label("Echo", systemImage: "repeat")
|
|
|
|
|
}
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2022-06-28 20:20:02 -07:00
|
|
|
Text("If set, any packets you send will be echoed back to your device.")
|
|
|
|
|
.font(.caption)
|
2022-06-22 15:52:51 -07:00
|
|
|
|
|
|
|
|
Picker("Baud Rate", selection: $baudRate ) {
|
|
|
|
|
ForEach(SerialBaudRates.allCases) { sbr in
|
|
|
|
|
Text(sbr.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
|
|
|
|
|
|
|
|
|
Picker("Timeout", selection: $timeout ) {
|
|
|
|
|
ForEach(SerialTimeoutIntervals.allCases) { sti in
|
|
|
|
|
Text(sti.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-28 20:20:02 -07:00
|
|
|
Text("The amount of time to wait before we consider your packet as done.")
|
|
|
|
|
.font(.caption)
|
2022-06-22 15:52:51 -07:00
|
|
|
|
|
|
|
|
Picker("Mode", selection: $mode ) {
|
|
|
|
|
ForEach(SerialModeTypes.allCases) { smt in
|
|
|
|
|
Text(smt.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
|
|
|
|
}
|
|
|
|
|
Section(header: Text("GPIO")) {
|
|
|
|
|
|
|
|
|
|
Picker("Receive data (rxd) GPIO pin", selection: $rxd) {
|
|
|
|
|
ForEach(0..<40) {
|
|
|
|
|
|
|
|
|
|
if $0 == 0 {
|
|
|
|
|
|
|
|
|
|
Text("Unset")
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
Text("Pin \($0)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
|
|
|
|
|
|
|
|
|
Picker("Transmit data (txd) GPIO pin", selection: $txd) {
|
|
|
|
|
ForEach(0..<40) {
|
|
|
|
|
|
|
|
|
|
if $0 == 0 {
|
|
|
|
|
|
|
|
|
|
Text("Unset")
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
Text("Pin \($0)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-28 20:20:02 -07:00
|
|
|
Text("Set the GPIO pins for RXD and TXD.")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-12 06:52:09 -07:00
|
|
|
.disabled(node == nil)
|
2022-06-28 20:20:02 -07:00
|
|
|
|
|
|
|
|
Button {
|
|
|
|
|
|
|
|
|
|
isPresentingSaveConfirm = true
|
|
|
|
|
|
|
|
|
|
} label: {
|
|
|
|
|
|
|
|
|
|
Label("Save", systemImage: "square.and.arrow.down")
|
|
|
|
|
}
|
2022-07-08 12:05:39 -07:00
|
|
|
.disabled(bleManager.connectedPeripheral == nil || !hasChanges || !(node!.myInfo?.hasWifi ?? false))
|
2022-06-28 20:20:02 -07:00
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
|
|
|
|
.padding()
|
|
|
|
|
.confirmationDialog(
|
|
|
|
|
|
|
|
|
|
"Are you sure?",
|
2022-10-04 18:19:02 -07:00
|
|
|
isPresented: $isPresentingSaveConfirm,
|
|
|
|
|
titleVisibility: .visible
|
2022-06-28 20:20:02 -07:00
|
|
|
) {
|
2022-07-02 13:43:13 -07:00
|
|
|
Button("Save Serial Module Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") {
|
2022-06-28 20:20:02 -07:00
|
|
|
|
|
|
|
|
var sc = ModuleConfig.SerialConfig()
|
|
|
|
|
sc.enabled = enabled
|
2022-07-02 11:28:25 -07:00
|
|
|
sc.echo = echo
|
|
|
|
|
sc.rxd = UInt32(rxd)
|
|
|
|
|
sc.txd = UInt32(txd)
|
|
|
|
|
sc.baud = SerialBaudRates(rawValue: baudRate)!.protoEnumValue()
|
|
|
|
|
sc.timeout = UInt32(timeout)
|
|
|
|
|
sc.mode = SerialModeTypes(rawValue: mode)!.protoEnumValue()
|
2022-06-28 20:20:02 -07:00
|
|
|
|
2022-08-11 23:34:09 -07:00
|
|
|
let adminMessageId = bleManager.saveSerialModuleConfig(config: sc, fromUser: node!.user!, toUser: node!.user!)
|
2022-07-02 13:43:13 -07:00
|
|
|
|
|
|
|
|
if adminMessageId > 0 {
|
2022-06-28 20:20:02 -07:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-28 20:20:02 -07:00
|
|
|
|
2022-06-22 15:52:51 -07:00
|
|
|
.navigationTitle("Serial Config")
|
|
|
|
|
.navigationBarItems(trailing:
|
|
|
|
|
|
|
|
|
|
ZStack {
|
|
|
|
|
|
2022-07-01 19:44:25 -07:00
|
|
|
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
|
2022-06-22 15:52:51 -07:00
|
|
|
})
|
|
|
|
|
.onAppear {
|
|
|
|
|
|
2022-07-02 11:28:25 -07:00
|
|
|
if self.initialLoad{
|
|
|
|
|
|
|
|
|
|
self.bleManager.context = context
|
|
|
|
|
|
2022-09-27 22:18:50 -07:00
|
|
|
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)
|
2022-07-02 11:28:25 -07:00
|
|
|
|
|
|
|
|
self.hasChanges = false
|
|
|
|
|
self.initialLoad = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.onChange(of: enabled) { newEnabled in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.serialConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newEnabled != node!.serialConfig!.enabled { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: echo) { newEcho in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.serialConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newEcho != node!.serialConfig!.echo { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: rxd) { newRxd in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.serialConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newRxd != node!.serialConfig!.rxd { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: txd) { newTxd in
|
|
|
|
|
|
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
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.serialConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newBaud != node!.serialConfig!.baudRate { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: timeout) { newTimeout in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.serialConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newTimeout != node!.serialConfig!.timeout { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 11:28:25 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: mode) { newMode in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.serialConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newMode != node!.serialConfig!.mode { hasChanges = true }
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
}
|
|
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|