-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoctua-fan-control.py.save
81 lines (64 loc) · 1.99 KB
/
noctua-fan-control.py.save
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
import pigpio
import time
import signal
import sys
import os
# Configuration
FAN_PIN = 18 # BCM pin used to drive PWM fan
WAIT_TIME = 1 # [s] Time to wait between each refresh
PWM_FREQ = 25000 # [Hz] 25kHz for Noctua PWM control
# Configurable temperature and fan speed
MIN_TEMP = 40
MAX_TEMP = 70
FAN_LOW = 40
FAN_HIGH = 100
FAN_OFF = 0
FAN_MAX = 100
kill_now = False
# Get CPU's temperature
def getCpuTemperature():
res = os.popen('vcgencmd measure_temp').readline()
temp =(res.replace("temp=","").replace("'C\n",""))
#print("temp is {0}".format(temp)) # Uncomment for testing
return temp
# Set fan speed
def setFanSpeed(speed):
dc= int(speed*10000)
PI.hardware_PWM(FAN_PIN,PWM_FREQ,dc)
return()
# Handle fan speed
def handleFanSpeed():
temp = float(getCpuTemperature())
# Turn off the fan if temperature is below MIN_TEMP
if temp < MIN_TEMP:
setFanSpeed(FAN_OFF)
print("Fan OFF") # Uncomment for testing
# Set fan speed to MAXIMUM if the temperature is above MAX_TEMP
elif temp > MAX_TEMP:
setFanSpeed(FAN_MAX)
print("Fan MAX") # Uncomment for testing
# Caculate dynamic fan speed
else:
step = (FAN_HIGH - FAN_LOW)/(MAX_TEMP - MIN_TEMP)
temp -= MIN_TEMP
print(FAN_LOW + ( round(temp) * step )) # Uncomment for testing
setFanSpeed(FAN_LOW + ( round(temp) * step ))
return ()
def exit_gracefully(signum,frame):
print("sigterm captured")
setFanSpeed(FAN_HIGH)
kill_now = True
try:
# Setup GPIO pin
PI = pigpio.pi()
setFanSpeed(FAN_OFF)
signal.signal(signal.SIGTERM,exit_gracefully)
# Handle fan speed every WAIT_TIME sec
print("Noctua fan controller started")
while not kill_now:
handleFanSpeed()
time.sleep(WAIT_TIME)
print("noctua-fan-controller exited gracefully")
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
setFanSpeed(FAN_HIGH)
# resets all GPIO ports used by this function