forked from kounch/argonone
-
Notifications
You must be signed in to change notification settings - Fork 19
/
argononed.py
executable file
·119 lines (106 loc) · 3.04 KB
/
argononed.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
#!/opt/argonone/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python; tab-width: 4; indent-tabs-mode: nil; -*-
# PEP 8, PEP 263.
"""
Argon One Fan and Button Service Daemon
"""
import smbus
import RPi.GPIO as GPIO
import os
import time
from threading import Thread
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
shutdown_pin = 4
GPIO.setup(shutdown_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def shutdown_check():
while True:
pulsetime = 1
def wait_for_it():
level = GPIO.input(23)
while (GPIO.input(23)) :
time.sleep(0.01)
while GPIO.input(shutdown_pin) == GPIO.HIGH:
time.sleep(0.01)
pulsetime += 1
if pulsetime >= 2 and pulsetime <= 3:
os.system("reboot")
elif pulsetime >= 4 and pulsetime <= 5:
os.system("shutdown now -h")
def get_fanspeed(tempval, configlist):
for curconfig in configlist:
curpair = curconfig.split("=")
tempcfg = float(curpair[0])
fancfg = int(float(curpair[1]))
if tempval >= tempcfg:
return fancfg
return 0
def load_config(fname):
newconfig = []
try:
with open(fname, "r") as fp:
for curline in fp:
if not curline:
continue
tmpline = curline.strip()
if not tmpline:
continue
if tmpline[0] == "#":
continue
tmppair = tmpline.split("=")
if len(tmppair) != 2:
continue
tempval = 0
fanval = 0
try:
tempval = float(tmppair[0])
if tempval < 0 or tempval > 100:
continue
except:
continue
try:
fanval = int(float(tmppair[1]))
if fanval < 0 or fanval > 100:
continue
except:
continue
newconfig.append("{:5.1f}={}".format(tempval, fanval))
if len(newconfig) > 0:
newconfig.sort(reverse=True)
except:
return []
return newconfig
def temp_check():
fanconfig = ["65=100", "60=55", "55=10"]
tmpconfig = load_config("/etc/argononed.conf")
if len(tmpconfig) > 0:
fanconfig = tmpconfig
address = 0x1a
prevblock = 0
while True:
temp = os.popen("cat /sys/class/thermal/thermal_zone0/temp").readline()
val = float(temp) / 1000
block = get_fanspeed(val, fanconfig)
if block < prevblock:
time.sleep(30)
prevblock = block
try:
bus.write_byte_data(address,0,block)
except IOError:
temp = ""
time.sleep(30)
try:
t1 = Thread(target=shutdown_check)
t2 = Thread(target=temp_check)
t1.start()
t2.start()
except:
t1.stop()
t2.stop()
GPIO.cleanup()