Skip to content

Commit

Permalink
Add ability to save power mode settings
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesCJ60 committed Jun 17, 2024
1 parent 58b53b9 commit 3ba14c9
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 48 deletions.
94 changes: 94 additions & 0 deletions Framework Hub/Services/PowerModeSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Framework_Hub.Scripts.Windows.Misc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Framework_Hub.Services
{
internal class PowerModeSettings
{
public class PowerModePresets
{
public int _powerIndex { get; set; } = 2;

public int _pl1 { get; set; } = 999;
public int _pl2 { get; set; } = 999;

public int _pl1Max { get; set; } = 999;
public int _pl2Max { get; set; } = 999;
public int _pl1Min { get; set; } = 999;
public int _pl2Min { get; set; } = 999;

public int _temp { get; set; } = 100;

public int _allCO { get; set; } = 0;
public int _gfxCO { get; set; } = 0;

public int _pboOffset { get; set; } = 1;

public int _winPower { get; set; } = 2;

}

internal class PowerModeSettingsManager
{
private Dictionary<string, PowerModePresets> _settings;

private readonly string _configDirectory;
string _device = GetSystemInfo.Product;

// set up manager instance
public PowerModeSettingsManager(string configDirectory)
{
_configDirectory = configDirectory;
_settings = new Dictionary<string, PowerModePresets>();
LoadPresets();
}

// Get data from preset
public PowerModePresets GetPreset(int _powerMode)
{
if (_settings.ContainsKey($"{_device}_{_powerMode}"))
{
return _settings[$"{_device}_{_powerMode}"];
}
else
{
return null;
}
}

// Load all presents into string dictionary
private void LoadPresets()
{
if (File.Exists(_configDirectory))
{
string json = File.ReadAllText(_configDirectory);
_settings = JsonConvert.DeserializeObject<Dictionary<string, PowerModePresets>>(json);
}
else
{
_settings = new Dictionary<string, PowerModePresets>();
}
}

// Save preset to json file
public void SaveSettings(PowerModePresets _newPreset, int _powerMode)
{
_settings[$"{_device}_{_powerMode}"] = _newPreset;
SaveAppSettings();
}

// Save json file changes
private void SaveAppSettings()
{
string json = JsonConvert.SerializeObject(_settings, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(_configDirectory, json);
}
}
}
}
139 changes: 91 additions & 48 deletions Framework Hub/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Framework_Hub.Services;
using static Framework_Hub.Services.AppSettings;
using System.IO;
using static Framework_Hub.Services.PowerModeSettings;

namespace Framework_Hub.ViewModels
{
Expand Down Expand Up @@ -131,6 +132,7 @@ public string WinPowerText
}

AppSettingsManager appSettings = new AppSettingsManager("Settings.json");
PowerModeSettingsManager powerModeSettings = new PowerModeSettingsManager("PowerSettings.json");

public MainViewModel()
{
Expand All @@ -140,7 +142,7 @@ public MainViewModel()
PowerIndex = settings.lastPowerMode;
}
else PowerIndex = 2;


// Setup event to detect variable changes
var propertySelectors = new Expression<Func<MainViewModel, int>>[]
Expand Down Expand Up @@ -178,12 +180,14 @@ private async void OnPowerChange()
else if (WinPower == 1) WinPowerText = "\uec49";
else if (WinPower == 2) WinPowerText = "\uec4a";

if(lastPowerMode != PowerIndex)
if (lastPowerMode != PowerIndex)
{
SetUpTempPower();
lastPowerMode = PowerIndex;
}

SavePreset();

await ApplyPowerSettings();
}

Expand Down Expand Up @@ -223,7 +227,7 @@ public async Task ApplyPowerSettings()
RyzenAdj_Backend_Linux.set_fast_limit(RyzenAdj_Backend_Linux.ry, (uint)(PL2 * 1000));

// Set all core Curve Optimiser offset
if(AllCO < 0) RyzenAdj_Backend_Linux.set_coall(RyzenAdj_Backend_Linux.ry, Convert.ToUInt32(0x100000 - (uint)(-1 * AllCO)));
if (AllCO < 0) RyzenAdj_Backend_Linux.set_coall(RyzenAdj_Backend_Linux.ry, Convert.ToUInt32(0x100000 - (uint)(-1 * AllCO)));
else RyzenAdj_Backend_Linux.set_coall(RyzenAdj_Backend_Linux.ry, 0);

// Set iGPU Curve Optimiser offset
Expand All @@ -237,58 +241,78 @@ public async Task ApplyPowerSettings()

private void SetUpTempPower()
{
// Setup temp defaults for each power mode
if (GetSystemInfo.Product.Contains("16") && GetSystemInfo.IsGPUPresent("RX 7700S"))
if (powerModeSettings.GetPreset(PowerIndex) != null && File.Exists("PowerSettings.json"))
{
if (PowerIndex == 0)
{
PL1 = 85;
PL2 = 85;
WinPower = 0;
}
else if (PowerIndex == 1)
{
PL1 = 95;
PL2 = 95;
WinPower = 1;
}
else if (PowerIndex == 2)
{
PL1 = 100;
PL2 = 120;
WinPower = 2;
}

PL1Min = 10;
PL2Min = 10;
PL1Max = 140;
PL2Max = 140;
PowerModePresets _powerPreset = powerModeSettings.GetPreset(PowerIndex);
Temp = _powerPreset._temp;
PL1 = _powerPreset._pl1;
PL2 = _powerPreset._pl2;
PL1Max = _powerPreset._pl1Max;
PL2Max = _powerPreset._pl2Max;
PL1Min = _powerPreset._pl1Min;
PL2Min = _powerPreset._pl2Min;
AllCO = _powerPreset._allCO;
GfxCO = _powerPreset._gfxCO;
PboOffset = _powerPreset._pboOffset;
WinPower = _powerPreset._winPower;
}
else if (GetSystemInfo.Product.Contains("13"))
else
{
if (PowerIndex == 0)
{
PL1 = 15;
PL2 = 18;
WinPower = 0;
}
else if (PowerIndex == 1)
// Setup temp defaults for each power mode
if (GetSystemInfo.Product.Contains("16") && GetSystemInfo.IsGPUPresent("RX 7700S"))
{
PL1 = 28;
PL2 = 28;
WinPower = 1;
if (PowerIndex == 0)
{
PL1 = 85;
PL2 = 85;
WinPower = 0;
}
else if (PowerIndex == 1)
{
PL1 = 95;
PL2 = 95;
WinPower = 1;
}
else if (PowerIndex == 2)
{
PL1 = 100;
PL2 = 120;
WinPower = 2;
}

PL1Min = 10;
PL2Min = 10;
PL1Max = 140;
PL2Max = 140;
}
else if (PowerIndex == 2)
else if (GetSystemInfo.Product.Contains("13"))
{
PL1 = 35;
PL2 = 60;
WinPower = 2;
if (PowerIndex == 0)
{
PL1 = 15;
PL2 = 18;
WinPower = 0;
}
else if (PowerIndex == 1)
{
PL1 = 28;
PL2 = 28;
WinPower = 1;
}
else if (PowerIndex == 2)
{
PL1 = 35;
PL2 = 60;
WinPower = 2;
}

PL1Min = 5;
PL2Min = 5;
PL1Max = 60;
PL2Max = 60;

SavePreset();
}

PL1Min = 5;
PL2Min = 5;
PL1Max = 60;
PL2Max = 60;
}

Settings _settings = new Settings()
Expand All @@ -297,5 +321,24 @@ private void SetUpTempPower()
};
appSettings.SaveSettings(_settings);
}

private void SavePreset()
{
PowerModePresets _powerMode = new PowerModePresets()
{
_temp = Temp,
_pl1Max = PL1Max,
_pl2Max = PL2Max,
_pl1Min = PL1Min,
_pl2Min = PL2Min,
_pl1 = PL1,
_pl2 = PL2,
_winPower = WinPower,
_allCO = AllCO,
_gfxCO = GfxCO,
_pboOffset = PboOffset,
};
powerModeSettings.SaveSettings(_powerMode, PowerIndex);
}
}
}

0 comments on commit 3ba14c9

Please sign in to comment.