Save Lora config values

This commit is contained in:
Garth Vander Houwen 2022-06-20 00:13:04 -07:00
parent 52d27aa09a
commit f4a7cc8bfd
2 changed files with 149 additions and 62 deletions

View file

@ -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())
}
}

View file

@ -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())
}
}