From 272e58289a086ebd9dd0d8a694b72a47d69320b7 Mon Sep 17 00:00:00 2001 From: Justas Date: Mon, 14 Dec 2020 17:32:10 +0200 Subject: [PATCH] Implement MavFraming selection and Params saving --- SiKGUIWPF/Helpers.cs | 7 ++++ SiKGUIWPF/MainWindow.xaml | 8 +++-- SiKGUIWPF/MainWindow.xaml.cs | 7 ++-- SiKGUIWPF/MavVerToIdConverter.cs | 40 +++++++++++++++++++++ SiKLink/Constants.cs | 4 +-- SiKLink/SiKConfig.cs | 3 +- SiKLink/SiKInterface.cs | 61 ++++++++++++++++++++++++++++---- 7 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 SiKGUIWPF/MavVerToIdConverter.cs diff --git a/SiKGUIWPF/Helpers.cs b/SiKGUIWPF/Helpers.cs index e33c7d5..3f91532 100644 --- a/SiKGUIWPF/Helpers.cs +++ b/SiKGUIWPF/Helpers.cs @@ -19,11 +19,18 @@ You should have received a copy of the GNU Lesser General Public License using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; +using System.Windows.Controls; namespace SiKGUIWPF { class Helpers { public static ReadOnlyCollection SerialRates = new ReadOnlyCollection(new int[] { 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400 }); + public static ReadOnlyCollection MavVersions = new ReadOnlyCollection(new ComboBoxItem[] + { + new ComboBoxItem(){Content = "MavLink 1"}, + new ComboBoxItem(){Content = "MavLink 2"}, + new ComboBoxItem(){Content = "MavLink 2 Low Latency"} + }); } } diff --git a/SiKGUIWPF/MainWindow.xaml b/SiKGUIWPF/MainWindow.xaml index fee22c5..d1559de 100644 --- a/SiKGUIWPF/MainWindow.xaml +++ b/SiKGUIWPF/MainWindow.xaml @@ -22,7 +22,10 @@ along with this program.If not, see . xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SiKGUIWPF" mc:Ignorable="d" - Title="SiK Radio Configurator" Height="300" Width="650"> + Title="SiK Radio Configurator" Height="300" Width="700"> + + + @@ -73,7 +76,7 @@ along with this program.If not, see . @@ -151,3 +154,4 @@ along with this program.If not, see . + \ No newline at end of file diff --git a/SiKGUIWPF/MainWindow.xaml.cs b/SiKGUIWPF/MainWindow.xaml.cs index 6fd293f..59ab179 100644 --- a/SiKGUIWPF/MainWindow.xaml.cs +++ b/SiKGUIWPF/MainWindow.xaml.cs @@ -104,7 +104,7 @@ public MainWindow() foreach (var item in SiKLink.Constants.SiKSerialRates) SiKSerialSpeed.Items.Add(item); - foreach (var item in SiKLink.Constants.MavlinkFrame) + foreach (var item in Helpers.MavVersions) MavlinkFrame.Items.Add(item); foreach (var item in SiKLink.Constants.AirPower) @@ -119,7 +119,6 @@ public MainWindow() foreach (var item in Enumerable.Range(33, 99)) MaxWnd.Items.Add(item); - SiKConfig = _sikInterface.SiKConfig; } /// @@ -176,7 +175,7 @@ private void Button_ConnectClick(object sender, RoutedEventArgs e) } } - // Are we in the comman mode already? + // Are we in the command mode already? var in_command = _sikInterface.CheckCommandMode(); if (!in_command) { @@ -221,7 +220,7 @@ private void Button_ReadValuesClick(object sender, RoutedEventArgs e) private void Button_WriteValuesClick(object sender, RoutedEventArgs e) { - + _sikInterface.SaveParameters(); } private void Button_SaveEepromClick(object sender, RoutedEventArgs e) { diff --git a/SiKGUIWPF/MavVerToIdConverter.cs b/SiKGUIWPF/MavVerToIdConverter.cs new file mode 100644 index 0000000..1d00fb6 --- /dev/null +++ b/SiKGUIWPF/MavVerToIdConverter.cs @@ -0,0 +1,40 @@ +/* +SiK Link - GUI and control library for SiK radios. +Copyright(C) 2020 J. Poderys + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program.If not, see. +*/ +using System; +using System.Globalization; +using System.Windows.Controls; +using System.Windows.Data; + +namespace SiKGUIWPF +{ + [ValueConversion(typeof(int), typeof(ComboBoxItem))] + class MavVerToIdConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + int numeric_id = (int)value; + return Helpers.MavVersions[numeric_id]; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + ComboBoxItem item = (ComboBoxItem)value; + return Helpers.MavVersions.IndexOf(item); + } + } +} diff --git a/SiKLink/Constants.cs b/SiKLink/Constants.cs index 9daabc7..c0bb4ac 100644 --- a/SiKLink/Constants.cs +++ b/SiKLink/Constants.cs @@ -22,7 +22,7 @@ namespace SiKLink { public class Constants { - public static ReadOnlyCollection AirRates = new ReadOnlyCollection(new int[]{ 2, 4, 8, 16, 19, 24, 32, 48, 64, 96, 128, 192, 250 }); + public static ReadOnlyCollection AirRates = new ReadOnlyCollection(new int[] { 2, 4, 8, 16, 19, 24, 32, 48, 64, 96, 128, 192, 250 }); public static ReadOnlyCollection MavlinkFrame = new ReadOnlyCollection(new int[] { 0, 1, 2 }); public static ReadOnlyCollection SiKSerialRates = new ReadOnlyCollection(new int[] { 1, 2, 4, 9, 19, 38, 57, 115, 230 }); public static ReadOnlyCollection AirPower = new ReadOnlyCollection(new int[] { 1, 2, 5, 8, 11, 14, 17, 20 }); @@ -46,5 +46,5 @@ public enum SikParameters RTSCTS, MAX_WINDOW } - } + } } diff --git a/SiKLink/SiKConfig.cs b/SiKLink/SiKConfig.cs index 598a662..779e6f2 100644 --- a/SiKLink/SiKConfig.cs +++ b/SiKLink/SiKConfig.cs @@ -44,12 +44,11 @@ public class SiKConfig : INotifyPropertyChanged private string _boardFrequency; private string _bootloaderVersion; - // // ATS parameters https://github.com/ArduPilot/SiK/blob/master/Firmware/radio/parameters.h // Defaults: //{"FORMAT", PARAM_FORMAT_CURRENT}, //{"SERIAL_SPEED", 57}, // match APM default of 57600 - //{"AIR_SPEED", 64}, // relies on MAVLink flow control + //{"AIR_SPEED", 64}, // relies on MAVLink flow control //{"NETID", 25}, //{"TXPOWER", 20}, //{"ECC", 0}, diff --git a/SiKLink/SiKInterface.cs b/SiKLink/SiKInterface.cs index c93cdbe..a3fd75b 100644 --- a/SiKLink/SiKInterface.cs +++ b/SiKLink/SiKInterface.cs @@ -55,7 +55,7 @@ public bool PortConnected /// /// SiK radio in Command mode. /// - public bool CommandMode { get; private set; } = false; + public bool CommandMode { get; private set; } = false; /// /// Configuration parameters of the local SiK board. /// @@ -98,7 +98,7 @@ public bool Connect(string port, int baudrate) { return false; } - + return true; } @@ -229,7 +229,7 @@ public bool RebootRadio() { return false; } - + } /// /// Save current parameters to the EEPROM @@ -250,7 +250,7 @@ public bool SaveToEEPROM() { return true; } - else + else { return false; } @@ -290,7 +290,7 @@ public bool ReadEEPROMData() return false; } - foreach(var line in params_list) + foreach (var line in params_list) { // Skip echo if (line.StartsWith("AT")) @@ -299,7 +299,7 @@ public bool ReadEEPROMData() var tokens = line.Split(':'); var param_id = tokens[0]; var param_val = tokens[1].Split("=")[1]; - + switch (param_id) { case "S0": @@ -358,6 +358,55 @@ public bool ReadEEPROMData() return true; } /// + /// Transfer all parameter values to the radio. + /// + /// This function does not save parameters to the EEPROM! + /// true on success + public bool SaveParameters() + { + try + { + if (!WriteParameter(Constants.SikParameters.FORMAT, SiKConfig.ParameterFormat)) + return false; + if (!WriteParameter(Constants.SikParameters.SERIAL_SPEED, SiKConfig.SerialSpeed)) + return false; + if (!WriteParameter(Constants.SikParameters.AIR_SPEED, SiKConfig.AirSpeed)) + return false; + if (!WriteParameter(Constants.SikParameters.NETID, SiKConfig.NetworkID)) + return false; + if (!WriteParameter(Constants.SikParameters.TXPOWER, SiKConfig.TxPower)) + return false; + if (!WriteParameter(Constants.SikParameters.ECC, SiKConfig.ECC)) + return false; + if (!WriteParameter(Constants.SikParameters.MAVLINK, SiKConfig.MavlinkMode)) + return false; + if (!WriteParameter(Constants.SikParameters.OPPRESEND, SiKConfig.OpportunisticResend)) + return false; + if (!WriteParameter(Constants.SikParameters.MIN_FREQ, SiKConfig.MinFrequency)) + return false; + if (!WriteParameter(Constants.SikParameters.MAX_FREQ, SiKConfig.MaxFrequency)) + return false; + if (!WriteParameter(Constants.SikParameters.NUM_CHANNELS, SiKConfig.NumChannels)) + return false; + if (!WriteParameter(Constants.SikParameters.DUTY_CYCLE, SiKConfig.DutyCycle)) + return false; + if (!WriteParameter(Constants.SikParameters.LBT_RSSI, SiKConfig.LbtRssiThreshold)) + return false; + if (!WriteParameter(Constants.SikParameters.MANCHESTER, SiKConfig.ManchesterEncoding)) + return false; + if (!WriteParameter(Constants.SikParameters.RTSCTS, SiKConfig.UseRtsCts)) + return false; + if (!WriteParameter(Constants.SikParameters.MAX_WINDOW, SiKConfig.MaxWindowSize)) + return false; + + return true; + } + catch + { + return false; + } + } + /// /// Set SiK radio parameter value /// /// Parameter number