forked from scientifichackers/thinkfan-control-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfan.py
More file actions
executable file
·223 lines (177 loc) · 7.13 KB
/
fan.py
File metadata and controls
executable file
·223 lines (177 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/python3
import tkinter as tk
import subprocess
from time import sleep, time
from threading import Thread, Lock
import traceback
import re
core_info = re.compile(r'Core \d+:\s+\+(\d+.\d+).C')
temperature_list : list[float] = []
avg = lambda x : sum(x)/len(x)
refresh_rate = 0.5
last_setting = None
def make_columns(x: list, total_width: int) -> list:
l = (len(x) + 1) // 2
new_list = []
for i in range(l):
item = x[i] + (total_width//2 - len(x[i]))*' ' + x[l + i]
item += (total_width - len(item))*' '
new_list.append(item)
return new_list
def get_info() -> list[str]:
info_lines = subprocess.check_output("sensors").decode("utf-8").split("\n")
result : list[str] = []
temp_list = []
count = 0
for i in info_lines:
core = core_info.search(i)
if core is not None:
result.append("Core %d: " % count + core.group(1))
temp_list.append(float(core.group(1)))
count += 1
if "fan" in i:
result.append("Fan: " + i.split(":")[-1].strip())
global temperature_list
temperature_list = temp_list
result = make_columns(result, 30)
result.append(f'Highest: +{max(temp_list)}°C')
result.append(f'Average: +{round(avg(temp_list), 1)}°C')
return result
def set_speed(speed : str) -> str:
"""
Set speed of fan by changing level at /proc/acpi/ibm/fan
speed: 0-7, auto, disengaged, full-speed
"""
global last_setting
last_setting = speed
print("set level to %r" % speed)
return subprocess.check_output(
'echo level {0} | sudo tee "/proc/acpi/ibm/fan"'.format(speed),
shell=True
).decode()
def change_speed(speed : float, inc : float, min_speed : int, max_speed : int, allow_full_speed : bool = False) -> int:
new_speed = speed + inc
if new_speed <= min_speed:
new_speed = min_speed
if new_speed >= max_speed:
if allow_full_speed:
new_speed = max_speed
else:
new_speed = max_speed - 1
return int(new_speed)
class MainApplication(tk.Frame):
speeds = {0 : '0', 1 : '1', 2 : '2', 3 : '3', 4 : '4', 5 : '5', 6 : '6', 7 : '7', 8 : 'full-speed'}
def __init__(self, parent, *args, **kwargs):
self.keep_running : bool = True
self.lock = Lock()
self.policy = None
self.allow_full_speed = False
self.current_speed = 5
self.last_time_above_max = time()
self.last_time_below_max = time()
self.last_error = 0
self.error_acc = 0
self.PIDSignal = 5
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.parent.minsize(width=100, height=100)
main_label = tk.Label(parent, text="")
main_label.grid(row=0, column=0)
row1 = tk.Frame()
row1.grid()
for i in range(8):
tk.Button(row1, text=str(i), command=lambda x=i: self.set_speed_button(str(x))).grid(
row=0, column=i + 1
)
row2 = tk.Frame()
row2.grid()
tk.Button(row2, text="Auto", command=lambda: self.set_speed_button("auto")).grid(
row=0, column=0
)
tk.Button(row2, text="Full", command=lambda: self.set_speed_button("full-speed")).grid(
row=0, column=1
)
tk.Button(row2, text="On/off full", command=lambda: self.on_off_full()).grid(
row=0, column=3
)
tk.Button(row2, text="Shitty PID", command=lambda: self.enable_sPID()).grid(
row=0, column=4
)
def display_loop():
while self.keep_running:
sleep(refresh_rate)
if not self.lock.acquire(blocking=False):
print('Could not acquire lock. Canceling main application')
break
if self.policy:
self.policy()
main_label["text"] = "\n".join(get_info()) + f'\nLast setting : {last_setting}\n Full : {"On" if self.allow_full_speed else "Off"}'
self.lock.release()
self.thread = Thread(target=display_loop)
self.thread.daemon = True
self.thread.start()
def stop(self) -> None:
self.keep_running = False
if self.lock.acquire(timeout=refresh_rate*1.1):
print('Thread successfully stopped!')
else:
print('Thread *forcefully* stopped!')
def _clear_state(self) -> None:
self.policy = None
self.last_error = 0
self.error_acc = 0
self.PIDSignal = 5
def set_speed_button(self, speed : str) -> str:
self._clear_state()
return set_speed(speed)
def on_off_full(self) -> None:
self.allow_full_speed = not self.allow_full_speed
def enable_sPID(self) -> None:
self._clear_state()
self.policy = self.shitty_PID
# deprecated
def custom_auto(self, max_temp : float = 60, min_speed : int = 2, max_speed : int = 8, allow_full_speed : bool = False, delay_upwards : float = 3, delay_downwards : float = 5) -> str:
if avg(temperature_list) >= max_temp:
self.last_time_above_max = time()
if time() - self.last_time_below_max > delay_upwards:
self.current_speed = change_speed(self.current_speed, +1, min_speed, max_speed)
self.last_time_below_max = time()
else:
self.last_time_below_max = time()
if time() - self.last_time_above_max > delay_downwards:
self.current_speed = change_speed(self.current_speed, -1, min_speed, max_speed)
self.last_time_above_max = time()
return set_speed(speed=MainApplication.speeds[self.current_speed])
def shitty_PID(self, target : float = 65, min_speed : int = 2, max_speed : int = 7) -> str:
kP = 0.8
kD = 0
kI = 0.05
error = avg(temperature_list) - target
error_deriv = (error - self.last_error)/refresh_rate
self.error_acc += (error + self.last_error)*refresh_rate/2
self.error_acc = min(max(0.0, self.error_acc), (max_speed / kI) * 1.25 )
self.PIDSignal = kP * error + kD * error_deriv + kI * self.error_acc
self.PIDSignal = max(self.PIDSignal, float(min_speed))
output_signal = change_speed(0, self.PIDSignal, min_speed, max_speed, self.allow_full_speed)
self.current_speed = output_signal
print(f'PIDSignal: {self.PIDSignal:.2f}, PK: {kP * error:.2f}, PI: {kI * self.error_acc:.2f}, error: {error:.2f}, error_deriv {error_deriv:.2f}, error_acc: {self.error_acc:.2f}')
return set_speed(speed=MainApplication.speeds[self.current_speed])
if __name__ == "__main__":
root = tk.Tk()
root.title("Thinkfan Control")
app = MainApplication(root)
app.grid()
def on_closing():
root.destroy()
app.stop()
print('Closing: Speed set to "auto"')
set_speed('auto')
try:
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
except Exception:
print('An error occurred and the application will forcefully exit!')
print(traceback.format_exc())
finally:
on_closing()
print(f'Exiting {__file__}')