import time temp_file = '/sys/bus/iio/devices/iio:device0/in_temp160_temp_input' fanpwm_file = '/sys/class/hwmon/hwmon0/pwm1' temp_min = 35.0 temp_max = 75.0 temp_hist_up = 1.0 temp_hist_down = 5.0 temp_prev = 0.0 print("FAN Control start ...\n") # equation of a straight line: a*x + b*y + c = 0 # A = [temp_min; pwm_min], B = [temp_max, pwm_max] # A = [temp_min; 0], B = [temp_max, 255] # u = B - A = (temp_max - temp_min; pwm_max - pwm_min) # u = (temp_max - temp_min; 255 - 0) # n = (255, -(temp_max - temp_min)) # p: 255*x -(temp_max - temp_min)*y + c = 0 # c: A is on the p # c = -255*temp_min - (temp_max - temp_min)*0 c = -255 * temp_min f_temp = open(temp_file, 'r') while True: f_temp = open(temp_file, 'r') temp = float(f_temp.readline()) f_temp.close() if ((temp - temp_prev) >= temp_hist_up) or ((temp_prev - temp) >= temp_hist_down): pwm = (-c - (255 * temp)) / -(temp_max - temp_min) pwmi = int(pwm) if pwmi < 0: pwmi = 0 if pwmi > 255: pwmi = 255 #print(str(temp), str(temp_prev), str(pwm), str(pwmi)) f_pwm = open(fanpwm_file, 'w') f_pwm.write(str(pwmi)) f_pwm.close() temp_prev = temp time.sleep(1)