Skip to content

Commit aab5b71

Browse files
committed
XGM 2025 LED / fan controls #5008
1 parent af60392 commit aab5b71

File tree

7 files changed

+89
-68
lines changed

7 files changed

+89
-68
lines changed

app/Extra.Designer.cs

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Extra.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ public Extra()
423423
sliderBrightness.Value = InputDispatcher.GetBacklight();
424424
sliderBrightness.ValueChanged += SliderBrightness_ValueChanged;
425425

426-
panelXMG.Visible = (Program.acpi.DeviceGet(AsusACPI.GPUXGConnected) == 1);
427-
checkXMG.Checked = !(AppConfig.Get("xmg_light") == 0);
428-
checkXMG.CheckedChanged += CheckXMG_CheckedChanged;
426+
panelXGM.Visible = XGM.IsConnected();
427+
checkXGM.Checked = !(AppConfig.Get("xmg_light") == 0);
428+
checkXGM.CheckedChanged += CheckXGM_CheckedChanged;
429429

430430
numericBacklightTime.Value = AppConfig.Get("keyboard_timeout", 60);
431431
numericBacklightPluggedTime.Value = AppConfig.Get("keyboard_ac_timeout", 0);
@@ -744,10 +744,10 @@ private void NumericBacklightTime_ValueChanged(object? sender, EventArgs e)
744744
Program.inputDispatcher.InitBacklightTimer();
745745
}
746746

747-
private void CheckXMG_CheckedChanged(object? sender, EventArgs e)
747+
private void CheckXGM_CheckedChanged(object? sender, EventArgs e)
748748
{
749-
AppConfig.Set("xmg_light", (checkXMG.Checked ? 1 : 0));
750-
XGM.Light(checkXMG.Checked);
749+
AppConfig.Set("xmg_light", (checkXGM.Checked ? 1 : 0));
750+
XGM.Light(checkXGM.Checked);
751751
}
752752

753753
private void CheckUSBC_CheckedChanged(object? sender, EventArgs e)

app/Fans.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ private void ButtonReset_Click(object? sender, EventArgs e)
11861186

11871187
InitPowerPlan();
11881188

1189-
if (Program.acpi.IsXGConnected()) XGM.Reset();
1189+
XGM.Reset();
11901190

11911191

11921192
if (gpuVisible)

app/Gpu/GPUModeControl.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,6 @@ public bool AutoGPUMode(bool optimized = false, int delay = 0)
269269
}
270270

271271

272-
public void InitXGM()
273-
{
274-
if (Program.acpi.IsXGConnected())
275-
{
276-
//Program.acpi.DeviceSet(AsusACPI.GPUXGInit, 1, "XG Init");
277-
XGM.Init();
278-
}
279-
280-
}
281-
282272
public void ToggleXGM(bool silent = false)
283273
{
284274

@@ -314,8 +304,7 @@ public void ToggleXGM(bool silent = false)
314304
else
315305
Program.acpi.DeviceSet(AsusACPI.GPUXG, 1, "GPU XGM");
316306

317-
InitXGM();
318-
XGM.Light(AppConfig.Is("xmg_light"));
307+
XGM.Init();
319308

320309
await Task.Delay(TimeSpan.FromSeconds(15));
321310

app/Mode/ModeControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void SetPerformanceMode(int mode = -1, bool notify = false)
114114
});
115115

116116

117-
if (AppConfig.Is("xgm_fan") && Program.acpi.IsXGConnected()) XGM.Reset();
117+
if (AppConfig.Is("xgm_fan")) XGM.Reset();
118118

119119
if (notify) Toast();
120120

@@ -176,10 +176,10 @@ public void AutoFans(bool force = false)
176176
{
177177

178178
bool xgmFan = false;
179-
if (AppConfig.Is("xgm_fan") && Program.acpi.IsXGConnected())
179+
if (AppConfig.Is("xgm_fan"))
180180
{
181181
XGM.SetFan(AppConfig.GetFanConfig(AsusFan.XGM));
182-
xgmFan = true;
182+
xgmFan = Program.acpi.IsXGConnected();
183183
}
184184

185185
int cpuResult = Program.acpi.SetFanCurve(AsusFan.CPU, AppConfig.GetFanConfig(AsusFan.CPU));

app/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static void Main(string[] args)
134134
settingsForm.InitAura();
135135
settingsForm.InitMatrix();
136136

137-
gpuControl.InitXGM();
137+
XGM.Init();
138138

139139
SetAutoModes(init: true);
140140

app/USB/XGM.cs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,34 @@ namespace GHelper.USB
88
public static class XGM
99
{
1010
const int ASUS_ID = 0x0b05;
11+
static readonly int[] deviceIds = { 0x1970, 0x1a9a, 0x1C29};
1112

12-
static int[] deviceIds = { 0x1970, 0x1a9a};
13+
public static HidDevice? GetDevice()
14+
{
15+
try
16+
{
17+
return DeviceList.Local.GetHidDevices(ASUS_ID).FirstOrDefault(device =>
18+
deviceIds.Contains(device.ProductID) &&
19+
device.CanOpen &&
20+
device.GetMaxFeatureReportLength() >= 300);
21+
}
22+
catch (Exception ex)
23+
{
24+
Logger.WriteLine($"Error getting XGM device: {ex}");
25+
return null;
26+
}
27+
}
28+
29+
public static bool IsConnected()
30+
{
31+
return GetDevice() is not null;
32+
}
1333

1434
public static void Write(byte[] data)
1535
{
16-
HidDeviceLoader loader = new HidDeviceLoader();
1736
try
1837
{
19-
HidDevice device = loader.GetDevices(ASUS_ID).Where(device => deviceIds.Contains(device.ProductID) && device.CanOpen && device.GetMaxFeatureReportLength() >= 300).FirstOrDefault();
20-
38+
HidDevice? device = GetDevice();
2139
if (device is null)
2240
{
2341
Logger.WriteLine("XGM SUB device not found");
@@ -26,13 +44,10 @@ public static void Write(byte[] data)
2644

2745
using (HidStream hidStream = device.Open())
2846
{
29-
var payload = new byte[300];
30-
Array.Copy(data, payload, data.Length);
31-
47+
byte[] payload = new byte[300];
48+
data.CopyTo(payload, 0);
3249
hidStream.SetFeature(payload);
33-
Logger.WriteLine("XGM-" + device.ProductID + "|" + device.GetMaxFeatureReportLength() + ":" + BitConverter.ToString(data));
34-
35-
hidStream.Close();
50+
Logger.WriteLine($"XGM-{device.ProductID}|{device.GetMaxFeatureReportLength()}:{BitConverter.ToString(data)}");
3651
}
3752
}
3853
catch (Exception ex)
@@ -44,33 +59,50 @@ public static void Write(byte[] data)
4459

4560
public static void Init()
4661
{
47-
Write(Encoding.ASCII.GetBytes("^ASUS Tech.Inc."));
62+
Task.Run(() =>
63+
{
64+
if (IsConnected())
65+
{
66+
Write(Encoding.ASCII.GetBytes("^ASUS Tech.Inc."));
67+
Light(AppConfig.Is("xmg_light"));
68+
}
69+
});
4870
}
4971

5072
public static void Light(bool status)
5173
{
52-
Write(new byte[] { 0x5e, 0xc5, status ? (byte)0x50 : (byte)0 });
74+
Write([0x5e, 0xc5, status ? (byte)0x50 : (byte)0]);
5375
}
5476

5577
public static void InitLight()
5678
{
57-
if (Program.acpi.IsXGConnected()) Light(AppConfig.Is("xmg_light"));
79+
Task.Run(() =>
80+
{
81+
if (IsConnected()) Light(AppConfig.Is("xmg_light"));
82+
});
5883
}
5984

6085
public static void Reset()
6186
{
62-
Write(new byte[] { 0x5e, 0xd1, 0x02 });
87+
Task.Run(() =>
88+
{
89+
if (IsConnected()) Write([0x5e, 0xd1, 0x02]);
90+
});
6391
}
6492

6593
public static void SetFan(byte[] curve)
6694
{
67-
if (AsusACPI.IsInvalidCurve(curve)) return;
68-
69-
byte[] msg = new byte[19];
70-
Array.Copy(new byte[] { 0x5e, 0xd1, 0x01 }, msg, 3);
71-
Array.Copy(curve, 0, msg, 3, curve.Length);
72-
73-
Write(msg);
95+
Task.Run(() =>
96+
{
97+
if (IsConnected())
98+
{
99+
if (AsusACPI.IsInvalidCurve(curve)) return;
100+
byte[] msg = new byte[19];
101+
Array.Copy(new byte[] { 0x5e, 0xd1, 0x01 }, msg, 3);
102+
Array.Copy(curve, 0, msg, 3, curve.Length);
103+
Write(msg);
104+
}
105+
});
74106
}
75107
}
76108
}

0 commit comments

Comments
 (0)