From f9f96bd8075d3b20580ea3da7686e60241ec8780 Mon Sep 17 00:00:00 2001 From: IceStormNG Date: Sun, 23 Jul 2023 11:52:31 +0200 Subject: [PATCH] Restructured to generic Interface for easier extension. --- app/Peripherals/IPeripheral.cs | 32 +++++++++++++++ app/Peripherals/Mouse/AsusMouse.cs | 56 ++++++++++++++++++++------ app/Peripherals/PeripheralsProvider.cs | 35 +++++++++++----- 3 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 app/Peripherals/IPeripheral.cs diff --git a/app/Peripherals/IPeripheral.cs b/app/Peripherals/IPeripheral.cs new file mode 100644 index 00000000..93f6a57c --- /dev/null +++ b/app/Peripherals/IPeripheral.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GHelper.Peripherals +{ + public enum PeripheralType + { + Mouse, + Keyboard + } + + public interface IPeripheral + { + public bool IsDeviceReady { get; } + public bool Wireless { get; } + public int Battery { get; } + public bool Charging { get; } + + public PeripheralType DeviceType(); + + public string GetDisplayName(); + + public bool HasBattery(); + + public void SynchronizeDevice(); + + public void ReadBattery(); + } +} diff --git a/app/Peripherals/Mouse/AsusMouse.cs b/app/Peripherals/Mouse/AsusMouse.cs index e0ea879e..830c270c 100644 --- a/app/Peripherals/Mouse/AsusMouse.cs +++ b/app/Peripherals/Mouse/AsusMouse.cs @@ -86,7 +86,7 @@ namespace GHelper.Peripherals.Mouse } } - public abstract class AsusMouse : Device + public abstract class AsusMouse : Device, IPeripheral { internal const int ASUS_MOUSE_PACKET_SIZE = 65; @@ -94,7 +94,7 @@ namespace GHelper.Peripherals.Mouse private readonly string path; - public bool MouseIsReady { get; protected set; } + public bool IsDeviceReady { get; protected set; } public bool Wireless { get; protected set; } public int Battery { get; protected set; } public bool Charging { get; protected set; } @@ -153,6 +153,13 @@ namespace GHelper.Peripherals.Mouse { //Use this to validate whether the device is still connected. //If not, this will also initiate the disconnect and cleanup sequence. + CheckConnection(); + } + + //Override this for non battery devices to check whether the connection is still there + //This function should automatically disconnect the device in GHelper if the device is no longer there or the pipe is broken. + public virtual void CheckConnection() + { ReadBattery(); } @@ -219,19 +226,23 @@ namespace GHelper.Peripherals.Mouse } public abstract string GetDisplayName(); + public PeripheralType DeviceType() + { + return PeripheralType.Mouse; + } - public virtual void SynchronizeMouse() + public virtual void SynchronizeDevice() { DpiSettings = new AsusMouseDPI[DPIProfileCount()]; ReadBattery(); - if (Wireless && Battery == 0 && Charging == false) + if (HasBattery() && Battery <= 0 && Charging == false) { //Likely only the dongle connected and the mouse is either sleeping or turned off. //The mouse will not respond with proper data, but empty responses at this point - MouseIsReady = false; + IsDeviceReady = false; return; } - MouseIsReady = true; + IsDeviceReady = true; ReadProfile(); ReadDPI(); @@ -244,6 +255,11 @@ namespace GHelper.Peripherals.Mouse // Battery // ------------------------------------------------------------------------------ + public virtual bool HasBattery() + { + return true; + } + protected virtual bool HasEnergySettings() { return false; @@ -294,16 +310,32 @@ namespace GHelper.Peripherals.Mouse public void ReadBattery() { + if (!HasBattery() && !HasEnergySettings()) + { + return; + } + byte[]? response = WriteForResponse(GetBatteryReportPacket()); if (response is null) return; - Battery = ParseBattery(response); - Charging = ParseChargingState(response); - PowerOffSetting = ParsePowerOffSetting(response); - LowBatteryWarning = ParseLowBatteryWarning(response); + if (HasBattery()) + { + Battery = ParseBattery(response); + Charging = ParseChargingState(response); + + //If the device goes to standby it will not report battery state anymore. + IsDeviceReady = Battery > 0; + + Logger.WriteLine(GetDisplayName() + ": Got Battery Percentage " + Battery + "% - Charging:" + Charging); + } + + if (HasEnergySettings()) + { + PowerOffSetting = ParsePowerOffSetting(response); + LowBatteryWarning = ParseLowBatteryWarning(response); + Logger.WriteLine(GetDisplayName() + ": Got Auto Power Off: " + PowerOffSetting + " - Low Battery Warnning at: " + LowBatteryWarning + "%"); + } - Logger.WriteLine(GetDisplayName() + ": Got Battery Percentage " + Battery + "% - Charging:" + Charging); - Logger.WriteLine(GetDisplayName() + ": Got Auto Power Off: " + PowerOffSetting + " - Low Battery Warnning at: " + LowBatteryWarning + "%"); } // ------------------------------------------------------------------------------ diff --git a/app/Peripherals/PeripheralsProvider.cs b/app/Peripherals/PeripheralsProvider.cs index 5a4e6e47..29851872 100644 --- a/app/Peripherals/PeripheralsProvider.cs +++ b/app/Peripherals/PeripheralsProvider.cs @@ -7,6 +7,8 @@ namespace GHelper.Peripherals { public class PeripheralsProvider { + public static object _LOCK = new object(); + public static List ConnectedMice = new List(); public static event EventHandler? DeviceChanged; @@ -16,15 +18,30 @@ namespace GHelper.Peripherals return ConnectedMice.Count > 0; } + //Expand if keyboards or other device get supported later. + public static bool IsAnyPeripheralConnect() + { + return IsMouseConnected(); + } + + public static List AllPeripherals() + { + List l = new List(); + l.AddRange(ConnectedMice); + + return l; + } + public static void RefreshBatteryForAllDevices() { - lock (ConnectedMice) + lock (_LOCK) { - foreach (AsusMouse m in ConnectedMice) + foreach (IPeripheral m in AllPeripherals()) { - if (!m.MouseIsReady) + if (!m.IsDeviceReady) { - m.SynchronizeMouse(); + //Try to sync the device if that hasn't been done yet + m.SynchronizeDevice(); } else { @@ -37,7 +54,7 @@ namespace GHelper.Peripherals public static void Disconnect(AsusMouse am) { - lock (ConnectedMice) + lock (_LOCK) { ConnectedMice.Remove(am); if (DeviceChanged is not null) @@ -49,7 +66,7 @@ namespace GHelper.Peripherals public static void Connect(AsusMouse am) { - lock (ConnectedMice) + lock (_LOCK) { if (ConnectedMice.Contains(am)) { @@ -72,11 +89,11 @@ namespace GHelper.Peripherals //Retry 3 times. Do not call this on main thread! It would block the UI int tries = 0; - while (!am.MouseIsReady && tries < 3) + while (!am.IsDeviceReady && tries < 3) { Thread.Sleep(250); Logger.WriteLine(am.GetDisplayName() + " synchronising. Try " + (tries + 1)); - am.SynchronizeMouse(); + am.SynchronizeDevice(); ++tries; } @@ -96,7 +113,7 @@ namespace GHelper.Peripherals { return; } - lock (ConnectedMice) + lock (_LOCK) { AsusMouse am = (AsusMouse)sender; ConnectedMice.Remove(am);