From f4a7cc8bfd95a8eeab30e4fc7f1024a1b8afc814 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 20 Jun 2022 00:13:04 -0700 Subject: [PATCH] Save Lora config values --- .../Views/Settings/DeviceConfig.swift | 114 +++++++++--------- .../Views/Settings/LoRaConfig.swift | 97 ++++++++++++++- 2 files changed, 149 insertions(+), 62 deletions(-) diff --git a/MeshtasticApple/Views/Settings/DeviceConfig.swift b/MeshtasticApple/Views/Settings/DeviceConfig.swift index bff27d04..90fbe166 100644 --- a/MeshtasticApple/Views/Settings/DeviceConfig.swift +++ b/MeshtasticApple/Views/Settings/DeviceConfig.swift @@ -44,78 +44,74 @@ struct DeviceConfig: View { @State private var isPresentingFactoryResetConfirm: Bool = false var body: some View { - - ZStack { - VStack { + VStack { - Form { + Form { + + Section(header: Text("Options")) { - - Section(header: Text("Options")) { - - Picker("Device Role", selection: $deviceRole ) { - ForEach(DeviceRoles.allCases) { dr in - Text(dr.description) - } + Picker("Device Role", selection: $deviceRole ) { + ForEach(DeviceRoles.allCases) { dr in + Text(dr.description) } - .pickerStyle(InlinePickerStyle()) - .padding(.top, 10) - .padding(.bottom, 10) - } - - Section(header: Text("Debug")) { - - Toggle(isOn: $serialEnabled) { - - Label("Serial Console", systemImage: "terminal") - } - .toggleStyle(SwitchToggleStyle(tint: .accentColor)) - - Toggle(isOn: $debugLogEnabled) { - - Label("Debug Log", systemImage: "ant.fill") - } - .toggleStyle(SwitchToggleStyle(tint: .accentColor)) } + .pickerStyle(InlinePickerStyle()) + .padding(.top, 10) + .padding(.bottom, 10) } - Button("Factory Reset", role: .destructive) { + Section(header: Text("Debug")) { - isPresentingFactoryResetConfirm = true - } - .disabled(bleManager.connectedPeripheral == nil) - .buttonStyle(.bordered) - .buttonBorderShape(.capsule) - .controlSize(.large) - .padding() - .confirmationDialog( - "Are you sure?", - isPresented: $isPresentingFactoryResetConfirm - ) { - Button("Erase all device settings?", role: .destructive) { - - if !bleManager.sendFactoryReset(destNum: bleManager.connectedPeripheral.num, wantResponse: false) { - - print("Factory Reset Failed") - } + Toggle(isOn: $serialEnabled) { + + Label("Serial Console", systemImage: "terminal") } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + + Toggle(isOn: $debugLogEnabled) { + + Label("Debug Log", systemImage: "ant.fill") + } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) } - Spacer() } - .navigationTitle("Device Config") - .navigationBarItems(trailing: - - ZStack { - - ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?????") - }) - .onAppear { - - self.bleManager.context = context + Button("Factory Reset", role: .destructive) { + + isPresentingFactoryResetConfirm = true } - .navigationViewStyle(StackNavigationViewStyle()) + .disabled(bleManager.connectedPeripheral == nil) + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() + .confirmationDialog( + "Are you sure?", + isPresented: $isPresentingFactoryResetConfirm + ) { + Button("Erase all device settings?", role: .destructive) { + + if !bleManager.sendFactoryReset(destNum: bleManager.connectedPeripheral.num, wantResponse: false) { + + print("Factory Reset Failed") + } + } + } + Spacer() } + + .navigationTitle("Device Config") + .navigationBarItems(trailing: + + ZStack { + + ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "?????") + }) + .onAppear { + + self.bleManager.context = context + } + .navigationViewStyle(StackNavigationViewStyle()) } } diff --git a/MeshtasticApple/Views/Settings/LoRaConfig.swift b/MeshtasticApple/Views/Settings/LoRaConfig.swift index 0217b08c..f4c2abe1 100644 --- a/MeshtasticApple/Views/Settings/LoRaConfig.swift +++ b/MeshtasticApple/Views/Settings/LoRaConfig.swift @@ -56,6 +56,39 @@ enum RegionCodes : Int, CaseIterable, Identifiable { } } } + + func protoEnumValue() -> Config.LoRaConfig.RegionCode { + + switch self { + + case .unset: + return Config.LoRaConfig.RegionCode.unset + case .us: + return Config.LoRaConfig.RegionCode.us + case .eu433: + return Config.LoRaConfig.RegionCode.eu433 + case .eu868: + return Config.LoRaConfig.RegionCode.eu868 + case .cn: + return Config.LoRaConfig.RegionCode.cn + case .jp: + return Config.LoRaConfig.RegionCode.jp + case .anz: + return Config.LoRaConfig.RegionCode.anz + case .kr: + return Config.LoRaConfig.RegionCode.kr + case .tw: + return Config.LoRaConfig.RegionCode.tw + case .ru: + return Config.LoRaConfig.RegionCode.ru + case .in: + return Config.LoRaConfig.RegionCode.in + case .nz865: + return Config.LoRaConfig.RegionCode.nz865 + case .th: + return Config.LoRaConfig.RegionCode.th + } + } } enum ModemPresets : Int, CaseIterable, Identifiable { @@ -90,6 +123,27 @@ enum ModemPresets : Int, CaseIterable, Identifiable { } } } + func protoEnumValue() -> Config.LoRaConfig.ModemPreset { + + switch self { + + case .LongFast: + return Config.LoRaConfig.ModemPreset.longFast + case .LongSlow: + return Config.LoRaConfig.ModemPreset.longSlow + case .VLongSlow: + return Config.LoRaConfig.ModemPreset.vlongSlow + case .MidSlow: + return Config.LoRaConfig.ModemPreset.midSlow + case .MidFast: + return Config.LoRaConfig.ModemPreset.midFast + case .ShortSlow: + return Config.LoRaConfig.ModemPreset.shortSlow + case .ShortFast: + return Config.LoRaConfig.ModemPreset.shortFast + + } + } } struct LoRaConfig: View { @@ -98,8 +152,9 @@ struct LoRaConfig: View { @EnvironmentObject var bleManager: BLEManager @State var region = 1 - @State var modemPreset = 0 - @State var numberOfHops = 0 + @State var modemPreset = 0 + @State var hopLimit = 0 + @State var hasChanges = false var body: some View { @@ -133,7 +188,7 @@ struct LoRaConfig: View { } Section(header: Text("Mesh Options")) { - Picker("Number of hops", selection: $numberOfHops) { + Picker("Number of hops", selection: $hopLimit) { ForEach(0..<8) { if $0 == 0 { Text("Default") @@ -145,6 +200,29 @@ struct LoRaConfig: View { .pickerStyle(DefaultPickerStyle()) } } + + Button { + + var lc = Config.LoRaConfig() + lc.hopLimit = UInt32(hopLimit) + lc.region = RegionCodes(rawValue: region)!.protoEnumValue() + lc.modemPreset = ModemPresets(rawValue: modemPreset)!.protoEnumValue() + + if bleManager.saveLoRaConfig(config: lc, destNum: bleManager.connectedPeripheral.num, wantResponse: false) { + + } else { + + } + + } label: { + Label("Save", systemImage: "square.and.arrow.down") + } + .disabled(bleManager.connectedPeripheral == nil || !hasChanges) + .buttonStyle(.bordered) + .buttonBorderShape(.capsule) + .controlSize(.large) + .padding() + } .navigationTitle("LoRa Config") .navigationBarItems(trailing: @@ -157,6 +235,19 @@ struct LoRaConfig: View { self.bleManager.context = context } + .onChange(of: region) { newRegion in + + hasChanges = true + } + .onChange(of: modemPreset) { newModemPreset in + + hasChanges = true + } + .onChange(of: hopLimit) { newHopLimit in + + hasChanges = true + } + .navigationViewStyle(StackNavigationViewStyle()) } }