-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancontrol.py
43 lines (36 loc) · 1.11 KB
/
fancontrol.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
import glob
import gpiozero
import time
import re
class DS18S20:
def __init__(self):
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
self.dev_file = device_folder + '/w1_slave'
def get(self):
while True:
with open(self.dev_file, 'r') as F:
lines = F.readlines()
if "YES" not in lines[0]:
time.sleep(0.2)
continue
m = re.search(r" t=(\d+)$", lines[1])
if m:
return int(m.group(1)) / 1000
Exception("boom")
def transform(val, off, fact, min_, max_):
x = (val - off) * fact
x = min(x, max_)
x = max(x, min_)
return x
sensor = DS18S20()
fan = gpiozero.PWMOutputDevice(pin=18)
while True:
cpu_temp = gpiozero.CPUTemperature().temperature
hdd_temp = sensor.get()
cpu_need = transform(cpu_temp, 35, 0.1, 0, 1)
hdd_need = transform(hdd_temp, 25, 0.1, 0, 1)
speed = max([cpu_need, hdd_need])
print(cpu_temp, cpu_need, hdd_temp, hdd_need, speed)
fan.value = speed
time.sleep(10)