-
Notifications
You must be signed in to change notification settings - Fork 12
/
pi_fan_tuner.py
126 lines (92 loc) · 2.96 KB
/
pi_fan_tuner.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python2.7
# coding:utf-8
import logging
from time import sleep
from RPi import GPIO
from cmdtree import INT
from cmdtree import command
from cmdtree import entry
from cmdtree import group
from cmdtree import option
GPIO_CONTROL_PORT = 12
FAN_ON_TEMPERATURE = 45
def get_cpu_temp():
with open("/sys/class/thermal/thermal_zone0/temp", 'r') as f:
temp = float(f.read()) / 1000
return temp
def init():
GPIO.setmode(GPIO.BOARD)
def set_debug():
logging.basicConfig(level=logging.DEBUG)
@group(name="port", help="Turn a pin port to `IN` or `OUT`")
def port_manage():
pass
@option("port", help="Port on GPIO Physical Port", type=INT, default=GPIO_CONTROL_PORT)
@port_manage.command("turn-out", help="Turn a port into `OUT` mode")
def turn_port_out(port):
GPIO.setup(port, GPIO.OUT)
GPIO.output(port, 1)
return True
@option("port", help="Port number of GPIO Physical Port", type=INT, default=GPIO_CONTROL_PORT)
@port_manage.command("turn-in", help="Turn a port into `IN` mode")
def turn_port_in(port):
GPIO.setup(port, GPIO.IN)
return False
@option("loop", help="loop until user interrupting execution.", is_flag=True)
@command("cpu-show")
def show_cpu_temperature(loop):
def show_temperature():
print("Cpu Temperature is: {0}".format(get_cpu_temp()))
if loop:
while True:
show_temperature()
sleep(1)
else:
show_temperature()
@option(
"on-t",
help="Temperature which triggers the fan on.",
type=INT,
default=FAN_ON_TEMPERATURE,
)
@option(
"port",
help="Port number of GPIO Physical Port",
type=INT,
default=GPIO_CONTROL_PORT,
)
@group(name="fan", help="Auto tune the cpu fan in `simple` or `auto` mode.")
def auto_fan():
pass
@auto_fan.command("on", help="Turn one the fan.")
def on_fan(port, **kwargs):
turn_port_out(port)
@auto_fan.command("off", help="Turn off the fan.")
def off_fan(port, **kwargs):
turn_port_in(port)
@option("debug", is_flag=True, default=False)
@auto_fan.command("simple", help="Simply turn on or off the fan in given temperature range.")
def simple_on_of(debug, port, on_t):
if debug:
set_debug()
fan_on = turn_port_in(port)
try:
while True:
temperature = get_cpu_temp()
if temperature >= on_t:
if not fan_on:
logging.debug("Temperature {0} CPU fan on.".format(temperature))
fan_on = turn_port_out(port)
else:
if fan_on:
logging.debug("Temperature {0} CPU fan off.".format(temperature))
fan_on = turn_port_in(port)
sleep(10)
except Exception:
logging.exception("Error occurs while tune fan status:")
GPIO.cleanup()
def main():
init()
entry()
if __name__ == '__main__':
main()