From 88f7570d7ff6698ae9a8834ea22b39f6f715913d Mon Sep 17 00:00:00 2001 From: OyyoDams Date: Sat, 10 Feb 2018 16:52:19 +0100 Subject: [PATCH 1/2] Working on RPI only --- .../recalbox/fsoverlay/etc/init.d/S92rpi-gpio | 58 +++++++ .../fsoverlay/recalbox/scripts/rpi-gpio.py | 143 ++++++++++++++++++ .../crt/crt-royale-ntsc-256px-composite.glslp | 0 .../crt/crt-royale-ntsc-320px-composite.glslp | 0 .../shaders/lr/crt/crtglow_gauss.glslp | 0 .../lr/crt/crtglow_gauss_ntsc_3phase.glslp | 0 .../shaders/lr/crt/crtglow_lanczos.glslp | 0 .../share_init/shaders/lr/crt/gtuv50.glslp | 0 .../shaders/lr/dithering/mdapt.glslp | 0 .../lr/handheld/console-border/psp-2x.glslp | 0 .../shaders/lr/hqx/single-pass/hq2x.glslp | 0 .../shaders/lr/hqx/single-pass/hq3x.glslp | 0 .../shaders/lr/hqx/single-pass/hq4x.glslp | 0 .../lr/ntsc/ntsc-256px-gauss-scanline.glslp | 0 .../ntsc-256px-svideo-gauss-scanline.glslp | 0 .../shaders/lr/ntsc/ntsc-256px-svideo.glslp | 0 .../shaders/lr/ntsc/ntsc-256px.glslp | 0 .../lr/ntsc/ntsc-320px-gauss-scanline.glslp | 0 .../ntsc-320px-svideo-gauss-scanline.glslp | 0 .../shaders/lr/ntsc/ntsc-320px-svideo.glslp | 0 .../shaders/lr/ntsc/ntsc-320px.glslp | 0 .../share_init/shaders/lr/ntsc/ntsc.glslp | 0 .../shaders/lr/xbr/super-xbr-2p.glslp | 0 .../lr/xbr/super-xbr-3p-smoother.glslp | 0 .../share_init/system/configs/rpi-gpio.ini | 10 ++ .../240p/introRecalboxCapcom320x240.mp4 | Bin .../resources/splash/introRecalboxKonami2.mp4 | Bin .../systems/doom/init/roms/doom.wad | Bin package/recalbox-system/rpi1/recalbox.conf | 3 + package/recalbox-system/rpi3/recalbox.conf | 3 + .../recalbox-system/rpizero2/recalbox.conf | 3 + 31 files changed, 220 insertions(+) create mode 100755 board/recalbox/fsoverlay/etc/init.d/S92rpi-gpio create mode 100644 board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crt-royale-ntsc-256px-composite.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crt-royale-ntsc-320px-composite.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_gauss.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_gauss_ntsc_3phase.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_lanczos.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/gtuv50.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/dithering/mdapt.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/handheld/console-border/psp-2x.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq2x.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq3x.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq4x.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-gauss-scanline.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-svideo-gauss-scanline.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-svideo.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-gauss-scanline.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-svideo-gauss-scanline.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-svideo.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/xbr/super-xbr-2p.glslp mode change 100755 => 100644 board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/xbr/super-xbr-3p-smoother.glslp create mode 100644 board/recalbox/fsoverlay/recalbox/share_init/system/configs/rpi-gpio.ini mode change 100644 => 100755 board/recalbox/fsoverlay/recalbox/system/resources/splash/240p/introRecalboxCapcom320x240.mp4 mode change 100644 => 100755 board/recalbox/fsoverlay/recalbox/system/resources/splash/introRecalboxKonami2.mp4 mode change 100755 => 100644 package/recalbox-romfs2/systems/doom/init/roms/doom.wad diff --git a/board/recalbox/fsoverlay/etc/init.d/S92rpi-gpio b/board/recalbox/fsoverlay/etc/init.d/S92rpi-gpio new file mode 100755 index 0000000000..f51f4a56c5 --- /dev/null +++ b/board/recalbox/fsoverlay/etc/init.d/S92rpi-gpio @@ -0,0 +1,58 @@ +#!/bin/sh +# +# Start rpi-gpio +# + +CONFFILE="/recalbox/share/system/recalbox.conf" +CONFPARAM="system.rpi.gpio" +CONFVALUE= +if [ -e $CONFFILE ]; then + CONFVALUE=$(sed -rn "s/^$CONFPARAM=(\w*)\s*.*$/\1/p" $CONFFILE | tail -n 1) +fi + +start() { + echo -n "Starting gpio: " + case "$CONFVALUE" in + "enable") + + /usr/bin/python /recalbox/scripts/rpi-gpio.py & + pid=$! + echo "$pid" > /tmp/rpi-gpio.pid + #wait "$pid" + echo "OK" + ;; + *) + echo "disabled" + exit 1 + esac +} + +stop() { + echo -n "Stopping gpio: " + if [[ -f /tmp/rpi-gpio.pid ]]; then + kill `cat /tmp/rpi-gpio.pid` + rm /tmp/rpi-gpio.pid + echo "OK" + else + echo "not running" + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart|reload) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 +esac + +exit $? + diff --git a/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py b/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py new file mode 100644 index 0000000000..d1a6fcdf4f --- /dev/null +++ b/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py @@ -0,0 +1,143 @@ +import RPi.GPIO as GPIO +import ConfigParser +import string +import time +from subprocess import check_output, CalledProcessError +import os +import thread +import datetime +import socket +import sys +import argparse +from datetime import datetime +from configgen import recalboxFiles +# this last one retrieves emulators bin names + +################################################################################ + +# Functions + +def getPIDs(process): + try: + pidlist = map(int, check_output(["pidof", process]).split()) + return True + except CalledProcessError: + pidlist = [] + return False + +# Function to increase volume +def volup(): + fin, fout = os.popen4("echo `amixer get 'PCM'|grep % |awk '{print $4}'|sed -e 's/\[//' -e 's/%\]//'`") + volinfo = int(fout.read()) + volinfo = volinfo + 2 + if (volinfo<=100): + os.system("amixer set 'PCM' "+str(volinfo)+"%") + +# Function to decrease volume +def voldown(): + fin, fout = os.popen4("echo `amixer get 'PCM'|grep % |awk '{print $4}'|sed -e 's/\[//' -e 's/%\]//'`") + volinfo = int(fout.read()) + volinfo = volinfo - 2 + if (volinfo>=50): + os.system("amixer set 'PCM' "+str(volinfo)+"%") + +# Function to mute / unmute +mute = False +def volmute(): + global mute + if (mute == False): + os.system("amixer set 'PCM' mute") + mute = True + else: + os.system("amixer set 'PCM' unmute") + mute = False + +# Function to send actions to retroarch +def retroarch_send(action): + # addressing information of target + IPADDR = "127.0.0.1" + PORTNUM = 55355 + + # enter the data content of the UDP packet + PACKETDATA = action + + # initialize a socket, think of it as a cable + # SOCK_DGRAM specifies that this is UDP + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + except socket.error: + print 'Failed to create socket' + #sys.exit() + #s.connect((IPADDR, PORTNUM)) + + # connect the socket, think of it as connecting the cable to the address location + + #data = bytes(PACKETDATA) + + s.sendto(PACKETDATA, (IPADDR, PORTNUM)) + +# Function to get user actions, and then send them to retroarch or kill other emulators +def useraction(action): + if (action == "QUIT" and getPIDs("retroarch") == False): + for bin in recalboxFiles.recalboxBins: + proc = os.path.basename(bin) + os.system("killall -9 "+proc) + elif (getPIDs("retroarch") == True and (action == "QUIT" or action == "RESET" or action == "FAST_FORWARD" or action == "FAST_FORWARD_HOLD" or action == "LOAD_STATE" or action == "SAVE_STATE" or action == "FULLSCREEN_TOGGLE" or action == "STATE_SLOT_PLUS" or action == "STATE_SLOT_MINUS" or action == "REWIND" or action == "MOVIE_RECORD_TOGGLE" or action == "PAUSE_TOGGLE" or action == "FRAMEADVANCE" or action == "SHADER_NEXT" or action == "SHADER_PREV" or action == "CHEAT_INDEX_PLUS" or action == "CHEAT_INDEX_MINUS" or action == "CHEAT_TOGGLE" or action == "SCREENSHOT" or action == "MUTE" or action == "NETPLAY_FLIP" or action == "SLOWMOTION" or action == "VOLUME_UP" or action == "VOLUME_DOWN" or action == "OVERLAY_NEXT" or action == "DISK_EJECT_TOGGLE" or action == "DISK_NEXT" or action == "DISK_PREV" or action == "GRAB_MOUSE_TOGGLE" or action == "MENU_TOGGLE")): + retroarch_send(action) + elif (action == "VOLUP"): + volup() + elif (action == "VOLDOWN"): + voldown() + elif (action == "VOLMUTE"): + volmute() + +# Define a threaded callback function to run in another thread when events are detected +def actionGPIO(channel): + timer = 0 + flag = True + hold = False + while flag: + if GPIO.input(channel) == False: + timer += 1 + if (timer >=20 and hold == False): + hold = True + print gpioconf.get("GPIO"+str(channel),"hold") + useraction(gpioconf.get("GPIO"+str(channel),"hold")) + elif GPIO.input(channel) == True: + #timer adds 1 each 0.1 seconds if timer = 10, button is pressed for 1s + if (timer > 0): #avoid false results + if (timer <10 and hold == False): + print gpioconf.get("GPIO"+str(channel),"quick") + useraction(gpioconf.get("GPIO"+str(channel),"quick")) + elif (timer >=10 and hold == False): + print gpioconf.get("GPIO"+str(channel),"standard") + useraction(gpioconf.get("GPIO"+str(channel),"standard")) + elif (hold == True): + print gpioconf.get("GPIO"+str(channel),"release") + useraction(gpioconf.get("GPIO"+str(channel),"release")) + timer = 0 + flag = False + hold = False + time.sleep(0.1) + +################################################################################ + +# Read the user-defined ini file + +gpioconf=ConfigParser.ConfigParser() +gpioconf.read('/recalbox/share/system/configs/rpi-gpio.ini') + +# GPIO Initialization +GPIO.setmode(GPIO.BCM) + +for each_section in gpioconf.sections(): + gpio = each_section.replace('GPIO','') + gpio=int(gpio) + GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.add_event_detect(gpio, GPIO.BOTH, callback=actionGPIO) + +print "Waiting for action" + +while True: + time.sleep(0.2) + diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crt-royale-ntsc-256px-composite.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crt-royale-ntsc-256px-composite.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crt-royale-ntsc-320px-composite.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crt-royale-ntsc-320px-composite.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_gauss.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_gauss.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_gauss_ntsc_3phase.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_gauss_ntsc_3phase.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_lanczos.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/crtglow_lanczos.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/gtuv50.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/crt/gtuv50.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/dithering/mdapt.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/dithering/mdapt.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/handheld/console-border/psp-2x.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/handheld/console-border/psp-2x.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq2x.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq2x.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq3x.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq3x.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq4x.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/hqx/single-pass/hq4x.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-gauss-scanline.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-gauss-scanline.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-svideo-gauss-scanline.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-svideo-gauss-scanline.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-svideo.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px-svideo.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-256px.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-gauss-scanline.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-gauss-scanline.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-svideo-gauss-scanline.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-svideo-gauss-scanline.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-svideo.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px-svideo.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc-320px.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/ntsc/ntsc.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/xbr/super-xbr-2p.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/xbr/super-xbr-2p.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/xbr/super-xbr-3p-smoother.glslp b/board/recalbox/fsoverlay/recalbox/share_init/shaders/lr/xbr/super-xbr-3p-smoother.glslp old mode 100755 new mode 100644 diff --git a/board/recalbox/fsoverlay/recalbox/share_init/system/configs/rpi-gpio.ini b/board/recalbox/fsoverlay/recalbox/share_init/system/configs/rpi-gpio.ini new file mode 100644 index 0000000000..0f635585e9 --- /dev/null +++ b/board/recalbox/fsoverlay/recalbox/share_init/system/configs/rpi-gpio.ini @@ -0,0 +1,10 @@ +[GPIO22] +quick=VOLUP +standard=VOLDOWN +hold=VOLMUTE +release= +[GPIO27] +quick=RESET +standard=QUIT +hold= +release= diff --git a/board/recalbox/fsoverlay/recalbox/system/resources/splash/240p/introRecalboxCapcom320x240.mp4 b/board/recalbox/fsoverlay/recalbox/system/resources/splash/240p/introRecalboxCapcom320x240.mp4 old mode 100644 new mode 100755 diff --git a/board/recalbox/fsoverlay/recalbox/system/resources/splash/introRecalboxKonami2.mp4 b/board/recalbox/fsoverlay/recalbox/system/resources/splash/introRecalboxKonami2.mp4 old mode 100644 new mode 100755 diff --git a/package/recalbox-romfs2/systems/doom/init/roms/doom.wad b/package/recalbox-romfs2/systems/doom/init/roms/doom.wad old mode 100755 new mode 100644 diff --git a/package/recalbox-system/rpi1/recalbox.conf b/package/recalbox-system/rpi1/recalbox.conf index ac01613250..74ffa65543 100644 --- a/package/recalbox-system/rpi1/recalbox.conf +++ b/package/recalbox-system/rpi1/recalbox.conf @@ -15,6 +15,9 @@ ;system.power.switch=PIN356ONOFFRESET # https://wiki.recalbox.com/en/tutorials/others/add-on-off-button-to-your-recalbox ;system.power.switch=PIN356PUSHRESET # https://wiki.recalbox.com/en/tutorials/others/add-on-off-button-to-your-recalbox +# Uncomment to enable custom GPIO script feature +;system.rpi.gpio=enable + ## fbcp FrameBuffer Copy Program ## For small TFT screen on GPIO and SPI ## See https://github.com/recalbox/recalbox-os/wiki/Utility---Use-of-fbcp-for-small-TFT-screen-%28EN%29 for details diff --git a/package/recalbox-system/rpi3/recalbox.conf b/package/recalbox-system/rpi3/recalbox.conf index d3ec04fe9e..95c17c1be7 100644 --- a/package/recalbox-system/rpi3/recalbox.conf +++ b/package/recalbox-system/rpi3/recalbox.conf @@ -15,6 +15,9 @@ ;system.power.switch=PIN356ONOFFRESET # https://wiki.recalbox.com/en/tutorials/others/add-on-off-button-to-your-recalbox ;system.power.switch=PIN356PUSHRESET # https://wiki.recalbox.com/en/tutorials/others/add-on-off-button-to-your-recalbox +# Uncomment to enable custom GPIO script feature +;system.rpi.gpio=enable + ## fbcp FrameBuffer Copy Program ## For small TFT screen on GPIO and SPI ## See https://github.com/recalbox/recalbox-os/wiki/Utility---Use-of-fbcp-for-small-TFT-screen-%28EN%29 for details diff --git a/package/recalbox-system/rpizero2/recalbox.conf b/package/recalbox-system/rpizero2/recalbox.conf index 2b385421fe..9cedf9317d 100644 --- a/package/recalbox-system/rpizero2/recalbox.conf +++ b/package/recalbox-system/rpizero2/recalbox.conf @@ -15,6 +15,9 @@ ;system.power.switch=PIN356ONOFFRESET # https://wiki.recalbox.com/en/tutorials/others/add-on-off-button-to-your-recalbox ;system.power.switch=PIN356PUSHRESET # https://wiki.recalbox.com/en/tutorials/others/add-on-off-button-to-your-recalbox +# Uncomment to enable custom GPIO script feature +;system.rpi.gpio=enable + ## fbcp FrameBuffer Copy Program ## For small TFT screen on GPIO and SPI ## See https://github.com/recalbox/recalbox-os/wiki/Utility---Use-of-fbcp-for-small-TFT-screen-%28EN%29 for details -- GitLab From 8c9c123dd2ac5439febdb0bba69403d2bd895543 Mon Sep 17 00:00:00 2001 From: Bkg2k Date: Sun, 8 Mar 2020 14:16:05 +0100 Subject: [PATCH 2/2] perf: cleanup & optimizations --- .../fsoverlay/recalbox/scripts/rpi-gpio.py | 181 +++++++++--------- 1 file changed, 87 insertions(+), 94 deletions(-) diff --git a/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py b/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py index d1a6fcdf4f..fa1470c2c6 100644 --- a/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py +++ b/board/recalbox/fsoverlay/recalbox/scripts/rpi-gpio.py @@ -1,143 +1,136 @@ -import RPi.GPIO as GPIO -import ConfigParser -import string +import os import time from subprocess import check_output, CalledProcessError -import os -import thread -import datetime -import socket -import sys -import argparse -from datetime import datetime -from configgen import recalboxFiles -# this last one retrieves emulators bin names + +import RPi.GPIO as GPIO +from configgen import recalboxFiles # this last one retrieves emulators bin names ################################################################################ +RETROARCH_COMMANDS = ("QUIT", "RESET", "FAST_FORWARD", "FAST_FORWARD_HOLD", "LOAD_STATE", "SAVE_STATE", "FULLSCREEN_TOGGLE", "STATE_SLOT_PLUS", "STATE_SLOT_MINUS", "REWIND", "MOVIE_RECORD_TOGGLE", "PAUSE_TOGGLE", "FRAMEADVANCE", "SHADER_NEXT", "SHADER_PREV", "CHEAT_INDEX_PLUS", "CHEAT_INDEX_MINUS", "CHEAT_TOGGLE", "SCREENSHOT", "MUTE", "NETPLAY_FLIP", "SLOWMOTION", "VOLUME_UP", "VOLUME_DOWN", "OVERLAY_NEXT", "DISK_EJECT_TOGGLE", "DISK_NEXT", "DISK_PREV", "GRAB_MOUSE_TOGGLE", "MENU_TOGGLE") + # Functions def getPIDs(process): try: pidlist = map(int, check_output(["pidof", process]).split()) - return True + return True except CalledProcessError: pidlist = [] - return False + return False + # Function to increase volume def volup(): - fin, fout = os.popen4("echo `amixer get 'PCM'|grep % |awk '{print $4}'|sed -e 's/\[//' -e 's/%\]//'`") - volinfo = int(fout.read()) - volinfo = volinfo + 2 - if (volinfo<=100): - os.system("amixer set 'PCM' "+str(volinfo)+"%") + fin, fout = os.popen4("echo `amixer get 'PCM'|grep % |awk '{print $4}'|sed -e 's/\[//' -e 's/%\]//'`") + volinfo = int(fout.read()) + volinfo = volinfo + 2 + if volinfo <= 100: + os.system("amixer set 'PCM' " + str(volinfo) + "%") + # Function to decrease volume def voldown(): - fin, fout = os.popen4("echo `amixer get 'PCM'|grep % |awk '{print $4}'|sed -e 's/\[//' -e 's/%\]//'`") - volinfo = int(fout.read()) - volinfo = volinfo - 2 - if (volinfo>=50): - os.system("amixer set 'PCM' "+str(volinfo)+"%") + fin, fout = os.popen4("echo `amixer get 'PCM'|grep % |awk '{print $4}'|sed -e 's/\[//' -e 's/%\]//'`") + volinfo = int(fout.read()) + volinfo = volinfo - 2 + if volinfo >= 50: + os.system("amixer set 'PCM' " + str(volinfo) + "%") + # Function to mute / unmute mute = False + + def volmute(): - global mute - if (mute == False): - os.system("amixer set 'PCM' mute") - mute = True - else: - os.system("amixer set 'PCM' unmute") - mute = False + global mute + if not mute: + os.system("amixer set 'PCM' mute") + mute = True + else: + os.system("amixer set 'PCM' unmute") + mute = False + # Function to send actions to retroarch def retroarch_send(action): - # addressing information of target - IPADDR = "127.0.0.1" - PORTNUM = 55355 + # addressing information of target + IPADDR = "127.0.0.1" + PORTNUM = 55355 - # enter the data content of the UDP packet - PACKETDATA = action + # enter the data content of the UDP packet + PACKETDATA = action.decode("utf-8") - # initialize a socket, think of it as a cable - # SOCK_DGRAM specifies that this is UDP - try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - except socket.error: - print 'Failed to create socket' - #sys.exit() - #s.connect((IPADDR, PORTNUM)) - - # connect the socket, think of it as connecting the cable to the address location - - #data = bytes(PACKETDATA) + # initialize a socket, think of it as a cable + # SOCK_DGRAM specifies that this is UDP + import socket + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.sendto(PACKETDATA, (IPADDR, PORTNUM)) + except socket.error: + print 'Failed to create socket' + # sys.exit() - s.sendto(PACKETDATA, (IPADDR, PORTNUM)) # Function to get user actions, and then send them to retroarch or kill other emulators def useraction(action): - if (action == "QUIT" and getPIDs("retroarch") == False): - for bin in recalboxFiles.recalboxBins: - proc = os.path.basename(bin) - os.system("killall -9 "+proc) - elif (getPIDs("retroarch") == True and (action == "QUIT" or action == "RESET" or action == "FAST_FORWARD" or action == "FAST_FORWARD_HOLD" or action == "LOAD_STATE" or action == "SAVE_STATE" or action == "FULLSCREEN_TOGGLE" or action == "STATE_SLOT_PLUS" or action == "STATE_SLOT_MINUS" or action == "REWIND" or action == "MOVIE_RECORD_TOGGLE" or action == "PAUSE_TOGGLE" or action == "FRAMEADVANCE" or action == "SHADER_NEXT" or action == "SHADER_PREV" or action == "CHEAT_INDEX_PLUS" or action == "CHEAT_INDEX_MINUS" or action == "CHEAT_TOGGLE" or action == "SCREENSHOT" or action == "MUTE" or action == "NETPLAY_FLIP" or action == "SLOWMOTION" or action == "VOLUME_UP" or action == "VOLUME_DOWN" or action == "OVERLAY_NEXT" or action == "DISK_EJECT_TOGGLE" or action == "DISK_NEXT" or action == "DISK_PREV" or action == "GRAB_MOUSE_TOGGLE" or action == "MENU_TOGGLE")): - retroarch_send(action) - elif (action == "VOLUP"): - volup() - elif (action == "VOLDOWN"): - voldown() - elif (action == "VOLMUTE"): - volmute() + if action == "QUIT" and not getPIDs("retroarch"): # TODO: check gpi script and use emulationstation child process list + for exe in recalboxFiles.recalboxBins: + proc = os.path.basename(exe) + os.system("killall -9 " + proc) + elif getPIDs("retroarch") and action in RETROARCH_COMMANDS: + retroarch_send(action) + elif action == "VOLUP": + volup() + elif action == "VOLDOWN": + voldown() + elif action == "VOLMUTE": + volmute() + # Define a threaded callback function to run in another thread when events are detected def actionGPIO(channel): - timer = 0 - flag = True - hold = False - while flag: - if GPIO.input(channel) == False: - timer += 1 - if (timer >=20 and hold == False): - hold = True - print gpioconf.get("GPIO"+str(channel),"hold") - useraction(gpioconf.get("GPIO"+str(channel),"hold")) - elif GPIO.input(channel) == True: - #timer adds 1 each 0.1 seconds if timer = 10, button is pressed for 1s - if (timer > 0): #avoid false results - if (timer <10 and hold == False): - print gpioconf.get("GPIO"+str(channel),"quick") - useraction(gpioconf.get("GPIO"+str(channel),"quick")) - elif (timer >=10 and hold == False): - print gpioconf.get("GPIO"+str(channel),"standard") - useraction(gpioconf.get("GPIO"+str(channel),"standard")) - elif (hold == True): - print gpioconf.get("GPIO"+str(channel),"release") - useraction(gpioconf.get("GPIO"+str(channel),"release")) - timer = 0 - flag = False - hold = False - time.sleep(0.1) + timer = 0 + hold = False + while not GPIO.input(channel): + timer += 1 + if timer >= 20 and not hold: + hold = True + print gpioconf.get("GPIO" + str(channel), "hold") + useraction(gpioconf.get("GPIO" + str(channel), "hold")) + time.sleep(0.1) + + # timer adds 1 each 0.1 seconds if timer = 10, button is pressed for 1s + if timer > 0: # avoid false results + if timer < 10 and not hold: + print gpioconf.get("GPIO" + str(channel), "quick") + useraction(gpioconf.get("GPIO" + str(channel), "quick")) + elif timer >= 10 and not hold: + print gpioconf.get("GPIO" + str(channel), "standard") + useraction(gpioconf.get("GPIO" + str(channel), "standard")) + elif hold: + print gpioconf.get("GPIO" + str(channel), "release") + useraction(gpioconf.get("GPIO" + str(channel), "release")) + ################################################################################ # Read the user-defined ini file -gpioconf=ConfigParser.ConfigParser() +import ConfigParser +gpioconf = ConfigParser.ConfigParser() gpioconf.read('/recalbox/share/system/configs/rpi-gpio.ini') # GPIO Initialization GPIO.setmode(GPIO.BCM) for each_section in gpioconf.sections(): - gpio = each_section.replace('GPIO','') - gpio=int(gpio) - GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) - GPIO.add_event_detect(gpio, GPIO.BOTH, callback=actionGPIO) + gpio = each_section.replace('GPIO', '') + gpio = int(gpio) + GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.add_event_detect(gpio, GPIO.BOTH, callback=actionGPIO) print "Waiting for action" while True: - time.sleep(0.2) - + time.sleep(0.2) -- GitLab