From fea8e8c559416d7fbf60168dc6d21f2c297f431f Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 18:05:19 -0700 Subject: Nicer decimals, enter press not needed to enter values --- grapher/Field.cs | 49 ++++++++++++--------- grapher/FieldXY.cs | 15 +++++++ grapher/Form1.Designer.cs | 110 +++++++++++++++++++++++----------------------- 3 files changed, 99 insertions(+), 75 deletions(-) diff --git a/grapher/Field.cs b/grapher/Field.cs index 5ef057d..d34472b 100644 --- a/grapher/Field.cs +++ b/grapher/Field.cs @@ -10,6 +10,12 @@ namespace grapher { public class Field { + #region Constants + + public const string DefaultFormatString = "#.#########"; + + #endregion Constants + #region Enums public enum FieldState @@ -34,6 +40,7 @@ namespace grapher DefaultData = defaultData; State = FieldState.Undefined; ContainingForm = containingForm; + FormatString = DefaultFormatString; box.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyDown); box.Leave += new System.EventHandler(FocusLeave); @@ -50,6 +57,8 @@ namespace grapher public double Data { get; private set; } + public string FormatString { get; set; } + public string DefaultText { get; } public FieldState State { get; private set; } @@ -163,15 +172,8 @@ namespace grapher { if (State == FieldState.Typing) { - if (PreviousState == FieldState.Default) - { - SetToDefault(); - } - else if (PreviousState == FieldState.Entered) - { - SetToEntered(); - Box.Text = DecimalString(Data); - } + TextToData(); + SetToEntered(); } } @@ -179,15 +181,8 @@ namespace grapher { if (e.KeyCode == Keys.Enter) { - try - { - Data = Convert.ToDouble(((TextBox)sender).Text); - } - catch - { - } - - Box.Text = DecimalString(Data); + TextToData(); + e.Handled = true; e.SuppressKeyPress = true; @@ -201,11 +196,25 @@ namespace grapher } } - private static string DecimalString(double value) + private void TextToData() + { + try + { + Data = Convert.ToDouble(Box.Text); + } + catch + { + } + + Box.Text = DecimalString(Data); + } + + private string DecimalString(double value) { - return value.ToString("N2"); + return value.ToString(FormatString); } + #endregion Methods } } diff --git a/grapher/FieldXY.cs b/grapher/FieldXY.cs index 7338962..87bda18 100644 --- a/grapher/FieldXY.cs +++ b/grapher/FieldXY.cs @@ -9,14 +9,26 @@ namespace grapher { public class FieldXY { + public const int DefaultSeparation = 6; + + public const string ShortenedFormatString = "#.###"; + public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData) { XField = new Field(xBox, containingForm, defaultData); YField = new Field(yBox, containingForm, defaultData); + YField.FormatString = ShortenedFormatString; LockCheckBox = lockCheckBox; LockCheckBox.CheckedChanged += new System.EventHandler(CheckChanged); + + XField.Box.Width = (YField.Box.Left + YField.Box.Width - XField.Box.Left - DefaultSeparation) / 2; + YField.Box.Width = XField.Box.Width; + DefaultWidthX = XField.Box.Width; DefaultWidthY = YField.Box.Width; + + YField.Box.Left = XField.Box.Left + XField.Box.Width + DefaultSeparation; + CombinedWidth = DefaultWidthX + DefaultWidthY + YField.Box.Left - (XField.Box.Left + DefaultWidthX); SetCombined(); } @@ -72,6 +84,7 @@ namespace grapher YField.SetToUnavailable(); YField.Box.Hide(); XField.Box.Width = CombinedWidth; + XField.FormatString = Field.DefaultFormatString; } public void SetSeparate() @@ -81,6 +94,8 @@ namespace grapher XField.Box.Width = DefaultWidthX; YField.Box.Width = DefaultWidthY; + XField.FormatString = ShortenedFormatString; + if (XField.State == Field.FieldState.Default) { YField.SetToDefault(); diff --git a/grapher/Form1.Designer.cs b/grapher/Form1.Designer.cs index 849801b..bcfb9b5 100644 --- a/grapher/Form1.Designer.cs +++ b/grapher/Form1.Designer.cs @@ -30,15 +30,15 @@ namespace grapher /// private void InitializeComponent() { - System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea4 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); - System.Windows.Forms.DataVisualization.Charting.Legend legend4 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series(); - System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); - System.Windows.Forms.DataVisualization.Charting.Legend legend5 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series(); - System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea6 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); - System.Windows.Forms.DataVisualization.Charting.Legend legend6 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend3 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.AccelerationChart = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.accelTypeDrop = new System.Windows.Forms.ComboBox(); this.sensitivityBoxX = new System.Windows.Forms.TextBox(); @@ -82,20 +82,20 @@ namespace grapher // // AccelerationChart // - chartArea4.AxisX.Title = "Speed (counts/ms)"; - chartArea4.AxisY.Title = "Sensitivity (magnitude ratio)"; - chartArea4.Name = "ChartArea1"; - this.AccelerationChart.ChartAreas.Add(chartArea4); - legend4.Name = "Legend1"; - this.AccelerationChart.Legends.Add(legend4); - this.AccelerationChart.Location = new System.Drawing.Point(242, 0); + chartArea1.AxisX.Title = "Speed (counts/ms)"; + chartArea1.AxisY.Title = "Sensitivity (magnitude ratio)"; + chartArea1.Name = "ChartArea1"; + this.AccelerationChart.ChartAreas.Add(chartArea1); + legend1.Name = "Legend1"; + this.AccelerationChart.Legends.Add(legend1); + this.AccelerationChart.Location = new System.Drawing.Point(240, 0); this.AccelerationChart.Name = "AccelerationChart"; - series4.ChartArea = "ChartArea1"; - series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series4.Legend = "Legend1"; - series4.Name = "Accelerated Sensitivity"; - this.AccelerationChart.Series.Add(series4); - this.AccelerationChart.Size = new System.Drawing.Size(721, 328); + series1.ChartArea = "ChartArea1"; + series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series1.Legend = "Legend1"; + series1.Name = "Accelerated Sensitivity"; + this.AccelerationChart.Series.Add(series1); + this.AccelerationChart.Size = new System.Drawing.Size(723, 328); this.AccelerationChart.TabIndex = 0; this.AccelerationChart.Text = "chart1"; // @@ -144,7 +144,7 @@ namespace grapher // this.accelerationBox.Location = new System.Drawing.Point(106, 125); this.accelerationBox.Name = "accelerationBox"; - this.accelerationBox.Size = new System.Drawing.Size(70, 20); + this.accelerationBox.Size = new System.Drawing.Size(69, 20); this.accelerationBox.TabIndex = 7; // // constantOneLabel @@ -195,7 +195,7 @@ namespace grapher // this.weightBoxSecond.Location = new System.Drawing.Point(144, 177); this.weightBoxSecond.Name = "weightBoxSecond"; - this.weightBoxSecond.Size = new System.Drawing.Size(32, 20); + this.weightBoxSecond.Size = new System.Drawing.Size(31, 20); this.weightBoxSecond.TabIndex = 14; // // limitBox @@ -268,7 +268,7 @@ namespace grapher // // capBoxY // - this.capBoxY.Location = new System.Drawing.Point(145, 151); + this.capBoxY.Location = new System.Drawing.Point(144, 151); this.capBoxY.Name = "capBoxY"; this.capBoxY.Size = new System.Drawing.Size(31, 20); this.capBoxY.TabIndex = 23; @@ -309,7 +309,7 @@ namespace grapher // LockXYLabel // this.LockXYLabel.AutoSize = true; - this.LockXYLabel.Location = new System.Drawing.Point(175, 30); + this.LockXYLabel.Location = new System.Drawing.Point(174, 30); this.LockXYLabel.Name = "LockXYLabel"; this.LockXYLabel.Size = new System.Drawing.Size(60, 13); this.LockXYLabel.TabIndex = 27; @@ -317,39 +317,39 @@ namespace grapher // // VelocityChart // - chartArea5.AxisX.Title = "Speed (count/ms)"; - chartArea5.AxisY.Title = "Output Speed (counts/ms)"; - chartArea5.Name = "ChartArea1"; - this.VelocityChart.ChartAreas.Add(chartArea5); - legend5.Name = "Legend1"; - this.VelocityChart.Legends.Add(legend5); - this.VelocityChart.Location = new System.Drawing.Point(242, 334); + chartArea2.AxisX.Title = "Speed (count/ms)"; + chartArea2.AxisY.Title = "Output Speed (counts/ms)"; + chartArea2.Name = "ChartArea1"; + this.VelocityChart.ChartAreas.Add(chartArea2); + legend2.Name = "Legend1"; + this.VelocityChart.Legends.Add(legend2); + this.VelocityChart.Location = new System.Drawing.Point(240, 334); this.VelocityChart.Name = "VelocityChart"; - series5.ChartArea = "ChartArea1"; - series5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series5.Legend = "Legend1"; - series5.Name = "Mouse Velocity"; - this.VelocityChart.Series.Add(series5); - this.VelocityChart.Size = new System.Drawing.Size(721, 307); + series2.ChartArea = "ChartArea1"; + series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series2.Legend = "Legend1"; + series2.Name = "Mouse Velocity"; + this.VelocityChart.Series.Add(series2); + this.VelocityChart.Size = new System.Drawing.Size(723, 307); this.VelocityChart.TabIndex = 28; this.VelocityChart.Text = "chart1"; // // GainChart // - chartArea6.AxisX.Title = "Speed (counts/ms)"; - chartArea6.AxisY.Title = "Slope of Velocity Chart"; - chartArea6.Name = "ChartArea1"; - this.GainChart.ChartAreas.Add(chartArea6); - legend6.Name = "Legend1"; - this.GainChart.Legends.Add(legend6); - this.GainChart.Location = new System.Drawing.Point(242, 647); + chartArea3.AxisX.Title = "Speed (counts/ms)"; + chartArea3.AxisY.Title = "Slope of Velocity Chart"; + chartArea3.Name = "ChartArea1"; + this.GainChart.ChartAreas.Add(chartArea3); + legend3.Name = "Legend1"; + this.GainChart.Legends.Add(legend3); + this.GainChart.Location = new System.Drawing.Point(240, 647); this.GainChart.Name = "GainChart"; - series6.ChartArea = "ChartArea1"; - series6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series6.Legend = "Legend1"; - series6.Name = "Velocity Gain"; - this.GainChart.Series.Add(series6); - this.GainChart.Size = new System.Drawing.Size(721, 309); + series3.ChartArea = "ChartArea1"; + series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series3.Legend = "Legend1"; + series3.Name = "Velocity Gain"; + this.GainChart.Series.Add(series3); + this.GainChart.Size = new System.Drawing.Size(723, 309); this.GainChart.TabIndex = 29; this.GainChart.Text = "chart1"; // @@ -395,7 +395,7 @@ namespace grapher this.sensitivityToolStripMenuItem, this.velocityGainToolStripMenuItem}); this.capStyleToolStripMenuItem.Name = "capStyleToolStripMenuItem"; - this.capStyleToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.capStyleToolStripMenuItem.Size = new System.Drawing.Size(123, 22); this.capStyleToolStripMenuItem.Text = "Cap Style"; // // sensitivityToolStripMenuItem @@ -403,13 +403,13 @@ namespace grapher this.sensitivityToolStripMenuItem.Checked = true; this.sensitivityToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; this.sensitivityToolStripMenuItem.Name = "sensitivityToolStripMenuItem"; - this.sensitivityToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.sensitivityToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.sensitivityToolStripMenuItem.Text = "Sensitivity"; // // velocityGainToolStripMenuItem // this.velocityGainToolStripMenuItem.Name = "velocityGainToolStripMenuItem"; - this.velocityGainToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.velocityGainToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.velocityGainToolStripMenuItem.Text = "Velocity Gain"; // // RawAcceleration -- cgit v1.2.3 From ae18a1a06060ef66d317af521032ac22fcd88eb3 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 18:18:34 -0700 Subject: Fixed some edge cases around not using enter --- grapher/Field.cs | 14 ++++++++++---- grapher/FieldXY.cs | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/grapher/Field.cs b/grapher/Field.cs index d34472b..8d75172 100644 --- a/grapher/Field.cs +++ b/grapher/Field.cs @@ -12,7 +12,7 @@ namespace grapher { #region Constants - public const string DefaultFormatString = "#.#########"; + public const string DefaultFormatString = "0.#########"; #endregion Constants @@ -173,7 +173,6 @@ namespace grapher if (State == FieldState.Typing) { TextToData(); - SetToEntered(); } } @@ -185,8 +184,6 @@ namespace grapher e.Handled = true; e.SuppressKeyPress = true; - - SetToEntered(); } else if (e.KeyCode == Keys.Escape) { @@ -207,6 +204,15 @@ namespace grapher } Box.Text = DecimalString(Data); + + if (string.Equals(Box.Text, DefaultText)) + { + SetToDefault(); + } + else + { + SetToEntered(); + } } private string DecimalString(double value) diff --git a/grapher/FieldXY.cs b/grapher/FieldXY.cs index 87bda18..87e0b9c 100644 --- a/grapher/FieldXY.cs +++ b/grapher/FieldXY.cs @@ -9,9 +9,9 @@ namespace grapher { public class FieldXY { - public const int DefaultSeparation = 6; + public const int DefaultSeparation = 4; - public const string ShortenedFormatString = "#.###"; + public const string ShortenedFormatString = "0.###"; public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData) { -- cgit v1.2.3 From a6a9f6785eb416ac48d34bb320265c812efc600b Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 19:04:23 -0700 Subject: Add ability to have x\y graphs --- grapher/AccelCharts.cs | 50 +++++++++++----- grapher/AccelGUI.cs | 6 +- grapher/ChartXY.cs | 142 ++++++++++++++++++++++++++++++++++++++++++++++ grapher/Form1.Designer.cs | 85 ++++++++++++++++++++++++++- grapher/Form1.cs | 65 +-------------------- grapher/grapher.csproj | 1 + 6 files changed, 268 insertions(+), 81 deletions(-) create mode 100644 grapher/ChartXY.cs diff --git a/grapher/AccelCharts.cs b/grapher/AccelCharts.cs index 62c60e5..9952016 100644 --- a/grapher/AccelCharts.cs +++ b/grapher/AccelCharts.cs @@ -11,16 +11,16 @@ namespace grapher { public class AccelCharts { - public const int ChartSeparation = 10; + public const int ChartSeparationVertical = 10; /// Needed to show full contents in form. Unsure why. public const int FormHeightPadding = 35; public AccelCharts( Form form, - Chart sensitivityChart, - Chart velocityChart, - Chart gainChart, + ChartXY sensitivityChart, + ChartXY velocityChart, + ChartXY gainChart, ToolStripMenuItem enableVelocityAndGain) { ContaingForm = form; @@ -29,11 +29,11 @@ namespace grapher GainChart = gainChart; EnableVelocityAndGain = enableVelocityAndGain; - SensitivityChart.Top = 0; - VelocityChart.Height = SensitivityChart.Height; - VelocityChart.Top = SensitivityChart.Height + ChartSeparation; - GainChart.Height = SensitivityChart.Height; - GainChart.Top = VelocityChart.Top + VelocityChart.Height + ChartSeparation; + SensitivityChart.SetTop(0); + VelocityChart.SetHeight(SensitivityChart.Height); + VelocityChart.SetTop(SensitivityChart.Height + ChartSeparationVertical); + GainChart.SetHeight(SensitivityChart.Height); + GainChart.SetTop(VelocityChart.Top + VelocityChart.Height + ChartSeparationVertical); Rectangle screenRectangle = ContaingForm.RectangleToScreen(ContaingForm.ClientRectangle); FormBorderHeight = screenRectangle.Top - ContaingForm.Top; @@ -42,15 +42,16 @@ namespace grapher EnableVelocityAndGain.CheckedChanged += new System.EventHandler(OnEnableCheckStateChange); HideVelocityAndGain(); + ShowCombined(); } public Form ContaingForm { get; } - public Chart SensitivityChart { get; } + public ChartXY SensitivityChart { get; } - public Chart VelocityChart { get; } + public ChartXY VelocityChart { get; } - public Chart GainChart { get; } + public ChartXY GainChart { get; } public ToolStripMenuItem EnableVelocityAndGain { get; } @@ -78,9 +79,9 @@ namespace grapher VelocityChart.Show(); GainChart.Show(); ContaingForm.Height = SensitivityChart.Height + - ChartSeparation + + ChartSeparationVertical + VelocityChart.Height + - ChartSeparation + + ChartSeparationVertical + GainChart.Height + FormBorderHeight; } @@ -91,5 +92,26 @@ namespace grapher GainChart.Hide(); ContaingForm.Height = SensitivityChart.Height + FormBorderHeight; } + + private void ShowXandYSeparate() + { + SensitivityChart.SetSeparate(); + VelocityChart.SetSeparate(); + GainChart.SetSeparate(); + UpdateFormWidth(); + } + + private void ShowCombined() + { + SensitivityChart.SetCombined(); + VelocityChart.SetCombined(); + GainChart.SetCombined(); + UpdateFormWidth(); + } + + private void UpdateFormWidth() + { + ContaingForm.Width = SensitivityChart.Left + SensitivityChart.Width; + } } } diff --git a/grapher/AccelGUI.cs b/grapher/AccelGUI.cs index ae05359..54564b3 100644 --- a/grapher/AccelGUI.cs +++ b/grapher/AccelGUI.cs @@ -169,9 +169,9 @@ namespace grapher lastOutputMagnitude = outMagnitude; } - AccelCharts.SensitivityChart.Series[0].Points.DataBindXY(OrderedAccelPoints.Keys, OrderedAccelPoints.Values); - AccelCharts.VelocityChart.Series[0].Points.DataBindXY(OrderedVelocityPoints.Keys, OrderedVelocityPoints.Values); - AccelCharts.GainChart.Series[0].Points.DataBindXY(OrderedGainPoints.Keys, OrderedGainPoints.Values); + AccelCharts.SensitivityChart.ChartX.Series[0].Points.DataBindXY(OrderedAccelPoints.Keys, OrderedAccelPoints.Values); + AccelCharts.VelocityChart.ChartX.Series[0].Points.DataBindXY(OrderedVelocityPoints.Keys, OrderedVelocityPoints.Values); + AccelCharts.GainChart.ChartX.Series[0].Points.DataBindXY(OrderedGainPoints.Keys, OrderedGainPoints.Values); } #endregion methods diff --git a/grapher/ChartXY.cs b/grapher/ChartXY.cs new file mode 100644 index 0000000..4bb1bd5 --- /dev/null +++ b/grapher/ChartXY.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; + +namespace grapher +{ + public class ChartXY + { + public const int ChartSeparationHorizontal = 10; + + public ChartXY(Chart chartX, Chart chartY) + { + ChartX = chartX; + ChartY = chartY; + + ChartY.Top = ChartX.Top; + ChartY.Height = ChartX.Height; + ChartY.Width = ChartX.Width; + ChartY.Left = ChartX.Left + ChartX.Width + ChartSeparationHorizontal; + + SetupChart(ChartX); + SetupChart(ChartY); + } + + public Chart ChartX { get; } + + public Chart ChartY { get; } + + public static void SetupChart(Chart chart) + { + chart.ChartAreas[0].AxisX.RoundAxisValues(); + + chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; + chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; + + chart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; + chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; + + chart.ChartAreas[0].CursorY.Interval = 0.001; + + chart.ChartAreas[0].CursorX.AutoScroll = true; + chart.ChartAreas[0].CursorY.AutoScroll = true; + + chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; + chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; + + chart.ChartAreas[0].CursorX.IsUserEnabled = true; + chart.ChartAreas[0].CursorY.IsUserEnabled = true; + } + + public int Height { + get + { + return ChartX.Height; + } + } + + public int Width { + get + { + if (Combined) + { + return ChartX.Width; + } + else + { + return ChartY.Left + ChartY.Width - ChartX.Left; + } + } + } + + public int Top { + get + { + return ChartX.Height; + } + } + + public int Left { + get + { + return ChartX.Left; + } + } + + public bool Combined { get; private set; } + + public void SetCombined() + { + if (!Combined) + { + ChartY.Hide(); + Combined = true; + } + } + + public void SetSeparate() + { + if (Combined) + { + if (ChartX.Visible) + { + ChartY.Show(); + } + + Combined = false; + } + } + + public void Hide() + { + ChartX.Hide(); + ChartY.Hide(); + } + + public void Show() + { + ChartX.Show(); + + if (!Combined) + { + ChartY.Show(); + } + } + + public void SetTop(int top) + { + ChartX.Top = top; + ChartY.Top = top; + } + + public void SetHeight(int height) + { + ChartX.Height = height; + ChartY.Height = height; + } + } +} diff --git a/grapher/Form1.Designer.cs b/grapher/Form1.Designer.cs index bcfb9b5..ab9c6b2 100644 --- a/grapher/Form1.Designer.cs +++ b/grapher/Form1.Designer.cs @@ -39,6 +39,15 @@ namespace grapher System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend3 = new System.Windows.Forms.DataVisualization.Charting.Legend(); System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea4 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend4 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend5 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea6 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend6 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.AccelerationChart = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.accelTypeDrop = new System.Windows.Forms.ComboBox(); this.sensitivityBoxX = new System.Windows.Forms.TextBox(); @@ -74,10 +83,16 @@ namespace grapher this.capStyleToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.sensitivityToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.velocityGainToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.AccelerationChartY = new System.Windows.Forms.DataVisualization.Charting.Chart(); + this.VelocityChartY = new System.Windows.Forms.DataVisualization.Charting.Chart(); + this.GainChartY = new System.Windows.Forms.DataVisualization.Charting.Chart(); ((System.ComponentModel.ISupportInitialize)(this.AccelerationChart)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.VelocityChart)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.GainChart)).BeginInit(); this.menuStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.AccelerationChartY)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.VelocityChartY)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.GainChartY)).BeginInit(); this.SuspendLayout(); // // AccelerationChart @@ -361,7 +376,7 @@ namespace grapher this.advancedToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(963, 24); + this.menuStrip1.Size = new System.Drawing.Size(1693, 24); this.menuStrip1.TabIndex = 30; this.menuStrip1.Text = "menuStrip1"; // @@ -412,11 +427,71 @@ namespace grapher this.velocityGainToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.velocityGainToolStripMenuItem.Text = "Velocity Gain"; // + // AccelerationChartY + // + chartArea4.AxisX.Title = "Speed (counts/ms)"; + chartArea4.AxisY.Title = "Sensitivity (magnitude ratio)"; + chartArea4.Name = "ChartArea1"; + this.AccelerationChartY.ChartAreas.Add(chartArea4); + legend4.Name = "Legend1"; + this.AccelerationChartY.Legends.Add(legend4); + this.AccelerationChartY.Location = new System.Drawing.Point(969, 0); + this.AccelerationChartY.Name = "AccelerationChartY"; + series4.ChartArea = "ChartArea1"; + series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series4.Legend = "Legend1"; + series4.Name = "Accelerated Sensitivity"; + this.AccelerationChartY.Series.Add(series4); + this.AccelerationChartY.Size = new System.Drawing.Size(723, 328); + this.AccelerationChartY.TabIndex = 31; + this.AccelerationChartY.Text = "chart1"; + // + // VelocityChartY + // + chartArea5.AxisX.Title = "Speed (count/ms)"; + chartArea5.AxisY.Title = "Output Speed (counts/ms)"; + chartArea5.Name = "ChartArea1"; + this.VelocityChartY.ChartAreas.Add(chartArea5); + legend5.Name = "Legend1"; + this.VelocityChartY.Legends.Add(legend5); + this.VelocityChartY.Location = new System.Drawing.Point(970, 334); + this.VelocityChartY.Name = "VelocityChartY"; + series5.ChartArea = "ChartArea1"; + series5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series5.Legend = "Legend1"; + series5.Name = "Mouse Velocity"; + this.VelocityChartY.Series.Add(series5); + this.VelocityChartY.Size = new System.Drawing.Size(723, 307); + this.VelocityChartY.TabIndex = 32; + this.VelocityChartY.Text = "chart1"; + // + // GainChartY + // + chartArea6.AxisX.Title = "Speed (counts/ms)"; + chartArea6.AxisY.Title = "Slope of Velocity Chart"; + chartArea6.Name = "ChartArea1"; + this.GainChartY.ChartAreas.Add(chartArea6); + legend6.Name = "Legend1"; + this.GainChartY.Legends.Add(legend6); + this.GainChartY.Location = new System.Drawing.Point(970, 647); + this.GainChartY.Name = "GainChartY"; + series6.ChartArea = "ChartArea1"; + series6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series6.Legend = "Legend1"; + series6.Name = "Velocity Gain"; + this.GainChartY.Series.Add(series6); + this.GainChartY.Size = new System.Drawing.Size(723, 309); + this.GainChartY.TabIndex = 33; + this.GainChartY.Text = "chart1"; + // // RawAcceleration // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(963, 958); + this.ClientSize = new System.Drawing.Size(1693, 958); + this.Controls.Add(this.GainChartY); + this.Controls.Add(this.VelocityChartY); + this.Controls.Add(this.AccelerationChartY); this.Controls.Add(this.GainChart); this.Controls.Add(this.VelocityChart); this.Controls.Add(this.LockXYLabel); @@ -454,6 +529,9 @@ namespace grapher ((System.ComponentModel.ISupportInitialize)(this.GainChart)).EndInit(); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.AccelerationChartY)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.VelocityChartY)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.GainChartY)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -496,6 +574,9 @@ namespace grapher private System.Windows.Forms.ToolStripMenuItem capStyleToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem sensitivityToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem velocityGainToolStripMenuItem; + private System.Windows.Forms.DataVisualization.Charting.Chart AccelerationChartY; + private System.Windows.Forms.DataVisualization.Charting.Chart VelocityChartY; + private System.Windows.Forms.DataVisualization.Charting.Chart GainChartY; } } diff --git a/grapher/Form1.cs b/grapher/Form1.cs index 518ffc3..da9bd5f 100644 --- a/grapher/Form1.cs +++ b/grapher/Form1.cs @@ -126,9 +126,9 @@ namespace grapher this, new AccelCharts( this, - AccelerationChart, - VelocityChart, - GainChart, + new ChartXY(AccelerationChart, AccelerationChartY), + new ChartXY(VelocityChart, VelocityChartY), + new ChartXY(GainChart, GainChartY), showVelocityGainToolStripMenuItem), managedAcceleration, accelerationOptions, @@ -141,65 +141,6 @@ namespace grapher limitOrExponent, midpoint, writeButton); - - this.AccelerationChart.ChartAreas[0].AxisX.RoundAxisValues(); - - this.AccelerationChart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; - this.AccelerationChart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; - - this.AccelerationChart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; - this.AccelerationChart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; - - this.AccelerationChart.ChartAreas[0].CursorY.Interval = 0.001; - - this.AccelerationChart.ChartAreas[0].CursorX.AutoScroll = true; - this.AccelerationChart.ChartAreas[0].CursorY.AutoScroll = true; - - this.AccelerationChart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; - this.AccelerationChart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; - - this.AccelerationChart.ChartAreas[0].CursorX.IsUserEnabled = true; - this.AccelerationChart.ChartAreas[0].CursorY.IsUserEnabled = true; - - - this.VelocityChart.ChartAreas[0].AxisX.RoundAxisValues(); - - this.VelocityChart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; - this.VelocityChart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; - - this.VelocityChart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; - this.VelocityChart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; - - this.VelocityChart.ChartAreas[0].CursorY.Interval = 0.001; - - this.VelocityChart.ChartAreas[0].CursorX.AutoScroll = true; - this.VelocityChart.ChartAreas[0].CursorY.AutoScroll = true; - - this.VelocityChart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; - this.VelocityChart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; - - this.VelocityChart.ChartAreas[0].CursorX.IsUserEnabled = true; - this.VelocityChart.ChartAreas[0].CursorY.IsUserEnabled = true; - - - this.GainChart.ChartAreas[0].AxisX.RoundAxisValues(); - - this.GainChart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; - this.GainChart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; - - this.GainChart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; - this.GainChart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; - - this.GainChart.ChartAreas[0].CursorY.Interval = 0.001; - - this.GainChart.ChartAreas[0].CursorX.AutoScroll = true; - this.GainChart.ChartAreas[0].CursorY.AutoScroll = true; - - this.GainChart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; - this.GainChart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; - - this.GainChart.ChartAreas[0].CursorX.IsUserEnabled = true; - this.GainChart.ChartAreas[0].CursorY.IsUserEnabled = true; } #endregion Constructor diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj index da70b46..2cce072 100644 --- a/grapher/grapher.csproj +++ b/grapher/grapher.csproj @@ -51,6 +51,7 @@ + -- cgit v1.2.3 From cc531c79f2bd664551071ef315a54814bd9ab914 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 19:22:21 -0700 Subject: Reorganized solution into directories --- grapher/AccelCharts.cs | 117 ----------------- grapher/AccelGUI.cs | 180 -------------------------- grapher/AccelOptions.cs | 79 ------------ grapher/CapOptions.cs | 137 -------------------- grapher/ChartXY.cs | 142 --------------------- grapher/Field.cs | 226 --------------------------------- grapher/FieldXY.cs | 130 ------------------- grapher/Models/AccelGUI.cs | 180 ++++++++++++++++++++++++++ grapher/Models/Charts/AccelCharts.cs | 117 +++++++++++++++++ grapher/Models/Charts/ChartXY.cs | 142 +++++++++++++++++++++ grapher/Models/Fields/Field.cs | 226 +++++++++++++++++++++++++++++++++ grapher/Models/Fields/FieldXY.cs | 130 +++++++++++++++++++ grapher/Models/Options/AccelOptions.cs | 79 ++++++++++++ grapher/Models/Options/CapOptions.cs | 137 ++++++++++++++++++++ grapher/Models/Options/Option.cs | 58 +++++++++ grapher/Models/Options/OptionXY.cs | 80 ++++++++++++ grapher/Option.cs | 58 --------- grapher/OptionXY.cs | 80 ------------ grapher/VectorXY.cs | 33 ----- grapher/grapher.csproj | 19 ++- 20 files changed, 1158 insertions(+), 1192 deletions(-) delete mode 100644 grapher/AccelCharts.cs delete mode 100644 grapher/AccelGUI.cs delete mode 100644 grapher/AccelOptions.cs delete mode 100644 grapher/CapOptions.cs delete mode 100644 grapher/ChartXY.cs delete mode 100644 grapher/Field.cs delete mode 100644 grapher/FieldXY.cs create mode 100644 grapher/Models/AccelGUI.cs create mode 100644 grapher/Models/Charts/AccelCharts.cs create mode 100644 grapher/Models/Charts/ChartXY.cs create mode 100644 grapher/Models/Fields/Field.cs create mode 100644 grapher/Models/Fields/FieldXY.cs create mode 100644 grapher/Models/Options/AccelOptions.cs create mode 100644 grapher/Models/Options/CapOptions.cs create mode 100644 grapher/Models/Options/Option.cs create mode 100644 grapher/Models/Options/OptionXY.cs delete mode 100644 grapher/Option.cs delete mode 100644 grapher/OptionXY.cs delete mode 100644 grapher/VectorXY.cs diff --git a/grapher/AccelCharts.cs b/grapher/AccelCharts.cs deleted file mode 100644 index 9952016..0000000 --- a/grapher/AccelCharts.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using System.Windows.Forms.DataVisualization.Charting; - -namespace grapher -{ - public class AccelCharts - { - public const int ChartSeparationVertical = 10; - - /// Needed to show full contents in form. Unsure why. - public const int FormHeightPadding = 35; - - public AccelCharts( - Form form, - ChartXY sensitivityChart, - ChartXY velocityChart, - ChartXY gainChart, - ToolStripMenuItem enableVelocityAndGain) - { - ContaingForm = form; - SensitivityChart = sensitivityChart; - VelocityChart = velocityChart; - GainChart = gainChart; - EnableVelocityAndGain = enableVelocityAndGain; - - SensitivityChart.SetTop(0); - VelocityChart.SetHeight(SensitivityChart.Height); - VelocityChart.SetTop(SensitivityChart.Height + ChartSeparationVertical); - GainChart.SetHeight(SensitivityChart.Height); - GainChart.SetTop(VelocityChart.Top + VelocityChart.Height + ChartSeparationVertical); - - Rectangle screenRectangle = ContaingForm.RectangleToScreen(ContaingForm.ClientRectangle); - FormBorderHeight = screenRectangle.Top - ContaingForm.Top; - - EnableVelocityAndGain.Click += new System.EventHandler(OnEnableClick); - EnableVelocityAndGain.CheckedChanged += new System.EventHandler(OnEnableCheckStateChange); - - HideVelocityAndGain(); - ShowCombined(); - } - - public Form ContaingForm { get; } - - public ChartXY SensitivityChart { get; } - - public ChartXY VelocityChart { get; } - - public ChartXY GainChart { get; } - - public ToolStripMenuItem EnableVelocityAndGain { get; } - - private int FormBorderHeight { get; } - - private void OnEnableClick(object sender, EventArgs e) - { - EnableVelocityAndGain.Checked = !EnableVelocityAndGain.Checked; - } - - private void OnEnableCheckStateChange(object sender, EventArgs e) - { - if (EnableVelocityAndGain.Checked) - { - ShowVelocityAndGain(); - } - else - { - HideVelocityAndGain(); - } - } - - private void ShowVelocityAndGain() - { - VelocityChart.Show(); - GainChart.Show(); - ContaingForm.Height = SensitivityChart.Height + - ChartSeparationVertical + - VelocityChart.Height + - ChartSeparationVertical + - GainChart.Height + - FormBorderHeight; - } - - private void HideVelocityAndGain() - { - VelocityChart.Hide(); - GainChart.Hide(); - ContaingForm.Height = SensitivityChart.Height + FormBorderHeight; - } - - private void ShowXandYSeparate() - { - SensitivityChart.SetSeparate(); - VelocityChart.SetSeparate(); - GainChart.SetSeparate(); - UpdateFormWidth(); - } - - private void ShowCombined() - { - SensitivityChart.SetCombined(); - VelocityChart.SetCombined(); - GainChart.SetCombined(); - UpdateFormWidth(); - } - - private void UpdateFormWidth() - { - ContaingForm.Width = SensitivityChart.Left + SensitivityChart.Width; - } - } -} diff --git a/grapher/AccelGUI.cs b/grapher/AccelGUI.cs deleted file mode 100644 index 54564b3..0000000 --- a/grapher/AccelGUI.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using System.Windows.Forms.DataVisualization.Charting; - -namespace grapher -{ - public class AccelGUI - { - public struct MagnitudeData - { - public double magnitude; - public int x; - public int y; - } - - public static ReadOnlyCollection Magnitudes = GetMagnitudes(); - - #region constructors - - public AccelGUI( - RawAcceleration accelForm, - AccelCharts accelCharts, - ManagedAccel managedAccel, - AccelOptions accelOptions, - OptionXY sensitivity, - Option rotation, - OptionXY weight, - CapOptions cap, - Option offset, - Option acceleration, - Option limtOrExp, - Option midpoint, - Button writeButton) - { - AccelForm = accelForm; - AccelCharts = accelCharts; - ManagedAcceleration = managedAccel; - AccelerationOptions = accelOptions; - Sensitivity = sensitivity; - Rotation = rotation; - Weight = weight; - Cap = cap; - Offset = offset; - Acceleration = acceleration; - LimitOrExponent = limtOrExp; - Midpoint = midpoint; - WriteButton = writeButton; - - OrderedAccelPoints = new SortedDictionary(); - OrderedVelocityPoints = new SortedDictionary(); - OrderedGainPoints = new SortedDictionary(); - - - ManagedAcceleration.ReadFromDriver(); - UpdateGraph(); - } - - #endregion constructors - - #region properties - - public RawAcceleration AccelForm { get; } - - public AccelCharts AccelCharts { get; } - - public ManagedAccel ManagedAcceleration { get; } - - public AccelOptions AccelerationOptions { get; } - - public OptionXY Sensitivity { get; } - - public Option Rotation { get; } - - public OptionXY Weight { get; } - - public CapOptions Cap { get; } - - public Option Offset { get; } - - public Option Acceleration { get; } - - public Option LimitOrExponent { get; } - - public Option Midpoint { get; } - - public Button WriteButton { get; } - - public SortedDictionary OrderedAccelPoints { get; } - - public SortedDictionary OrderedVelocityPoints { get; } - - public SortedDictionary OrderedGainPoints { get; } - - #endregion properties - - #region methods - - public static ReadOnlyCollection GetMagnitudes() - { - var magnitudes = new List(); - for (int i = 0; i < 100; i++) - { - for (int j = 0; j <= i; j++) - { - MagnitudeData magnitudeData; - magnitudeData.magnitude = Magnitude(i, j); - magnitudeData.x = i; - magnitudeData.y = j; - magnitudes.Add(magnitudeData); - } - } - - magnitudes.Sort((m1, m2) => m1.magnitude.CompareTo(m2.magnitude)); - - return magnitudes.AsReadOnly(); - } - - public static double Magnitude(int x, int y) - { - return Math.Sqrt(x * x + y * y); - } - - public static double Magnitude(double x, double y) - { - return Math.Sqrt(x * x + y * y); - } - - public void UpdateGraph() - { - OrderedAccelPoints.Clear(); - OrderedVelocityPoints.Clear(); - OrderedGainPoints.Clear(); - - double lastInputMagnitude = 0; - double lastOutputMagnitude = 0; - - foreach (var magnitudeData in Magnitudes) - { - var output = ManagedAcceleration.Accelerate(magnitudeData.x, magnitudeData.y, 1); - - var outMagnitude = Magnitude(output.Item1, output.Item2); - var ratio = magnitudeData.magnitude > 0 ? outMagnitude / magnitudeData.magnitude : Sensitivity.Fields.X; - - var inDiff = magnitudeData.magnitude - lastInputMagnitude; - var outDiff = outMagnitude - lastOutputMagnitude; - var slope = inDiff > 0 ? outDiff / inDiff : Sensitivity.Fields.X; - - if (!OrderedAccelPoints.ContainsKey(magnitudeData.magnitude)) - { - OrderedAccelPoints.Add(magnitudeData.magnitude, ratio); - } - - if (!OrderedVelocityPoints.ContainsKey(magnitudeData.magnitude)) - { - OrderedVelocityPoints.Add(magnitudeData.magnitude, outMagnitude); - } - - if (!OrderedGainPoints.ContainsKey(magnitudeData.magnitude)) - { - OrderedGainPoints.Add(magnitudeData.magnitude, slope); - } - - lastInputMagnitude = magnitudeData.magnitude; - lastOutputMagnitude = outMagnitude; - } - - AccelCharts.SensitivityChart.ChartX.Series[0].Points.DataBindXY(OrderedAccelPoints.Keys, OrderedAccelPoints.Values); - AccelCharts.VelocityChart.ChartX.Series[0].Points.DataBindXY(OrderedVelocityPoints.Keys, OrderedVelocityPoints.Values); - AccelCharts.GainChart.ChartX.Series[0].Points.DataBindXY(OrderedGainPoints.Keys, OrderedGainPoints.Values); - } - - #endregion methods - } - -} diff --git a/grapher/AccelOptions.cs b/grapher/AccelOptions.cs deleted file mode 100644 index b233552..0000000 --- a/grapher/AccelOptions.cs +++ /dev/null @@ -1,79 +0,0 @@ -using grapher.Layouts; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace grapher -{ - public class AccelOptions - { - public const int PossibleOptionsCount = 4; - public const int PossibleOptionsXYCount = 2; - - public static readonly Dictionary AccelerationTypes = new List - { - new DefaultLayout(), - new LinearLayout(), - new ClassicLayout(), - new NaturalLayout(), - new LogLayout(), - new SigmoidLayout(), - new PowerLayout(), - new OffLayout() - }.ToDictionary(k => k.Name); - - public AccelOptions( - ComboBox accelDropdown, - Option[] options, - OptionXY[] optionsXY, - Button writeButton) - { - AccelDropdown = accelDropdown; - AccelDropdown.Items.Clear(); - AccelDropdown.Items.AddRange(AccelerationTypes.Keys.Skip(1).ToArray()); - AccelDropdown.SelectedIndexChanged += new System.EventHandler(OnIndexChanged); - - if (options.Length > PossibleOptionsCount) - { - throw new Exception("Layout given too many options."); - } - - if (optionsXY.Length > PossibleOptionsXYCount) - { - throw new Exception("Layout given too many options."); - } - - Options = options; - OptionsXY = optionsXY; - WriteButton = writeButton; - - Layout("Default"); - } - - public Button WriteButton { get; } - - public ComboBox AccelDropdown { get; } - - public int AccelerationIndex { get; private set; } - - public Option[] Options { get; } - - public OptionXY[] OptionsXY { get; } - - private void OnIndexChanged(object sender, EventArgs e) - { - var accelerationTypeString = AccelDropdown.SelectedItem.ToString(); - Layout(accelerationTypeString); - } - - private void Layout(string type) - { - var accelerationType = AccelerationTypes[type]; - AccelerationIndex = accelerationType.Index; - accelerationType.Layout(Options, OptionsXY, WriteButton); - } - } -} diff --git a/grapher/CapOptions.cs b/grapher/CapOptions.cs deleted file mode 100644 index 2ee7f6b..0000000 --- a/grapher/CapOptions.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace grapher -{ - public class CapOptions - { - public CapOptions( - ToolStripMenuItem sensitivityCapCheck, - ToolStripMenuItem velocityGainCapCheck, - OptionXY capOption, - OptionXY weightOption) - { - - SensitivityCapCheck = sensitivityCapCheck; - VelocityGainCapCheck = velocityGainCapCheck; - CapOption = capOption; - WeightOption = weightOption; - - SensitivityCapCheck.Click += new System.EventHandler(OnSensitivityCapCheckClick); - VelocityGainCapCheck.Click += new System.EventHandler(OnVelocityGainCapCheckClick); - - SensitivityCapCheck.CheckedChanged += new System.EventHandler(OnSensitivityCapCheckedChange); - VelocityGainCapCheck.CheckedChanged += new System.EventHandler(OnVelocityGainCapCheckedChange); - - EnableSensitivityCap(); - } - - ToolStripMenuItem SensitivityCapCheck { get; } - - ToolStripMenuItem VelocityGainCapCheck { get; } - - OptionXY CapOption { get; } - - OptionXY WeightOption { get; } - - public double SensitivityCapX { - get - { - if (IsSensitivityGain) - { - return CapOption.Fields.X; - } - else - { - return 0; - } - } - } - - public double SensitivityCapY { - get - { - if (IsSensitivityGain) - { - return CapOption.Fields.Y; - } - else - { - return 0; - } - } - } - - public double VelocityGainCap { - get - { - if (IsSensitivityGain) - { - return 0; - } - else - { - return CapOption.Fields.X; - } - } - } - - public bool IsSensitivityGain { get; private set; } - - void OnSensitivityCapCheckClick(object sender, EventArgs e) - { - if (!SensitivityCapCheck.Checked) - { - VelocityGainCapCheck.Checked = false; - SensitivityCapCheck.Checked = true; - } - } - - void OnVelocityGainCapCheckClick(object sender, EventArgs e) - { - if (!VelocityGainCapCheck.Checked) - { - VelocityGainCapCheck.Checked = true; - SensitivityCapCheck.Checked = false; - } - } - - void OnSensitivityCapCheckedChange(object sender, EventArgs e) - { - if (SensitivityCapCheck.Checked == true) - { - EnableSensitivityCap(); - } - } - - void OnVelocityGainCapCheckedChange(object sender, EventArgs e) - { - if (SensitivityCapCheck.Checked == true) - { - EnableVelocityGainCap(); - } - } - - void EnableSensitivityCap() - { - IsSensitivityGain = true; - CapOption.Fields.LockCheckBox.Enabled = true; - WeightOption.Fields.LockCheckBox.Enabled = true; - CapOption.SetName("Sensitivity Cap"); - } - - void EnableVelocityGainCap() - { - IsSensitivityGain = false; - CapOption.Fields.LockCheckBox.Checked = true; - CapOption.Fields.LockCheckBox.Enabled = false; - WeightOption.Fields.LockCheckBox.Checked = true; - WeightOption.Fields.LockCheckBox.Enabled = false; - CapOption.SetName("Velocity Gain Cap"); - } - } -} diff --git a/grapher/ChartXY.cs b/grapher/ChartXY.cs deleted file mode 100644 index 4bb1bd5..0000000 --- a/grapher/ChartXY.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using System.Windows.Forms.DataVisualization.Charting; - -namespace grapher -{ - public class ChartXY - { - public const int ChartSeparationHorizontal = 10; - - public ChartXY(Chart chartX, Chart chartY) - { - ChartX = chartX; - ChartY = chartY; - - ChartY.Top = ChartX.Top; - ChartY.Height = ChartX.Height; - ChartY.Width = ChartX.Width; - ChartY.Left = ChartX.Left + ChartX.Width + ChartSeparationHorizontal; - - SetupChart(ChartX); - SetupChart(ChartY); - } - - public Chart ChartX { get; } - - public Chart ChartY { get; } - - public static void SetupChart(Chart chart) - { - chart.ChartAreas[0].AxisX.RoundAxisValues(); - - chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; - chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; - - chart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; - chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; - - chart.ChartAreas[0].CursorY.Interval = 0.001; - - chart.ChartAreas[0].CursorX.AutoScroll = true; - chart.ChartAreas[0].CursorY.AutoScroll = true; - - chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; - chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; - - chart.ChartAreas[0].CursorX.IsUserEnabled = true; - chart.ChartAreas[0].CursorY.IsUserEnabled = true; - } - - public int Height { - get - { - return ChartX.Height; - } - } - - public int Width { - get - { - if (Combined) - { - return ChartX.Width; - } - else - { - return ChartY.Left + ChartY.Width - ChartX.Left; - } - } - } - - public int Top { - get - { - return ChartX.Height; - } - } - - public int Left { - get - { - return ChartX.Left; - } - } - - public bool Combined { get; private set; } - - public void SetCombined() - { - if (!Combined) - { - ChartY.Hide(); - Combined = true; - } - } - - public void SetSeparate() - { - if (Combined) - { - if (ChartX.Visible) - { - ChartY.Show(); - } - - Combined = false; - } - } - - public void Hide() - { - ChartX.Hide(); - ChartY.Hide(); - } - - public void Show() - { - ChartX.Show(); - - if (!Combined) - { - ChartY.Show(); - } - } - - public void SetTop(int top) - { - ChartX.Top = top; - ChartY.Top = top; - } - - public void SetHeight(int height) - { - ChartX.Height = height; - ChartY.Height = height; - } - } -} diff --git a/grapher/Field.cs b/grapher/Field.cs deleted file mode 100644 index 8d75172..0000000 --- a/grapher/Field.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace grapher -{ - public class Field - { - #region Constants - - public const string DefaultFormatString = "0.#########"; - - #endregion Constants - - #region Enums - - public enum FieldState - { - Undefined, - Default, - Typing, - Entered, - Unavailable, - } - - #endregion Enums - - - #region Constructors - - public Field(TextBox box, Form containingForm, double defaultData) - { - DefaultText = DecimalString(defaultData); - Box = box; - Data = defaultData; - DefaultData = defaultData; - State = FieldState.Undefined; - ContainingForm = containingForm; - FormatString = DefaultFormatString; - box.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyDown); - box.Leave += new System.EventHandler(FocusLeave); - - SetToDefault(); - } - - #endregion Constructors - - #region Properties - - public TextBox Box { get; } - - private Form ContainingForm { get; } - - public double Data { get; private set; } - - public string FormatString { get; set; } - - public string DefaultText { get; } - - public FieldState State { get; private set; } - - public FieldState PreviousState { get; private set; } - - private double DefaultData { get; } - - #endregion Properties - - #region Methods - - public void SetToDefault() - { - if (State != FieldState.Default) - { - Box.BackColor = Color.White; - Box.ForeColor = Color.Gray; - State = FieldState.Default; - PreviousState = FieldState.Default; - } - - Data = DefaultData; - Box.Text = DefaultText; - ContainingForm.ActiveControl = null; - } - - public void SetToTyping() - { - if (State != FieldState.Typing) - { - Box.BackColor = Color.White; - Box.ForeColor = Color.Black; - - PreviousState = State; - State = FieldState.Typing; - } - - Box.Text = string.Empty; - } - - public void SetToEntered() - { - if (State != FieldState.Entered) - { - Box.BackColor = Color.AntiqueWhite; - Box.ForeColor = Color.DarkGray; - - PreviousState = State; - State = FieldState.Entered; - } - - ContainingForm.ActiveControl = null; - } - - public void SetToEntered(double value) - { - SetToEntered(); - - Data = value; - Box.Text = DecimalString(Data); - } - - public void SetToUnavailable() - { - if (State != FieldState.Unavailable) - { - Box.BackColor = Color.LightGray; - Box.ForeColor = Color.LightGray; - Box.Text = string.Empty; - - PreviousState = State; - State = FieldState.Unavailable; - } - } - - public void KeyDown(object sender, KeyEventArgs e) - { - switch(State) - { - case FieldState.Default: - if (e.KeyCode == Keys.Enter) - { - SetToDefault(); - } - else - { - SetToTyping(); - } - break; - - case FieldState.Entered: - if (e.KeyCode != Keys.Enter) - { - SetToTyping(); - } - break; - case FieldState.Typing: - HandleTyping(sender, e); - break; - case FieldState.Unavailable: - Box.Text = string.Empty; - ContainingForm.ActiveControl = null; - break; - default: - break; - } - } - - private void FocusLeave(object sender, EventArgs e) - { - if (State == FieldState.Typing) - { - TextToData(); - } - } - - private void HandleTyping(object sender, KeyEventArgs e) - { - if (e.KeyCode == Keys.Enter) - { - TextToData(); - - e.Handled = true; - e.SuppressKeyPress = true; - } - else if (e.KeyCode == Keys.Escape) - { - ContainingForm.ActiveControl = null; - e.Handled = true; - e.SuppressKeyPress = true; - } - } - - private void TextToData() - { - try - { - Data = Convert.ToDouble(Box.Text); - } - catch - { - } - - Box.Text = DecimalString(Data); - - if (string.Equals(Box.Text, DefaultText)) - { - SetToDefault(); - } - else - { - SetToEntered(); - } - } - - private string DecimalString(double value) - { - return value.ToString(FormatString); - } - - - #endregion Methods - } -} diff --git a/grapher/FieldXY.cs b/grapher/FieldXY.cs deleted file mode 100644 index 87e0b9c..0000000 --- a/grapher/FieldXY.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace grapher -{ - public class FieldXY - { - public const int DefaultSeparation = 4; - - public const string ShortenedFormatString = "0.###"; - - public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData) - { - XField = new Field(xBox, containingForm, defaultData); - YField = new Field(yBox, containingForm, defaultData); - YField.FormatString = ShortenedFormatString; - LockCheckBox = lockCheckBox; - LockCheckBox.CheckedChanged += new System.EventHandler(CheckChanged); - - XField.Box.Width = (YField.Box.Left + YField.Box.Width - XField.Box.Left - DefaultSeparation) / 2; - YField.Box.Width = XField.Box.Width; - - DefaultWidthX = XField.Box.Width; - DefaultWidthY = YField.Box.Width; - - YField.Box.Left = XField.Box.Left + XField.Box.Width + DefaultSeparation; - - CombinedWidth = DefaultWidthX + DefaultWidthY + YField.Box.Left - (XField.Box.Left + DefaultWidthX); - SetCombined(); - } - public double X - { - get => XField.Data; - } - - public double Y - { - get - { - if (Combined) - { - return X; - } - else - { - return YField.Data; - } - } - } - - public CheckBox LockCheckBox { get; } - - public Field XField { get; } - - public Field YField { get; } - - private bool Combined { get; set; } - - private int DefaultWidthX { get; } - - private int DefaultWidthY { get; } - - private int CombinedWidth { get; } - - private void CheckChanged(object sender, EventArgs e) - { - if (LockCheckBox.CheckState == CheckState.Checked) - { - SetCombined(); - } - else - { - SetSeparate(); - } - } - - public void SetCombined() - { - Combined = true; - YField.SetToUnavailable(); - YField.Box.Hide(); - XField.Box.Width = CombinedWidth; - XField.FormatString = Field.DefaultFormatString; - } - - public void SetSeparate() - { - Combined = false; - - XField.Box.Width = DefaultWidthX; - YField.Box.Width = DefaultWidthY; - - XField.FormatString = ShortenedFormatString; - - if (XField.State == Field.FieldState.Default) - { - YField.SetToDefault(); - } - else - { - YField.SetToEntered(XField.Data); - } - - if (XField.Box.Visible) - { - YField.Box.Show(); - } - } - - public void Show() - { - XField.Box.Show(); - - if (!Combined) - { - YField.Box.Show(); - } - } - - public void Hide() - { - XField.Box.Hide(); - YField.Box.Hide(); - } - } -} diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs new file mode 100644 index 0000000..54564b3 --- /dev/null +++ b/grapher/Models/AccelGUI.cs @@ -0,0 +1,180 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; + +namespace grapher +{ + public class AccelGUI + { + public struct MagnitudeData + { + public double magnitude; + public int x; + public int y; + } + + public static ReadOnlyCollection Magnitudes = GetMagnitudes(); + + #region constructors + + public AccelGUI( + RawAcceleration accelForm, + AccelCharts accelCharts, + ManagedAccel managedAccel, + AccelOptions accelOptions, + OptionXY sensitivity, + Option rotation, + OptionXY weight, + CapOptions cap, + Option offset, + Option acceleration, + Option limtOrExp, + Option midpoint, + Button writeButton) + { + AccelForm = accelForm; + AccelCharts = accelCharts; + ManagedAcceleration = managedAccel; + AccelerationOptions = accelOptions; + Sensitivity = sensitivity; + Rotation = rotation; + Weight = weight; + Cap = cap; + Offset = offset; + Acceleration = acceleration; + LimitOrExponent = limtOrExp; + Midpoint = midpoint; + WriteButton = writeButton; + + OrderedAccelPoints = new SortedDictionary(); + OrderedVelocityPoints = new SortedDictionary(); + OrderedGainPoints = new SortedDictionary(); + + + ManagedAcceleration.ReadFromDriver(); + UpdateGraph(); + } + + #endregion constructors + + #region properties + + public RawAcceleration AccelForm { get; } + + public AccelCharts AccelCharts { get; } + + public ManagedAccel ManagedAcceleration { get; } + + public AccelOptions AccelerationOptions { get; } + + public OptionXY Sensitivity { get; } + + public Option Rotation { get; } + + public OptionXY Weight { get; } + + public CapOptions Cap { get; } + + public Option Offset { get; } + + public Option Acceleration { get; } + + public Option LimitOrExponent { get; } + + public Option Midpoint { get; } + + public Button WriteButton { get; } + + public SortedDictionary OrderedAccelPoints { get; } + + public SortedDictionary OrderedVelocityPoints { get; } + + public SortedDictionary OrderedGainPoints { get; } + + #endregion properties + + #region methods + + public static ReadOnlyCollection GetMagnitudes() + { + var magnitudes = new List(); + for (int i = 0; i < 100; i++) + { + for (int j = 0; j <= i; j++) + { + MagnitudeData magnitudeData; + magnitudeData.magnitude = Magnitude(i, j); + magnitudeData.x = i; + magnitudeData.y = j; + magnitudes.Add(magnitudeData); + } + } + + magnitudes.Sort((m1, m2) => m1.magnitude.CompareTo(m2.magnitude)); + + return magnitudes.AsReadOnly(); + } + + public static double Magnitude(int x, int y) + { + return Math.Sqrt(x * x + y * y); + } + + public static double Magnitude(double x, double y) + { + return Math.Sqrt(x * x + y * y); + } + + public void UpdateGraph() + { + OrderedAccelPoints.Clear(); + OrderedVelocityPoints.Clear(); + OrderedGainPoints.Clear(); + + double lastInputMagnitude = 0; + double lastOutputMagnitude = 0; + + foreach (var magnitudeData in Magnitudes) + { + var output = ManagedAcceleration.Accelerate(magnitudeData.x, magnitudeData.y, 1); + + var outMagnitude = Magnitude(output.Item1, output.Item2); + var ratio = magnitudeData.magnitude > 0 ? outMagnitude / magnitudeData.magnitude : Sensitivity.Fields.X; + + var inDiff = magnitudeData.magnitude - lastInputMagnitude; + var outDiff = outMagnitude - lastOutputMagnitude; + var slope = inDiff > 0 ? outDiff / inDiff : Sensitivity.Fields.X; + + if (!OrderedAccelPoints.ContainsKey(magnitudeData.magnitude)) + { + OrderedAccelPoints.Add(magnitudeData.magnitude, ratio); + } + + if (!OrderedVelocityPoints.ContainsKey(magnitudeData.magnitude)) + { + OrderedVelocityPoints.Add(magnitudeData.magnitude, outMagnitude); + } + + if (!OrderedGainPoints.ContainsKey(magnitudeData.magnitude)) + { + OrderedGainPoints.Add(magnitudeData.magnitude, slope); + } + + lastInputMagnitude = magnitudeData.magnitude; + lastOutputMagnitude = outMagnitude; + } + + AccelCharts.SensitivityChart.ChartX.Series[0].Points.DataBindXY(OrderedAccelPoints.Keys, OrderedAccelPoints.Values); + AccelCharts.VelocityChart.ChartX.Series[0].Points.DataBindXY(OrderedVelocityPoints.Keys, OrderedVelocityPoints.Values); + AccelCharts.GainChart.ChartX.Series[0].Points.DataBindXY(OrderedGainPoints.Keys, OrderedGainPoints.Values); + } + + #endregion methods + } + +} diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs new file mode 100644 index 0000000..9952016 --- /dev/null +++ b/grapher/Models/Charts/AccelCharts.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; + +namespace grapher +{ + public class AccelCharts + { + public const int ChartSeparationVertical = 10; + + /// Needed to show full contents in form. Unsure why. + public const int FormHeightPadding = 35; + + public AccelCharts( + Form form, + ChartXY sensitivityChart, + ChartXY velocityChart, + ChartXY gainChart, + ToolStripMenuItem enableVelocityAndGain) + { + ContaingForm = form; + SensitivityChart = sensitivityChart; + VelocityChart = velocityChart; + GainChart = gainChart; + EnableVelocityAndGain = enableVelocityAndGain; + + SensitivityChart.SetTop(0); + VelocityChart.SetHeight(SensitivityChart.Height); + VelocityChart.SetTop(SensitivityChart.Height + ChartSeparationVertical); + GainChart.SetHeight(SensitivityChart.Height); + GainChart.SetTop(VelocityChart.Top + VelocityChart.Height + ChartSeparationVertical); + + Rectangle screenRectangle = ContaingForm.RectangleToScreen(ContaingForm.ClientRectangle); + FormBorderHeight = screenRectangle.Top - ContaingForm.Top; + + EnableVelocityAndGain.Click += new System.EventHandler(OnEnableClick); + EnableVelocityAndGain.CheckedChanged += new System.EventHandler(OnEnableCheckStateChange); + + HideVelocityAndGain(); + ShowCombined(); + } + + public Form ContaingForm { get; } + + public ChartXY SensitivityChart { get; } + + public ChartXY VelocityChart { get; } + + public ChartXY GainChart { get; } + + public ToolStripMenuItem EnableVelocityAndGain { get; } + + private int FormBorderHeight { get; } + + private void OnEnableClick(object sender, EventArgs e) + { + EnableVelocityAndGain.Checked = !EnableVelocityAndGain.Checked; + } + + private void OnEnableCheckStateChange(object sender, EventArgs e) + { + if (EnableVelocityAndGain.Checked) + { + ShowVelocityAndGain(); + } + else + { + HideVelocityAndGain(); + } + } + + private void ShowVelocityAndGain() + { + VelocityChart.Show(); + GainChart.Show(); + ContaingForm.Height = SensitivityChart.Height + + ChartSeparationVertical + + VelocityChart.Height + + ChartSeparationVertical + + GainChart.Height + + FormBorderHeight; + } + + private void HideVelocityAndGain() + { + VelocityChart.Hide(); + GainChart.Hide(); + ContaingForm.Height = SensitivityChart.Height + FormBorderHeight; + } + + private void ShowXandYSeparate() + { + SensitivityChart.SetSeparate(); + VelocityChart.SetSeparate(); + GainChart.SetSeparate(); + UpdateFormWidth(); + } + + private void ShowCombined() + { + SensitivityChart.SetCombined(); + VelocityChart.SetCombined(); + GainChart.SetCombined(); + UpdateFormWidth(); + } + + private void UpdateFormWidth() + { + ContaingForm.Width = SensitivityChart.Left + SensitivityChart.Width; + } + } +} diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs new file mode 100644 index 0000000..4bb1bd5 --- /dev/null +++ b/grapher/Models/Charts/ChartXY.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; + +namespace grapher +{ + public class ChartXY + { + public const int ChartSeparationHorizontal = 10; + + public ChartXY(Chart chartX, Chart chartY) + { + ChartX = chartX; + ChartY = chartY; + + ChartY.Top = ChartX.Top; + ChartY.Height = ChartX.Height; + ChartY.Width = ChartX.Width; + ChartY.Left = ChartX.Left + ChartX.Width + ChartSeparationHorizontal; + + SetupChart(ChartX); + SetupChart(ChartY); + } + + public Chart ChartX { get; } + + public Chart ChartY { get; } + + public static void SetupChart(Chart chart) + { + chart.ChartAreas[0].AxisX.RoundAxisValues(); + + chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; + chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; + + chart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; + chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; + + chart.ChartAreas[0].CursorY.Interval = 0.001; + + chart.ChartAreas[0].CursorX.AutoScroll = true; + chart.ChartAreas[0].CursorY.AutoScroll = true; + + chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; + chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; + + chart.ChartAreas[0].CursorX.IsUserEnabled = true; + chart.ChartAreas[0].CursorY.IsUserEnabled = true; + } + + public int Height { + get + { + return ChartX.Height; + } + } + + public int Width { + get + { + if (Combined) + { + return ChartX.Width; + } + else + { + return ChartY.Left + ChartY.Width - ChartX.Left; + } + } + } + + public int Top { + get + { + return ChartX.Height; + } + } + + public int Left { + get + { + return ChartX.Left; + } + } + + public bool Combined { get; private set; } + + public void SetCombined() + { + if (!Combined) + { + ChartY.Hide(); + Combined = true; + } + } + + public void SetSeparate() + { + if (Combined) + { + if (ChartX.Visible) + { + ChartY.Show(); + } + + Combined = false; + } + } + + public void Hide() + { + ChartX.Hide(); + ChartY.Hide(); + } + + public void Show() + { + ChartX.Show(); + + if (!Combined) + { + ChartY.Show(); + } + } + + public void SetTop(int top) + { + ChartX.Top = top; + ChartY.Top = top; + } + + public void SetHeight(int height) + { + ChartX.Height = height; + ChartY.Height = height; + } + } +} diff --git a/grapher/Models/Fields/Field.cs b/grapher/Models/Fields/Field.cs new file mode 100644 index 0000000..8d75172 --- /dev/null +++ b/grapher/Models/Fields/Field.cs @@ -0,0 +1,226 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class Field + { + #region Constants + + public const string DefaultFormatString = "0.#########"; + + #endregion Constants + + #region Enums + + public enum FieldState + { + Undefined, + Default, + Typing, + Entered, + Unavailable, + } + + #endregion Enums + + + #region Constructors + + public Field(TextBox box, Form containingForm, double defaultData) + { + DefaultText = DecimalString(defaultData); + Box = box; + Data = defaultData; + DefaultData = defaultData; + State = FieldState.Undefined; + ContainingForm = containingForm; + FormatString = DefaultFormatString; + box.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyDown); + box.Leave += new System.EventHandler(FocusLeave); + + SetToDefault(); + } + + #endregion Constructors + + #region Properties + + public TextBox Box { get; } + + private Form ContainingForm { get; } + + public double Data { get; private set; } + + public string FormatString { get; set; } + + public string DefaultText { get; } + + public FieldState State { get; private set; } + + public FieldState PreviousState { get; private set; } + + private double DefaultData { get; } + + #endregion Properties + + #region Methods + + public void SetToDefault() + { + if (State != FieldState.Default) + { + Box.BackColor = Color.White; + Box.ForeColor = Color.Gray; + State = FieldState.Default; + PreviousState = FieldState.Default; + } + + Data = DefaultData; + Box.Text = DefaultText; + ContainingForm.ActiveControl = null; + } + + public void SetToTyping() + { + if (State != FieldState.Typing) + { + Box.BackColor = Color.White; + Box.ForeColor = Color.Black; + + PreviousState = State; + State = FieldState.Typing; + } + + Box.Text = string.Empty; + } + + public void SetToEntered() + { + if (State != FieldState.Entered) + { + Box.BackColor = Color.AntiqueWhite; + Box.ForeColor = Color.DarkGray; + + PreviousState = State; + State = FieldState.Entered; + } + + ContainingForm.ActiveControl = null; + } + + public void SetToEntered(double value) + { + SetToEntered(); + + Data = value; + Box.Text = DecimalString(Data); + } + + public void SetToUnavailable() + { + if (State != FieldState.Unavailable) + { + Box.BackColor = Color.LightGray; + Box.ForeColor = Color.LightGray; + Box.Text = string.Empty; + + PreviousState = State; + State = FieldState.Unavailable; + } + } + + public void KeyDown(object sender, KeyEventArgs e) + { + switch(State) + { + case FieldState.Default: + if (e.KeyCode == Keys.Enter) + { + SetToDefault(); + } + else + { + SetToTyping(); + } + break; + + case FieldState.Entered: + if (e.KeyCode != Keys.Enter) + { + SetToTyping(); + } + break; + case FieldState.Typing: + HandleTyping(sender, e); + break; + case FieldState.Unavailable: + Box.Text = string.Empty; + ContainingForm.ActiveControl = null; + break; + default: + break; + } + } + + private void FocusLeave(object sender, EventArgs e) + { + if (State == FieldState.Typing) + { + TextToData(); + } + } + + private void HandleTyping(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + TextToData(); + + e.Handled = true; + e.SuppressKeyPress = true; + } + else if (e.KeyCode == Keys.Escape) + { + ContainingForm.ActiveControl = null; + e.Handled = true; + e.SuppressKeyPress = true; + } + } + + private void TextToData() + { + try + { + Data = Convert.ToDouble(Box.Text); + } + catch + { + } + + Box.Text = DecimalString(Data); + + if (string.Equals(Box.Text, DefaultText)) + { + SetToDefault(); + } + else + { + SetToEntered(); + } + } + + private string DecimalString(double value) + { + return value.ToString(FormatString); + } + + + #endregion Methods + } +} diff --git a/grapher/Models/Fields/FieldXY.cs b/grapher/Models/Fields/FieldXY.cs new file mode 100644 index 0000000..87e0b9c --- /dev/null +++ b/grapher/Models/Fields/FieldXY.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class FieldXY + { + public const int DefaultSeparation = 4; + + public const string ShortenedFormatString = "0.###"; + + public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData) + { + XField = new Field(xBox, containingForm, defaultData); + YField = new Field(yBox, containingForm, defaultData); + YField.FormatString = ShortenedFormatString; + LockCheckBox = lockCheckBox; + LockCheckBox.CheckedChanged += new System.EventHandler(CheckChanged); + + XField.Box.Width = (YField.Box.Left + YField.Box.Width - XField.Box.Left - DefaultSeparation) / 2; + YField.Box.Width = XField.Box.Width; + + DefaultWidthX = XField.Box.Width; + DefaultWidthY = YField.Box.Width; + + YField.Box.Left = XField.Box.Left + XField.Box.Width + DefaultSeparation; + + CombinedWidth = DefaultWidthX + DefaultWidthY + YField.Box.Left - (XField.Box.Left + DefaultWidthX); + SetCombined(); + } + public double X + { + get => XField.Data; + } + + public double Y + { + get + { + if (Combined) + { + return X; + } + else + { + return YField.Data; + } + } + } + + public CheckBox LockCheckBox { get; } + + public Field XField { get; } + + public Field YField { get; } + + private bool Combined { get; set; } + + private int DefaultWidthX { get; } + + private int DefaultWidthY { get; } + + private int CombinedWidth { get; } + + private void CheckChanged(object sender, EventArgs e) + { + if (LockCheckBox.CheckState == CheckState.Checked) + { + SetCombined(); + } + else + { + SetSeparate(); + } + } + + public void SetCombined() + { + Combined = true; + YField.SetToUnavailable(); + YField.Box.Hide(); + XField.Box.Width = CombinedWidth; + XField.FormatString = Field.DefaultFormatString; + } + + public void SetSeparate() + { + Combined = false; + + XField.Box.Width = DefaultWidthX; + YField.Box.Width = DefaultWidthY; + + XField.FormatString = ShortenedFormatString; + + if (XField.State == Field.FieldState.Default) + { + YField.SetToDefault(); + } + else + { + YField.SetToEntered(XField.Data); + } + + if (XField.Box.Visible) + { + YField.Box.Show(); + } + } + + public void Show() + { + XField.Box.Show(); + + if (!Combined) + { + YField.Box.Show(); + } + } + + public void Hide() + { + XField.Box.Hide(); + YField.Box.Hide(); + } + } +} diff --git a/grapher/Models/Options/AccelOptions.cs b/grapher/Models/Options/AccelOptions.cs new file mode 100644 index 0000000..b233552 --- /dev/null +++ b/grapher/Models/Options/AccelOptions.cs @@ -0,0 +1,79 @@ +using grapher.Layouts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class AccelOptions + { + public const int PossibleOptionsCount = 4; + public const int PossibleOptionsXYCount = 2; + + public static readonly Dictionary AccelerationTypes = new List + { + new DefaultLayout(), + new LinearLayout(), + new ClassicLayout(), + new NaturalLayout(), + new LogLayout(), + new SigmoidLayout(), + new PowerLayout(), + new OffLayout() + }.ToDictionary(k => k.Name); + + public AccelOptions( + ComboBox accelDropdown, + Option[] options, + OptionXY[] optionsXY, + Button writeButton) + { + AccelDropdown = accelDropdown; + AccelDropdown.Items.Clear(); + AccelDropdown.Items.AddRange(AccelerationTypes.Keys.Skip(1).ToArray()); + AccelDropdown.SelectedIndexChanged += new System.EventHandler(OnIndexChanged); + + if (options.Length > PossibleOptionsCount) + { + throw new Exception("Layout given too many options."); + } + + if (optionsXY.Length > PossibleOptionsXYCount) + { + throw new Exception("Layout given too many options."); + } + + Options = options; + OptionsXY = optionsXY; + WriteButton = writeButton; + + Layout("Default"); + } + + public Button WriteButton { get; } + + public ComboBox AccelDropdown { get; } + + public int AccelerationIndex { get; private set; } + + public Option[] Options { get; } + + public OptionXY[] OptionsXY { get; } + + private void OnIndexChanged(object sender, EventArgs e) + { + var accelerationTypeString = AccelDropdown.SelectedItem.ToString(); + Layout(accelerationTypeString); + } + + private void Layout(string type) + { + var accelerationType = AccelerationTypes[type]; + AccelerationIndex = accelerationType.Index; + accelerationType.Layout(Options, OptionsXY, WriteButton); + } + } +} diff --git a/grapher/Models/Options/CapOptions.cs b/grapher/Models/Options/CapOptions.cs new file mode 100644 index 0000000..2ee7f6b --- /dev/null +++ b/grapher/Models/Options/CapOptions.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class CapOptions + { + public CapOptions( + ToolStripMenuItem sensitivityCapCheck, + ToolStripMenuItem velocityGainCapCheck, + OptionXY capOption, + OptionXY weightOption) + { + + SensitivityCapCheck = sensitivityCapCheck; + VelocityGainCapCheck = velocityGainCapCheck; + CapOption = capOption; + WeightOption = weightOption; + + SensitivityCapCheck.Click += new System.EventHandler(OnSensitivityCapCheckClick); + VelocityGainCapCheck.Click += new System.EventHandler(OnVelocityGainCapCheckClick); + + SensitivityCapCheck.CheckedChanged += new System.EventHandler(OnSensitivityCapCheckedChange); + VelocityGainCapCheck.CheckedChanged += new System.EventHandler(OnVelocityGainCapCheckedChange); + + EnableSensitivityCap(); + } + + ToolStripMenuItem SensitivityCapCheck { get; } + + ToolStripMenuItem VelocityGainCapCheck { get; } + + OptionXY CapOption { get; } + + OptionXY WeightOption { get; } + + public double SensitivityCapX { + get + { + if (IsSensitivityGain) + { + return CapOption.Fields.X; + } + else + { + return 0; + } + } + } + + public double SensitivityCapY { + get + { + if (IsSensitivityGain) + { + return CapOption.Fields.Y; + } + else + { + return 0; + } + } + } + + public double VelocityGainCap { + get + { + if (IsSensitivityGain) + { + return 0; + } + else + { + return CapOption.Fields.X; + } + } + } + + public bool IsSensitivityGain { get; private set; } + + void OnSensitivityCapCheckClick(object sender, EventArgs e) + { + if (!SensitivityCapCheck.Checked) + { + VelocityGainCapCheck.Checked = false; + SensitivityCapCheck.Checked = true; + } + } + + void OnVelocityGainCapCheckClick(object sender, EventArgs e) + { + if (!VelocityGainCapCheck.Checked) + { + VelocityGainCapCheck.Checked = true; + SensitivityCapCheck.Checked = false; + } + } + + void OnSensitivityCapCheckedChange(object sender, EventArgs e) + { + if (SensitivityCapCheck.Checked == true) + { + EnableSensitivityCap(); + } + } + + void OnVelocityGainCapCheckedChange(object sender, EventArgs e) + { + if (SensitivityCapCheck.Checked == true) + { + EnableVelocityGainCap(); + } + } + + void EnableSensitivityCap() + { + IsSensitivityGain = true; + CapOption.Fields.LockCheckBox.Enabled = true; + WeightOption.Fields.LockCheckBox.Enabled = true; + CapOption.SetName("Sensitivity Cap"); + } + + void EnableVelocityGainCap() + { + IsSensitivityGain = false; + CapOption.Fields.LockCheckBox.Checked = true; + CapOption.Fields.LockCheckBox.Enabled = false; + WeightOption.Fields.LockCheckBox.Checked = true; + WeightOption.Fields.LockCheckBox.Enabled = false; + CapOption.SetName("Velocity Gain Cap"); + } + } +} diff --git a/grapher/Models/Options/Option.cs b/grapher/Models/Options/Option.cs new file mode 100644 index 0000000..eb5105e --- /dev/null +++ b/grapher/Models/Options/Option.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class Option + { + public Option(Field field, Label label) + { + Field = field; + Label = label; + } + + public Option(TextBox box, Form containingForm, double defaultData, Label label) + : this(new Field(box, containingForm, defaultData), label) + { + } + + public Option(TextBox box, Form containingForm, double defaultData, Label label, string startingName) + : this(box, containingForm, defaultData, label) + { + SetName(startingName); + } + + public Field Field { get; } + + public Label Label { get; } + + public void SetName(string name) + { + Label.Text = name; + Label.Left = Convert.ToInt32((Field.Box.Left / 2.0) - (Label.Width / 2.0)); + } + + public void Hide() + { + Field.Box.Hide(); + Label.Hide(); + } + + public void Show() + { + Field.Box.Show(); + Label.Show(); + } + + public void Show(string name) + { + SetName(name); + + Show(); + } + } +} diff --git a/grapher/Models/Options/OptionXY.cs b/grapher/Models/Options/OptionXY.cs new file mode 100644 index 0000000..ca1559d --- /dev/null +++ b/grapher/Models/Options/OptionXY.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class OptionXY + { + public OptionXY(FieldXY fields, Label label) + { + Fields = fields; + Label = label; + } + + public OptionXY( + TextBox xBox, + TextBox yBox, + CheckBox lockCheckBox, + Form containingForm, + double defaultData, + Label label) + : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData), label) + { + } + + public OptionXY( + TextBox xBox, + TextBox yBox, + CheckBox lockCheckBox, + Form containingForm, + double defaultData, + Label label, + string startingName): + this( + xBox, + yBox, + lockCheckBox, + containingForm, + defaultData, + label) + { + SetName(startingName); + } + + public FieldXY Fields { get; } + + public Label Label { get; } + + public void SetName(string name) + { + Label.Text = name; + Label.Left = Convert.ToInt32((Fields.XField.Box.Left / 2.0) - (Label.Width / 2.0)); + } + + public void Hide() + { + Fields.Hide(); + Fields.LockCheckBox.Hide(); + Label.Hide(); + } + + public void Show() + { + Fields.Show(); + Fields.LockCheckBox.Show(); + Label.Show(); + } + + public void Show(string name) + { + SetName(name); + + Show(); + } + + } +} diff --git a/grapher/Option.cs b/grapher/Option.cs deleted file mode 100644 index eb5105e..0000000 --- a/grapher/Option.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace grapher -{ - public class Option - { - public Option(Field field, Label label) - { - Field = field; - Label = label; - } - - public Option(TextBox box, Form containingForm, double defaultData, Label label) - : this(new Field(box, containingForm, defaultData), label) - { - } - - public Option(TextBox box, Form containingForm, double defaultData, Label label, string startingName) - : this(box, containingForm, defaultData, label) - { - SetName(startingName); - } - - public Field Field { get; } - - public Label Label { get; } - - public void SetName(string name) - { - Label.Text = name; - Label.Left = Convert.ToInt32((Field.Box.Left / 2.0) - (Label.Width / 2.0)); - } - - public void Hide() - { - Field.Box.Hide(); - Label.Hide(); - } - - public void Show() - { - Field.Box.Show(); - Label.Show(); - } - - public void Show(string name) - { - SetName(name); - - Show(); - } - } -} diff --git a/grapher/OptionXY.cs b/grapher/OptionXY.cs deleted file mode 100644 index ca1559d..0000000 --- a/grapher/OptionXY.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace grapher -{ - public class OptionXY - { - public OptionXY(FieldXY fields, Label label) - { - Fields = fields; - Label = label; - } - - public OptionXY( - TextBox xBox, - TextBox yBox, - CheckBox lockCheckBox, - Form containingForm, - double defaultData, - Label label) - : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData), label) - { - } - - public OptionXY( - TextBox xBox, - TextBox yBox, - CheckBox lockCheckBox, - Form containingForm, - double defaultData, - Label label, - string startingName): - this( - xBox, - yBox, - lockCheckBox, - containingForm, - defaultData, - label) - { - SetName(startingName); - } - - public FieldXY Fields { get; } - - public Label Label { get; } - - public void SetName(string name) - { - Label.Text = name; - Label.Left = Convert.ToInt32((Fields.XField.Box.Left / 2.0) - (Label.Width / 2.0)); - } - - public void Hide() - { - Fields.Hide(); - Fields.LockCheckBox.Hide(); - Label.Hide(); - } - - public void Show() - { - Fields.Show(); - Fields.LockCheckBox.Show(); - Label.Show(); - } - - public void Show(string name) - { - SetName(name); - - Show(); - } - - } -} diff --git a/grapher/VectorXY.cs b/grapher/VectorXY.cs deleted file mode 100644 index 53c9e68..0000000 --- a/grapher/VectorXY.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace grapher -{ - public class VectorXY - { - public VectorXY(double x) - { - X = x; - Y = x; - } - - public VectorXY(double x, double y) - { - X = x; - Y = y; - } - - public double X { get; set; } - - public double Y { get; set; } - - public void SetBoth(double value) - { - X = value; - Y = value; - } - } -} diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj index 2cce072..fe29bfd 100644 --- a/grapher/grapher.csproj +++ b/grapher/grapher.csproj @@ -47,13 +47,13 @@ - - - - - - - + + + + + + + Form @@ -69,11 +69,10 @@ - - + + - Form1.cs -- cgit v1.2.3 From b6254f32a6cfbd40bd1919950b344160f38bf1e4 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 19:51:38 -0700 Subject: Factor accel calculations into calculation classes --- grapher/Models/AccelGUI.cs | 94 ++------------------------ grapher/Models/Calculations/AccelCalculator.cs | 91 +++++++++++++++++++++++++ grapher/Models/Calculations/AccelData.cs | 32 +++++++++ grapher/Models/Charts/AccelCharts.cs | 14 +++- grapher/Models/Charts/ChartXY.cs | 6 ++ grapher/grapher.csproj | 2 + 6 files changed, 150 insertions(+), 89 deletions(-) create mode 100644 grapher/Models/Calculations/AccelCalculator.cs create mode 100644 grapher/Models/Calculations/AccelData.cs diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs index 54564b3..5a46a73 100644 --- a/grapher/Models/AccelGUI.cs +++ b/grapher/Models/AccelGUI.cs @@ -1,4 +1,5 @@ -using System; +using grapher.Models.Calculations; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -11,14 +12,6 @@ namespace grapher { public class AccelGUI { - public struct MagnitudeData - { - public double magnitude; - public int x; - public int y; - } - - public static ReadOnlyCollection Magnitudes = GetMagnitudes(); #region constructors @@ -50,11 +43,7 @@ namespace grapher LimitOrExponent = limtOrExp; Midpoint = midpoint; WriteButton = writeButton; - - OrderedAccelPoints = new SortedDictionary(); - OrderedVelocityPoints = new SortedDictionary(); - OrderedGainPoints = new SortedDictionary(); - + AccelData = new AccelData(); ManagedAcceleration.ReadFromDriver(); UpdateGraph(); @@ -90,88 +79,17 @@ namespace grapher public Button WriteButton { get; } - public SortedDictionary OrderedAccelPoints { get; } - - public SortedDictionary OrderedVelocityPoints { get; } - - public SortedDictionary OrderedGainPoints { get; } + public AccelData AccelData { get; } #endregion properties #region methods - public static ReadOnlyCollection GetMagnitudes() - { - var magnitudes = new List(); - for (int i = 0; i < 100; i++) - { - for (int j = 0; j <= i; j++) - { - MagnitudeData magnitudeData; - magnitudeData.magnitude = Magnitude(i, j); - magnitudeData.x = i; - magnitudeData.y = j; - magnitudes.Add(magnitudeData); - } - } - - magnitudes.Sort((m1, m2) => m1.magnitude.CompareTo(m2.magnitude)); - - return magnitudes.AsReadOnly(); - } - - public static double Magnitude(int x, int y) - { - return Math.Sqrt(x * x + y * y); - } - - public static double Magnitude(double x, double y) - { - return Math.Sqrt(x * x + y * y); - } public void UpdateGraph() { - OrderedAccelPoints.Clear(); - OrderedVelocityPoints.Clear(); - OrderedGainPoints.Clear(); - - double lastInputMagnitude = 0; - double lastOutputMagnitude = 0; - - foreach (var magnitudeData in Magnitudes) - { - var output = ManagedAcceleration.Accelerate(magnitudeData.x, magnitudeData.y, 1); - - var outMagnitude = Magnitude(output.Item1, output.Item2); - var ratio = magnitudeData.magnitude > 0 ? outMagnitude / magnitudeData.magnitude : Sensitivity.Fields.X; - - var inDiff = magnitudeData.magnitude - lastInputMagnitude; - var outDiff = outMagnitude - lastOutputMagnitude; - var slope = inDiff > 0 ? outDiff / inDiff : Sensitivity.Fields.X; - - if (!OrderedAccelPoints.ContainsKey(magnitudeData.magnitude)) - { - OrderedAccelPoints.Add(magnitudeData.magnitude, ratio); - } - - if (!OrderedVelocityPoints.ContainsKey(magnitudeData.magnitude)) - { - OrderedVelocityPoints.Add(magnitudeData.magnitude, outMagnitude); - } - - if (!OrderedGainPoints.ContainsKey(magnitudeData.magnitude)) - { - OrderedGainPoints.Add(magnitudeData.magnitude, slope); - } - - lastInputMagnitude = magnitudeData.magnitude; - lastOutputMagnitude = outMagnitude; - } - - AccelCharts.SensitivityChart.ChartX.Series[0].Points.DataBindXY(OrderedAccelPoints.Keys, OrderedAccelPoints.Values); - AccelCharts.VelocityChart.ChartX.Series[0].Points.DataBindXY(OrderedVelocityPoints.Keys, OrderedVelocityPoints.Values); - AccelCharts.GainChart.ChartX.Series[0].Points.DataBindXY(OrderedGainPoints.Keys, OrderedGainPoints.Values); + AccelCalculator.Calculate(AccelData, ManagedAcceleration, Sensitivity.Fields.X); + AccelCharts.Bind(AccelData); } #endregion methods diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs new file mode 100644 index 0000000..2d8d892 --- /dev/null +++ b/grapher/Models/Calculations/AccelCalculator.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Calculations +{ + public static class AccelCalculator + { + public struct MagnitudeData + { + public double magnitude; + public int x; + public int y; + } + + public static ReadOnlyCollection Magnitudes = GetMagnitudes(); + + public static void Calculate(AccelData data, ManagedAccel accel, double starter) + { + data.Clear(); + + double lastInputMagnitude = 0; + double lastOutputMagnitude = 0; + + foreach (var magnitudeData in Magnitudes) + { + var output = accel.Accelerate(magnitudeData.x, magnitudeData.y, 1); + + var outMagnitude = Magnitude(output.Item1, output.Item2); + var ratio = magnitudeData.magnitude > 0 ? outMagnitude / magnitudeData.magnitude : starter; + + var inDiff = magnitudeData.magnitude - lastInputMagnitude; + var outDiff = outMagnitude - lastOutputMagnitude; + var slope = inDiff > 0 ? outDiff / inDiff : starter; + + if (!data.OrderedAccelPoints.ContainsKey(magnitudeData.magnitude)) + { + data.OrderedAccelPoints.Add(magnitudeData.magnitude, ratio); + } + + if (!data.OrderedVelocityPoints.ContainsKey(magnitudeData.magnitude)) + { + data.OrderedVelocityPoints.Add(magnitudeData.magnitude, outMagnitude); + } + + if (!data.OrderedGainPoints.ContainsKey(magnitudeData.magnitude)) + { + data.OrderedGainPoints.Add(magnitudeData.magnitude, slope); + } + + lastInputMagnitude = magnitudeData.magnitude; + lastOutputMagnitude = outMagnitude; + } + + } + + public static ReadOnlyCollection GetMagnitudes() + { + var magnitudes = new List(); + for (int i = 0; i < 100; i++) + { + for (int j = 0; j <= i; j++) + { + MagnitudeData magnitudeData; + magnitudeData.magnitude = Magnitude(i, j); + magnitudeData.x = i; + magnitudeData.y = j; + magnitudes.Add(magnitudeData); + } + } + + magnitudes.Sort((m1, m2) => m1.magnitude.CompareTo(m2.magnitude)); + + return magnitudes.AsReadOnly(); + } + + public static double Magnitude(int x, int y) + { + return Math.Sqrt(x * x + y * y); + } + + public static double Magnitude(double x, double y) + { + return Math.Sqrt(x * x + y * y); + } + + } +} diff --git a/grapher/Models/Calculations/AccelData.cs b/grapher/Models/Calculations/AccelData.cs new file mode 100644 index 0000000..d304660 --- /dev/null +++ b/grapher/Models/Calculations/AccelData.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Calculations +{ + public class AccelData + { + public AccelData() + { + OrderedAccelPoints = new SortedDictionary(); + OrderedVelocityPoints = new SortedDictionary(); + OrderedGainPoints = new SortedDictionary(); + } + + public SortedDictionary OrderedAccelPoints { get; } + + public SortedDictionary OrderedVelocityPoints { get; } + + public SortedDictionary OrderedGainPoints { get; } + + public void Clear() + { + OrderedAccelPoints.Clear(); + OrderedVelocityPoints.Clear(); + OrderedGainPoints.Clear(); + } + + } +} diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs index 9952016..4ed44c7 100644 --- a/grapher/Models/Charts/AccelCharts.cs +++ b/grapher/Models/Charts/AccelCharts.cs @@ -1,4 +1,5 @@ -using System; +using grapher.Models.Calculations; +using System; using System.Collections.Generic; using System.Drawing; using System.Linq; @@ -55,8 +56,17 @@ namespace grapher public ToolStripMenuItem EnableVelocityAndGain { get; } + private bool Combined { get; set; } + private int FormBorderHeight { get; } + public void Bind(AccelData data) + { + SensitivityChart.Bind(data.OrderedAccelPoints); + VelocityChart.Bind(data.OrderedVelocityPoints); + GainChart.Bind(data.OrderedGainPoints); + } + private void OnEnableClick(object sender, EventArgs e) { EnableVelocityAndGain.Checked = !EnableVelocityAndGain.Checked; @@ -99,6 +109,7 @@ namespace grapher VelocityChart.SetSeparate(); GainChart.SetSeparate(); UpdateFormWidth(); + Combined = false; } private void ShowCombined() @@ -107,6 +118,7 @@ namespace grapher VelocityChart.SetCombined(); GainChart.SetCombined(); UpdateFormWidth(); + Combined = true; } private void UpdateFormWidth() diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs index 4bb1bd5..a57801e 100644 --- a/grapher/Models/Charts/ChartXY.cs +++ b/grapher/Models/Charts/ChartXY.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -89,6 +90,11 @@ namespace grapher public bool Combined { get; private set; } + public void Bind(IDictionary data) + { + ChartX.Series[0].Points.DataBindXY(data.Keys, data.Values); + } + public void SetCombined() { if (!Combined) diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj index fe29bfd..cc36a5e 100644 --- a/grapher/grapher.csproj +++ b/grapher/grapher.csproj @@ -47,6 +47,8 @@ + + -- cgit v1.2.3 From 30e1391b224ae028f4476e06a07415a0285ac6c9 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 20:39:53 -0700 Subject: Almost working --- grapher/Form1.cs | 22 ++++---- grapher/Models/Calculations/AccelCalculator.cs | 71 +++++++++++++++++++++----- grapher/Models/Calculations/AccelChartData.cs | 31 +++++++++++ grapher/Models/Calculations/AccelData.cs | 19 ++++--- grapher/Models/Charts/AccelCharts.cs | 59 ++++++++++++++++----- grapher/Models/Charts/ChartXY.cs | 6 +++ grapher/Models/Fields/FieldXY.cs | 7 ++- grapher/Models/Options/OptionXY.cs | 11 ++-- grapher/grapher.csproj | 1 + 9 files changed, 175 insertions(+), 52 deletions(-) create mode 100644 grapher/Models/Calculations/AccelChartData.cs diff --git a/grapher/Form1.cs b/grapher/Form1.cs index da9bd5f..a90cfbc 100644 --- a/grapher/Form1.cs +++ b/grapher/Form1.cs @@ -89,10 +89,19 @@ namespace grapher Marshal.FreeHGlobal(args_ptr); - var sensitivity = new OptionXY(sensitivityBoxX, sensitivityBoxY, sensXYLock, this, 1, sensitivityLabel, "Sensitivity"); + var accelCharts = new AccelCharts( + this, + new ChartXY(AccelerationChart, AccelerationChartY), + new ChartXY(VelocityChart, VelocityChartY), + new ChartXY(GainChart, GainChartY), + showVelocityGainToolStripMenuItem, + new CheckBox[] { sensXYLock, weightXYLock, capXYLock }); + + + var sensitivity = new OptionXY(sensitivityBoxX, sensitivityBoxY, sensXYLock, this, 1, sensitivityLabel, "Sensitivity", accelCharts); var rotation = new Option(rotationBox, this, 0, rotationLabel, "Rotation"); - var weight = new OptionXY(weightBoxFirst, weightBoxSecond, weightXYLock, this, 1, weightLabel, "Weight"); - var cap = new OptionXY(capBoxX, capBoxY, capXYLock, this, 0, capLabel, "Cap"); + var weight = new OptionXY(weightBoxFirst, weightBoxSecond, weightXYLock, this, 1, weightLabel, "Weight", accelCharts); + var cap = new OptionXY(capBoxX, capBoxY, capXYLock, this, 0, capLabel, "Cap", accelCharts); var offset = new Option(offsetBox, this, 0, offsetLabel, "Offset"); // The name and layout of these options is handled by AccelerationOptions object. @@ -124,12 +133,7 @@ namespace grapher AccelGUI = new AccelGUI( this, - new AccelCharts( - this, - new ChartXY(AccelerationChart, AccelerationChartY), - new ChartXY(VelocityChart, VelocityChartY), - new ChartXY(GainChart, GainChartY), - showVelocityGainToolStripMenuItem), + accelCharts, managedAcceleration, accelerationOptions, sensitivity, diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs index 2d8d892..a768ee9 100644 --- a/grapher/Models/Calculations/AccelCalculator.cs +++ b/grapher/Models/Calculations/AccelCalculator.cs @@ -9,6 +9,9 @@ namespace grapher.Models.Calculations { public static class AccelCalculator { + public const int MaxCombined = 100; + public const int MaxXY = 150; + public struct MagnitudeData { public double magnitude; @@ -16,51 +19,59 @@ namespace grapher.Models.Calculations public int y; } - public static ReadOnlyCollection Magnitudes = GetMagnitudes(); + public static ReadOnlyCollection MagnitudesCombined = GetMagnitudes(); + public static ReadOnlyCollection MagnitudesX = GetMagnitudesX(); + public static ReadOnlyCollection MagnitudesY = GetMagnitudesY(); public static void Calculate(AccelData data, ManagedAccel accel, double starter) { data.Clear(); + Calculate(data.Combined, accel, starter, MagnitudesCombined); + Calculate(data.X, accel, starter, MagnitudesX); + Calculate(data.Y, accel, starter, MagnitudesY); + } + + public static void Calculate(AccelChartData data, ManagedAccel accel, double starter, ICollection magnitudeData) + { double lastInputMagnitude = 0; double lastOutputMagnitude = 0; - foreach (var magnitudeData in Magnitudes) + foreach (var magnitudeDatum in MagnitudesCombined) { - var output = accel.Accelerate(magnitudeData.x, magnitudeData.y, 1); + var output = accel.Accelerate(magnitudeDatum.x, magnitudeDatum.y, 1); var outMagnitude = Magnitude(output.Item1, output.Item2); - var ratio = magnitudeData.magnitude > 0 ? outMagnitude / magnitudeData.magnitude : starter; + var ratio = magnitudeDatum.magnitude > 0 ? outMagnitude / magnitudeDatum.magnitude : starter; - var inDiff = magnitudeData.magnitude - lastInputMagnitude; + var inDiff = magnitudeDatum.magnitude - lastInputMagnitude; var outDiff = outMagnitude - lastOutputMagnitude; var slope = inDiff > 0 ? outDiff / inDiff : starter; - if (!data.OrderedAccelPoints.ContainsKey(magnitudeData.magnitude)) + if (!data.AccelPoints.ContainsKey(magnitudeDatum.magnitude)) { - data.OrderedAccelPoints.Add(magnitudeData.magnitude, ratio); + data.AccelPoints.Add(magnitudeDatum.magnitude, ratio); } - if (!data.OrderedVelocityPoints.ContainsKey(magnitudeData.magnitude)) + if (!data.VelocityPoints.ContainsKey(magnitudeDatum.magnitude)) { - data.OrderedVelocityPoints.Add(magnitudeData.magnitude, outMagnitude); + data.VelocityPoints.Add(magnitudeDatum.magnitude, outMagnitude); } - if (!data.OrderedGainPoints.ContainsKey(magnitudeData.magnitude)) + if (!data.GainPoints.ContainsKey(magnitudeDatum.magnitude)) { - data.OrderedGainPoints.Add(magnitudeData.magnitude, slope); + data.GainPoints.Add(magnitudeDatum.magnitude, slope); } - lastInputMagnitude = magnitudeData.magnitude; + lastInputMagnitude = magnitudeDatum.magnitude; lastOutputMagnitude = outMagnitude; } - } public static ReadOnlyCollection GetMagnitudes() { var magnitudes = new List(); - for (int i = 0; i < 100; i++) + for (int i = 0; i < MaxCombined; i++) { for (int j = 0; j <= i; j++) { @@ -77,6 +88,38 @@ namespace grapher.Models.Calculations return magnitudes.AsReadOnly(); } + public static ReadOnlyCollection GetMagnitudesX() + { + var magnitudes = new List(); + + for (int i = 0; i < MaxXY; i++) + { + MagnitudeData magnitudeData; + magnitudeData.magnitude = i; + magnitudeData.x = i; + magnitudeData.y = 0; + magnitudes.Add(magnitudeData); + } + + return magnitudes.AsReadOnly(); + } + + public static ReadOnlyCollection GetMagnitudesY() + { + var magnitudes = new List(); + + for (int i = 0; i < MaxXY; i++) + { + MagnitudeData magnitudeData; + magnitudeData.magnitude = i; + magnitudeData.x = 0; + magnitudeData.y = i; + magnitudes.Add(magnitudeData); + } + + return magnitudes.AsReadOnly(); + } + public static double Magnitude(int x, int y) { return Math.Sqrt(x * x + y * y); diff --git a/grapher/Models/Calculations/AccelChartData.cs b/grapher/Models/Calculations/AccelChartData.cs new file mode 100644 index 0000000..243827a --- /dev/null +++ b/grapher/Models/Calculations/AccelChartData.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Calculations +{ + public class AccelChartData + { + public AccelChartData() + { + AccelPoints = new SortedDictionary(); + VelocityPoints = new SortedDictionary(); + GainPoints = new SortedDictionary(); + } + + public SortedDictionary AccelPoints { get; } + + public SortedDictionary VelocityPoints { get; } + + public SortedDictionary GainPoints { get; } + + public void Clear() + { + AccelPoints.Clear(); + VelocityPoints.Clear(); + GainPoints.Clear(); + } + } +} diff --git a/grapher/Models/Calculations/AccelData.cs b/grapher/Models/Calculations/AccelData.cs index d304660..a2a6a3a 100644 --- a/grapher/Models/Calculations/AccelData.cs +++ b/grapher/Models/Calculations/AccelData.cs @@ -10,23 +10,22 @@ namespace grapher.Models.Calculations { public AccelData() { - OrderedAccelPoints = new SortedDictionary(); - OrderedVelocityPoints = new SortedDictionary(); - OrderedGainPoints = new SortedDictionary(); + Combined = new AccelChartData(); + X = new AccelChartData(); + Y = new AccelChartData(); } - public SortedDictionary OrderedAccelPoints { get; } + public AccelChartData Combined { get; } - public SortedDictionary OrderedVelocityPoints { get; } + public AccelChartData X { get; } - public SortedDictionary OrderedGainPoints { get; } + public AccelChartData Y { get; } public void Clear() { - OrderedAccelPoints.Clear(); - OrderedVelocityPoints.Clear(); - OrderedGainPoints.Clear(); + Combined.Clear(); + X.Clear(); + Y.Clear(); } - } } diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs index 4ed44c7..0728abc 100644 --- a/grapher/Models/Charts/AccelCharts.cs +++ b/grapher/Models/Charts/AccelCharts.cs @@ -22,13 +22,15 @@ namespace grapher ChartXY sensitivityChart, ChartXY velocityChart, ChartXY gainChart, - ToolStripMenuItem enableVelocityAndGain) + ToolStripMenuItem enableVelocityAndGain, + ICollection checkBoxesXY) { ContaingForm = form; SensitivityChart = sensitivityChart; VelocityChart = velocityChart; GainChart = gainChart; EnableVelocityAndGain = enableVelocityAndGain; + CheckBoxesXY = checkBoxesXY; SensitivityChart.SetTop(0); VelocityChart.SetHeight(SensitivityChart.Height); @@ -56,15 +58,38 @@ namespace grapher public ToolStripMenuItem EnableVelocityAndGain { get; } + private ICollection CheckBoxesXY { get; } + private bool Combined { get; set; } private int FormBorderHeight { get; } public void Bind(AccelData data) { - SensitivityChart.Bind(data.OrderedAccelPoints); - VelocityChart.Bind(data.OrderedVelocityPoints); - GainChart.Bind(data.OrderedGainPoints); + if (Combined) + { + SensitivityChart.Bind(data.Combined.AccelPoints); + VelocityChart.Bind(data.Combined.VelocityPoints); + GainChart.Bind(data.Combined.GainPoints); + } + else + { + SensitivityChart.BindXY(data.X.AccelPoints, data.Y.AccelPoints); + VelocityChart.BindXY(data.X.VelocityPoints, data.Y.VelocityPoints); + GainChart.BindXY(data.X.GainPoints, data.Y.GainPoints); + } + } + + public void RefreshXY() + { + if (CheckBoxesXY.All(box => box.Checked)) + { + ShowCombined(); + } + else + { + ShowXandYSeparate(); + } } private void OnEnableClick(object sender, EventArgs e) @@ -105,20 +130,26 @@ namespace grapher private void ShowXandYSeparate() { - SensitivityChart.SetSeparate(); - VelocityChart.SetSeparate(); - GainChart.SetSeparate(); - UpdateFormWidth(); - Combined = false; + if (Combined) + { + SensitivityChart.SetSeparate(); + VelocityChart.SetSeparate(); + GainChart.SetSeparate(); + UpdateFormWidth(); + Combined = false; + } } private void ShowCombined() { - SensitivityChart.SetCombined(); - VelocityChart.SetCombined(); - GainChart.SetCombined(); - UpdateFormWidth(); - Combined = true; + if (!Combined) + { + SensitivityChart.SetCombined(); + VelocityChart.SetCombined(); + GainChart.SetCombined(); + UpdateFormWidth(); + Combined = true; + } } private void UpdateFormWidth() diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs index a57801e..7bb7ac8 100644 --- a/grapher/Models/Charts/ChartXY.cs +++ b/grapher/Models/Charts/ChartXY.cs @@ -95,6 +95,12 @@ namespace grapher ChartX.Series[0].Points.DataBindXY(data.Keys, data.Values); } + public void BindXY(IDictionary dataX, IDictionary dataY) + { + ChartX.Series[0].Points.DataBindXY(dataX.Keys, dataX.Values); + ChartY.Series[0].Points.DataBindXY(dataY.Keys, dataY.Values); + } + public void SetCombined() { if (!Combined) diff --git a/grapher/Models/Fields/FieldXY.cs b/grapher/Models/Fields/FieldXY.cs index 87e0b9c..609af9d 100644 --- a/grapher/Models/Fields/FieldXY.cs +++ b/grapher/Models/Fields/FieldXY.cs @@ -13,13 +13,14 @@ namespace grapher public const string ShortenedFormatString = "0.###"; - public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData) + public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData, AccelCharts accelCharts) { XField = new Field(xBox, containingForm, defaultData); YField = new Field(yBox, containingForm, defaultData); YField.FormatString = ShortenedFormatString; LockCheckBox = lockCheckBox; LockCheckBox.CheckedChanged += new System.EventHandler(CheckChanged); + AccelCharts = accelCharts; XField.Box.Width = (YField.Box.Left + YField.Box.Width - XField.Box.Left - DefaultSeparation) / 2; YField.Box.Width = XField.Box.Width; @@ -58,6 +59,8 @@ namespace grapher public Field YField { get; } + private AccelCharts AccelCharts { get; } + private bool Combined { get; set; } private int DefaultWidthX { get; } @@ -76,6 +79,8 @@ namespace grapher { SetSeparate(); } + + AccelCharts.RefreshXY(); } public void SetCombined() diff --git a/grapher/Models/Options/OptionXY.cs b/grapher/Models/Options/OptionXY.cs index ca1559d..90a46d7 100644 --- a/grapher/Models/Options/OptionXY.cs +++ b/grapher/Models/Options/OptionXY.cs @@ -21,8 +21,9 @@ namespace grapher CheckBox lockCheckBox, Form containingForm, double defaultData, - Label label) - : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData), label) + Label label, + AccelCharts accelCharts) + : this(new FieldXY(xBox, yBox, lockCheckBox, containingForm, defaultData, accelCharts), label) { } @@ -33,14 +34,16 @@ namespace grapher Form containingForm, double defaultData, Label label, - string startingName): + string startingName, + AccelCharts accelCharts): this( xBox, yBox, lockCheckBox, containingForm, defaultData, - label) + label, + accelCharts) { SetName(startingName); } diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj index cc36a5e..6ce0fe4 100644 --- a/grapher/grapher.csproj +++ b/grapher/grapher.csproj @@ -48,6 +48,7 @@ + -- cgit v1.2.3 From 93a22c08b3223b040c3a3644fc3c4ef82fc576f0 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Thu, 13 Aug 2020 01:52:49 -0700 Subject: Dot to show mouse move --- grapher/Form1.Designer.cs | 114 +++- grapher/Form1.cs | 13 +- grapher/Models/AccelGUI.cs | 15 +- grapher/Models/Calculations/AccelCalculator.cs | 10 +- grapher/Models/Calculations/AccelChartData.cs | 12 + grapher/Models/Calculations/AccelData.cs | 32 ++ grapher/Models/Charts/AccelCharts.cs | 73 ++- grapher/Models/Charts/ChartXY.cs | 90 ++- grapher/Models/Mouse/MouseWatcher.cs | 723 +++++++++++++++++++++++++ grapher/grapher.csproj | 1 + 10 files changed, 1009 insertions(+), 74 deletions(-) create mode 100644 grapher/Models/Mouse/MouseWatcher.cs diff --git a/grapher/Form1.Designer.cs b/grapher/Form1.Designer.cs index ab9c6b2..a4c2931 100644 --- a/grapher/Form1.Designer.cs +++ b/grapher/Form1.Designer.cs @@ -33,21 +33,27 @@ namespace grapher System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend(); System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend3 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea4 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend4 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series7 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series8 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend5 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series9 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series10 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea6 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend6 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series11 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series12 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.AccelerationChart = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.accelTypeDrop = new System.Windows.Forms.ComboBox(); this.sensitivityBoxX = new System.Windows.Forms.TextBox(); @@ -86,6 +92,7 @@ namespace grapher this.AccelerationChartY = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.VelocityChartY = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.GainChartY = new System.Windows.Forms.DataVisualization.Charting.Chart(); + this.MouseLabel = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.AccelerationChart)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.VelocityChart)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.GainChart)).BeginInit(); @@ -109,7 +116,13 @@ namespace grapher series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series1.Legend = "Legend1"; series1.Name = "Accelerated Sensitivity"; + series2.ChartArea = "ChartArea1"; + series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; + series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); + series2.Legend = "Legend1"; + series2.Name = "LastAccelVal"; this.AccelerationChart.Series.Add(series1); + this.AccelerationChart.Series.Add(series2); this.AccelerationChart.Size = new System.Drawing.Size(723, 328); this.AccelerationChart.TabIndex = 0; this.AccelerationChart.Text = "chart1"; @@ -340,11 +353,17 @@ namespace grapher this.VelocityChart.Legends.Add(legend2); this.VelocityChart.Location = new System.Drawing.Point(240, 334); this.VelocityChart.Name = "VelocityChart"; - series2.ChartArea = "ChartArea1"; - series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series2.Legend = "Legend1"; - series2.Name = "Mouse Velocity"; - this.VelocityChart.Series.Add(series2); + series3.ChartArea = "ChartArea1"; + series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series3.Legend = "Legend1"; + series3.Name = "Mouse Velocity"; + series4.ChartArea = "ChartArea1"; + series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; + series4.Color = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); + series4.Legend = "Legend1"; + series4.Name = "LastVelocityVal"; + this.VelocityChart.Series.Add(series3); + this.VelocityChart.Series.Add(series4); this.VelocityChart.Size = new System.Drawing.Size(723, 307); this.VelocityChart.TabIndex = 28; this.VelocityChart.Text = "chart1"; @@ -359,11 +378,17 @@ namespace grapher this.GainChart.Legends.Add(legend3); this.GainChart.Location = new System.Drawing.Point(240, 647); this.GainChart.Name = "GainChart"; - series3.ChartArea = "ChartArea1"; - series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series3.Legend = "Legend1"; - series3.Name = "Velocity Gain"; - this.GainChart.Series.Add(series3); + series5.ChartArea = "ChartArea1"; + series5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series5.Legend = "Legend1"; + series5.Name = "Velocity Gain"; + series6.ChartArea = "ChartArea1"; + series6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; + series6.Color = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); + series6.Legend = "Legend1"; + series6.Name = "LastGainVal"; + this.GainChart.Series.Add(series5); + this.GainChart.Series.Add(series6); this.GainChart.Size = new System.Drawing.Size(723, 309); this.GainChart.TabIndex = 29; this.GainChart.Text = "chart1"; @@ -437,11 +462,17 @@ namespace grapher this.AccelerationChartY.Legends.Add(legend4); this.AccelerationChartY.Location = new System.Drawing.Point(969, 0); this.AccelerationChartY.Name = "AccelerationChartY"; - series4.ChartArea = "ChartArea1"; - series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series4.Legend = "Legend1"; - series4.Name = "Accelerated Sensitivity"; - this.AccelerationChartY.Series.Add(series4); + series7.ChartArea = "ChartArea1"; + series7.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series7.Legend = "Legend1"; + series7.Name = "Accelerated Sensitivity"; + series8.ChartArea = "ChartArea1"; + series8.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; + series8.Color = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); + series8.Legend = "Legend1"; + series8.Name = "LastAccelVal"; + this.AccelerationChartY.Series.Add(series7); + this.AccelerationChartY.Series.Add(series8); this.AccelerationChartY.Size = new System.Drawing.Size(723, 328); this.AccelerationChartY.TabIndex = 31; this.AccelerationChartY.Text = "chart1"; @@ -456,11 +487,17 @@ namespace grapher this.VelocityChartY.Legends.Add(legend5); this.VelocityChartY.Location = new System.Drawing.Point(970, 334); this.VelocityChartY.Name = "VelocityChartY"; - series5.ChartArea = "ChartArea1"; - series5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series5.Legend = "Legend1"; - series5.Name = "Mouse Velocity"; - this.VelocityChartY.Series.Add(series5); + series9.ChartArea = "ChartArea1"; + series9.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series9.Legend = "Legend1"; + series9.Name = "Mouse Velocity"; + series10.ChartArea = "ChartArea1"; + series10.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; + series10.Color = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); + series10.Legend = "Legend1"; + series10.Name = "LastVelocityVal"; + this.VelocityChartY.Series.Add(series9); + this.VelocityChartY.Series.Add(series10); this.VelocityChartY.Size = new System.Drawing.Size(723, 307); this.VelocityChartY.TabIndex = 32; this.VelocityChartY.Text = "chart1"; @@ -475,20 +512,36 @@ namespace grapher this.GainChartY.Legends.Add(legend6); this.GainChartY.Location = new System.Drawing.Point(970, 647); this.GainChartY.Name = "GainChartY"; - series6.ChartArea = "ChartArea1"; - series6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series6.Legend = "Legend1"; - series6.Name = "Velocity Gain"; - this.GainChartY.Series.Add(series6); + series11.ChartArea = "ChartArea1"; + series11.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series11.Legend = "Legend1"; + series11.Name = "Velocity Gain"; + series12.ChartArea = "ChartArea1"; + series12.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; + series12.Color = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); + series12.Legend = "Legend1"; + series12.Name = "LastGainVal"; + this.GainChartY.Series.Add(series11); + this.GainChartY.Series.Add(series12); this.GainChartY.Size = new System.Drawing.Size(723, 309); this.GainChartY.TabIndex = 33; this.GainChartY.Text = "chart1"; // + // MouseLabel + // + this.MouseLabel.AutoSize = true; + this.MouseLabel.Location = new System.Drawing.Point(1, 24); + this.MouseLabel.Name = "MouseLabel"; + this.MouseLabel.Size = new System.Drawing.Size(80, 13); + this.MouseLabel.TabIndex = 34; + this.MouseLabel.Text = "Last (x, y): (x, y)"; + // // RawAcceleration // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1693, 958); + this.Controls.Add(this.MouseLabel); this.Controls.Add(this.GainChartY); this.Controls.Add(this.VelocityChartY); this.Controls.Add(this.AccelerationChartY); @@ -577,6 +630,7 @@ namespace grapher private System.Windows.Forms.DataVisualization.Charting.Chart AccelerationChartY; private System.Windows.Forms.DataVisualization.Charting.Chart VelocityChartY; private System.Windows.Forms.DataVisualization.Charting.Chart GainChartY; + private System.Windows.Forms.Label MouseLabel; } } diff --git a/grapher/Form1.cs b/grapher/Form1.cs index a90cfbc..3df1b67 100644 --- a/grapher/Form1.cs +++ b/grapher/Form1.cs @@ -144,7 +144,8 @@ namespace grapher acceleration, limitOrExponent, midpoint, - writeButton); + writeButton, + MouseLabel); } #endregion Constructor @@ -157,6 +158,16 @@ namespace grapher #region Methods + protected override void WndProc(ref Message m) + { + if (m.Msg == 0x00ff) + { + AccelGUI.MouseWatcher.ReadMouseMove(m); + } + + base.WndProc(ref m); + } + private void Form1_Load(object sender, EventArgs e) { diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs index 5a46a73..a63bf07 100644 --- a/grapher/Models/AccelGUI.cs +++ b/grapher/Models/AccelGUI.cs @@ -1,4 +1,5 @@ using grapher.Models.Calculations; +using grapher.Models.Mouse; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -11,7 +12,7 @@ using System.Windows.Forms.DataVisualization.Charting; namespace grapher { public class AccelGUI - { + { #region constructors @@ -28,7 +29,8 @@ namespace grapher Option acceleration, Option limtOrExp, Option midpoint, - Button writeButton) + Button writeButton, + Label mouseMoveLabel) { AccelForm = accelForm; AccelCharts = accelCharts; @@ -43,10 +45,11 @@ namespace grapher LimitOrExponent = limtOrExp; Midpoint = midpoint; WriteButton = writeButton; - AccelData = new AccelData(); ManagedAcceleration.ReadFromDriver(); UpdateGraph(); + + MouseWatcher = new MouseWatcher(AccelForm, mouseMoveLabel, AccelCharts); } #endregion constructors @@ -79,7 +82,7 @@ namespace grapher public Button WriteButton { get; } - public AccelData AccelData { get; } + public MouseWatcher MouseWatcher { get; } #endregion properties @@ -88,8 +91,8 @@ namespace grapher public void UpdateGraph() { - AccelCalculator.Calculate(AccelData, ManagedAcceleration, Sensitivity.Fields.X); - AccelCharts.Bind(AccelData); + AccelCalculator.Calculate(AccelCharts.AccelData, ManagedAcceleration, Sensitivity.Fields.X, Sensitivity.Fields.Y); + AccelCharts.Bind(); } #endregion methods diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs index a768ee9..d61a59b 100644 --- a/grapher/Models/Calculations/AccelCalculator.cs +++ b/grapher/Models/Calculations/AccelCalculator.cs @@ -23,13 +23,13 @@ namespace grapher.Models.Calculations public static ReadOnlyCollection MagnitudesX = GetMagnitudesX(); public static ReadOnlyCollection MagnitudesY = GetMagnitudesY(); - public static void Calculate(AccelData data, ManagedAccel accel, double starter) + public static void Calculate(AccelData data, ManagedAccel accel, double starterX, double starterY) { data.Clear(); - Calculate(data.Combined, accel, starter, MagnitudesCombined); - Calculate(data.X, accel, starter, MagnitudesX); - Calculate(data.Y, accel, starter, MagnitudesY); + Calculate(data.Combined, accel, starterX, MagnitudesCombined); + Calculate(data.X, accel, starterY, MagnitudesX); + Calculate(data.Y, accel, starterY, MagnitudesY); } public static void Calculate(AccelChartData data, ManagedAccel accel, double starter, ICollection magnitudeData) @@ -37,7 +37,7 @@ namespace grapher.Models.Calculations double lastInputMagnitude = 0; double lastOutputMagnitude = 0; - foreach (var magnitudeDatum in MagnitudesCombined) + foreach (var magnitudeDatum in magnitudeData) { var output = accel.Accelerate(magnitudeDatum.x, magnitudeDatum.y, 1); diff --git a/grapher/Models/Calculations/AccelChartData.cs b/grapher/Models/Calculations/AccelChartData.cs index 243827a..a3354aa 100644 --- a/grapher/Models/Calculations/AccelChartData.cs +++ b/grapher/Models/Calculations/AccelChartData.cs @@ -27,5 +27,17 @@ namespace grapher.Models.Calculations VelocityPoints.Clear(); GainPoints.Clear(); } + + public (double, double, double) FindInValuesFromOut(double outVelocityValue) + { + var velIdx = ~VelocityPoints.Values.ToList().BinarySearch(outVelocityValue); + + if (velIdx < 0) + { + velIdx = ~velIdx; + } + + return (VelocityPoints.ElementAt(velIdx).Key, AccelPoints.ElementAt(velIdx).Value, GainPoints.ElementAt(velIdx).Value); + } } } diff --git a/grapher/Models/Calculations/AccelData.cs b/grapher/Models/Calculations/AccelData.cs index a2a6a3a..a8f3307 100644 --- a/grapher/Models/Calculations/AccelData.cs +++ b/grapher/Models/Calculations/AccelData.cs @@ -3,11 +3,13 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using static grapher.AccelCharts; namespace grapher.Models.Calculations { public class AccelData { + public AccelData() { Combined = new AccelChartData(); @@ -27,5 +29,35 @@ namespace grapher.Models.Calculations X.Clear(); Y.Clear(); } + + public void CalculateDots(int x, int y, ref EstimatedPoints estimation) + { + var magnitude = AccelCalculator.Magnitude(x, y); + + estimation.CombinedVelocity.Y = magnitude; + estimation.XVelocity.Y = Math.Abs(x); + estimation.YVelocity.Y = Math.Abs(y); + + (var inCombVel, var combSens, var combGain) = Combined.FindInValuesFromOut(magnitude); + estimation.CombinedVelocity.X = inCombVel; + estimation.CombinedSensitivity.X = inCombVel; + estimation.CombinedGain.X = inCombVel; + estimation.CombinedSensitivity.Y = combSens; + estimation.CombinedGain.Y = combGain; + + (var inXVel, var xSens, var xGain) = X.FindInValuesFromOut(estimation.XVelocity.Y); + estimation.XVelocity.X = inXVel; + estimation.XSensitivity.X = inXVel; + estimation.XGain.X = inXVel; + estimation.XSensitivity.Y = xSens; + estimation.XGain.Y = xGain; + + (var inYVel, var ySens, var yGain) = Y.FindInValuesFromOut(estimation.YVelocity.Y); + estimation.YVelocity.X = inYVel; + estimation.YSensitivity.X = inYVel; + estimation.YGain.X = inYVel; + estimation.YSensitivity.Y = ySens; + estimation.YGain.Y = yGain; + } } } diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs index 0728abc..e593bb9 100644 --- a/grapher/Models/Charts/AccelCharts.cs +++ b/grapher/Models/Charts/AccelCharts.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Security.Permissions; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,6 +13,27 @@ namespace grapher { public class AccelCharts { + public struct ChartPoint + { + public double X; + public double Y; + } + + public struct EstimatedPoints + { + public ChartPoint CombinedVelocity; + public ChartPoint CombinedSensitivity; + public ChartPoint CombinedGain; + + public ChartPoint XVelocity; + public ChartPoint XSensitivity; + public ChartPoint XGain; + + public ChartPoint YVelocity; + public ChartPoint YSensitivity; + public ChartPoint YGain; + } + public const int ChartSeparationVertical = 10; /// Needed to show full contents in form. Unsure why. @@ -31,6 +53,22 @@ namespace grapher GainChart = gainChart; EnableVelocityAndGain = enableVelocityAndGain; CheckBoxesXY = checkBoxesXY; + AccelData = new AccelData(); + + Estimated = new EstimatedPoints + { + CombinedVelocity = new ChartPoint { X = 0, Y = 0 }, + CombinedSensitivity = new ChartPoint { X = 0, Y = 0 }, + CombinedGain = new ChartPoint { X = 0, Y = 0 }, + + XVelocity = new ChartPoint { X = 0, Y = 0 }, + XSensitivity = new ChartPoint { X = 0, Y = 0 }, + XGain = new ChartPoint { X = 0, Y = 0 }, + + YVelocity = new ChartPoint { X = 0, Y = 0 }, + YSensitivity = new ChartPoint { X = 0, Y = 0 }, + YGain = new ChartPoint { X = 0, Y = 0 }, + }; SensitivityChart.SetTop(0); VelocityChart.SetHeight(SensitivityChart.Height); @@ -45,6 +83,7 @@ namespace grapher EnableVelocityAndGain.CheckedChanged += new System.EventHandler(OnEnableCheckStateChange); HideVelocityAndGain(); + Combined = false; ShowCombined(); } @@ -58,25 +97,37 @@ namespace grapher public ToolStripMenuItem EnableVelocityAndGain { get; } + public AccelData AccelData { get; } + + private EstimatedPoints Estimated; + private ICollection CheckBoxesXY { get; } private bool Combined { get; set; } private int FormBorderHeight { get; } - public void Bind(AccelData data) + public void MakeDots(int x, int y) + { + AccelData.CalculateDots(x, y, ref Estimated); + SensitivityChart.DrawPoints(Estimated.CombinedSensitivity, Estimated.XSensitivity, Estimated.YSensitivity); + VelocityChart.DrawPoints(Estimated.CombinedVelocity, Estimated.XVelocity, Estimated.YVelocity); + GainChart.DrawPoints(Estimated.CombinedGain, Estimated.XGain, Estimated.YGain); + } + + public void Bind() { if (Combined) { - SensitivityChart.Bind(data.Combined.AccelPoints); - VelocityChart.Bind(data.Combined.VelocityPoints); - GainChart.Bind(data.Combined.GainPoints); + SensitivityChart.Bind(AccelData.Combined.AccelPoints); + VelocityChart.Bind(AccelData.Combined.VelocityPoints); + GainChart.Bind(AccelData.Combined.GainPoints); } else { - SensitivityChart.BindXY(data.X.AccelPoints, data.Y.AccelPoints); - VelocityChart.BindXY(data.X.VelocityPoints, data.Y.VelocityPoints); - GainChart.BindXY(data.X.GainPoints, data.Y.GainPoints); + SensitivityChart.BindXY(AccelData.X.AccelPoints, AccelData.Y.AccelPoints); + VelocityChart.BindXY(AccelData.X.VelocityPoints, AccelData.Y.VelocityPoints); + GainChart.BindXY(AccelData.X.GainPoints, AccelData.Y.GainPoints); } } @@ -132,11 +183,13 @@ namespace grapher { if (Combined) { + Combined = false; + SensitivityChart.SetSeparate(); VelocityChart.SetSeparate(); GainChart.SetSeparate(); UpdateFormWidth(); - Combined = false; + Bind(); } } @@ -144,11 +197,13 @@ namespace grapher { if (!Combined) { + Combined = true; + SensitivityChart.SetCombined(); VelocityChart.SetCombined(); GainChart.SetCombined(); UpdateFormWidth(); - Combined = true; + Bind(); } } diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs index 7bb7ac8..2c0ce2c 100644 --- a/grapher/Models/Charts/ChartXY.cs +++ b/grapher/Models/Charts/ChartXY.cs @@ -1,18 +1,26 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; +using static grapher.AccelCharts; namespace grapher { public class ChartXY { + #region Consts + public const int ChartSeparationHorizontal = 10; + #endregion Consts + + #region Constructors + public ChartXY(Chart chartX, Chart chartY) { ChartX = chartX; @@ -27,31 +35,13 @@ namespace grapher SetupChart(ChartY); } - public Chart ChartX { get; } - - public Chart ChartY { get; } - - public static void SetupChart(Chart chart) - { - chart.ChartAreas[0].AxisX.RoundAxisValues(); - - chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; - chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; - - chart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; - chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; - - chart.ChartAreas[0].CursorY.Interval = 0.001; + #endregion Constructors - chart.ChartAreas[0].CursorX.AutoScroll = true; - chart.ChartAreas[0].CursorY.AutoScroll = true; + #region Properties - chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; - chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; + public Chart ChartX { get; } - chart.ChartAreas[0].CursorX.IsUserEnabled = true; - chart.ChartAreas[0].CursorY.IsUserEnabled = true; - } + public Chart ChartY { get; } public int Height { get @@ -77,7 +67,7 @@ namespace grapher public int Top { get { - return ChartX.Height; + return ChartX.Top; } } @@ -90,6 +80,58 @@ namespace grapher public bool Combined { get; private set; } + #endregion Properties + + #region Methods + + public static void SetupChart(Chart chart) + { + chart.ChartAreas[0].AxisX.RoundAxisValues(); + + chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; + chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; + + chart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01; + chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001; + + chart.ChartAreas[0].CursorY.Interval = 0.001; + + chart.ChartAreas[0].CursorX.AutoScroll = true; + chart.ChartAreas[0].CursorY.AutoScroll = true; + + chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; + chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; + + chart.ChartAreas[0].CursorX.IsUserEnabled = true; + chart.ChartAreas[0].CursorY.IsUserEnabled = true; + + chart.Series[1].Points.Clear(); + chart.Series[1].Points.AddXY(0, 0); + } + + public static void DrawPoint(Chart chart, ChartPoint point) + { + chart.Series[1].Points[0].XValue = point.X; + chart.Series[1].Points[0].YValues[0] = point.Y; + } + + public void DrawPoints(ChartPoint CombinedPoint, ChartPoint XPoint, ChartPoint YPoint) + { + if (Combined) + { + DrawPoint(ChartX, CombinedPoint); + } + else + { + DrawPoint(ChartX, XPoint); + } + + if (ChartY.Visible) + { + DrawPoint(ChartY, YPoint); + } + } + public void Bind(IDictionary data) { ChartX.Series[0].Points.DataBindXY(data.Keys, data.Values); @@ -150,5 +192,7 @@ namespace grapher ChartX.Height = height; ChartY.Height = height; } + + #endregion Methods } } diff --git a/grapher/Models/Mouse/MouseWatcher.cs b/grapher/Models/Mouse/MouseWatcher.cs new file mode 100644 index 0000000..9b1479e --- /dev/null +++ b/grapher/Models/Mouse/MouseWatcher.cs @@ -0,0 +1,723 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Mouse +{ + public class MouseWatcher + { + /// + /// Enumeration containing HID usage page flags. + /// + public enum HIDUsagePage : ushort + { + /// Unknown usage page. + Undefined = 0x00, + /// Generic desktop controls. + Generic = 0x01, + /// Simulation controls. + Simulation = 0x02, + /// Virtual reality controls. + VR = 0x03, + /// Sports controls. + Sport = 0x04, + /// Games controls. + Game = 0x05, + /// Keyboard controls. + Keyboard = 0x07, + /// LED controls. + LED = 0x08, + /// Button. + Button = 0x09, + /// Ordinal. + Ordinal = 0x0A, + /// Telephony. + Telephony = 0x0B, + /// Consumer. + Consumer = 0x0C, + /// Digitizer. + Digitizer = 0x0D, + /// Physical interface device. + PID = 0x0F, + /// Unicode. + Unicode = 0x10, + /// Alphanumeric display. + AlphaNumeric = 0x14, + /// Medical instruments. + Medical = 0x40, + /// Monitor page 0. + MonitorPage0 = 0x80, + /// Monitor page 1. + MonitorPage1 = 0x81, + /// Monitor page 2. + MonitorPage2 = 0x82, + /// Monitor page 3. + MonitorPage3 = 0x83, + /// Power page 0. + PowerPage0 = 0x84, + /// Power page 1. + PowerPage1 = 0x85, + /// Power page 2. + PowerPage2 = 0x86, + /// Power page 3. + PowerPage3 = 0x87, + /// Bar code scanner. + BarCode = 0x8C, + /// Scale page. + Scale = 0x8D, + /// Magnetic strip reading devices. + MSR = 0x8E + } + + /// + /// Enumeration containing the HID usage values. + /// + public enum HIDUsage : ushort + { + /// + Pointer = 0x01, + /// + Mouse = 0x02, + /// + Joystick = 0x04, + /// + Gamepad = 0x05, + /// + Keyboard = 0x06, + /// + Keypad = 0x07, + /// + SystemControl = 0x80, + /// + X = 0x30, + /// + Y = 0x31, + /// + Z = 0x32, + /// + RelativeX = 0x33, + /// + RelativeY = 0x34, + /// + RelativeZ = 0x35, + /// + Slider = 0x36, + /// + Dial = 0x37, + /// + Wheel = 0x38, + /// + HatSwitch = 0x39, + /// + CountedBuffer = 0x3A, + /// + ByteCount = 0x3B, + /// + MotionWakeup = 0x3C, + /// + VX = 0x40, + /// + VY = 0x41, + /// + VZ = 0x42, + /// + VBRX = 0x43, + /// + VBRY = 0x44, + /// + VBRZ = 0x45, + /// + VNO = 0x46, + /// + SystemControlPower = 0x81, + /// + SystemControlSleep = 0x82, + /// + SystemControlWake = 0x83, + /// + SystemControlContextMenu = 0x84, + /// + SystemControlMainMenu = 0x85, + /// + SystemControlApplicationMenu = 0x86, + /// + SystemControlHelpMenu = 0x87, + /// + SystemControlMenuExit = 0x88, + /// + SystemControlMenuSelect = 0x89, + /// + SystemControlMenuRight = 0x8A, + /// + SystemControlMenuLeft = 0x8B, + /// + SystemControlMenuUp = 0x8C, + /// + SystemControlMenuDown = 0x8D, + /// + KeyboardNoEvent = 0x00, + /// + KeyboardRollover = 0x01, + /// + KeyboardPostFail = 0x02, + /// + KeyboardUndefined = 0x03, + /// + KeyboardaA = 0x04, + /// + KeyboardzZ = 0x1D, + /// + Keyboard1 = 0x1E, + /// + Keyboard0 = 0x27, + /// + KeyboardLeftControl = 0xE0, + /// + KeyboardLeftShift = 0xE1, + /// + KeyboardLeftALT = 0xE2, + /// + KeyboardLeftGUI = 0xE3, + /// + KeyboardRightControl = 0xE4, + /// + KeyboardRightShift = 0xE5, + /// + KeyboardRightALT = 0xE6, + /// + KeyboardRightGUI = 0xE7, + /// + KeyboardScrollLock = 0x47, + /// + KeyboardNumLock = 0x53, + /// + KeyboardCapsLock = 0x39, + /// + KeyboardF1 = 0x3A, + /// + KeyboardF12 = 0x45, + /// + KeyboardReturn = 0x28, + /// + KeyboardEscape = 0x29, + /// + KeyboardDelete = 0x2A, + /// + KeyboardPrintScreen = 0x46, + /// + LEDNumLock = 0x01, + /// + LEDCapsLock = 0x02, + /// + LEDScrollLock = 0x03, + /// + LEDCompose = 0x04, + /// + LEDKana = 0x05, + /// + LEDPower = 0x06, + /// + LEDShift = 0x07, + /// + LEDDoNotDisturb = 0x08, + /// + LEDMute = 0x09, + /// + LEDToneEnable = 0x0A, + /// + LEDHighCutFilter = 0x0B, + /// + LEDLowCutFilter = 0x0C, + /// + LEDEqualizerEnable = 0x0D, + /// + LEDSoundFieldOn = 0x0E, + /// + LEDSurroundFieldOn = 0x0F, + /// + LEDRepeat = 0x10, + /// + LEDStereo = 0x11, + /// + LEDSamplingRateDirect = 0x12, + /// + LEDSpinning = 0x13, + /// + LEDCAV = 0x14, + /// + LEDCLV = 0x15, + /// + LEDRecordingFormatDet = 0x16, + /// + LEDOffHook = 0x17, + /// + LEDRing = 0x18, + /// + LEDMessageWaiting = 0x19, + /// + LEDDataMode = 0x1A, + /// + LEDBatteryOperation = 0x1B, + /// + LEDBatteryOK = 0x1C, + /// + LEDBatteryLow = 0x1D, + /// + LEDSpeaker = 0x1E, + /// + LEDHeadset = 0x1F, + /// + LEDHold = 0x20, + /// + LEDMicrophone = 0x21, + /// + LEDCoverage = 0x22, + /// + LEDNightMode = 0x23, + /// + LEDSendCalls = 0x24, + /// + LEDCallPickup = 0x25, + /// + LEDConference = 0x26, + /// + LEDStandBy = 0x27, + /// + LEDCameraOn = 0x28, + /// + LEDCameraOff = 0x29, + /// + LEDOnLine = 0x2A, + /// + LEDOffLine = 0x2B, + /// + LEDBusy = 0x2C, + /// + LEDReady = 0x2D, + /// + LEDPaperOut = 0x2E, + /// + LEDPaperJam = 0x2F, + /// + LEDRemote = 0x30, + /// + LEDForward = 0x31, + /// + LEDReverse = 0x32, + /// + LEDStop = 0x33, + /// + LEDRewind = 0x34, + /// + LEDFastForward = 0x35, + /// + LEDPlay = 0x36, + /// + LEDPause = 0x37, + /// + LEDRecord = 0x38, + /// + LEDError = 0x39, + /// + LEDSelectedIndicator = 0x3A, + /// + LEDInUseIndicator = 0x3B, + /// + LEDMultiModeIndicator = 0x3C, + /// + LEDIndicatorOn = 0x3D, + /// + LEDIndicatorFlash = 0x3E, + /// + LEDIndicatorSlowBlink = 0x3F, + /// + LEDIndicatorFastBlink = 0x40, + /// + LEDIndicatorOff = 0x41, + /// + LEDFlashOnTime = 0x42, + /// + LEDSlowBlinkOnTime = 0x43, + /// + LEDSlowBlinkOffTime = 0x44, + /// + LEDFastBlinkOnTime = 0x45, + /// + LEDFastBlinkOffTime = 0x46, + /// + LEDIndicatorColor = 0x47, + /// + LEDRed = 0x48, + /// + LEDGreen = 0x49, + /// + LEDAmber = 0x4A, + /// + LEDGenericIndicator = 0x3B, + /// + TelephonyPhone = 0x01, + /// + TelephonyAnsweringMachine = 0x02, + /// + TelephonyMessageControls = 0x03, + /// + TelephonyHandset = 0x04, + /// + TelephonyHeadset = 0x05, + /// + TelephonyKeypad = 0x06, + /// + TelephonyProgrammableButton = 0x07, + /// + SimulationRudder = 0xBA, + /// + SimulationThrottle = 0xBB + } + + /// Enumeration containing flags for a raw input device. + [Flags()] + public enum RawInputDeviceFlags + { + /// No flags. + None = 0, + /// If set, this removes the top level collection from the inclusion list. This tells the operating system to stop reading from a device which matches the top level collection. + Remove = 0x00000001, + /// If set, this specifies the top level collections to exclude when reading a complete usage page. This flag only affects a TLC whose usage page is already specified with PageOnly. + Exclude = 0x00000010, + /// If set, this specifies all devices whose top level collection is from the specified usUsagePage. Note that Usage must be zero. To exclude a particular top level collection, use Exclude. + PageOnly = 0x00000020, + /// If set, this prevents any devices specified by UsagePage or Usage from generating legacy messages. This is only for the mouse and keyboard. + NoLegacy = 0x00000030, + /// If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that WindowHandle must be specified. + InputSink = 0x00000100, + /// If set, the mouse button click does not activate the other window. + CaptureMouse = 0x00000200, + /// If set, the application-defined keyboard device hotkeys are not handled. However, the system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default, all keyboard hotkeys are handled. NoHotKeys can be specified even if NoLegacy is not specified and WindowHandle is NULL. + NoHotKeys = 0x00000200, + /// If set, application keys are handled. NoLegacy must be specified. Keyboard only. + AppKeys = 0x00000400 + } + + /// Value type for raw input devices. + [StructLayout(LayoutKind.Sequential)] + public struct RAWINPUTDEVICE + { + /// Top level collection Usage page for the raw input device. + public HIDUsagePage UsagePage; + /// Top level collection Usage for the raw input device. + public HIDUsage Usage; + /// Mode flag that specifies how to interpret the information provided by UsagePage and Usage. + public RawInputDeviceFlags Flags; + /// Handle to the target device. If NULL, it follows the keyboard focus. + public IntPtr WindowHandle; + } + + /// + /// Contains the raw input from a device. + /// + [StructLayout(LayoutKind.Sequential)] + public struct RawInput + { + /// + /// Header for the data. + /// + public RAWINPUTHEADER Header; + public Union Data; + [StructLayout(LayoutKind.Explicit)] + public struct Union + { + /// + /// Mouse raw input data. + /// + [FieldOffset(0)] + public RawMouse Mouse; + /// + /// Keyboard raw input data. + /// + [FieldOffset(0)] + public RawKeyboard Keyboard; + /// + /// HID raw input data. + /// + [FieldOffset(0)] + public RawHID HID; + } + } + + /// + /// Value type for raw input from a HID. + /// + [StructLayout(LayoutKind.Sequential)] + public struct RawHID + { + /// Size of the HID data in bytes. + public int Size; + /// Number of HID in Data. + public int Count; + /// Data for the HID. + public IntPtr Data; + } + + /// + /// Value type for a raw input header. + /// + [StructLayout(LayoutKind.Sequential)] + public struct RAWINPUTHEADER + { + /// Type of device the input is coming from. + public RawInputType Type; + /// Size of the packet of data. + public int Size; + /// Handle to the device sending the data. + public IntPtr Device; + /// wParam from the window message. + public IntPtr wParam; + } + + /// + /// Enumeration containing the type device the raw input is coming from. + /// + public enum RawInputType + { + /// + /// Mouse input. + /// + Mouse = 0, + + /// + /// Keyboard input. + /// + Keyboard = 1, + + /// + /// Human interface device input. + /// + HID = 2, + + /// + /// Another device that is not the keyboard or the mouse. + /// + Other = 3 + } + + /// + /// Contains information about the state of the mouse. + /// + [StructLayout(LayoutKind.Sequential)] + public struct RawMouse + { + /// + /// The mouse state. + /// + public RawMouseFlags Flags; + + [StructLayout(LayoutKind.Explicit)] + public struct Data + { + [FieldOffset(0)] + public uint Buttons; + /// + /// If the mouse wheel is moved, this will contain the delta amount. + /// + [FieldOffset(2)] + public ushort ButtonData; + /// + /// Flags for the event. + /// + [FieldOffset(0)] + public RawMouseButtons ButtonFlags; + } + + public Data _Data; + + /// + /// Raw button data. + /// + public uint RawButtons; + /// + /// The motion in the X direction. This is signed relative motion or + /// absolute motion, depending on the value of usFlags. + /// + public int LastX; + /// + /// The motion in the Y direction. This is signed relative motion or absolute motion, + /// depending on the value of usFlags. + /// + public int LastY; + /// + /// The device-specific additional information for the event. + /// + public uint ExtraInformation; + } + + /// + /// Enumeration containing the flags for raw mouse data. + /// + [Flags()] + public enum RawMouseFlags + : ushort + { + /// Relative to the last position. + MoveRelative = 0, + /// Absolute positioning. + MoveAbsolute = 1, + /// Coordinate data is mapped to a virtual desktop. + VirtualDesktop = 2, + /// Attributes for the mouse have changed. + AttributesChanged = 4 + } + + /// + /// Enumeration containing the button data for raw mouse input. + /// + [Flags()] + public enum RawMouseButtons + : ushort + { + /// No button. + None = 0, + /// Left (button 1) down. + LeftDown = 0x0001, + /// Left (button 1) up. + LeftUp = 0x0002, + /// Right (button 2) down. + RightDown = 0x0004, + /// Right (button 2) up. + RightUp = 0x0008, + /// Middle (button 3) down. + MiddleDown = 0x0010, + /// Middle (button 3) up. + MiddleUp = 0x0020, + /// Button 4 down. + Button4Down = 0x0040, + /// Button 4 up. + Button4Up = 0x0080, + /// Button 5 down. + Button5Down = 0x0100, + /// Button 5 up. + Button5Up = 0x0200, + /// Mouse wheel moved. + MouseWheel = 0x0400 + } + + /// + /// Value type for raw input from a keyboard. + /// + [StructLayout(LayoutKind.Sequential)] + public struct RawKeyboard + { + /// Scan code for key depression. + public short MakeCode; + /// Scan code information. + public RawKeyboardFlags Flags; + /// Reserved. + public short Reserved; + /// Virtual key code. + public ushort VirtualKey; + /// Corresponding window message. + public uint Message; + /// Extra information. + public int ExtraInformation; + } + + /// + /// Enumeration containing flags for raw keyboard input. + /// + [Flags] + public enum RawKeyboardFlags : ushort + { + /// + KeyMake = 0, + /// + KeyBreak = 1, + /// + KeyE0 = 2, + /// + KeyE1 = 4, + /// + TerminalServerSetLED = 8, + /// + TerminalServerShadow = 0x10, + /// + TerminalServerVKPACKET = 0x20 + } + + /// + /// Enumeration contanining the command types to issue. + /// + public enum RawInputCommand + { + /// + /// Get input data. + /// + Input = 0x10000003, + /// + /// Get header data. + /// + Header = 0x10000005 + } + + [DllImport("user32.dll")] + public static extern bool RegisterRawInputDevices([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] RAWINPUTDEVICE[] pRawInputDevices, int uiNumDevices, int cbSize); + + /// + /// Function to retrieve raw input data. + /// + /// Handle to the raw input. + /// Command to issue when retrieving data. + /// Raw input data. + /// Number of bytes in the array. + /// Size of the header. + /// 0 if successful if pData is null, otherwise number of bytes if pData is not null. + [DllImport("user32.dll")] + public static extern int GetRawInputData(IntPtr hRawInput, RawInputCommand uiCommand, out RawInput pData, ref int pcbSize, int cbSizeHeader); + + public MouseWatcher(Form containingForm, Label display, AccelCharts accelCharts) + { + ContainingForm = containingForm; + Display = display; + AccelCharts = accelCharts; + + RAWINPUTDEVICE device = new RAWINPUTDEVICE(); + device.WindowHandle = ContainingForm.Handle; + device.UsagePage = HIDUsagePage.Generic; + device.Usage = HIDUsage.Mouse; + device.Flags = RawInputDeviceFlags.InputSink; + + RAWINPUTDEVICE[] devices = new RAWINPUTDEVICE[1]; + devices[0] = device; + RegisterRawInputDevices(devices, 1, Marshal.SizeOf(typeof(RAWINPUTDEVICE))); + } + + Form ContainingForm { get; } + + Label Display { get; } + + AccelCharts AccelCharts { get; } + + public void OnMouseMove(int x, int y) + { + Display.Text = $"Last (x, y): ({x}, {y})"; + AccelCharts.MakeDots(x, y); + } + + public void ReadMouseMove(Message message) + { + RawInput rawInput = new RawInput(); + int outSize = 0; + int size = Marshal.SizeOf(typeof(RawInput)); + + outSize = GetRawInputData((IntPtr)message.LParam, RawInputCommand.Input, out rawInput, ref size, Marshal.SizeOf(typeof(RAWINPUTHEADER))); + + if (rawInput.Data.Mouse.LastX != 0 || rawInput.Data.Mouse.LastY != 0) + { + OnMouseMove(rawInput.Data.Mouse.LastX, rawInput.Data.Mouse.LastY); + } + + } + } +} diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj index 6ce0fe4..4cae319 100644 --- a/grapher/grapher.csproj +++ b/grapher/grapher.csproj @@ -52,6 +52,7 @@ + -- cgit v1.2.3 From 6602649bd7f9a9849b25fe55a5e5e8a617f40b70 Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Thu, 13 Aug 2020 13:39:40 -0700 Subject: All works smoothly --- grapher/Form1.Designer.cs | 1 + grapher/Form1.cs | 5 ++ grapher/Models/Calculations/AccelCalculator.cs | 2 + grapher/Models/Calculations/AccelChartData.cs | 31 ++++++++++-- grapher/Models/Calculations/AccelData.cs | 63 +++++++++++++---------- grapher/Models/Charts/AccelCharts.cs | 70 +++++++++++--------------- grapher/Models/Charts/ChartXY.cs | 41 ++++++++++----- grapher/Models/Charts/EstimatedPoints.cs | 25 +++++++++ grapher/Models/Mouse/PointData.cs | 46 +++++++++++++++++ grapher/grapher.csproj | 2 + 10 files changed, 201 insertions(+), 85 deletions(-) create mode 100644 grapher/Models/Charts/EstimatedPoints.cs create mode 100644 grapher/Models/Mouse/PointData.cs diff --git a/grapher/Form1.Designer.cs b/grapher/Form1.Designer.cs index a4c2931..f30de99 100644 --- a/grapher/Form1.Designer.cs +++ b/grapher/Form1.Designer.cs @@ -577,6 +577,7 @@ namespace grapher this.Name = "RawAcceleration"; this.Text = "Raw Acceleration Graph"; this.Load += new System.EventHandler(this.Form1_Load); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.RawAcceleration_Paint); ((System.ComponentModel.ISupportInitialize)(this.AccelerationChart)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.VelocityChart)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.GainChart)).EndInit(); diff --git a/grapher/Form1.cs b/grapher/Form1.cs index 3df1b67..d8db6fc 100644 --- a/grapher/Form1.cs +++ b/grapher/Form1.cs @@ -193,5 +193,10 @@ namespace grapher } #endregion Methods + + private void RawAcceleration_Paint(object sender, PaintEventArgs e) + { + AccelGUI.AccelCharts.DrawPoints(); + } } } diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs index d61a59b..e194854 100644 --- a/grapher/Models/Calculations/AccelCalculator.cs +++ b/grapher/Models/Calculations/AccelCalculator.cs @@ -66,6 +66,8 @@ namespace grapher.Models.Calculations lastInputMagnitude = magnitudeDatum.magnitude; lastOutputMagnitude = outMagnitude; } + + data.OrderedVelocityPointsList.AddRange(data.VelocityPoints.Values.ToList()); } public static ReadOnlyCollection GetMagnitudes() diff --git a/grapher/Models/Calculations/AccelChartData.cs b/grapher/Models/Calculations/AccelChartData.cs index a3354aa..fa31f35 100644 --- a/grapher/Models/Calculations/AccelChartData.cs +++ b/grapher/Models/Calculations/AccelChartData.cs @@ -13,6 +13,8 @@ namespace grapher.Models.Calculations AccelPoints = new SortedDictionary(); VelocityPoints = new SortedDictionary(); GainPoints = new SortedDictionary(); + OrderedVelocityPointsList = new List(); + OutVelocityToPoints = new Dictionary(); } public SortedDictionary AccelPoints { get; } @@ -21,23 +23,42 @@ namespace grapher.Models.Calculations public SortedDictionary GainPoints { get; } + public List OrderedVelocityPointsList { get; } + + public Dictionary OutVelocityToPoints { get; } + public void Clear() { AccelPoints.Clear(); VelocityPoints.Clear(); GainPoints.Clear(); + OrderedVelocityPointsList.Clear(); + OutVelocityToPoints.Clear(); } public (double, double, double) FindInValuesFromOut(double outVelocityValue) { - var velIdx = ~VelocityPoints.Values.ToList().BinarySearch(outVelocityValue); - - if (velIdx < 0) + if (OutVelocityToPoints.TryGetValue(outVelocityValue, out var values)) { - velIdx = ~velIdx; + return values; } + else + { + var velIdx = OrderedVelocityPointsList.BinarySearch(outVelocityValue); + + if (velIdx < 0) + { + velIdx = ~velIdx; + } - return (VelocityPoints.ElementAt(velIdx).Key, AccelPoints.ElementAt(velIdx).Value, GainPoints.ElementAt(velIdx).Value); + velIdx = Math.Min(velIdx, VelocityPoints.Count - 1); + + values = (VelocityPoints.ElementAt(velIdx).Key, AccelPoints.ElementAt(velIdx).Value, GainPoints.ElementAt(velIdx).Value); + + OutVelocityToPoints.Add(outVelocityValue, values); + + return values; + } } } } diff --git a/grapher/Models/Calculations/AccelData.cs b/grapher/Models/Calculations/AccelData.cs index a8f3307..6fb4767 100644 --- a/grapher/Models/Calculations/AccelData.cs +++ b/grapher/Models/Calculations/AccelData.cs @@ -1,4 +1,5 @@ -using System; +using grapher.Models.Charts; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,11 +11,18 @@ namespace grapher.Models.Calculations public class AccelData { - public AccelData() + public AccelData( + EstimatedPoints combined, + EstimatedPoints x, + EstimatedPoints y) { Combined = new AccelChartData(); X = new AccelChartData(); Y = new AccelChartData(); + + Estimated = combined; + EstimatedX = x; + EstimatedY = y; } public AccelChartData Combined { get; } @@ -23,6 +31,12 @@ namespace grapher.Models.Calculations public AccelChartData Y { get; } + private EstimatedPoints Estimated { get; } + + private EstimatedPoints EstimatedX { get; } + + private EstimatedPoints EstimatedY { get; } + public void Clear() { Combined.Clear(); @@ -30,34 +44,31 @@ namespace grapher.Models.Calculations Y.Clear(); } - public void CalculateDots(int x, int y, ref EstimatedPoints estimation) + public void CalculateDots(int x, int y) { var magnitude = AccelCalculator.Magnitude(x, y); - estimation.CombinedVelocity.Y = magnitude; - estimation.XVelocity.Y = Math.Abs(x); - estimation.YVelocity.Y = Math.Abs(y); - (var inCombVel, var combSens, var combGain) = Combined.FindInValuesFromOut(magnitude); - estimation.CombinedVelocity.X = inCombVel; - estimation.CombinedSensitivity.X = inCombVel; - estimation.CombinedGain.X = inCombVel; - estimation.CombinedSensitivity.Y = combSens; - estimation.CombinedGain.Y = combGain; - - (var inXVel, var xSens, var xGain) = X.FindInValuesFromOut(estimation.XVelocity.Y); - estimation.XVelocity.X = inXVel; - estimation.XSensitivity.X = inXVel; - estimation.XGain.X = inXVel; - estimation.XSensitivity.Y = xSens; - estimation.XGain.Y = xGain; - - (var inYVel, var ySens, var yGain) = Y.FindInValuesFromOut(estimation.YVelocity.Y); - estimation.YVelocity.X = inYVel; - estimation.YSensitivity.X = inYVel; - estimation.YGain.X = inYVel; - estimation.YSensitivity.Y = ySens; - estimation.YGain.Y = yGain; + Estimated.Velocity.Set(inCombVel, magnitude); + Estimated.Sensitivity.Set(inCombVel, combSens); + Estimated.Gain.Set(inCombVel, combGain); } + + public void CalculateDotsXY(int x, int y) + { + var magnitudeX = Math.Abs(x); + var magnitudeY = Math.Abs(y); + + (var inXVel, var xSens, var xGain) = X.FindInValuesFromOut(magnitudeX); + EstimatedX.Velocity.Set(inXVel, magnitudeX); + EstimatedX.Sensitivity.Set(inXVel, xSens); + EstimatedX.Gain.Set(inXVel, xGain); + + (var inYVel, var ySens, var yGain) = Y.FindInValuesFromOut(magnitudeY); + EstimatedY.Velocity.Set(inYVel, magnitudeY); + EstimatedY.Sensitivity.Set(inYVel, ySens); + EstimatedY.Gain.Set(inYVel, yGain); + } + } } diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs index e593bb9..42377c4 100644 --- a/grapher/Models/Charts/AccelCharts.cs +++ b/grapher/Models/Charts/AccelCharts.cs @@ -1,4 +1,5 @@ using grapher.Models.Calculations; +using grapher.Models.Charts; using System; using System.Collections.Generic; using System.Drawing; @@ -13,27 +14,6 @@ namespace grapher { public class AccelCharts { - public struct ChartPoint - { - public double X; - public double Y; - } - - public struct EstimatedPoints - { - public ChartPoint CombinedVelocity; - public ChartPoint CombinedSensitivity; - public ChartPoint CombinedGain; - - public ChartPoint XVelocity; - public ChartPoint XSensitivity; - public ChartPoint XGain; - - public ChartPoint YVelocity; - public ChartPoint YSensitivity; - public ChartPoint YGain; - } - public const int ChartSeparationVertical = 10; /// Needed to show full contents in form. Unsure why. @@ -47,28 +27,21 @@ namespace grapher ToolStripMenuItem enableVelocityAndGain, ICollection checkBoxesXY) { + Estimated = new EstimatedPoints(); + EstimatedX = new EstimatedPoints(); + EstimatedY = new EstimatedPoints(); + AccelData = new AccelData(Estimated, EstimatedX, EstimatedY); + ContaingForm = form; SensitivityChart = sensitivityChart; VelocityChart = velocityChart; GainChart = gainChart; EnableVelocityAndGain = enableVelocityAndGain; CheckBoxesXY = checkBoxesXY; - AccelData = new AccelData(); - - Estimated = new EstimatedPoints - { - CombinedVelocity = new ChartPoint { X = 0, Y = 0 }, - CombinedSensitivity = new ChartPoint { X = 0, Y = 0 }, - CombinedGain = new ChartPoint { X = 0, Y = 0 }, - - XVelocity = new ChartPoint { X = 0, Y = 0 }, - XSensitivity = new ChartPoint { X = 0, Y = 0 }, - XGain = new ChartPoint { X = 0, Y = 0 }, - YVelocity = new ChartPoint { X = 0, Y = 0 }, - YSensitivity = new ChartPoint { X = 0, Y = 0 }, - YGain = new ChartPoint { X = 0, Y = 0 }, - }; + SensitivityChart.SetPointBinds(Estimated.Sensitivity, EstimatedX.Sensitivity, EstimatedY.Sensitivity); + VelocityChart.SetPointBinds(Estimated.Velocity, EstimatedX.Velocity, EstimatedY.Velocity); + GainChart.SetPointBinds(Estimated.Gain, EstimatedX.Gain, EstimatedY.Gain); SensitivityChart.SetTop(0); VelocityChart.SetHeight(SensitivityChart.Height); @@ -99,7 +72,11 @@ namespace grapher public AccelData AccelData { get; } - private EstimatedPoints Estimated; + private EstimatedPoints Estimated { get; } + + private EstimatedPoints EstimatedX { get; } + + private EstimatedPoints EstimatedY { get; } private ICollection CheckBoxesXY { get; } @@ -109,10 +86,21 @@ namespace grapher public void MakeDots(int x, int y) { - AccelData.CalculateDots(x, y, ref Estimated); - SensitivityChart.DrawPoints(Estimated.CombinedSensitivity, Estimated.XSensitivity, Estimated.YSensitivity); - VelocityChart.DrawPoints(Estimated.CombinedVelocity, Estimated.XVelocity, Estimated.YVelocity); - GainChart.DrawPoints(Estimated.CombinedGain, Estimated.XGain, Estimated.YGain); + if (Combined) + { + AccelData.CalculateDots(x, y); + } + else + { + AccelData.CalculateDotsXY(x, y); + } + } + + public void DrawPoints() + { + SensitivityChart.DrawPoints(); + VelocityChart.DrawPoints(); + GainChart.DrawPoints(); } public void Bind() diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs index 2c0ce2c..c0c8713 100644 --- a/grapher/Models/Charts/ChartXY.cs +++ b/grapher/Models/Charts/ChartXY.cs @@ -1,4 +1,6 @@ -using System; +using grapher.Models.Charts; +using grapher.Models.Mouse; +using System; using System.Collections; using System.Collections.Generic; using System.Drawing; @@ -80,6 +82,12 @@ namespace grapher public bool Combined { get; private set; } + private PointData CombinedPointData { get; set; } + + private PointData XPointData { get; set; } + + private PointData YPointData { get; set; } + #endregion Properties #region Methods @@ -109,26 +117,33 @@ namespace grapher chart.Series[1].Points.AddXY(0, 0); } - public static void DrawPoint(Chart chart, ChartPoint point) + public static void DrawPoint(Chart chart, PointData point) { - chart.Series[1].Points[0].XValue = point.X; - chart.Series[1].Points[0].YValues[0] = point.Y; + if (chart.Visible) + { + (var x, var y) = point.Get(); + chart.Series[1].Points.DataBindXY(x, y); + chart.Update(); + } } - public void DrawPoints(ChartPoint CombinedPoint, ChartPoint XPoint, ChartPoint YPoint) + public void SetPointBinds(PointData combined, PointData x, PointData y) { - if (Combined) + CombinedPointData = combined; + XPointData = x; + YPointData = y; + } + + public void DrawPoints() + { + if(Combined) { - DrawPoint(ChartX, CombinedPoint); + DrawPoint(ChartX, CombinedPointData); } else { - DrawPoint(ChartX, XPoint); - } - - if (ChartY.Visible) - { - DrawPoint(ChartY, YPoint); + DrawPoint(ChartX, XPointData); + DrawPoint(ChartY, YPointData); } } diff --git a/grapher/Models/Charts/EstimatedPoints.cs b/grapher/Models/Charts/EstimatedPoints.cs new file mode 100644 index 0000000..fa0718b --- /dev/null +++ b/grapher/Models/Charts/EstimatedPoints.cs @@ -0,0 +1,25 @@ +using grapher.Models.Mouse; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Charts +{ + public class EstimatedPoints + { + public EstimatedPoints() + { + Sensitivity = new PointData(); + Velocity = new PointData(); + Gain = new PointData(); + } + + public PointData Sensitivity { get; } + + public PointData Velocity { get; } + + public PointData Gain { get; } + } +} diff --git a/grapher/Models/Mouse/PointData.cs b/grapher/Models/Mouse/PointData.cs new file mode 100644 index 0000000..d0273d5 --- /dev/null +++ b/grapher/Models/Mouse/PointData.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace grapher.Models.Mouse +{ + public class PointData + { + public PointData() + { + Lock = new Object(); + X = new double[] { 0 }; + Y = new double[] { 0 }; + } + + public Object Lock { get; } + + private double[] X { get; set; } + private double[] Y { get; set; } + + public void Set(double x, double y) + { + lock(Lock) + { + X[0] = x; + Y[0] = y; + } + } + + public (double[], double[]) Get() + { + double[] xRet; + double[] yRet; + + lock(Lock) + { + xRet = X; + yRet = Y; + } + + return (xRet, yRet); + } + } +} diff --git a/grapher/grapher.csproj b/grapher/grapher.csproj index 4cae319..cbda661 100644 --- a/grapher/grapher.csproj +++ b/grapher/grapher.csproj @@ -52,7 +52,9 @@ + + -- cgit v1.2.3 From 32323636b4b5390c114fc2a6d845b7621a983cdc Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Thu, 13 Aug 2020 20:56:41 -0700 Subject: Fix initial points, add poll time constant --- grapher/Models/AccelGUI.cs | 2 +- grapher/Models/Calculations/AccelCalculator.cs | 17 ++++++++++---- grapher/Models/Calculations/AccelChartData.cs | 5 +--- grapher/Models/Calculations/AccelData.cs | 32 +++++++++++++------------- grapher/Models/Charts/AccelCharts.cs | 6 ++--- grapher/Models/Charts/ChartXY.cs | 2 +- grapher/Models/Mouse/MouseWatcher.cs | 16 ++++++++----- grapher/Models/Mouse/PointData.cs | 11 +++------ wrapper/wrapper.cpp | 10 ++++++++ wrapper/wrapper.hpp | 4 ++++ 10 files changed, 62 insertions(+), 43 deletions(-) diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs index a63bf07..8eb2226 100644 --- a/grapher/Models/AccelGUI.cs +++ b/grapher/Models/AccelGUI.cs @@ -91,7 +91,7 @@ namespace grapher public void UpdateGraph() { - AccelCalculator.Calculate(AccelCharts.AccelData, ManagedAcceleration, Sensitivity.Fields.X, Sensitivity.Fields.Y); + AccelCalculator.Calculate(AccelCharts.AccelData, ManagedAcceleration); AccelCharts.Bind(); } diff --git a/grapher/Models/Calculations/AccelCalculator.cs b/grapher/Models/Calculations/AccelCalculator.cs index e194854..bba3c32 100644 --- a/grapher/Models/Calculations/AccelCalculator.cs +++ b/grapher/Models/Calculations/AccelCalculator.cs @@ -23,13 +23,13 @@ namespace grapher.Models.Calculations public static ReadOnlyCollection MagnitudesX = GetMagnitudesX(); public static ReadOnlyCollection MagnitudesY = GetMagnitudesY(); - public static void Calculate(AccelData data, ManagedAccel accel, double starterX, double starterY) + public static void Calculate(AccelData data, ManagedAccel accel) { data.Clear(); - Calculate(data.Combined, accel, starterX, MagnitudesCombined); - Calculate(data.X, accel, starterY, MagnitudesX); - Calculate(data.Y, accel, starterY, MagnitudesY); + Calculate(data.Combined, accel, accel.GetSensitivityX(), MagnitudesCombined); + Calculate(data.X, accel, accel.GetSensitivityX(), MagnitudesX); + Calculate(data.Y, accel, accel.GetSensitivityY(), MagnitudesY); } public static void Calculate(AccelChartData data, ManagedAccel accel, double starter, ICollection magnitudeData) @@ -132,5 +132,14 @@ namespace grapher.Models.Calculations return Math.Sqrt(x * x + y * y); } + public static double Velocity(int x, int y, double time) + { + return Magnitude(x, y) / time; + } + + public static double Velocity(double x, double y, double time) + { + return Magnitude(x, y) / time; + } } } diff --git a/grapher/Models/Calculations/AccelChartData.cs b/grapher/Models/Calculations/AccelChartData.cs index fa31f35..20142a7 100644 --- a/grapher/Models/Calculations/AccelChartData.cs +++ b/grapher/Models/Calculations/AccelChartData.cs @@ -36,7 +36,7 @@ namespace grapher.Models.Calculations OutVelocityToPoints.Clear(); } - public (double, double, double) FindInValuesFromOut(double outVelocityValue) + public (double, double, double) FindPointValuesFromOut(double outVelocityValue) { if (OutVelocityToPoints.TryGetValue(outVelocityValue, out var values)) { @@ -52,11 +52,8 @@ namespace grapher.Models.Calculations } velIdx = Math.Min(velIdx, VelocityPoints.Count - 1); - values = (VelocityPoints.ElementAt(velIdx).Key, AccelPoints.ElementAt(velIdx).Value, GainPoints.ElementAt(velIdx).Value); - OutVelocityToPoints.Add(outVelocityValue, values); - return values; } } diff --git a/grapher/Models/Calculations/AccelData.cs b/grapher/Models/Calculations/AccelData.cs index 6fb4767..683c67e 100644 --- a/grapher/Models/Calculations/AccelData.cs +++ b/grapher/Models/Calculations/AccelData.cs @@ -44,30 +44,30 @@ namespace grapher.Models.Calculations Y.Clear(); } - public void CalculateDots(int x, int y) + public void CalculateDots(int x, int y, double timeInMs) { - var magnitude = AccelCalculator.Magnitude(x, y); + var magnitude = AccelCalculator.Velocity(x, y, timeInMs); - (var inCombVel, var combSens, var combGain) = Combined.FindInValuesFromOut(magnitude); + (var inCombVel, var combSens, var combGain) = Combined.FindPointValuesFromOut(magnitude); Estimated.Velocity.Set(inCombVel, magnitude); Estimated.Sensitivity.Set(inCombVel, combSens); Estimated.Gain.Set(inCombVel, combGain); } - public void CalculateDotsXY(int x, int y) + public void CalculateDotsXY(int x, int y, double timeInMs) { - var magnitudeX = Math.Abs(x); - var magnitudeY = Math.Abs(y); - - (var inXVel, var xSens, var xGain) = X.FindInValuesFromOut(magnitudeX); - EstimatedX.Velocity.Set(inXVel, magnitudeX); - EstimatedX.Sensitivity.Set(inXVel, xSens); - EstimatedX.Gain.Set(inXVel, xGain); - - (var inYVel, var ySens, var yGain) = Y.FindInValuesFromOut(magnitudeY); - EstimatedY.Velocity.Set(inYVel, magnitudeY); - EstimatedY.Sensitivity.Set(inYVel, ySens); - EstimatedY.Gain.Set(inYVel, yGain); + var outX = Math.Abs(x); + var outY = Math.Abs(y); + + (var inXVelocity, var xSensitivity, var xGain) = X.FindPointValuesFromOut(outX); + EstimatedX.Velocity.Set(inXVelocity, outX); + EstimatedX.Sensitivity.Set(inXVelocity, xSensitivity); + EstimatedX.Gain.Set(inXVelocity, xGain); + + (var inYVelocity, var ySensitivity, var yGain) = Y.FindPointValuesFromOut(outY); + EstimatedY.Velocity.Set(inYVelocity, outY); + EstimatedY.Sensitivity.Set(inYVelocity, ySensitivity); + EstimatedY.Gain.Set(inYVelocity, yGain); } } diff --git a/grapher/Models/Charts/AccelCharts.cs b/grapher/Models/Charts/AccelCharts.cs index 42377c4..1aa3909 100644 --- a/grapher/Models/Charts/AccelCharts.cs +++ b/grapher/Models/Charts/AccelCharts.cs @@ -84,15 +84,15 @@ namespace grapher private int FormBorderHeight { get; } - public void MakeDots(int x, int y) + public void MakeDots(int x, int y, double timeInMs) { if (Combined) { - AccelData.CalculateDots(x, y); + AccelData.CalculateDots(x, y, timeInMs); } else { - AccelData.CalculateDotsXY(x, y); + AccelData.CalculateDotsXY(x, y, timeInMs); } } diff --git a/grapher/Models/Charts/ChartXY.cs b/grapher/Models/Charts/ChartXY.cs index c0c8713..81874a2 100644 --- a/grapher/Models/Charts/ChartXY.cs +++ b/grapher/Models/Charts/ChartXY.cs @@ -121,7 +121,7 @@ namespace grapher { if (chart.Visible) { - (var x, var y) = point.Get(); + point.Get(out var x, out var y); chart.Series[1].Points.DataBindXY(x, y); chart.Update(); } diff --git a/grapher/Models/Mouse/MouseWatcher.cs b/grapher/Models/Mouse/MouseWatcher.cs index 9b1479e..fea4e2d 100644 --- a/grapher/Models/Mouse/MouseWatcher.cs +++ b/grapher/Models/Mouse/MouseWatcher.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -691,18 +692,21 @@ namespace grapher.Models.Mouse RAWINPUTDEVICE[] devices = new RAWINPUTDEVICE[1]; devices[0] = device; RegisterRawInputDevices(devices, 1, Marshal.SizeOf(typeof(RAWINPUTDEVICE))); + PollTime = 1; } - Form ContainingForm { get; } + private Form ContainingForm { get; } - Label Display { get; } + private Label Display { get; } - AccelCharts AccelCharts { get; } + private AccelCharts AccelCharts { get; } - public void OnMouseMove(int x, int y) + private double PollTime { get; } + + public void OnMouseMove(int x, int y, double timeInMs) { Display.Text = $"Last (x, y): ({x}, {y})"; - AccelCharts.MakeDots(x, y); + AccelCharts.MakeDots(x, y, timeInMs); } public void ReadMouseMove(Message message) @@ -715,7 +719,7 @@ namespace grapher.Models.Mouse if (rawInput.Data.Mouse.LastX != 0 || rawInput.Data.Mouse.LastY != 0) { - OnMouseMove(rawInput.Data.Mouse.LastX, rawInput.Data.Mouse.LastY); + OnMouseMove(rawInput.Data.Mouse.LastX, rawInput.Data.Mouse.LastY, PollTime); } } diff --git a/grapher/Models/Mouse/PointData.cs b/grapher/Models/Mouse/PointData.cs index d0273d5..12a6e73 100644 --- a/grapher/Models/Mouse/PointData.cs +++ b/grapher/Models/Mouse/PointData.cs @@ -29,18 +29,13 @@ namespace grapher.Models.Mouse } } - public (double[], double[]) Get() + public void Get(out double[] x, out double[] y) { - double[] xRet; - double[] yRet; - lock(Lock) { - xRet = X; - yRet = Y; + x = X; + y = Y; } - - return (xRet, yRet); } } } diff --git a/wrapper/wrapper.cpp b/wrapper/wrapper.cpp index 0afb386..6a92caa 100644 --- a/wrapper/wrapper.cpp +++ b/wrapper/wrapper.cpp @@ -5,6 +5,16 @@ using namespace rawaccel; using namespace System; +double ManagedAccel::GetSensitivityX() +{ + return modifier_instance->sensitivity.x; +} + +double ManagedAccel::GetSensitivityY() +{ + return modifier_instance->sensitivity.y; +} + Tuple^ ManagedAccel::Accelerate(int x, int y, double time) { vec2d input_vec2d = { diff --git a/wrapper/wrapper.hpp b/wrapper/wrapper.hpp index 20ee095..b086672 100644 --- a/wrapper/wrapper.hpp +++ b/wrapper/wrapper.hpp @@ -40,6 +40,10 @@ public: } } + double GetSensitivityX(); + + double GetSensitivityY(); + mouse_modifier* GetInstance() { return modifier_instance; -- cgit v1.2.3