Skip to content

Commit

Permalink
Added CPU / GPU temps monitoring and Charge/discharge rates in Watts
Browse files Browse the repository at this point in the history
  • Loading branch information
seerge committed Feb 21, 2023
1 parent 81a0019 commit f209f21
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 79 deletions.
9 changes: 6 additions & 3 deletions GHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net7.0-windows8.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>True</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<IsPublishable>True</IsPublishable>
<ApplicationManifest>app.manifest</ApplicationManifest>
<StartupObject>GHelper.Program</StartupObject>
<ApplicationIcon>Resources\standard.ico</ApplicationIcon>
<ApplicationIcon>favicon.ico</ApplicationIcon>
<Platforms>AnyCPU;x64</Platforms>
<SupportedOSPlatformVersion>8.0</SupportedOSPlatformVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -24,13 +26,14 @@


<ItemGroup>
<Content Include="Resources\standard.ico">
<Content Include="favicon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="hidlibrary" Version="3.3.40" />
<PackageReference Include="LibreHardwareMonitorLib" Version="0.9.1" />
<PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>
Expand Down
8 changes: 7 additions & 1 deletion GHelper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GHelper", "GHelper.csproj", "{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GHelper", "GHelper.csproj", "{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B6E44CC6-5D28-4CB9-8EE2-BE9D6238E2D6}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -13,13 +13,19 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Debug|x64.ActiveCfg = Release|x64
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Debug|x64.Build.0 = Release|x64
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Release|Any CPU.Build.0 = Release|Any CPU
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Release|x64.ActiveCfg = Release|x64
{D6138BB1-8FDB-4835-87EF-2FE41A3DD604}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
142 changes: 136 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Runtime.InteropServices;
using HidLibrary;
using System.Text.Json;
using LibreHardwareMonitor.Hardware;
using System.Threading;

public class ASUSWmi
{
Expand Down Expand Up @@ -224,6 +226,13 @@ static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
int AcValueIndex);

[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerWriteACValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
int AcValueIndex);

[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerReadACValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
Expand All @@ -232,14 +241,15 @@ static extern UInt32 PowerReadACValueIndex(IntPtr RootPowerKey,
out IntPtr AcValueIndex
);



[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerWriteACValueIndex(IntPtr RootPowerKey,
static extern UInt32 PowerReadDCValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
int AcValueIndex);
out IntPtr AcValueIndex
);



[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerSetActiveScheme(IntPtr RootPowerKey,
Expand Down Expand Up @@ -410,14 +420,25 @@ public static void SetCPUBoost(int boost = 0)
{
Guid activeSchemeGuid = GetActiveScheme();

var hr = PowerWriteACValueIndex(
var hrAC = PowerWriteACValueIndex(
IntPtr.Zero,
activeSchemeGuid,
GUID_CPU,
GUID_BOOST,
boost);

PowerSetActiveScheme(IntPtr.Zero, activeSchemeGuid);

var hrDC = PowerWriteDCValueIndex(
IntPtr.Zero,
activeSchemeGuid,
GUID_CPU,
GUID_BOOST,
boost);

PowerSetActiveScheme(IntPtr.Zero, activeSchemeGuid);


}
}

Expand All @@ -432,6 +453,7 @@ public class Aura
public const int Breathe = 1;
public const int Strobe = 2;
public const int Rainbow = 3;
public const int Dingding = 10;

public const int SpeedSlow = 0;
public const int SpeedMedium = 1;
Expand Down Expand Up @@ -489,6 +511,106 @@ public static void ApplyAura()
}


public class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
}


public class HardwareMonitor
{

Computer computer;

public float? cpuTemp = -1;
public float? gpuTemp = -1;
public float? batteryDischarge = -1;
public float? batteryCharge = -1;

public HardwareMonitor()
{
computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsBatteryEnabled = true,
};

}

public void ReadSensors()
{

computer.Open();
computer.Accept(new UpdateVisitor());

cpuTemp = -1;
gpuTemp = -1;
batteryDischarge = -1;
batteryCharge = -1;

foreach (IHardware hardware in computer.Hardware)
{
//Debug.WriteLine("Hardware: {0}", hardware.Name);
//Debug.WriteLine("Hardware: {0}", hardware.HardwareType);

foreach (ISensor sensor in hardware.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
if (hardware.HardwareType.ToString().Contains("Cpu") && sensor.Name.Contains("Core"))
{
cpuTemp = sensor.Value;
//Debug.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);
}

if (hardware.HardwareType.ToString().Contains("Gpu") && sensor.Name.Contains("Core"))
{
gpuTemp = sensor.Value;
}

//Debug.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);

}
else if (sensor.SensorType == SensorType.Power)
{
if (sensor.Name.Contains("Discharge"))
{
batteryDischarge = sensor.Value;
}

if (sensor.Name.Contains("Charge"))
{
batteryCharge = sensor.Value;
}
}



}
}

}

public void StopReading()
{
computer.Close();
}

}



namespace GHelper
{
static class Program
Expand All @@ -499,7 +621,10 @@ static class Program
public static AppConfig config;

public static SettingsForm settingsForm;
public static ToastForm toastForm;

public static Startup scheduler;
public static HardwareMonitor hwmonitor;

// The main entry point for the application
public static void Main()
Expand Down Expand Up @@ -534,10 +659,13 @@ public static void Main()

settingsForm.SetStartupCheck(scheduler.IsScheduled());

bool isPlugged = (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online);
bool isPlugged = (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online);
settingsForm.AutoGPUMode(isPlugged ? 1 : 0);
settingsForm.AutoScreen(isPlugged ? 1 : 0);

hwmonitor = new HardwareMonitor();
IntPtr dummy = settingsForm.Handle;

Application.Run();

}
Expand All @@ -557,10 +685,12 @@ static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{
case 56: // Rog button
case 174: // FN+F5

settingsForm.BeginInvoke(delegate
{
settingsForm.CyclePerformanceMode();
});

return;
case 179: // FN+F4
settingsForm.BeginInvoke(delegate
Expand Down
10 changes: 10 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,7 @@
<data name="icons8-laptop-48" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-laptop-48.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons8-speed-96" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-speed-96.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added Resources/icons8-speed-96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f209f21

Please sign in to comment.