@godhunter74
| import RPi.GPIO as GPIO |
| import time |
| import os |
| import thread |
| import datetime |
| import socket |
| import sys |
| import argpa**e |
| from datetime import datetime |
| from configgen import recalboxFiles |
| |
| |
| pa**er = argpa**e.ArgumentPa**er(description='power manager') |
| pa**er.add_argument("-m", help="mode onoff or push", type=str, required=True) |
| args = pa**er.pa**e_args() |
| |
| mode = args.m |
| |
| IPADDR = "127.0.0.1" |
| PORTNUM = 55355 |
| |
| |
| POWERPLUS = 3 |
| RESETPLUS = 2 |
| LED = 14 |
| |
| GPIO.setwarnings(False) |
| GPIO.setmode(GPIO.BCM) |
| |
| GPIO.setup(RESETPLUS, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| |
| |
| |
| GPIO.setup(POWERPLUS, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| |
| |
| |
| GPIO.setup(LED, GPIO.OUT) |
| GPIO.output(LED, True) |
| |
| |
| |
| |
| |
| def button_pressed(channel): |
| if channel == POWERPLUS: |
| speed=0.15 |
| shutdownstring="shutdown -h now" |
| nwcommand="QUIT" |
| |
| elif channel == RESETPLUS: |
| speed=0.05 |
| shutdownstring="shutdown -r now" |
| nwcommand="RESET" |
| |
| timer = 0 |
| flag = True |
| while flag: |
| if GPIO.input(channel) == False: |
| timer += 1 |
| print "Button pressed" |
| elif GPIO.input(channel) == True: |
| |
| print "Button released" |
| print timer |
| |
| |
| if (timer > 10): |
| offreset(speed, shutdownstring) |
| print "shutdown" |
| elif (timer >1): |
| retroarch(nwcommand) |
| print "retroarch" |
| killthats**t(channel) |
| |
| timer = 0 |
| flag = False |
| time.sleep(0.1) |
| |
| |
| |
| def killthats**t(channel): |
| if channel == POWERPLUS: |
| for bin in recalboxFiles.recalboxBins: |
| print bin |
| proc = os.path.basename(bin) |
| print proc |
| os.system("killall -9 "+proc) |
| |
| |
| def offreset(speed, shutdownstring): |
| thread.start_new_thread( blink, (speed, )) |
| flag=True |
| pids = [pid for pid in os.listdir('/proc') if pid.isdigit()] |
| os.system("/etc/init.d/S31emulationstation stop") |
| while flag: |
| flag = False |
| for pid in pids: |
| try: |
| print pid |
| commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read() |
| if "emulationstation" in commandpath: |
| flag = True |
| except IOError: |
| continue |
| os.system(shutdownstring) |
| |
| |
| def blink(speed): |
| while True: |
| GPIO.output(LED, False) |
| time.sleep(speed) |
| GPIO.output(LED, True) |
| time.sleep(speed) |
| |
| |
| |
| def retroarch(nwcommand): |
| try: |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| except socket.error: |
| print 'Failed to create socket' |
| sys.exit() |
| s.sendto(nwcommand, (IPADDR, PORTNUM)) |
| |
| |
| GPIO.add_event_detect(RESETPLUS, GPIO.BOTH, callback=button_pressed, bouncetime=2) |
| GPIO.add_event_detect(POWERPLUS, GPIO.BOTH, callback=button_pressed, bouncetime=2) |
| while True: |
| time.sleep(0.2) |