Last active
November 12, 2020 13:25
-
-
Save estshorter/6c580f2f17b946581e2ac4fb4072d46d to your computer and use it in GitHub Desktop.
argononed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import time | |
from threading import Thread | |
import RPi.GPIO as GPIO | |
import smbus | |
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 | |
GPIO.wait_for_edge(shutdown_pin, GPIO.RISING) | |
time.sleep(0.01) | |
while GPIO.input(shutdown_pin) == GPIO.HIGH: | |
time.sleep(0.01) | |
pulsetime += 1 | |
if 2 <= pulsetime <= 3: | |
print("Rebooting...") | |
os.system("reboot") | |
elif 4 <= pulsetime <= 5: | |
print("Shuting down...") | |
os.system("shutdown now -h") | |
def get_fanspeed(tempval, configlist): | |
for config in configlist: | |
tempcfg, fancfg = config | |
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 TypeError: | |
continue | |
try: | |
fanval = int(tmppair[1]) | |
if fanval < 0 or fanval > 100: | |
continue | |
except TypeError: | |
continue | |
newconfig.append((tempval, fanval)) | |
if len(newconfig) > 0: | |
newconfig.sort(reverse=True) | |
except: | |
return [] | |
return newconfig | |
def temp_check(): | |
fanconfig = load_config("/etc/argononed.conf") or ["65=100", "60=55", "55=10"] | |
address = 0x1A | |
prev_speed = 0 | |
while True: | |
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f: | |
temp = int(f.readline())/1000 | |
fanspeed = get_fanspeed(temp, fanconfig) | |
if fanspeed < prev_speed: | |
time.sleep(30) | |
prev_speed = fanspeed | |
try: | |
bus.write_byte(address, fanspeed) | |
except IOError: | |
pass | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment