Created
February 25, 2019 18:13
-
-
Save CookiePLMonster/b493e835d87d5df2a79d5a610eb16e9d to your computer and use it in GitHub Desktop.
Safe Shutdown Script
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/env python3 | |
from gpiozero import Button, LED, OutputDevice | |
import os | |
from signal import pause | |
import subprocess | |
from time import sleep | |
resetPin = 2 | |
ledPin = 14 | |
powerenPin = 4 | |
fanPin = 17 | |
hold = 2 | |
led = LED(ledPin,initial_value=True) | |
power = OutputDevice(powerenPin,initial_value=True) | |
fan = OutputDevice(fanPin,initial_value=False) | |
reboot_long_press = None | |
#functions that handle button events | |
def reboot_press(): | |
global reboot_long_press | |
reboot_long_press = False | |
def reboot_hold(): | |
led.blink(.25,.25) | |
global reboot_long_press | |
reboot_long_press = True | |
def reboot_release(): | |
led.on() | |
output_es = int(subprocess.check_output(['/opt/RetroFlag/multi_switch.sh', '--es-pid'])) | |
output_rc = int(subprocess.check_output(['/opt/RetroFlag/multi_switch.sh', '--rc-pid'])) | |
if reboot_long_press: | |
if output_rc: | |
os.system("/opt/RetroFlag/multi_switch.sh --closeemu") | |
if output_es: | |
os.system("/opt/RetroFlag/multi_switch.sh --es-reboot") | |
else: | |
os.system("sudo reboot") | |
else: | |
if output_rc: | |
os.system("/opt/RetroFlag/multi_switch.sh --closeemu") | |
elif output_es: | |
os.system("/opt/RetroFlag/multi_switch.sh --es-restart") | |
rebootBtn = Button(resetPin,hold_time=hold) | |
rebootBtn.when_pressed = reboot_press | |
rebootBtn.when_held = reboot_hold | |
rebootBtn.when_released = reboot_release | |
def getCPUtemp(): | |
res = os.popen('vcgencmd measure_temp').readline() | |
return float(res.replace("temp=","").replace("'C\n","")) | |
fanOn = False | |
forcedFanState = False | |
while True: | |
reqFanState = None | |
governor = None | |
with open('/dev/shm/fan_governor','r') as f: | |
governor = f.readline().strip().lower() | |
if governor == 'alwayson': | |
reqFanState = True | |
forcedFanState = True | |
elif governor == 'alwaysoff': | |
reqFanState = False | |
forcedFanState = True | |
else: | |
fanOnTemp = 65 | |
fanOffTemp = 45 | |
if governor == 'quiet': | |
fanOnTemp = 70 | |
fanOffTemp = 60 | |
cpuTemp = int(getCPUtemp()) | |
if forcedFanState: | |
reqFanState = False | |
forcedFanState = False | |
if cpuTemp >= fanOnTemp: | |
reqFanState = True | |
elif cpuTemp < fanOffTemp: | |
reqFanState = False | |
if not fanOn and reqFanState is True: | |
fan.on() | |
fanOn = True | |
elif fanOn and reqFanState is False: | |
fan.off() | |
fanOn = False | |
sleep(5.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment