diff --git a/app/Ally/AllyControl.cs b/app/Ally/AllyControl.cs index f0c826ff..61741b3b 100644 --- a/app/Ally/AllyControl.cs +++ b/app/Ally/AllyControl.cs @@ -1,10 +1,13 @@ using GHelper.Gpu.AMD; using GHelper.Helpers; using GHelper.Input; +using GHelper.Mode; using GHelper.USB; using HidSharp; using System.Text; + + namespace GHelper.Ally { @@ -32,17 +35,28 @@ namespace GHelper.Ally public class AllyControl { - System.Timers.Timer timer = default!; + static System.Timers.Timer timer = default!; static AmdGpuControl amdControl = new AmdGpuControl(); SettingsForm settings; static ControllerMode _mode = ControllerMode.Auto; static ControllerMode _applyMode = ControllerMode.Mouse; + static int _autoCount = 0; + static int _upCount = 0; + static int _downCount = 0; + + static int tdpMin = 6; + static int tdpStable = tdpMin; + static int tdpCurrent = -1; + + static bool autoTDP = false; + static int fpsLimit = -1; + public const string BindA = "01-01"; public const string BindB = "01-02"; public const string BindX = "01-03"; @@ -280,32 +294,116 @@ namespace GHelper.Ally public AllyControl(SettingsForm settingsForm) { if (!AppConfig.IsAlly()) return; - settings = settingsForm; - timer = new System.Timers.Timer(300); - timer.Elapsed += Timer_Elapsed; + if (timer is null) + { + timer = new System.Timers.Timer(300); + timer.Elapsed += Timer_Elapsed; + Logger.WriteLine("Ally timer"); + } + } + private int GetMaxTDP() + { + int tdp = AppConfig.GetMode("limit_total"); + if (tdp > 0) return tdp; + switch (Modes.GetCurrentBase()) + { + case 1: + return 25; + case 2: + return 10; + default: + return 15; + } + } + + private int GetTDP() + { + if (tdpCurrent < 0) tdpCurrent = GetMaxTDP(); + return tdpCurrent; + } + + private void SetTDP(int tdp, string log) + { + if (tdp < tdpStable) tdp = tdpStable; + + int max = GetMaxTDP(); + if (tdp > max) tdp = max; + + if (tdp == tdpCurrent) return; + if (!autoTDP) return; + + Program.acpi.DeviceSet(AsusACPI.PPT_APUA0, tdp, log); + Program.acpi.DeviceSet(AsusACPI.PPT_APUA3, tdp, null); + Program.acpi.DeviceSet(AsusACPI.PPT_APUC1, tdp, null); + + tdpCurrent = tdp; } private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) { + if (!autoTDP && _mode != ControllerMode.Auto) return; + float fps = amdControl.GetFPS(); - ControllerMode newMode = (fps > 0) ? ControllerMode.Gamepad : ControllerMode.Mouse; - - if (_applyMode != newMode) _autoCount++; - else _autoCount = 0; - - if (_mode != ControllerMode.Auto) return; - - if (_autoCount > 2) + if (autoTDP && fpsLimit > 0 && fpsLimit <= 120) { - _autoCount = 0; - ApplyMode(newMode); - Logger.WriteLine(fps.ToString()); + int power = (int)amdControl.GetGpuPower(); + //Debug.WriteLine($"{power}: {fps}"); + + if (fps <= fpsLimit * 0.8) _upCount++; + else _upCount = 0; + + if (fps >= fpsLimit * 0.90) _downCount++; + else _downCount = 0; + + var tdp = GetTDP(); + if (_upCount >= 1) + { + _downCount = 0; + _upCount = 0; + SetTDP(tdp + 1, $"AutoTDP+ [{power}]{fps}"); + } + + if (_downCount >= 8 && power < tdp) + { + _upCount = 0; + _downCount--; + SetTDP(tdp - 1, $"AutoTDP- [{power}]{fps}"); + } } + if (_mode == ControllerMode.Auto) + { + ControllerMode newMode = (fps > 0) ? ControllerMode.Gamepad : ControllerMode.Mouse; + + if (_applyMode != newMode) _autoCount++; + else _autoCount = 0; + + if (_autoCount == 3) + { + _autoCount = 0; + ApplyMode(newMode); + Logger.WriteLine($"Controller Mode {fps}: {newMode}"); + } + } + + } + + public void ToggleAutoTDP() + { + autoTDP = !autoTDP; + tdpCurrent = -1; + + if (!autoTDP) + { + Program.modeControl.SetPerformanceMode(); + } + + settings.VisualiseAutoTDP(autoTDP); + } public void Init() @@ -319,7 +417,6 @@ namespace GHelper.Ally fpsLimit = amdControl.GetFPSLimit(); settings.VisualiseFPSLimit(fpsLimit); - } public void ToggleFPSLimit() @@ -479,7 +576,7 @@ namespace GHelper.Ally DecodeBinding(KeyR2).CopyTo(bindings, 38); //AsusHid.WriteInput(CommandReady, null); - AsusHid.WriteInput(bindings, $"B{zone}"); + AsusHid.WriteInput(bindings, null); } @@ -495,19 +592,19 @@ namespace GHelper.Ally (byte)AppConfig.Get("ls_max", 100), (byte)AppConfig.Get("rs_min", 0), (byte)AppConfig.Get("rs_max", 100) - }, "StickDeadzone"); + }, null); AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xd1, 5, 4, (byte)AppConfig.Get("lt_min", 0), (byte)AppConfig.Get("lt_max", 100), (byte)AppConfig.Get("rt_min", 0), (byte)AppConfig.Get("rt_max", 100) - }, "TriggerDeadzone"); + }, null); AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xd1, 6, 2, (byte)AppConfig.Get("vibra", 100), (byte)AppConfig.Get("vibra", 100) - }, "Vibration"); + }, null); } @@ -572,18 +669,11 @@ namespace GHelper.Ally _mode = mode; AppConfig.Set("controller_mode", (int)mode); + amdControl.StopFPS(); ApplyMode(mode, init); - if (mode == ControllerMode.Auto) - { - amdControl.StartFPS(); - timer.Start(); - } - else - { - timer.Stop(); - amdControl.StopFPS(); - } + amdControl.StartFPS(); + timer.Start(); settings.VisualiseController(mode); } diff --git a/app/Gpu/AMD/AmdGpuControl.cs b/app/Gpu/AMD/AmdGpuControl.cs index 6583f12a..e10bf191 100644 --- a/app/Gpu/AMD/AmdGpuControl.cs +++ b/app/Gpu/AMD/AmdGpuControl.cs @@ -119,6 +119,18 @@ public class AmdGpuControl : IGpuControl } + public int? GetGpuPower() + { + if (_adlContextHandle == nint.Zero || _iGPU == null) return null; + if (ADL2_New_QueryPMLogData_Get(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, out ADLPMLogDataOutput adlpmLogDataOutput) != Adl2.ADL_SUCCESS) return null; + + ADLSingleSensorData gpuUsage = adlpmLogDataOutput.Sensors[(int)ADLSensorType.PMLOG_ASIC_POWER]; + if (gpuUsage.Supported == 0) return null; + + return gpuUsage.Value; + + } + public bool SetVariBright(int enabled) { diff --git a/app/Settings.Designer.cs b/app/Settings.Designer.cs index 130b0ce1..b05ac806 100644 --- a/app/Settings.Designer.cs +++ b/app/Settings.Designer.cs @@ -135,6 +135,7 @@ namespace GHelper labelGamma = new Label(); pictureGamma = new PictureBox(); labelGammaTitle = new Label(); + buttonAutoTDP = new RButton(); panelMatrix.SuspendLayout(); panelMatrixAuto.SuspendLayout(); tableLayoutMatrix.SuspendLayout(); @@ -766,6 +767,7 @@ namespace GHelper tableAMD.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F)); tableAMD.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F)); tableAMD.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F)); + tableAMD.Controls.Add(buttonAutoTDP, 0, 0); tableAMD.Controls.Add(buttonOverlay, 0, 0); tableAMD.Controls.Add(buttonFPS, 0, 0); tableAMD.Dock = DockStyle.Top; @@ -1893,6 +1895,28 @@ namespace GHelper labelGammaTitle.TabIndex = 37; labelGammaTitle.Text = "Flicker-free Dimming"; // + // buttonAutoTDP + // + buttonAutoTDP.Activated = false; + buttonAutoTDP.BackColor = SystemColors.ControlLightLight; + buttonAutoTDP.BorderColor = Color.Transparent; + buttonAutoTDP.BorderRadius = 5; + buttonAutoTDP.Dock = DockStyle.Fill; + buttonAutoTDP.FlatAppearance.BorderSize = 0; + buttonAutoTDP.FlatStyle = FlatStyle.Flat; + buttonAutoTDP.ForeColor = SystemColors.ControlText; + buttonAutoTDP.Image = Properties.Resources.icons8_gauge_32; + buttonAutoTDP.ImageAlign = ContentAlignment.MiddleRight; + buttonAutoTDP.Location = new Point(528, 4); + buttonAutoTDP.Margin = new Padding(4); + buttonAutoTDP.Name = "buttonAutoTDP"; + buttonAutoTDP.Secondary = false; + buttonAutoTDP.Size = new Size(255, 72); + buttonAutoTDP.TabIndex = 13; + buttonAutoTDP.Text = "AutoTDP"; + buttonAutoTDP.TextImageRelation = TextImageRelation.ImageBeforeText; + buttonAutoTDP.UseVisualStyleBackColor = false; + // // SettingsForm // AutoScaleDimensions = new SizeF(192F, 192F); @@ -2095,5 +2119,6 @@ namespace GHelper private RButton buttonInstallColor; private Label labelVisual; private RButton buttonFHD; + private RButton buttonAutoTDP; } } diff --git a/app/Settings.cs b/app/Settings.cs index 50695137..a07dc2d5 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -222,7 +222,9 @@ namespace GHelper buttonUpdates.Click += ButtonUpdates_Click; - sliderBattery.ValueChanged += SliderBattery_ValueChanged; + sliderBattery.MouseUp += SliderBattery_MouseUp; + sliderBattery.KeyUp += SliderBattery_KeyUp; + Program.trayIcon.MouseMove += TrayIcon_MouseMove; sensorTimer = new System.Timers.Timer(AppConfig.Get("sensor_timer", 1000)); @@ -249,6 +251,9 @@ namespace GHelper buttonFPS.Click += ButtonFPS_Click; buttonOverlay.Click += ButtonOverlay_Click; + + buttonAutoTDP.Click += ButtonAutoTDP_Click; + buttonAutoTDP.BorderColor = colorTurbo; Text = "G-Helper " + (ProcessHelper.IsUserAdministrator() ? "—" : "-") + " " + AppConfig.GetModelShort(); TopMost = AppConfig.Is("topmost"); @@ -272,6 +277,21 @@ namespace GHelper screenControl.ToogleFHD(); } + private void SliderBattery_KeyUp(object? sender, KeyEventArgs e) + { + BatteryControl.SetBatteryChargeLimit(sliderBattery.Value); + } + + private void SliderBattery_MouseUp(object? sender, MouseEventArgs e) + { + BatteryControl.SetBatteryChargeLimit(sliderBattery.Value); + } + + private void ButtonAutoTDP_Click(object? sender, EventArgs e) + { + allyControl.ToggleAutoTDP(); + } + private void LabelCharge_Click(object? sender, EventArgs e) { BatteryControl.BatteryReport(); @@ -496,6 +516,12 @@ namespace GHelper buttonFPS.Text = "FPS Limit " + ((limit > 0 && limit <= 120) ? limit : "OFF"); } + public void VisualiseAutoTDP(bool status) + { + Logger.WriteLine($"Auto TDP: {status}"); + buttonAutoTDP.Activated = status; + } + private void SettingsForm_LostFocus(object? sender, EventArgs e) { lastLostFocus = DateTimeOffset.Now.ToUnixTimeMilliseconds(); @@ -736,11 +762,6 @@ namespace GHelper gpuControl.ToggleXGM(); } - private void SliderBattery_ValueChanged(object? sender, EventArgs e) - { - BatteryControl.SetBatteryChargeLimit(sliderBattery.Value); - } - public void SetVersionLabel(string label, bool update = false) {