[SOLVED] Variables with runcommand-onstart.sh
-
Now that I (kind of) have my secondary display working I was messing around with the
runcommand-onstart.sh
to display an image when you start a game, and to useruncommand-onend.sh
to display another image when you exit a game. So far, so good, but what I'd like to do is pass the script a variable (the game name) so I can display a different image for each game being played. I tried searching but either I'm using the wrong keywords or I don't know what I'm looking for.my very simple test script is
sudo python ~/Adafruit_Python_ILI9341/examples/imageparam.py ~/Adafruit_Python_ILI9341/examples/smw.jpg
the
~/Adafruit_Python_ILI9341/examples/smw.jpg
part is the location of the image I want to display withimageparam.py
.If I could figure out how to get the ROM name from RetroPie I could use that to select a different image.
Are there variables (ROM name, system, etc) that the runcommand scripts can use? If so, is there a list somewhere?
-
These scripts are called with this line:
bash "$script" "$system" "$emulator" "$rom" "$command" </dev/tty 2>>"$log"
So the arg1 is the system, arg2 is the emulator, arg3 is the full path to the rom, and the arg4 is the full command line used to invoke the emulator with the ROM.
And I think that redirecting messages to stderr is a good practice, so you can see it in runcommand.log later.
-
Based on the script from the scraping images post
system="$1" rom="$3" rom_bn="${rom##*/}" rom_bn="${rom_bn%.*}"
it looks like
rom_btn
is the actual game name, but can you explain what's going on with the (I assume) regular expressions that are stripping out the path and extension? -
it looks like
rom_btn
is the actual game nameactually it is the rom filename excluding the full path and the extension. Let me explain:
# get the full path filename of the rom rom="$3" # rom_bn receives $rom excluding everything from the first char to the last slash '/' rom_bn="${rom##*/}" # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.' rom_bn="${rom_bn%.*}"
You can find more info about this in the Advanced Bash-Scripting Guide, section "Manipulating Strings". The tricks I used here are in the subsection named "Substring Removal".
http://tldp.org/LDP/abs/html/string-manipulation.htmlI hope it helps you! ;-)
-
@obsidianspider not wishing to side-track your thread, but can I ask what hardware you are using to obtain a second display? I use my Pi as a lightweight PC as well as a gaming system, and would find a second monitor very useful...
-
@meleu Thanks! That helps me understand what I'm trying to pass with these variables.
@RetroResolution Right now I'm still learning how to get it working properly, but I'm using a 2.2" TFT I got from eBay.
-
@obsidianspider cool, thanks! Best of luck
-
Woo hoo! It's working!
# /opt/retropie/configs/all/runcommand-onstart.sh # get the full path filename of the ROM rom=$3 # rom_bn receives $rom excluding everything from the first char to the last slash '/' rom_bn="${rom##*/}" # rom_bn receives $rom_bn excluding everything from the last char to the first dot '.' rom_bn="${rom_bn%.*}" # get the system name system=$1 # set the image file to the first result matching the ROM name - accounts for various possible file extensions img="$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}" -type f -name "${rom_bn}-image.*" | head -1)" # check to see if there was a file found (length of the file name not zero), if not, use an image based on system if [[ -z "${img}" ]]; then img="/home/pi/Adafruit_Python_ILI9341/examples/$system.jpg" fi # run the python script to display the image sudo python ~/Adafruit_Python_ILI9341/examples/imageparam.py "$img"
[Edit] Updated script to use @meleu 's line for looking for any file regardless of extension, used a different method to see if the file exists
[Edit 2] Updated syntax for checking for zero length strings and an updated findNow back to getting the darn screen to turn on consistently…
-
@obsidianspider great, dude!
just a touch: there's no need for that else (img=$img
) ;-) -
@meleu Thanks for the tip on the
else
. I updated the script above.Looks like I need to do a bit more tweaking with my "is there an image to display" part of the script.
I have some games with a .png extension for the image and instead of saying the file isn't found, it displays a blank screen instead of the default image and a few other games where there's a different image name than the ROM name (due to me tweaking after scraping) and things are hanging instead of displaying the default.
-
@obsidianspider maybe this line can give you what you need:
img=$(ls -1 "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}/${rom_bn}-image".* | head -1)
[EDIT: the command is "ls dash one", not "ls dash el". ;-)]
[EDIT2: use double quotes to avoid problems with roms with spaces in file name.] -
@meleu That helped a lot. Things are working now. I've updated the script above.
Here's a short video of it working with a game that has art, a game with no art, and another game with art, always reverting back to the RetroPie image when on EmulationStation. I don't know if there's a way to detect what system is up on EmulationStation as you're browsing, but maybe I can look into that in the future.
-
@obsidianspider really cool man! I remember of somebody asking for this kind of trick here in the forum. And I think he would like to see it, but I can't remember his name...
-
@meleu said in Variables with runcommand-onstart.sh:
@obsidianspider maybe this line can give you what you need:
img=$(ls -1 "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}/${rom_bn}-image".* | head -1)
[EDIT: the command is "ls dash one", not "ls dash el". ;-)]
[EDIT2: use double quotes to avoid problems with roms with spaces in file name.]ls will throw an error if there is no matching filename - you can redirect stderr or alternatively use find
img="$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}" -type f -name "${rom_bn}-image.*" | head -1)"
-
@BuZz Thanks! I updated the script to use
find
-
@obsidianspider Your updated script with comments is really cool to share with others the bash tricks.
I only would like to share two tips with you:
- The use of
[[ ... ]]
is preferred over[ ... ]
ortest ...
; it reduces errors as no pathname expansion or word splitting takes place between[[
and]]
(I learned it in RetroPie Shell Style Guide) - The common practice to check if a string is empty is to use
[[ -z "$string" ]]
.
- The use of
-
@obsidianspider
Oh! I look more carefully: This line is wrong:img=$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}/${rom_bn}-image".* | head -1)
What BuZz suggested is:
img="$(find "/opt/retropie/configs/all/emulationstation/downloaded_images/${system}" -type f -name "${rom_bn}-image.*" | head -1)"
-
Thanks for all the help, I've updated the script with the suggestions you gave.
I'm learning a LOT from this community and I want to share what I've learned so someone else can make something even better with their own project. The comments help because a lot of the examples you see online don't explain what a particular thing is doing, they just say "do this" but not why you should do that.
Now that I've got the screen working and finding images properly the next step is a pushbutton to control a script that toggles the backlight…
-
Looks interesting! We've had a Pi in a Gameboy, now we need someone to create their own DS-like console with the artwork displayed on the second screen (!)
-
Hey guys! Just sharing a
find
trick I've learned today...Using this:
find /directory -type f -name "filename.*" -print -quit
Is better than use this:
find /directory -type f -name "filename.*" | head -1
Benefit: the
-quit
makesfind
exit immediately after the first match. Well, if we want the first match only, there's no need to let thefind
keep looking for files after the first match.
OBS.: when using-quit
you have to use-print
, otherwise thefind
won't print the file name.
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.