Skip to content

Commit

Permalink
Headers, more settings, better thermal calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend0fHell committed Feb 8, 2024
1 parent 3989feb commit c245514
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 24 deletions.
16 changes: 16 additions & 0 deletions CodeMaid.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SteveCadwallader.CodeMaid.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<userSettings>
<SteveCadwallader.CodeMaid.Properties.Settings>
<setting name="Cleaning_UpdateFileHeaderCPlusPlus" serializeAs="String">
<value>// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)</value>
</setting>
</SteveCadwallader.CodeMaid.Properties.Settings>
</userSettings>
</configuration>
51 changes: 33 additions & 18 deletions FanControl++.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// FanControl++.cpp : Defines the entry point for the application.
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
// FanControl++.cpp : Defines the entry point for the application.
//

#include "framework.h"
Expand Down Expand Up @@ -178,8 +180,10 @@ static BOOL MainThread(HWND hWnd) noexcept(false) {
}

int temp = asus_control.get_thermal();
update_average_temperature(temp);

if (toggle_change_fan_speed) {
int perc = calc_fan_percent(temp, current_mode);
int perc = calc_fan_percent(current_mode);
if (perc < 0) _dErr(_ts(L"[Thread] Invalid fan percent!"));
else asus_control.set_fan_speed(perc);
}
Expand All @@ -190,6 +194,12 @@ static BOOL MainThread(HWND hWnd) noexcept(false) {

if (thread_term) break;

if (current_settings_hwnd) {
std::wstring tool_tip = _ts(L"CPU: ") + _ts(asus_control.current_cpu_thermal) + _ts(L"°C | GPU: ") + _ts(asus_control.current_gpu_thermal) + _ts(L"°C\n")
+ _ts(L"Fan: ") + _ts(asus_control.get_fan_speed()) + _ts(L"RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
SetWindowTextW(GetDlgItem(current_settings_hwnd, IDC_STATIC_INFO), tool_tip.c_str());
}

SYSTEMTIME st;
GetSystemTime(&st);
ULONGLONG time_taken = convert_to_ull(st) - last_update_thread;
Expand All @@ -208,10 +218,10 @@ static void ShowAboutMessage(HWND hWnd) {
LPCWSTR str_title = L"About";
WCHAR str_content[512];
std::wstring tmp_str;
std::wstring version = L"1.0";
std::wstring version = CURRENT_VERSION;
#ifdef _DEBUG
version += L" (Debug), ";
#else
#else
version += L" (Release), ";
#endif
version += (sizeof(void*) == 8 ? L"x64" : L"x86");
Expand Down Expand Up @@ -327,13 +337,15 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
ShowAboutMessage(hWnd);
break;
case ID_POPUP_SETTINGS:
if(current_settings_hwnd == 0) DialogBox(hInst, MAKEINTRESOURCE(IDD_SETTINGSBOX), hWnd, Settings);
if (current_settings_hwnd == 0) DialogBox(hInst, MAKEINTRESOURCE(IDD_SETTINGSBOX), hWnd, Settings);
else {
SetForegroundWindow(current_settings_hwnd); // focus on the window
}
break;
case ID_POPUP_ENABLE: // Toggle Enable
toggle_change_fan_speed = !toggle_change_fan_speed;
settings.sections[L"General"][L"Enable"] = _ts(toggle_change_fan_speed);
write_settings(settings);
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
break;
case ID_POPUP_STARTUP: // Toggle Startup
Expand Down Expand Up @@ -370,7 +382,6 @@ HRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam, LO
// Message handler for settings box.
INT_PTR CALLBACK Settings(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

UNREFERENCED_PARAMETER(lParam);
switch (message)
{
Expand Down Expand Up @@ -435,20 +446,23 @@ INT_PTR CALLBACK Settings(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
settings.sections[L"Curves"][L"FanBalanced"] = _ts(tmp_balanced);
settings.sections[L"Curves"][L"FanTurbo"] = _ts(tmp_turbo);

// update enable
toggle_change_fan_speed = IsDlgButtonChecked(hDlg, IDC_CHECK_ENABLE);
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);

// update startup
bool tmp_enable = IsDlgButtonChecked(hDlg, IDC_CHECK_ENABLE);
bool tmp_startup = IsDlgButtonChecked(hDlg, IDC_CHECK_STARTUP);

// close dialog
EndDialog(hDlg, LOWORD(wParam));

if (tmp_startup != startup) {
startup = tmp_startup;
if (!ChangeStartupBehavior(startup)) startup = !startup; // revert if failed
settings.sections[L"General"][L"Startup"] = _ts(startup);
}

// close dialog
EndDialog(hDlg, LOWORD(wParam));
if (tmp_enable != toggle_change_fan_speed) {
toggle_change_fan_speed = tmp_enable;
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
settings.sections[L"General"][L"Enable"] = _ts(toggle_change_fan_speed);
}

// write settings
write_settings(settings);
Expand Down Expand Up @@ -504,13 +518,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
read_settings(settings);
inipp::extract(settings.sections[L"General"][L"UpdateInterval"], update_interval);
inipp::extract(settings.sections[L"General"][L"CurrentMode"], current_mode);
int tmp_startup = 0;
inipp::extract(settings.sections[L"General"][L"Startup"], tmp_startup);
startup = tmp_startup;
int tmp_re = 0;
inipp::extract(settings.sections[L"General"][L"Startup"], tmp_re);
startup = tmp_re;
ChangeStartupBehavior(startup);

bool toggle_change_fan_speed = TRUE;
asus_control.set_fan_test_mode(0x01);
inipp::extract(settings.sections[L"General"][L"Enable"], tmp_re);
toggle_change_fan_speed = tmp_re;
asus_control.set_fan_test_mode(tmp_re ? 0x01 : 0x00);

// Display tray icon
if (!InitTray(hInst, hWnd, nid)) {
Expand Down
6 changes: 5 additions & 1 deletion FanControl++.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once

#include "resource.h"
Expand All @@ -9,4 +11,6 @@
#include "temp_handle.h"
#include "tray.h"

#include "settings.h"
#include "settings.h"

const std::wstring CURRENT_VERSION = L"1.1.0";
Binary file modified FanControl++.rc
Binary file not shown.
4 changes: 3 additions & 1 deletion Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define IDC_CHECK_STARTUP 1006
#define IDC_BUTTON_CANCEL 1007
#define IDC_BUTTON_OK 1008
#define IDC_TEXT_INFO 1013
#define IDC_STATIC_INFO 1013
#define ID_POPUP_ECO 32772
#define ID_POPUP_BALANCED 32773
#define ID_POPUP_TURBO 32774
Expand All @@ -39,7 +41,7 @@
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32787
#define _APS_NEXT_CONTROL_VALUE 1013
#define _APS_NEXT_CONTROL_VALUE 1014
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
2 changes: 2 additions & 0 deletions controller.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#include "controller.h"

bool AsusDLL::failed(const std::wstring reason = _ts(L"Failed"), bool shown = true) {
Expand Down
2 changes: 2 additions & 0 deletions controller.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once

#include "utils.h"
Expand Down
2 changes: 2 additions & 0 deletions framework.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
// header.h : include file for standard system include files,
// or project specific include files
//
Expand Down
3 changes: 3 additions & 0 deletions settings.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#include "settings.h"

inipp::Ini<wchar_t> current_settings_data;
Expand Down Expand Up @@ -35,6 +37,7 @@ BOOL create_default_settings() {
settings.sections[L"General"][L"TrayIconFontSize"] = _ts(L"48");
settings.sections[L"General"][L"CurrentMode"] = _ts(ID_POPUP_BALANCED);
settings.sections[L"General"][L"Startup"] = _ts(0);
settings.sections[L"General"][L"Enable"] = _ts(1);

settings.sections[L"Curves"][L"FanEco"] = _ts(L"55c:0%;60c:30%;70c:50%;77c:70%;100c:80%");
settings.sections[L"Curves"][L"FanBalanced"] = _ts(L"45c:0%;60c:35%;65c:40%;70c:50%;75c:55%;80c:70%;85c:80%;90c:90%");
Expand Down
2 changes: 2 additions & 0 deletions settings.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once
#include "utils.h"
#include <fstream>
Expand Down
2 changes: 2 additions & 0 deletions targetver.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once

// // Including SDKDDKVer.h defines the highest available Windows platform.
Expand Down
33 changes: 31 additions & 2 deletions temp_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#include "temp_handle.h"

std::vector<std::pair<ULONG, ULONG>> convert_fan_curve(std::wstring& str)
Expand Down Expand Up @@ -69,8 +71,35 @@ bool validator(wchar_t* str)
return validator(tmp_str);
}

ULONG calc_fan_percent(ULONG temperature, int mode)
std::queue<ULONG> temp_history;
std::queue<ULONGLONG> temp_update_history;
ULONG total_temp = 0;

void update_average_temperature(ULONG temperature) {
SYSTEMTIME st;
GetSystemTime(&st);

ULONGLONG current_time = convert_to_ull(st);

// only keep temperature in the last 8s
while (!temp_history.empty() && current_time - temp_update_history.front() > 8000) {
total_temp -= temp_history.front();
temp_history.pop();
temp_update_history.pop();
}
total_temp += temperature;
temp_history.push(temperature);
temp_update_history.push(current_time);
}

float get_average_temperature() {
if (temp_history.empty()) return 0;
return (1.0f * total_temp) / temp_history.size();
}

ULONG calc_fan_percent(int mode)
{
float temperature = get_average_temperature();
inipp::Ini<wchar_t> settings;
read_settings(settings);
std::wstring fan_str;
Expand Down Expand Up @@ -113,5 +142,5 @@ ULONG calc_fan_percent(ULONG temperature, int mode)
ULONG percent_gap = fan_curve[rgt_idx].second - fan_curve[lft_idx].second;
ULONG temp_gap = fan_curve[rgt_idx].first - fan_curve[lft_idx].first;
if (temp_gap == 0UL) return fan_curve[lft_idx].second;
return fan_curve[lft_idx].second + percent_gap * (temperature - fan_curve[lft_idx].first) / temp_gap;
return fan_curve[lft_idx].second + ((temperature - 1.0f * fan_curve[lft_idx].first) * percent_gap) / (1.0f * temp_gap);
}
6 changes: 5 additions & 1 deletion temp_handle.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once

#include "utils.h"
#include "settings.h"
#include <queue>

ULONG calc_fan_percent(ULONG temperature, int mode);
ULONG calc_fan_percent(int mode);
void update_average_temperature(ULONG temperature);
bool validator(std::wstring& str);
bool validator(wchar_t* str);
4 changes: 3 additions & 1 deletion tray.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#include "tray.h"

HDC _hdc, _hdc_mask;
Expand Down Expand Up @@ -260,7 +262,7 @@ BOOL UpdateTray(HWND hWnd, NOTIFYICONDATAW & nid, AsusDLL & asus_control, int& c

std::wstring tool_tip = _ts(L"Currently ") + (asus_control.current_fan_test_mode ? _ts(L"enabled!") : _ts(L"disabled!"));
tool_tip += _ts(L"\nCPU: ") + _ts(asus_control.current_cpu_thermal) + _ts(L"°C | GPU: ") + _ts(asus_control.current_gpu_thermal) + _ts(L"°C\n")
+ _ts(L"Speed: ") + _ts(asus_control.get_fan_speed()) + _ts(L"RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
+ _ts(L"Fan: ") + _ts(asus_control.get_fan_speed()) + _ts(L"RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
ZeroMemory(&nid.szTip, sizeof(nid.szTip)); // clear the string
wmemcpy_s(nid.szTip, 128, tool_tip.c_str(), tool_tip.length());

Expand Down
2 changes: 2 additions & 0 deletions tray.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once

#include "controller.h"
Expand Down
2 changes: 2 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#include "utils.h"

#ifdef _DEBUG
Expand Down
2 changes: 2 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FanControl++
// (c) 2024 Pham Nhat Quang (Legend0fHell)
#pragma once
#include <Windows.h>
#include <string>
Expand Down

0 comments on commit c245514

Please sign in to comment.