RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    [Tutorial] Get text output from bash files into your python script!

    Scheduled Pinned Locked Moved Ideas and Development
    cyperghostpython
    38 Posts 10 Posters 10.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cyperghostC
      cyperghost @caver01
      last edited by cyperghost

      @caver01 Well yes... but from the companies view - I won't trust a script that is not written by myself. It needs to be maintained and so you (as company) may run into a way of relation. So keep it simple and other users may read the PR and modify the scipts ;) That's the best way and keeps away from trouble

      1 Reply Last reply Reply Quote 0
      • F
        fastpop72 @cyperghost
        last edited by

        @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

        In the case of the NESPi+ and the orignial script you can use this method, too

        Get the script from here - GITHUB

        To install take following steps:
        0. Install original script from retroflag wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash

        1. Login with SSH
        2. Got to installed directory with cd /opt/RetroFlag
        3. download the multi_switch script with sudo wget https://raw.githubusercontent.com/crcerror/ES-generic-shutdown/master/multi_switch.sh
        4. Make script executable sudo chmod +x multi_switch.sh
        5. Remove old script with sudo rm SafeShutdown.py
        6. Get new script with sudo wget https://raw.githubusercontent.com/crcerror/retroflag-picase/master/SafeShutdown.py

        Python code: SafeShutdown.py

        #!/usr/bin/env python3
        from gpiozero import Button, LED
        import os 
        from signal import pause
        import subprocess
        
        powerPin = 3 
        resetPin = 2 
        ledPin = 14 
        powerenPin = 4 
        hold = 1
        led = LED(ledPin)
        led.on()
        power = LED(powerenPin)
        power.on()
        
        #functions that handle button events
        def when_pressed():
         led.blink(.2,.2)
         output = int(subprocess.check_output(['./multi_switch.sh', '--es-pid']))
         if output:
             os.system("./multi_switch.sh --es-poweroff")
         else:
             os.system("sudo shutdown -h now")
            
        def when_released():
         led.on()
        
        def reboot():
         output = int(subprocess.check_output(['./multi_switch.sh', '--es-pid']))
         if output:
             os.system("./multi_switch.sh --es-restart")
         else:
             os.system("sudo reboot")
         
        btn = Button(powerPin, hold_time=hold)
        rebootBtn = Button(resetPin)
        rebootBtn.when_pressed = reboot 
        btn.when_pressed = when_pressed
        btn.when_released = when_released
        pause()
        

        Python and the GPIOZERO libary seems to be overloaded. As I did not create this script.
        But it demonstrates how bash files output can be used to do actions ;)---

        Just tried this procedure with my new Nespi Case+, then rebooted. Unfortunately now when i turn off the switch it blinks the red led of the case and does nothing. If i press the reset button does nothing. I had to revert to original Nespi script.

        cyperghostC 1 Reply Last reply Reply Quote 0
        • cyperghostC
          cyperghost @fastpop72
          last edited by

          @fastpop72 Is the multi_switch.sh script located into same folder as the python script? Is it made executable? It works for me but I don't give support for that.

          F 1 Reply Last reply Reply Quote 0
          • F
            fastpop72 @cyperghost
            last edited by

            @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

            @fastpop72 Is the multi_switch.sh script located into same folder as the python script? Is it made executable? It works for me but I don't give support for that.

            Sure it was in the same folder "/opt/RetroFlag", i deleted the original SafeShutdown python script, installed yours, changed exec permission with chmod, but when i press the switch to turn off the led start blinking but does nothing. Reset neither.
            I noticed this discussion started 2 months ago... maybe the original nespi python script has changed since and your mod doesn't work anymore... no problem, i reverted to original nespy script.

            cyperghostC 1 Reply Last reply Reply Quote 0
            • cyperghostC
              cyperghost @fastpop72
              last edited by cyperghost

              @fastpop72 I changed internal pathes from ./multi_switch.sh, to /opt/RetroFlag/multi_switch.sh that should work now ;)

              F 1 Reply Last reply Reply Quote 0
              • F
                fastpop72 @cyperghost
                last edited by

                @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

                @fastpop72 I changed internal pathes from ./multi_switch.sh, to /opt/RetroFlag/multi_switch.sh

                Wow... maybe your modified SafeShutdown script doesn't recall correctly the other multiswitch file if the path is not complete? I will give it a try and let you know.

                cyperghostC 1 Reply Last reply Reply Quote 1
                • cyperghostC
                  cyperghost @fastpop72
                  last edited by cyperghost

                  @fastpop72 I've improved the script a bit for more natural behaviour

                  1. If you press restart if emulator is currently running, then you will be kicked back to ES main menu
                  2. If you press restart in ES main screen, ES will be restartet (no reboot!), good for quick saving metadata or internal saves.
                  3. If you press power-off then Raspberry will shutdown
                  
                  All metadata is always saved
                  Best -> cyperghost
                  
                  F 1 Reply Last reply Reply Quote 1
                  • F
                    fastpop72 @cyperghost
                    last edited by

                    @cyperghost said in [Tutorial] Get text output from bash files into your python script!:

                    @fastpop72 I've improved the script a bit for more natural behaviour

                    1. If you press restart if emulator is currently running, then you will be kicked back to ES main menu
                    2. If you press restart in ES main screen, ES will be restartet (no reboot!), good for quick saving metadata or internal saves.
                    3. If you press power-off then Raspberry will shutdown
                    
                    All metadata is always saved
                    Best -> cyperghost
                    
                    

                    Your new modified script WORKS LIKE A CHARM NOW!!! It's exactly what i'm looking for... Now i can press my Nespi+ reset button to exit any game and return to ES! And the Power/shutdown button turn off the whole system VERY QUICKLY!! Few seconds and the Nespi+ is completely off! THANKS THANKS THANKS!

                    1 Reply Last reply Reply Quote 1
                    • P
                      paul4tune
                      last edited by

                      I have tried the manual install, as well as your auto install, I for the life of me cant get it to work and i don't know why.
                      I am new to the retropie world, i have a nespi plus, pi3b+, 128gig card running a retro gaming image.
                      I'm running PuTTY for the SSH connection

                      P 1 Reply Last reply Reply Quote 0
                      • P
                        paul4tune @paul4tune
                        last edited by

                        @paul4tune my fault I completely forgot to switch the safe switch to on inside the nespi all working

                        1 Reply Last reply Reply Quote 1
                        • Serj TargarienS
                          Serj Targarien
                          last edited by

                          @cyperghost , maybe I'm missing something really simple here, but I'm a bit confused.

                          I'm going to receive my NesPi+ Case, probably today, and I'm looking for the most efficient shut down script to shut the system down. I would like to shut it down completely, including the fan, while saving ES settings and metadata, and if possible saved games even if the system is shut down while using retroarch (it doesn't if you don't exit it properly, even if you saved in game) and preventing SD card corruption. As a bonus I would like to show a splash screen while shutting down, just for the looks, but that's not mandatory.

                          Would this script be better for that or the Multi Switch Shutdown Script as explained here? https://retropie.org.uk/forum/topic/17415/multi-switch-shutdown-script What are the fundamental differences between the two of them? Sorry if I've missed some simple post explaining just that, real life has had me busy lately and I haven't been able to find lots of time for the forums.

                          Thanks in advance for the answers.

                          cyperghostC 1 Reply Last reply Reply Quote 0
                          • cyperghostC
                            cyperghost @Serj Targarien
                            last edited by

                            @serj-targarien

                            I would like to shut it down completely, including the fan, while saving ES settings and metadata, and if possible saved games even if the system is shut down while using retroarch (it doesn't if you don't exit it properly, even if you saved in game) and preventing SD card corruption.

                            That's all the script is doing for you.

                            Would this script be better for that or the Multi Switch Shutdown Script as explained here?

                            The "Multi Switch Shutdown Script" (MS3) fullfills two purposes. It can act as real shutdown script then you need to call it via autostart and set parameters to it. Or it can be used as command line tool to get info about status of emulators and EmulationStation. Annother shutdown script (like the modified NESPi+ python script) can read that status and set different actions.

                            What are the fundamental differences between the two of them?

                            See I wrote that script to make setup of some power devices as easy as possible. But someone wants a LED to lid, annother wants to play a song during shutdown and you want to display a picture. So it's easier to write your own scripts and invoke MS3 to do the prober shutdown action.

                            The big difference between the MS3-bash script and this python is the routine how the trigger status is asked. The bash script uses the classic polling (one request in every 1 second) the python script are using interrupts which are better for mashine handling. The python script is a bit more relieable.... That's all.

                            1 Reply Last reply Reply Quote 1
                            • Serj TargarienS
                              Serj Targarien
                              last edited by

                              @cyperghost thanks for the extensive answer. Really appreciated.

                              1 Reply Last reply Reply Quote 0
                              • S
                                Sithel
                                last edited by

                                @cyperghost
                                I had to register just to say thank you! Your scripts are on point. The way the reset button and power button works now is perfect and RetroFlag should have done it this way in the first place. I use your rom manager script to and awesome work! Thanks again :)

                                cyperghostC 1 Reply Last reply Reply Quote 1
                                • cyperghostC
                                  cyperghost @Sithel
                                  last edited by

                                  @sithel I really appreciate your comment. But take your time to stay here there are much more things to discover. Currently a new port of openbor is made, lots of nice themes are available and did you ever heard about retroachievements?

                                  1 Reply Last reply Reply Quote 0
                                  • C
                                    CapNinja
                                    last edited by

                                    Sorry if this is too obvious, but can I install this using a snespi+ instead?

                                    cyperghostC 1 Reply Last reply Reply Quote 0
                                    • cyperghostC
                                      cyperghost @CapNinja
                                      last edited by

                                      @CapNinja yes. Retroflag did not change any line of code since the NESPi so this is working with SuperPi and the new MegaPi case, too.
                                      I have a US version of their SuperPi-case and the code is working best.

                                      C 1 Reply Last reply Reply Quote 1
                                      • C
                                        CapNinja @cyperghost
                                        last edited by

                                        @cyperghost
                                        Thanks

                                        1 Reply Last reply Reply Quote 0
                                        • benjaminjayB
                                          benjaminjay
                                          last edited by

                                          I have a 3B+ in the Nespi case and followed the "automatic install" instructions but the power and reset buttons don't work. Do I need to install the script from RetroFlag first?

                                          cyperghostC 1 Reply Last reply Reply Quote 0
                                          • cyperghostC
                                            cyperghost @benjaminjay
                                            last edited by

                                            @benjaminjay If no switch is working for you then the script from retroflag will also fail. You should set small switch in SAFE SHUTDOWN ON position and/or recheck wiring.

                                            benjaminjayB 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            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.