Skip to content

Instantly share code, notes, and snippets.

@TurnOffNOD
Forked from amazingfate/control-pwm-fanspeed.py
Created January 24, 2023 16:23
Show Gist options
  • Save TurnOffNOD/2a14b8b7bf7d65e74c05b4a7e2043b42 to your computer and use it in GitHub Desktop.
Save TurnOffNOD/2a14b8b7bf7d65e74c05b4a7e2043b42 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from pathlib import Path
from threading import Timer
cpu_temp_file = "/sys/class/thermal/thermal_zone0/temp"
duty_cycle_file = "/sys/class/pwm/pwmchip0/pwm0/duty_cycle"
temp_wall = 65000
lowest_fan_speed = 3000
def read_cpu_temp():
read_temp_file = Path(cpu_temp_file).read_text().replace("\n", "")
return read_temp_file
def get_current_fan_speed():
current_fan_speed = Path(duty_cycle_file).read_text().replace("\n", "")
return current_fan_speed
def set_fan_speed(speed):
try:
with open(duty_cycle_file, "w") as speed_file:
speed_file.write(str(speed))
except Exception as e:
print(e)
return False
return True
def set_fan_according_to_temp():
current_cpu_temp = read_cpu_temp()
current_fan_speed = get_current_fan_speed()
if int(current_cpu_temp) < temp_wall:
print("cpu is lower than %s" % temp_wall)
if int(current_fan_speed) > 0:
print("going to lower fan speed")
if int(current_fan_speed) == lowest_fan_speed:
set_fan_res = set_fan_speed(0)
else:
set_fan_res = set_fan_speed(int(current_fan_speed) - 1000)
else:
print("cpu is higher than %s" % temp_wall)
if int(current_fan_speed) < 10000:
print("going to speed up fan")
if int(current_fan_speed) == 0:
set_fan_res = set_fan_speed(lowest_fan_speed)
else:
set_fan_res = set_fan_speed(int(current_fan_speed) + 1000)
class RepeatingTimer(Timer):
def run(self):
while not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.wait(self.interval)
t = RepeatingTimer(10.0, set_fan_according_to_temp)
t.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment