Created
August 6, 2022 15:43
-
-
Save amazingfate/38b12841d8a79e06771a7ba8013372ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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