Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Dec 26, 2024
2 parents dcf9a4c + 8cb73ee commit b083216
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 203 deletions.
3 changes: 3 additions & 0 deletions HandheldCompanion/Devices/AOKZOE/AOKZOEA1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public override bool Open()

public override void Close()
{
if (!IsOpen)
return;

LogManager.LogInformation("Locked {0} OEM button", ButtonFlags.OEM3);
ECRamDirectWrite(0x4F1, ECDetails, 0x00);
ECRamDirectWrite(0x4F2, ECDetails, 0x00);
Expand Down
13 changes: 11 additions & 2 deletions HandheldCompanion/Devices/ASUS/ROGAlly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ public ROGAlly()
[KeyCode.F17],
false, ButtonFlags.OEM4
));

SettingsManager.SettingValueChanged += SettingsManager_SettingValueChanged;
}

private byte[] flushBufferWriteChanges = new byte[64]
Expand Down Expand Up @@ -344,6 +342,15 @@ public override bool Open()
// force M1/M2 to send F17 and F18
ConfigureController(true);

// manage events
SettingsManager.SettingValueChanged += SettingsManager_SettingValueChanged;

// raise events
if (SettingsManager.IsInitialized)
{
SettingsManager_SettingValueChanged("BatteryChargeLimit", SettingsManager.GetString("BatteryChargeLimit"), false);
}

return true;
}

Expand All @@ -370,6 +377,8 @@ public override void Close()

hidDevices.Clear();

SettingsManager.SettingValueChanged -= SettingsManager_SettingValueChanged;

base.Close();
}

Expand Down
71 changes: 51 additions & 20 deletions HandheldCompanion/Devices/AYANEO/AYANEODeviceCEc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HandheldCompanion.Inputs;
using HandheldCompanion.Managers;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading;
Expand Down Expand Up @@ -88,30 +89,17 @@ public override bool Open()
this.CEcControl_RgbHoldControl();
}

// manage events
SettingsManager.SettingValueChanged += SettingsManager_SettingValueChanged;
PowerManager.RemainingChargePercentChanged += PowerManager_RemainingChargePercentChanged;

return true;
}

private void PowerManager_RemainingChargePercentChanged(object? sender, object e)
{
// Check if BatteryChargeLimit is enabled in SettingsManager
bool isBatteryChargeLimitEnabled = SettingsManager.GetBoolean("BatteryChargeLimit");
if (isBatteryChargeLimitEnabled)
// raise events
if (SettingsManager.IsInitialized)
{
// Get the current battery percentage
int batteryPercentage = PowerManager.RemainingChargePercent;
if (batteryPercentage >= 80)
{
// Call the function to open the charge bypass
CEcControl_BypassChargeOpen();
}
else if (batteryPercentage < 80)
{
// Call the function to close the charge bypass
CEcControl_BypassChargeClose();
}
SettingsManager_SettingValueChanged("BatteryChargeLimit", SettingsManager.GetString("BatteryChargeLimit"), false);
}

return true;
}

public override void Close()
Expand All @@ -121,11 +109,54 @@ public override void Close()
this.CEcControl_RgbReleaseControl();
}

SettingsManager.SettingValueChanged -= SettingsManager_SettingValueChanged;
PowerManager.RemainingChargePercentChanged -= PowerManager_RemainingChargePercentChanged;

base.Close();
}

private void SettingsManager_SettingValueChanged(string name, object value, bool temporary)
{
switch (name)
{
case "BatteryChargeLimit":
{
bool enabled = Convert.ToBoolean(value);
switch (enabled)
{
case true:
CEcControl_BypassChargeOpen();
break;
case false:
CEcControl_BypassChargeClose();
break;
}
}
break;
}
}

private void PowerManager_RemainingChargePercentChanged(object? sender, object e)
{
// Check if BatteryChargeLimit is enabled in SettingsManager
bool isBatteryChargeLimitEnabled = SettingsManager.GetBoolean("BatteryChargeLimit");
if (!isBatteryChargeLimitEnabled)
return;

// Get the current battery percentage
int batteryPercentage = PowerManager.RemainingChargePercent;
if (batteryPercentage >= 80)
{
// Call the function to open the charge bypass
CEcControl_BypassChargeOpen();
}
else if (batteryPercentage < 80)
{
// Call the function to close the charge bypass
CEcControl_BypassChargeClose();
}
}

public override bool SetLedStatus(bool status)
{
lock (this.updateLock)
Expand Down
145 changes: 84 additions & 61 deletions HandheldCompanion/Devices/IDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,79 @@ public IDevice()
OSPowerMode = OSPowerMode.BetterPerformance,
TDPOverrideValues = new double[] { this.nTDP[0], this.nTDP[1], this.nTDP[2] }
});
}

public virtual bool Open()
{
if (openLibSys != null)
return true;

try
{
// initialize OpenLibSys
openLibSys = new OpenLibSys();

// Check support library sutatus
var status = openLibSys.GetStatus();
switch (status)
{
case (uint)OlsStatus.NO_ERROR:
break;
default:
LogManager.LogError("Couldn't initialize OpenLibSys. ErrorCode: {0}", status);
return false;
}

// Check WinRing0 status
var dllstatus = (OlsDllStatus)openLibSys.GetDllStatus();
switch (dllstatus)
{
case (uint)OlsDllStatus.OLS_DLL_NO_ERROR:
break;
default:
LogManager.LogError("Couldn't initialize OpenLibSys. ErrorCode: {0}", dllstatus);
return false;
}
}
catch (Exception ex)
{
LogManager.LogError("Couldn't initialize OpenLibSys. ErrorCode: {0}", ex.Message);
Close();
return false;
}

// manage events
VirtualManager.ControllerSelected += VirtualManager_ControllerSelected;
DeviceManager.UsbDeviceArrived += GenericDeviceUpdated;
DeviceManager.UsbDeviceRemoved += GenericDeviceUpdated;

// raise events
if (VirtualManager.IsInitialized)
{
VirtualManager_ControllerSelected(VirtualManager.HIDmode);
}

if (DeviceManager.IsInitialized)
{
GenericDeviceUpdated(null, Guid.Empty);
}

return true;
}

public virtual void Close()
{
if (openLibSys is null)
return;

SetFanControl(false);

openLibSys.Dispose();
openLibSys = null;

VirtualManager.ControllerSelected -= VirtualManager_ControllerSelected;
DeviceManager.UsbDeviceArrived -= GenericDeviceUpdated;
DeviceManager.UsbDeviceRemoved -= GenericDeviceUpdated;
}

private void VirtualManager_ControllerSelected(HIDmode mode)
Expand Down Expand Up @@ -601,59 +670,6 @@ public bool HasMotionSensor()
return Capabilities.HasFlag(DeviceCapabilities.InternalSensor) || Capabilities.HasFlag(DeviceCapabilities.ExternalSensor);
}

public virtual bool Open()
{
if (openLibSys != null)
return true;

try
{
// initialize OpenLibSys
openLibSys = new OpenLibSys();

// Check support library sutatus
var status = openLibSys.GetStatus();
switch (status)
{
case (uint)OlsStatus.NO_ERROR:
break;
default:
LogManager.LogError("Couldn't initialize OpenLibSys. ErrorCode: {0}", status);
return false;
}

// Check WinRing0 status
var dllstatus = (OlsDllStatus)openLibSys.GetDllStatus();
switch (dllstatus)
{
case (uint)OlsDllStatus.OLS_DLL_NO_ERROR:
break;
default:
LogManager.LogError("Couldn't initialize OpenLibSys. ErrorCode: {0}", dllstatus);
return false;
}
}
catch (Exception ex)
{
LogManager.LogError("Couldn't initialize OpenLibSys. ErrorCode: {0}", ex.Message);
Close();
return false;
}

return true;
}

public virtual void Close()
{
if (openLibSys is null)
return;

SetFanControl(false);

openLibSys.Dispose();
openLibSys = null;
}

public virtual bool IsReady()
{
return true;
Expand Down Expand Up @@ -760,36 +776,43 @@ public virtual bool SetLEDPreset(LEDPreset? preset)
[Obsolete("ECRamReadByte is deprecated, please use ECRamReadByte with ECDetails instead.")]
public virtual byte ECRamReadByte(ushort address)
{
if (!IsOpen)
return 0;

try
{
return openLibSys.ReadIoPortByte(address);
}
catch (Exception ex)
{
LogManager.LogError("Couldn't read byte from address {0} using OpenLibSys. ErrorCode: {1}", address,
ex.Message);
LogManager.LogError("Couldn't read byte from address {0} using OpenLibSys. ErrorCode: {1}", address, ex.Message);
return 0;
}
}

[Obsolete("ECRamWriteByte is deprecated, please use ECRamDirectWrite with ECDetails instead.")]
public virtual bool ECRamWriteByte(ushort address, byte data)
{
if (!IsOpen)
return false;

try
{
openLibSys.WriteIoPortByte(address, data);
return true;
}
catch (Exception ex)
{
LogManager.LogError("Couldn't write byte to address {0} using OpenLibSys. ErrorCode: {1}", address,
ex.Message);
LogManager.LogError("Couldn't write byte to address {0} using OpenLibSys. ErrorCode: {1}", address, ex.Message);
return false;
}
}

public virtual byte ECRamReadByte(ushort address, ECDetails details)
{
if (!IsOpen)
return 0;

var addr_upper = (byte)((address >> 8) & byte.MaxValue);
var addr_lower = (byte)(address & byte.MaxValue);

Expand Down Expand Up @@ -820,6 +843,9 @@ public virtual byte ECRamReadByte(ushort address, ECDetails details)

public virtual bool ECRamDirectWrite(ushort address, ECDetails details, byte data)
{
if (!IsOpen)
return false;

byte addr_upper = (byte)((address >> 8) & byte.MaxValue);
byte addr_lower = (byte)(address & byte.MaxValue);

Expand Down Expand Up @@ -871,12 +897,9 @@ protected bool IsECReady()
{
DateTime timeout = DateTime.Now.AddMilliseconds(250);
while (DateTime.Now < timeout)
{
if ((ECRamReadByte(EC_SC) & EC_IBF) == 0x0)
{
return true;
}
}

return false;
}

Expand Down
Loading

0 comments on commit b083216

Please sign in to comment.