diff options
| author | JacobPalecki <[email protected]> | 2020-08-31 23:03:46 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-08-31 23:03:46 -0700 |
| commit | 471fe599bab6ba0632ddd1dacd20c7fc42db0eee (patch) | |
| tree | 90a82ee14dbb112621657efbd2523ed35f59d154 /grapher/Models/Serialized | |
| parent | Merge pull request #16 from JacobPalecki/Misc (diff) | |
| parent | add independent xy accel to driver (diff) | |
| download | rawaccel-471fe599bab6ba0632ddd1dacd20c7fc42db0eee.tar.xz rawaccel-471fe599bab6ba0632ddd1dacd20c7fc42db0eee.zip | |
Merge pull request #17 from a1xd/indep
Indep
Diffstat (limited to 'grapher/Models/Serialized')
| -rw-r--r-- | grapher/Models/Serialized/DriverSettings.cs | 87 | ||||
| -rw-r--r-- | grapher/Models/Serialized/ModifierArgs.cs | 75 | ||||
| -rw-r--r-- | grapher/Models/Serialized/RawAccelSettings.cs | 25 | ||||
| -rw-r--r-- | grapher/Models/Serialized/SettingsManager.cs | 115 |
4 files changed, 142 insertions, 160 deletions
diff --git a/grapher/Models/Serialized/DriverSettings.cs b/grapher/Models/Serialized/DriverSettings.cs new file mode 100644 index 0000000..91d7e9f --- /dev/null +++ b/grapher/Models/Serialized/DriverSettings.cs @@ -0,0 +1,87 @@ +using System; +using System.Runtime.InteropServices; + +namespace grapher.Models.Serialized +{ + public enum AccelMode + { + linear, classic, natural, logarithmic, sigmoid, naturalgain, sigmoidgain, power, noaccel + } + + [StructLayout(LayoutKind.Sequential)] + public struct AccelArgs + { + public double offset; + public double accel; + public double limit; + public double exponent; + public double midpoint; + public double powerScale; + public double powerExponent; + public double weight; + public double rate; + public double scaleCap; + public double gainCap; + }; + + [StructLayout(LayoutKind.Sequential)] + public struct Vec2 <T> + { + public T x; + public T y; + } + + [StructLayout(LayoutKind.Sequential)] + [Serializable] + public class DriverSettings + { + private static readonly IntPtr UnmanagedSettingsHandle = + Marshal.AllocHGlobal(Marshal.SizeOf<DriverSettings>()); + + public double rotation; + public bool combineMagnitudes; + public Vec2<AccelMode> modes; + public Vec2<AccelArgs> args; + public Vec2<double> sensitivity; + public double minimumTime; + + public static DriverSettings GetActive() + { + DriverInterop.GetActiveSettings(UnmanagedSettingsHandle); + return Marshal.PtrToStructure<DriverSettings>(UnmanagedSettingsHandle); + } + + public static void SetActive(DriverSettings settings) + { + Marshal.StructureToPtr(settings, UnmanagedSettingsHandle, false); + DriverInterop.SetActiveSettings(UnmanagedSettingsHandle); + } + + public void SendToDriver() + { + SetActive(this); + } + + public void SendToDriverAndUpdate(ManagedAccel accel) + { + SendToDriver(); + accel.UpdateFromSettings(UnmanagedSettingsHandle); + } + + public bool verify() + { + /* + if (args.accel < 0 || args.rate < 0) + bad_arg("accel can not be negative, use a negative weight to compensate"); + if (args.rate > 1) bad_arg("rate can not be greater than 1"); + if (args.exponent <= 1) bad_arg("exponent must be greater than 1"); + if (args.limit <= 1) bad_arg("limit must be greater than 1"); + if (args.power_scale <= 0) bad_arg("scale must be positive"); + if (args.power_exp <= 0) bad_arg("exponent must be positive"); + if (args.midpoint < 0) bad_arg("midpoint must not be negative"); + if (args.time_min <= 0) bad_arg("min time must be positive"); + */ + return true; + } + } +} diff --git a/grapher/Models/Serialized/ModifierArgs.cs b/grapher/Models/Serialized/ModifierArgs.cs deleted file mode 100644 index 206a3c9..0000000 --- a/grapher/Models/Serialized/ModifierArgs.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace grapher.Models.Serialized -{ - public enum accel_mode - { - linear=1, classic, natural, logarithmic, sigmoid, power, naturalgain, sigmoidgain, noaccel - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - [Serializable] - public struct vec2d - { - public double x; - public double y; - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - [Serializable] - public struct accel_args - { - public double offset; - public double accel; - public double limit; - public double exponent; - public double midpoint; - public double power_scale; - public double gain_cap; - public vec2d weight; - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - [Serializable] - public struct accel_fn_args - { - public accel_args acc_args; - public int accel_mode; - public double time_min; - public vec2d cap; - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - [Serializable] - public struct modifier_args - { - public double degrees; - public vec2d sens; - public accel_fn_args acc_fn_args; - - public modifier_args(ManagedAccel managedAccel) - { - degrees = managedAccel.Rotation; - sens.x = managedAccel.SensitivityX; - sens.y = managedAccel.SensitivityY; - acc_fn_args.accel_mode = managedAccel.Type; - acc_fn_args.time_min = managedAccel.MinimumTime; - acc_fn_args.cap.x = managedAccel.CapX; - acc_fn_args.cap.y = managedAccel.CapY; - acc_fn_args.acc_args.accel = managedAccel.Acceleration; - acc_fn_args.acc_args.exponent = managedAccel.LimitExp; - acc_fn_args.acc_args.gain_cap = managedAccel.GainCap; - acc_fn_args.acc_args.limit = managedAccel.LimitExp; - acc_fn_args.acc_args.midpoint = managedAccel.Midpoint; - acc_fn_args.acc_args.offset = managedAccel.Offset; - acc_fn_args.acc_args.power_scale = managedAccel.PowerScale; - acc_fn_args.acc_args.weight.x = managedAccel.WeightX; - acc_fn_args.acc_args.weight.y = managedAccel.WeightY; - } - } -} diff --git a/grapher/Models/Serialized/RawAccelSettings.cs b/grapher/Models/Serialized/RawAccelSettings.cs index 21a7f0c..7aed917 100644 --- a/grapher/Models/Serialized/RawAccelSettings.cs +++ b/grapher/Models/Serialized/RawAccelSettings.cs @@ -24,17 +24,16 @@ namespace grapher.Models.Serialized public RawAccelSettings() { } public RawAccelSettings( - ManagedAccel managedAccel, + DriverSettings accelSettings, GUISettings guiSettings) { - AccelerationSettings = new modifier_args(managedAccel); + AccelerationSettings = accelSettings; GUISettings = guiSettings; } - public GUISettings GUISettings { get; set; } - public modifier_args AccelerationSettings { get; set; } + public DriverSettings AccelerationSettings { get; set; } public static RawAccelSettings Load() { @@ -42,23 +41,19 @@ namespace grapher.Models.Serialized } public static RawAccelSettings Load(string file) - { - if (!Exists(file)) + { + try { - throw new Exception($"Settings file does not exist at {file}"); + return JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings); } - - RawAccelSettings deserializedSettings; - try + catch (FileNotFoundException e) { - deserializedSettings = JsonConvert.DeserializeObject<RawAccelSettings>(File.ReadAllText(file), SerializerSettings); + throw new FileNotFoundException($"Settings file does not exist at {file}", e); } - catch(Exception e) + catch (JsonSerializationException e) { - throw new Exception($"Settings file at {file} does not contain valid Raw Accel Settings.", e); + throw new JsonSerializationException($"Settings file at {file} does not contain valid Raw Accel Settings.", e); } - - return deserializedSettings; } public static bool Exists() diff --git a/grapher/Models/Serialized/SettingsManager.cs b/grapher/Models/Serialized/SettingsManager.cs index 848606d..fc58387 100644 --- a/grapher/Models/Serialized/SettingsManager.cs +++ b/grapher/Models/Serialized/SettingsManager.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; +using Newtonsoft.Json; +using System; using System.Windows.Forms; namespace grapher.Models.Serialized @@ -32,63 +28,38 @@ namespace grapher.Models.Serialized private ToolStripMenuItem AutoWriteMenuItem { get; set; } - public void UpdateActiveSettings( - int mode, - double degrees, - double sensitivityX, - double sensitivityY, - double weightX, - double weightY, - double capX, - double capY, - double offset, - double accel, - double limitOrExp, - double midpoint, - double gainCap) + public void UpdateActiveSettings(DriverSettings settings) { - ActiveAccel.UpdateAccel( - mode, - degrees, - sensitivityX, - sensitivityY, - weightX, - weightY, - capX, - capY, - offset, - accel, - limitOrExp, - midpoint, - gainCap); + try + { + settings.SendToDriverAndUpdate(ActiveAccel); - RawAccelSettings.AccelerationSettings = new modifier_args(ActiveAccel); - RawAccelSettings.GUISettings = new GUISettings + RawAccelSettings.AccelerationSettings = settings; + RawAccelSettings.GUISettings = new GUISettings { AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, DPI = (int)DpiField.Data, PollRate = (int)PollRateField.Data }; - RawAccelSettings.Save(); + RawAccelSettings.Save(); + } + catch (DriverWriteCDException) + { + Console.WriteLine("write on cooldown"); + } } - public void UpdateActiveAccelFromFileSettings() + public void UpdateActiveAccelFromFileSettings(DriverSettings settings) { - ActiveAccel.UpdateAccel( - RawAccelSettings.AccelerationSettings.acc_fn_args.accel_mode, - RawAccelSettings.AccelerationSettings.degrees, - RawAccelSettings.AccelerationSettings.sens.x, - RawAccelSettings.AccelerationSettings.sens.y, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.weight.x, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.weight.y, - RawAccelSettings.AccelerationSettings.acc_fn_args.cap.x, - RawAccelSettings.AccelerationSettings.acc_fn_args.cap.y, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.offset, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.accel, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.exponent, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.midpoint, - RawAccelSettings.AccelerationSettings.acc_fn_args.acc_args.gain_cap); + try + { + settings.SendToDriverAndUpdate(ActiveAccel); + } + catch (DriverWriteCDException) + { + Console.WriteLine("write on cd during file init"); + } DpiField.SetToEntered(RawAccelSettings.GUISettings.DPI); PollRateField.SetToEntered(RawAccelSettings.GUISettings.PollRate); AutoWriteMenuItem.Checked = RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup; @@ -96,28 +67,32 @@ namespace grapher.Models.Serialized public void Startup() { - ActiveAccel.ReadFromDriver(); - - if(RawAccelSettings.Exists()) + if (RawAccelSettings.Exists()) { - RawAccelSettings = RawAccelSettings.Load(); - if (RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup) + try { - UpdateActiveAccelFromFileSettings(); - } - } - else - { - RawAccelSettings = new RawAccelSettings( - ActiveAccel, - new GUISettings + RawAccelSettings = RawAccelSettings.Load(); + if (RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup) { - AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, - DPI = (int)DpiField.Data, - PollRate = (int)PollRateField.Data - }); - RawAccelSettings.Save(); + UpdateActiveAccelFromFileSettings(RawAccelSettings.AccelerationSettings); + } + return; + } + catch (JsonSerializationException e) + { + Console.WriteLine($"bad settings: {e}"); + } } + + RawAccelSettings = new RawAccelSettings( + DriverSettings.GetActive(), + new GUISettings + { + AutoWriteToDriverOnStartup = AutoWriteMenuItem.Checked, + DPI = (int)DpiField.Data, + PollRate = (int)PollRateField.Data + }); + RawAccelSettings.Save(); } } } |