From d4b8a09395c8e897705cbf030219982752ba215e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 5 Dec 2022 18:35:25 +0100 Subject: [PATCH] Add haptic style setting (disabled, weak, strong) --- RELEASE.md | 1 + .../Devices/SteamControllerHaptic.cs | 55 +++++++++++++------ SteamController/Profiles/X360HapticProfile.cs | 5 +- .../ProfilesSettings/X360HapticSettings.cs | 8 +++ 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index d0b0632..dde01ed 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -27,3 +27,4 @@ It does help this project on being supported. - Allow to select between touch keyboard or CTRL+WIN+O - Fix haptics not being fired both sides - Scale haptic intensity +- Add haptic style setting (disabled, weak, strong) diff --git a/SteamController/Devices/SteamControllerHaptic.cs b/SteamController/Devices/SteamControllerHaptic.cs index 19a0254..c896c86 100644 --- a/SteamController/Devices/SteamControllerHaptic.cs +++ b/SteamController/Devices/SteamControllerHaptic.cs @@ -51,43 +51,62 @@ namespace SteamController.Devices { public byte packet_type = 0xea; public byte len = 0xd; - public byte position = 0x00; - public byte amplitude = 0x2; // + public HapticPad position = HapticPad.Left; + public HapticStyle style = HapticStyle.Strong; // public byte unsure2 = 0x0; public sbyte intensity = 0x00; // -7..5 => -2dB..10dB public byte unsure3 = 0x4; public int tsA = 0; // timestamp? public int tsB = 0; - public SDCHapticPacket2() { } + public SDCHapticPacket2() + { + var ts = Random.Shared.Next(); + this.tsA = ts; + this.tsB = ts; + } + + public SDCHapticPacket2(HapticPad position, HapticStyle style, sbyte intensityDB) : this() + { + this.position = position; + this.style = style; + this.intensity = (sbyte)(intensityDB - 5); // convert from dB to values + } } - private Task?[] hapticTask = new Task?[byte.MaxValue]; + private Dictionary hapticTasks = new Dictionary(); - public bool SendHaptic(byte position, sbyte intensity) + public enum HapticPad : byte { - if (hapticTask[position]?.IsCompleted == false) + Left, + Right + }; + + public enum HapticStyle : byte + { + Disabled = 0, + Weak = 1, + Strong = 2 + }; + + public bool SendHaptic(HapticPad position, HapticStyle style, sbyte intensityDB) + { + if (hapticTasks.GetValueOrDefault(position)?.IsCompleted == false) return false; + if (style == HapticStyle.Disabled) + return true; - var ts = Random.Shared.Next(); + var haptic = new SDCHapticPacket2(position, style, intensityDB); - var haptic = new SDCHapticPacket2() - { - position = position, - intensity = (sbyte)(intensity - 5), // convert from dB to values - tsA = ts, - tsB = ts - }; - - Log.TraceLine("STEAM: Haptic: pos={0}, intensity={1}", - position, intensity); + Log.TraceLine("STEAM: Haptic: position={0}, style={1}, intensity={2}", + position, style, intensityDB); var bytes = new byte[Marshal.SizeOf()]; var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); try { Marshal.StructureToPtr(haptic, handle.AddrOfPinnedObject(), false); - hapticTask[position] = neptuneDevice.RequestFeatureReportAsync(bytes); + hapticTasks[position] = neptuneDevice.RequestFeatureReportAsync(bytes); return true; } catch (Exception e) diff --git a/SteamController/Profiles/X360HapticProfile.cs b/SteamController/Profiles/X360HapticProfile.cs index f877ce4..a1321b4 100644 --- a/SteamController/Profiles/X360HapticProfile.cs +++ b/SteamController/Profiles/X360HapticProfile.cs @@ -1,4 +1,5 @@ using SteamController.ProfilesSettings; +using HapticPad = SteamController.Devices.SteamController.HapticPad; namespace SteamController.Profiles { @@ -15,10 +16,10 @@ namespace SteamController.Profiles return Status.Done; if (GetHapticIntensity(context.X360.FeedbackLargeMotor, HapticSettings.LeftIntensity, out var leftIntensity)) - context.Steam.SendHaptic(1, leftIntensity); + context.Steam.SendHaptic(HapticPad.Right, HapticSettings.HapticStyle, leftIntensity); if (GetHapticIntensity(context.X360.FeedbackSmallMotor, HapticSettings.RightIntensity, out var rightIntensity)) - context.Steam.SendHaptic(0, rightIntensity); + context.Steam.SendHaptic(HapticPad.Left, HapticSettings.HapticStyle, rightIntensity); context.X360.ResetFeedback(); diff --git a/SteamController/ProfilesSettings/X360HapticSettings.cs b/SteamController/ProfilesSettings/X360HapticSettings.cs index fb0160c..3df9d51 100644 --- a/SteamController/ProfilesSettings/X360HapticSettings.cs +++ b/SteamController/ProfilesSettings/X360HapticSettings.cs @@ -17,6 +17,14 @@ namespace SteamController.ProfilesSettings { } + [UserScopedSettingAttribute()] + [DefaultSettingValueAttribute("Weak")] + public Devices.SteamController.HapticStyle HapticStyle + { + get { return ((Devices.SteamController.HapticStyle)(this["HapticStyle"])); } + set { this["HapticStyle"] = value; } + } + [UserScopedSettingAttribute()] [DefaultSettingValueAttribute("5")] [Description("Haptic intensity between -2dB and 10dB")]