Restructured to generic Interface for easier extension.

This commit is contained in:
IceStormNG
2023-07-23 11:52:31 +02:00
parent df616b486d
commit f9f96bd807
3 changed files with 102 additions and 21 deletions

View File

@@ -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();
}
}

View File

@@ -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 + "%");
}
// ------------------------------------------------------------------------------

View File

@@ -7,6 +7,8 @@ namespace GHelper.Peripherals
{
public class PeripheralsProvider
{
public static object _LOCK = new object();
public static List<AsusMouse> ConnectedMice = new List<AsusMouse>();
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<IPeripheral> AllPeripherals()
{
List<IPeripheral> l = new List<IPeripheral>();
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);