-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
162 lines (130 loc) · 4.42 KB
/
control.py
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
#!/usr/bin/env python3
from fanshim import FanShim
from threading import Lock
import colorsys
import psutil
import argparse
import time
import signal
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--threshold', type=float, default=-1, help='Temperature threshold in degrees C to enable fan')
parser.add_argument('--hysteresis', type=float, default=-1, help='Distance from threshold before fan is disabled')
parser.add_argument('--off-threshold', type=float, default=55.0, help='Temperature threshold in degrees C to enable fan')
parser.add_argument('--on-threshold', type=float, default=65.0, help='Temperature threshold in degrees C to disable fan')
parser.add_argument('--delay', type=float, default=2.0, help='Delay, in seconds, between temperature readings')
parser.add_argument('--preempt', action='store_true', default=False, help='Monitor CPU frequency and activate cooling premptively')
parser.add_argument('--verbose', action='store_true', default=False, help='Output temp and fan status messages')
parser.add_argument('--nobutton', action='store_true', default=False, help='Disable button input')
parser.add_argument('--noled', action='store_true', default=False, help='Disable LED control')
parser.add_argument('--brightness', type=float, default=255.0, help='LED brightness, from 0 to 255')
args = parser.parse_args()
print("""
Starting fan control with configuration
On threshold: {}
Off threshold {}""".format(args.on_threshold, args.off_threshold))
def clean_exit(signum, frame):
set_fan(False)
if not args.noled:
fanshim.set_light(0, 0, 0)
sys.exit(0)
def update_led_temperature(temp):
led_busy.acquire()
temp = float(temp)
temp -= args.off_threshold
temp /= float(args.on_threshold - args.off_threshold)
temp = max(0, min(1, temp))
temp = 1.0 - temp
temp *= 120.0
temp /= 360.0
r, g, b = [int(c * 255.0) for c in colorsys.hsv_to_rgb(temp, 1.0, args.brightness / 255.0)]
fanshim.set_light(r, g, b)
led_busy.release()
def get_cpu_temp():
t = psutil.sensors_temperatures()
for x in ['cpu-thermal', 'cpu_thermal']:
if x in t:
return t[x][0].current
print("Warning: Unable to get CPU temperature!")
return 0
def get_cpu_freq():
freq = psutil.cpu_freq()
return freq
def set_fan(status):
global enabled
changed = False
if status != enabled:
changed = True
fanshim.set_fan(status)
enabled = status
return changed
def set_automatic(status):
global armed, last_change
armed = status
last_change = 0
if args.threshold > -1 or args.hysteresis > -1:
print("""
The --threshold and --hysteresis parameters have been deprecated.
Use --on-threshold and --off-threshold instead!
""")
sys.exit(1)
fanshim = FanShim()
fanshim.set_hold_time(1.0)
fanshim.set_fan(False)
armed = True
enabled = False
led_busy = Lock()
enable = False
is_fast = False
last_change = 0
signal.signal(signal.SIGTERM, clean_exit)
if args.noled:
led_busy.acquire()
fanshim.set_light(0, 0, 0)
led_busy.release()
t = get_cpu_temp()
if t >= args.threshold:
last_change = get_cpu_temp()
set_fan(True)
if not args.nobutton:
@fanshim.on_release()
def release_handler(was_held):
global armed
if was_held:
set_automatic(not armed)
elif not armed:
set_fan(not enabled)
@fanshim.on_hold()
def held_handler():
global led_busy
if args.noled:
return
led_busy.acquire()
for _ in range(3):
fanshim.set_light(0, 0, 255)
time.sleep(0.04)
fanshim.set_light(0, 0, 0)
time.sleep(0.04)
led_busy.release()
try:
while True:
t = get_cpu_temp()
f = get_cpu_freq()
was_fast = is_fast
is_fast = (int(f.current) == int(f.max))
if args.verbose:
print("Current: {:05.02f} Target: {:05.02f} Freq {: 5.02f} Automatic: {} On: {}".format(t, args.off_threshold, f.current / 1000.0, armed, enabled))
if args.preempt and is_fast and was_fast:
enable = True
elif armed:
if t >= args.on_threshold:
enable = True
elif t <= args.off_threshold:
enable = False
if set_fan(enable):
last_change = t
if not args.noled:
update_led_temperature(t)
time.sleep(args.delay)
except KeyboardInterrupt:
pass