From 8c2341405dca600fed438aa7091f8838e398e177 Mon Sep 17 00:00:00 2001 From: IlyaChernov Date: Sun, 20 Sep 2020 00:25:31 +0300 Subject: [PATCH] Fix for app stability Jogging speed and distance selectors replaced with Non linear sliders --- grbl.Master.Model/ApplicationSettings.cs | 1 + grbl.Master.Model/CommandSource.cs | 11 +- .../Properties/Settings.Designer.cs | 14 +- .../Properties/Settings.settings | 3 + grbl.Master.Model/app.config | 3 + .../ApplicationSettingsService.cs | 4 + grbl.Master.UI/App.xaml | 1 + grbl.Master.UI/Converters/NonLinearDouble.cs | 44 ++++++ grbl.Master.UI/ViewModels/MasterViewModel.cs | 65 ++++++++- grbl.Master.UI/Views/MasterView.xaml | 132 ++++++++++-------- grbl.Master.UI/grbl.Master.UI.csproj | 2 + 11 files changed, 215 insertions(+), 65 deletions(-) create mode 100644 grbl.Master.UI/Converters/NonLinearDouble.cs diff --git a/grbl.Master.Model/ApplicationSettings.cs b/grbl.Master.Model/ApplicationSettings.cs index 21a9f00..5f4d297 100644 --- a/grbl.Master.Model/ApplicationSettings.cs +++ b/grbl.Master.Model/ApplicationSettings.cs @@ -8,6 +8,7 @@ public class ApplicationSettings : NotifyPropertyChanged public double JoggingDistance { get; set; } public ObservableCollection FeedRates { get; set; } = new ObservableCollection { 5, 10, 50, 100, 500, 1000 }; + public double SliderLinearity { get; set; } public double FeedRate { get; set; } diff --git a/grbl.Master.Model/CommandSource.cs b/grbl.Master.Model/CommandSource.cs index 3fa25a6..e5300fe 100644 --- a/grbl.Master.Model/CommandSource.cs +++ b/grbl.Master.Model/CommandSource.cs @@ -7,6 +7,7 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; + using System.Threading; public class CommandSource : NotifyPropertyChanged { @@ -14,6 +15,8 @@ public class CommandSource : NotifyPropertyChanged private bool _needsPurge; + private readonly SynchronizationContext _uiContext; + private ConcurrentQueue CommandQueue { get; } = new ConcurrentQueue(); public TimeSpan Elapsed => _stopWatch.Elapsed; @@ -31,6 +34,7 @@ public class CommandSource : NotifyPropertyChanged public CommandSource(CommandSourceType type, CommandSourceRunMode mode) { + _uiContext = SynchronizationContext.Current; Type = type; Mode = mode; } @@ -107,7 +111,12 @@ public void Purge() { CommandQueue.TryDequeue(out var dummy); } - CommandList.Clear(); + + _uiContext.Send( + state => + { + CommandList.Clear(); + }, null); _stopWatch.Reset(); } diff --git a/grbl.Master.Model/Properties/Settings.Designer.cs b/grbl.Master.Model/Properties/Settings.Designer.cs index 0cf0f03..f3feecb 100644 --- a/grbl.Master.Model/Properties/Settings.Designer.cs +++ b/grbl.Master.Model/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace grbl.Master.Model.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.4.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")] public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -106,5 +106,17 @@ public sealed partial class Settings : global::System.Configuration.ApplicationS this["JoggingSpeed"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("1")] + public double SliderLinearity { + get { + return ((double)(this["SliderLinearity"])); + } + set { + this["SliderLinearity"] = value; + } + } } } diff --git a/grbl.Master.Model/Properties/Settings.settings b/grbl.Master.Model/Properties/Settings.settings index cefe383..912feb2 100644 --- a/grbl.Master.Model/Properties/Settings.settings +++ b/grbl.Master.Model/Properties/Settings.settings @@ -23,5 +23,8 @@ 1000 + + 1 + \ No newline at end of file diff --git a/grbl.Master.Model/app.config b/grbl.Master.Model/app.config index 4fdacd9..cb3d769 100644 --- a/grbl.Master.Model/app.config +++ b/grbl.Master.Model/app.config @@ -48,6 +48,9 @@ 1000 + + 1 + diff --git a/grbl.Master.Service/ApplicationSettingsService.cs b/grbl.Master.Service/ApplicationSettingsService.cs index 59d61f8..b3a51c4 100644 --- a/grbl.Master.Service/ApplicationSettingsService.cs +++ b/grbl.Master.Service/ApplicationSettingsService.cs @@ -42,6 +42,8 @@ public void Save() Model.Properties.Settings.Default.JoggingSpeed = Settings.FeedRate; + Model.Properties.Settings.Default.SliderLinearity = Settings.SliderLinearity; + Model.Properties.Settings.Default.Save(); Model.Properties.Settings.Default.Reload(); } @@ -114,6 +116,8 @@ public void Load() Settings.JoggingDistance = Model.Properties.Settings.Default.JoggingDistance; Settings.FeedRate = Model.Properties.Settings.Default.JoggingSpeed; + + Settings.SliderLinearity = Model.Properties.Settings.Default.SliderLinearity; } } } diff --git a/grbl.Master.UI/App.xaml b/grbl.Master.UI/App.xaml index 4ac6b7e..e35d8c8 100644 --- a/grbl.Master.UI/App.xaml +++ b/grbl.Master.UI/App.xaml @@ -8,6 +8,7 @@ + diff --git a/grbl.Master.UI/Converters/NonLinearDouble.cs b/grbl.Master.UI/Converters/NonLinearDouble.cs new file mode 100644 index 0000000..35c30fc --- /dev/null +++ b/grbl.Master.UI/Converters/NonLinearDouble.cs @@ -0,0 +1,44 @@ +namespace grbl.Master.UI.Converters +{ + using System; + using System.Collections.ObjectModel; + using System.Globalization; + using System.Linq; + using System.Windows.Controls; + using System.Windows.Data; + using System.Windows.Media; + + public class NonLinearDouble : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (parameter != null && parameter is Label powLbl && powLbl.Content is double pow) + { + pow = pow < 1 ? 1 : pow; + if (value != null) + { + if (value is double val) + { + return Math.Pow(val, 1d / pow); + } + + if (value is ObservableCollection vals && vals.Any()) + { + return new DoubleCollection(vals.Select(val => Math.Pow(val, 1d / pow))); + } + } + } + + return 0d; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value != null && value is double val && parameter != null && parameter is Label powLbl && powLbl.Content is double pow) + { + return Math.Round(Math.Pow(val, pow), 3); + } + return 0d; + } + } +} \ No newline at end of file diff --git a/grbl.Master.UI/ViewModels/MasterViewModel.cs b/grbl.Master.UI/ViewModels/MasterViewModel.cs index 55b0272..2c3fe1f 100644 --- a/grbl.Master.UI/ViewModels/MasterViewModel.cs +++ b/grbl.Master.UI/ViewModels/MasterViewModel.cs @@ -21,12 +21,14 @@ using System.Reflection; using System.Threading; using System.Windows.Controls; + using System.Windows.Media; using System.Xml; using grbl.Master.Common.Enum; using grbl.Master.Common.Interfaces.BL; using grbl.Master.Common.Interfaces.Service; using grbl.Master.Model.Interface; + using grbl.Master.UI.Converters; using Xceed.Wpf.Toolkit; @@ -53,6 +55,8 @@ public class MasterViewModel : Screen private string _manualCommand; + private NonLinearDouble _nonLinearDoubleConverter = new NonLinearDouble(); + public MasterViewModel( IComService comService, IGrblStatus grblStatus, @@ -119,6 +123,10 @@ public ObservableCollection JoggingDistances } } + public double JoggingDistancesFirst => JoggingDistances.Any() ? JoggingDistances.First() : 0; + + public double JoggingDistancesLast => JoggingDistances.Any() ? JoggingDistances.Last() : 0; + public ObservableCollection FeedRates { get => _applicationSettingsService.Settings.FeedRates; @@ -129,6 +137,25 @@ public ObservableCollection FeedRates } } + public double FeedRatesFirst => FeedRates.Any() ? FeedRates.First() : 0; + + public double FeedRatesLast => FeedRates.Any() ? FeedRates.Last() : 0; + + public double SliderLinearity + { + get => _applicationSettingsService.Settings.SliderLinearity < 1 ? 1 : _applicationSettingsService.Settings.SliderLinearity; + + set + { + _applicationSettingsService.Settings.SliderLinearity = value; + this.NotifyOfPropertyChange(() => SliderLinearity); + NotifyOfPropertyChange(() => FeedRates); + NotifyOfPropertyChange(() => JoggingDistances); + _applicationSettingsService.Save(); + } + } + + public ObservableCollection Macroses => _applicationSettingsService.Settings.Macroses; public TimeSpan Elapsed => _commandSender.FileCommands.Elapsed; @@ -168,6 +195,40 @@ public double SelectedFeedRate } } + /*public DoubleCollection FeedRateTicks + { + get + { + var result = new DoubleCollection(); + + FeedRates.ToList().ForEach( + item => + { + result.Add((double)_nonLinearDoubleConverter.Convert(item, null, 3d, CultureInfo.CurrentCulture)); + }); + + return result; + } + } + + public DoubleCollection JoggingDistanceTicks + { + get + { + var result = new DoubleCollection(); + + JoggingDistances.ToList().ForEach( + item => + { + result.Add((double)_nonLinearDoubleConverter.Convert(item, null, 4d, CultureInfo.CurrentCulture)); + }); + + return result; + } + }*/ + + + public int FileLinesCount => _commandSender.FileCommands.CommandCount; public int FileLinesProcessed => FileCommandsCollection.Count; @@ -368,7 +429,7 @@ public void RefreshMacroLog() public void RefreshFullLog() { - _commandSender.CommunicationLog.Clear();; + _commandSender.CommunicationLog.Clear(); ; } public void SendEnterCommand() @@ -549,7 +610,7 @@ public void CancelMacro() public void AddMacro() { - MacrosSelected = new Macros{Command = ""}; + MacrosSelected = new Macros { Command = "" }; } public void UpMacro(Macros macro) diff --git a/grbl.Master.UI/Views/MasterView.xaml b/grbl.Master.UI/Views/MasterView.xaml index 4200f7a..daa59fe 100644 --- a/grbl.Master.UI/Views/MasterView.xaml +++ b/grbl.Master.UI/Views/MasterView.xaml @@ -1,4 +1,4 @@ - @@ -28,7 +28,11 @@ + +