From 9a286691f43585248023db08a88e9c4144293148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 28 Nov 2022 10:14:05 +0100 Subject: [PATCH] Use high-precision timer for DeltaTime --- SteamController/Context.cs | 2 +- SteamController/Devices/SteamAction.cs | 15 ++------------- SteamController/Devices/SteamController.cs | 11 +++++++++++ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/SteamController/Context.cs b/SteamController/Context.cs index 1e64fae..2adbb1b 100644 --- a/SteamController/Context.cs +++ b/SteamController/Context.cs @@ -17,7 +17,7 @@ namespace SteamController public List Profiles { get; } = new List(); public List Managers { get; } = new List(); - public List? orderedProfiles; + private List? orderedProfiles; public bool RequestEnable { get; set; } = true; public bool RequestDesktopMode { get; set; } = true; diff --git a/SteamController/Devices/SteamAction.cs b/SteamController/Devices/SteamAction.cs index d3fe1e0..1a8241b 100644 --- a/SteamController/Devices/SteamAction.cs +++ b/SteamController/Devices/SteamAction.cs @@ -12,8 +12,6 @@ namespace SteamController.Devices /// This is action controlled by Lizard mode public bool LizardButton { get; internal set; } public bool LizardMouse { get; internal set; } - public DateTime LastUpdated { get; protected set; } = DateTime.Now; - public double DeltaTime { get; protected set; } internal abstract void Reset(); internal abstract bool BeforeUpdate(byte[] buffer); @@ -23,13 +21,6 @@ namespace SteamController.Devices { } - protected void UpdateTime() - { - var now = DateTime.Now; - DeltaTime = (now - LastUpdated).TotalSeconds; - LastUpdated = now; - } - protected bool ValueCanBeUsed { get @@ -202,7 +193,6 @@ namespace SteamController.Devices { rawLastValue = rawValue; rawValue = newValue; - UpdateTime(); if (!rawLastValue && rawValue) { @@ -330,7 +320,7 @@ namespace SteamController.Devices case DeltaValueMode.AbsoluteTime: if (Math.Abs(Value) < Deadzone) return 0.0; - value = (int)(Value * DeltaTime); + value = (int)(Value * (Controller?.DeltaTime ?? 0.0)); break; case DeltaValueMode.Delta: @@ -343,7 +333,7 @@ namespace SteamController.Devices value = Value - LastValue; if (Math.Abs(Value) < MinChange) return 0.0; - value = (int)(value * DeltaTime); + value = (int)(value * (Controller?.DeltaTime ?? 0.0)); break; } @@ -364,7 +354,6 @@ namespace SteamController.Devices { rawLastValue = rawValue; rawValue = newValue; - UpdateTime(); // first time pressed, reset value as this is a Pad if (ActiveButton is not null && ActiveButton.JustPressed()) diff --git a/SteamController/Devices/SteamController.cs b/SteamController/Devices/SteamController.cs index 8979257..8ba9924 100644 --- a/SteamController/Devices/SteamController.cs +++ b/SteamController/Devices/SteamController.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using CommonHelpers; using hidapi; using PowerControl.External; @@ -12,6 +13,9 @@ namespace SteamController.Devices private const int ReadTimeout = 50; private hidapi.HidDevice neptuneDevice; + private Stopwatch stopwatch = new Stopwatch(); + private TimeSpan? lastUpdate; + public double DeltaTime { get; private set; } internal SteamController() { @@ -20,6 +24,8 @@ namespace SteamController.Devices neptuneDevice = new hidapi.HidDevice(VendorID, ProductID, 64); neptuneDevice.OpenDevice(); + + stopwatch.Start(); } public void Dispose() @@ -42,6 +48,11 @@ namespace SteamController.Devices internal void BeforeUpdate() { + var ts = stopwatch.Elapsed; + DeltaTime = lastUpdate is not null ? (ts - lastUpdate.Value).TotalSeconds : 0.0; + DeltaTime = Math.Min(DeltaTime, 0.1); // max update is 100ms + lastUpdate = ts; + LizardButtons = true; LizardMouse = true;