Skip to content

Commit c245514

Browse files
committed
Headers, more settings, better thermal calculations
1 parent 3989feb commit c245514

17 files changed

+115
-24
lines changed

CodeMaid.config

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="SteveCadwallader.CodeMaid.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
6+
</sectionGroup>
7+
</configSections>
8+
<userSettings>
9+
<SteveCadwallader.CodeMaid.Properties.Settings>
10+
<setting name="Cleaning_UpdateFileHeaderCPlusPlus" serializeAs="String">
11+
<value>// FanControl++
12+
// (c) 2024 Pham Nhat Quang (Legend0fHell)</value>
13+
</setting>
14+
</SteveCadwallader.CodeMaid.Properties.Settings>
15+
</userSettings>
16+
</configuration>

FanControl++.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// FanControl++.cpp : Defines the entry point for the application.
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
3+
// FanControl++.cpp : Defines the entry point for the application.
24
//
35

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

180182
int temp = asus_control.get_thermal();
183+
update_average_temperature(temp);
184+
181185
if (toggle_change_fan_speed) {
182-
int perc = calc_fan_percent(temp, current_mode);
186+
int perc = calc_fan_percent(current_mode);
183187
if (perc < 0) _dErr(_ts(L"[Thread] Invalid fan percent!"));
184188
else asus_control.set_fan_speed(perc);
185189
}
@@ -190,6 +194,12 @@ static BOOL MainThread(HWND hWnd) noexcept(false) {
190194

191195
if (thread_term) break;
192196

197+
if (current_settings_hwnd) {
198+
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")
199+
+ _ts(L"Fan: ") + _ts(asus_control.get_fan_speed()) + _ts(L"RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
200+
SetWindowTextW(GetDlgItem(current_settings_hwnd, IDC_STATIC_INFO), tool_tip.c_str());
201+
}
202+
193203
SYSTEMTIME st;
194204
GetSystemTime(&st);
195205
ULONGLONG time_taken = convert_to_ull(st) - last_update_thread;
@@ -208,10 +218,10 @@ static void ShowAboutMessage(HWND hWnd) {
208218
LPCWSTR str_title = L"About";
209219
WCHAR str_content[512];
210220
std::wstring tmp_str;
211-
std::wstring version = L"1.0";
221+
std::wstring version = CURRENT_VERSION;
212222
#ifdef _DEBUG
213223
version += L" (Debug), ";
214-
#else
224+
#else
215225
version += L" (Release), ";
216226
#endif
217227
version += (sizeof(void*) == 8 ? L"x64" : L"x86");
@@ -327,13 +337,15 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
327337
ShowAboutMessage(hWnd);
328338
break;
329339
case ID_POPUP_SETTINGS:
330-
if(current_settings_hwnd == 0) DialogBox(hInst, MAKEINTRESOURCE(IDD_SETTINGSBOX), hWnd, Settings);
340+
if (current_settings_hwnd == 0) DialogBox(hInst, MAKEINTRESOURCE(IDD_SETTINGSBOX), hWnd, Settings);
331341
else {
332342
SetForegroundWindow(current_settings_hwnd); // focus on the window
333343
}
334344
break;
335345
case ID_POPUP_ENABLE: // Toggle Enable
336346
toggle_change_fan_speed = !toggle_change_fan_speed;
347+
settings.sections[L"General"][L"Enable"] = _ts(toggle_change_fan_speed);
348+
write_settings(settings);
337349
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
338350
break;
339351
case ID_POPUP_STARTUP: // Toggle Startup
@@ -370,7 +382,6 @@ HRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam, LO
370382
// Message handler for settings box.
371383
INT_PTR CALLBACK Settings(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
372384
{
373-
374385
UNREFERENCED_PARAMETER(lParam);
375386
switch (message)
376387
{
@@ -435,20 +446,23 @@ INT_PTR CALLBACK Settings(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
435446
settings.sections[L"Curves"][L"FanBalanced"] = _ts(tmp_balanced);
436447
settings.sections[L"Curves"][L"FanTurbo"] = _ts(tmp_turbo);
437448

438-
// update enable
439-
toggle_change_fan_speed = IsDlgButtonChecked(hDlg, IDC_CHECK_ENABLE);
440-
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
441-
442-
// update startup
449+
bool tmp_enable = IsDlgButtonChecked(hDlg, IDC_CHECK_ENABLE);
443450
bool tmp_startup = IsDlgButtonChecked(hDlg, IDC_CHECK_STARTUP);
451+
452+
// close dialog
453+
EndDialog(hDlg, LOWORD(wParam));
454+
444455
if (tmp_startup != startup) {
445456
startup = tmp_startup;
446457
if (!ChangeStartupBehavior(startup)) startup = !startup; // revert if failed
447458
settings.sections[L"General"][L"Startup"] = _ts(startup);
448459
}
449460

450-
// close dialog
451-
EndDialog(hDlg, LOWORD(wParam));
461+
if (tmp_enable != toggle_change_fan_speed) {
462+
toggle_change_fan_speed = tmp_enable;
463+
asus_control.set_fan_test_mode(toggle_change_fan_speed ? 0x01 : 0x00);
464+
settings.sections[L"General"][L"Enable"] = _ts(toggle_change_fan_speed);
465+
}
452466

453467
// write settings
454468
write_settings(settings);
@@ -504,13 +518,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
504518
read_settings(settings);
505519
inipp::extract(settings.sections[L"General"][L"UpdateInterval"], update_interval);
506520
inipp::extract(settings.sections[L"General"][L"CurrentMode"], current_mode);
507-
int tmp_startup = 0;
508-
inipp::extract(settings.sections[L"General"][L"Startup"], tmp_startup);
509-
startup = tmp_startup;
521+
int tmp_re = 0;
522+
inipp::extract(settings.sections[L"General"][L"Startup"], tmp_re);
523+
startup = tmp_re;
510524
ChangeStartupBehavior(startup);
511525

512-
bool toggle_change_fan_speed = TRUE;
513-
asus_control.set_fan_test_mode(0x01);
526+
inipp::extract(settings.sections[L"General"][L"Enable"], tmp_re);
527+
toggle_change_fan_speed = tmp_re;
528+
asus_control.set_fan_test_mode(tmp_re ? 0x01 : 0x00);
514529

515530
// Display tray icon
516531
if (!InitTray(hInst, hWnd, nid)) {

FanControl++.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24

35
#include "resource.h"
@@ -9,4 +11,6 @@
911
#include "temp_handle.h"
1012
#include "tray.h"
1113

12-
#include "settings.h"
14+
#include "settings.h"
15+
16+
const std::wstring CURRENT_VERSION = L"1.1.0";

FanControl++.rc

1.63 KB
Binary file not shown.

Resource.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#define IDC_CHECK_STARTUP 1006
2222
#define IDC_BUTTON_CANCEL 1007
2323
#define IDC_BUTTON_OK 1008
24+
#define IDC_TEXT_INFO 1013
25+
#define IDC_STATIC_INFO 1013
2426
#define ID_POPUP_ECO 32772
2527
#define ID_POPUP_BALANCED 32773
2628
#define ID_POPUP_TURBO 32774
@@ -39,7 +41,7 @@
3941
#define _APS_NO_MFC 1
4042
#define _APS_NEXT_RESOURCE_VALUE 129
4143
#define _APS_NEXT_COMMAND_VALUE 32787
42-
#define _APS_NEXT_CONTROL_VALUE 1013
44+
#define _APS_NEXT_CONTROL_VALUE 1014
4345
#define _APS_NEXT_SYMED_VALUE 110
4446
#endif
4547
#endif

controller.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#include "controller.h"
24

35
bool AsusDLL::failed(const std::wstring reason = _ts(L"Failed"), bool shown = true) {

controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24

35
#include "utils.h"

framework.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
// header.h : include file for standard system include files,
24
// or project specific include files
35
//

settings.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#include "settings.h"
24

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

3942
settings.sections[L"Curves"][L"FanEco"] = _ts(L"55c:0%;60c:30%;70c:50%;77c:70%;100c:80%");
4043
settings.sections[L"Curves"][L"FanBalanced"] = _ts(L"45c:0%;60c:35%;65c:40%;70c:50%;75c:55%;80c:70%;85c:80%;90c:90%");

settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24
#include "utils.h"
35
#include <fstream>

targetver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24

35
// // Including SDKDDKVer.h defines the highest available Windows platform.

temp_handle.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#include "temp_handle.h"
24

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

72-
ULONG calc_fan_percent(ULONG temperature, int mode)
74+
std::queue<ULONG> temp_history;
75+
std::queue<ULONGLONG> temp_update_history;
76+
ULONG total_temp = 0;
77+
78+
void update_average_temperature(ULONG temperature) {
79+
SYSTEMTIME st;
80+
GetSystemTime(&st);
81+
82+
ULONGLONG current_time = convert_to_ull(st);
83+
84+
// only keep temperature in the last 8s
85+
while (!temp_history.empty() && current_time - temp_update_history.front() > 8000) {
86+
total_temp -= temp_history.front();
87+
temp_history.pop();
88+
temp_update_history.pop();
89+
}
90+
total_temp += temperature;
91+
temp_history.push(temperature);
92+
temp_update_history.push(current_time);
93+
}
94+
95+
float get_average_temperature() {
96+
if (temp_history.empty()) return 0;
97+
return (1.0f * total_temp) / temp_history.size();
98+
}
99+
100+
ULONG calc_fan_percent(int mode)
73101
{
102+
float temperature = get_average_temperature();
74103
inipp::Ini<wchar_t> settings;
75104
read_settings(settings);
76105
std::wstring fan_str;
@@ -113,5 +142,5 @@ ULONG calc_fan_percent(ULONG temperature, int mode)
113142
ULONG percent_gap = fan_curve[rgt_idx].second - fan_curve[lft_idx].second;
114143
ULONG temp_gap = fan_curve[rgt_idx].first - fan_curve[lft_idx].first;
115144
if (temp_gap == 0UL) return fan_curve[lft_idx].second;
116-
return fan_curve[lft_idx].second + percent_gap * (temperature - fan_curve[lft_idx].first) / temp_gap;
145+
return fan_curve[lft_idx].second + ((temperature - 1.0f * fan_curve[lft_idx].first) * percent_gap) / (1.0f * temp_gap);
117146
}

temp_handle.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24

35
#include "utils.h"
46
#include "settings.h"
7+
#include <queue>
58

6-
ULONG calc_fan_percent(ULONG temperature, int mode);
9+
ULONG calc_fan_percent(int mode);
10+
void update_average_temperature(ULONG temperature);
711
bool validator(std::wstring& str);
812
bool validator(wchar_t* str);

tray.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#include "tray.h"
24

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

261263
std::wstring tool_tip = _ts(L"Currently ") + (asus_control.current_fan_test_mode ? _ts(L"enabled!") : _ts(L"disabled!"));
262264
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")
263-
+ _ts(L"Speed: ") + _ts(asus_control.get_fan_speed()) + _ts(L"RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
265+
+ _ts(L"Fan: ") + _ts(asus_control.get_fan_speed()) + _ts(L"RPM (") + _ts(asus_control.current_fan_percent) + _ts(L"%)");
264266
ZeroMemory(&nid.szTip, sizeof(nid.szTip)); // clear the string
265267
wmemcpy_s(nid.szTip, 128, tool_tip.c_str(), tool_tip.length());
266268

tray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24

35
#include "controller.h"

utils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#include "utils.h"
24

35
#ifdef _DEBUG

utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// FanControl++
2+
// (c) 2024 Pham Nhat Quang (Legend0fHell)
13
#pragma once
24
#include <Windows.h>
35
#include <string>

0 commit comments

Comments
 (0)