-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan_ctrl.py
81 lines (61 loc) · 1.79 KB
/
fan_ctrl.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
#!/usr/bin/python3
import RPi.GPIO as GPIO
import json
from time import sleep
# Load User settings
with open(__file__.replace('.py', '.json')) as file:
config = json.load(file)
# Load Fan Profile
curve = config['profiles'][config['profile']]
# Use Broadcom GPIO Numbers
GPIO.setmode(GPIO.BCM)
# Initialize GPIO Pin
GPIO.setup(config['gpio_pin'], GPIO.OUT)
# Set PWM Frequency
fan = GPIO.PWM(config['gpio_pin'], config['pwm_freq'])
# PWM Start with fan turned off
fan.start(0)
# User Settings no longer needed
config.clear()
# The current CPU Fan status (on/off)
status = False
# Temp history, used for calculating trend
history = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# The main loop
while True:
# Get CPU Temp
with open("/sys/class/thermal/thermal_zone0/temp", "r") as sensor:
temp = int(float(sensor.read()) / 1000)
# Reset the fan speed
speed = 0
# Should the fan be turned off?
if temp <= 40:
# Turn off the fan
status = False
# Is the fan on or should it be?
elif status or temp >= 50:
# Clamp the temperature to the fan curve
if temp <= 50:
index = 0
elif temp > 80:
index = 30
else:
index = temp - 50
# Check if it's an upward trend
if ((sum(history) / 10) < temp) or temp < 50:
# Try to stabilize the temperature
# TODO: Need to test this further
if index < 30:
index += 1
# Remove the first entry
history.pop(0)
# Add temperature to the list
history.append(temp)
# Load fan speed from curve
speed = curve[index]
# Keep the fan on
status = True
# Update fan speed
fan.ChangeDutyCycle(speed)
# Go back to sleep... ZzZzZz
sleep(2.00)