Replacing Mausberry Script with a Python script
-
@barrymossel Interesting. I don't even have the reset connected. I see the usefulness of it, but it is also a risk. If someone hits reset instead of power, we have a potential corrupted card. Or, does the reset also do a safe reboot?
-
Oh, and we should probably point out, there is more than one mausberry circuit. I have the DIY button version which is supposed to work with either a toggle or a momentary switch. I use a momentary, so there is no ON or OFF position. It is just a trigger.
-
I have the latest, not DIY. I have a toggle switch attached for on/off and a momentary for reset. The reset will be attached not near the on/off switch, so no (probable) chance that someone presses that by accident. Because it does a hard reset and could potentially corrupt ts sd yes.
-
@caver01 said in Replacing Mausberry Script with a Python script:
@cyperghost Agreed. I too was surprised that it gets stuck the way it does during a soft shutdown.
:D
In any case, your suggestion about calling the POWERBLOCK script seems like a nice shortcut. Why aren't we doing that with the BASH script?
Well, that's a historical thing. I was asked "How can ES be proper shutdown by button switch?" so it was clear to implent the code directly into the mausberry script. Up to v1.2 the script was only able to switch off ES
A few days later the code part to end all emulators that were called via runcommand was also added to the mausberry script mainly by @meleu. A few days later annother one asked for powerdown script for the PowerBlock and this is till now the "bare" form of this script that can be called just one time by any other program.
I think it's easier to tell people... just edit your mausberry shutdown with
sudo nano ....
and add some lines.
To tell them "call annother script" there must be knowledge how the other script must be saved in the right place and it must be made executable and it must be called maybe via sudo.
There are other possibilites. Take a look to gpio-watch. This C written program is also able to use edge detection by button press and will call a script named by gpio button. It looks like it works for input and output events. So the mausberry script will just initiate all GPIOs and at last will call gpio-watch for a triggered event. If there is a trigger the shutdown script will be initiated ;)
EDIT: I bought 2 of the mausberry DIY today ;)
-
@cyperghost so, there are several possible options. I guess I’d like to simply see the last version of the Mausberry script evolve into a self-contained python variant.
-
@cyperghost said
EDIT: I bought 2 of the mausberry DIY today ;)
Did you get a couple of NPN transistors too?
-
@caver01 I have a few of transistors here but don't know what type they are. Have to look into the datasheets first. I would rather use the optofets as I know they work surely.
Your solution is also good and it's much much cheaper ;)
For the sript... I see two approches
- Use the inotify-tool - I have really no practice in this! So an advanced coder should introduce
- A script that checks evidence of "echo 1 > ..."-command within
runcommand.sh
emulationstation.sh
. That's very easy to code with help of thesed
command
-
@cyperghost
I don’t think we need inotify when that wasn’t required for the BASH script.As for number 2, I don’t follow. Why can’t we just borrow the same logic from the excellent updated Mausberry script and use python code instead of BASH? I just don’t know if I can simply embed the commands and reproduce the logic not knowing python myself.
-
@caver01 The main challenge is, that you need the event of quitting ES trapped.
The bash script with the 1 second loop will surly miss that. We are talking about a general solution with usecase for everyone. But now I get why you were confusd. I wroteruncommand.sh
... I ment/opt/retropie/supplementary/emulationstation/emulationstation.sh
So imho for now the best way is to put the echo-command in the
emulationstation.sh
. With help withgrep
-command you can check existance and withsed
-command you can write it in the script. Because this script is executed if ES ends and works flawless but as already mentioned your changes will get lost if ES is updated.I know @meleu is working on a solution for this so keep prepared! But it'll be surly better than simply putting some commands with
sed/grep
I just don’t know if I can simply embed the commands and reproduce the logic not knowing python myself.
That should work. But I'm the wrong person here to ask for.
But python is far better as it uses edge_detection. -
@cyperghost
Agreed. So, next step for me is to start to learn a little python. -
@caver01 So you see.... the solution is there ;) So you can stick on your python script and it's edge detection :)
-
@cyperghost Yes. I may work on this a little bit today. It's great to collaborate! Thanks for your help!
-
@caver01 Here is an very intersting thread https://retropie.org.uk/forum/topic/12930
-
This is great!
I'm just finishing up my Mausberry mod to fit a NESPi case. The reset switch has been removed. For those who doesn't know, it is a function of the Atmel Tiny25 chip, you just reset the chip directly, effectively risking corrupting the card, which is why I won't use it.
But I've connected the Reset button on my NESPi to GPIO instead, so it can be used for something more useful. F.ex. one click = reset ROM, double click = reboot etc.
Excellent to see the script being transferred to Python rather than BASH, I'm a super-newbie with Python, but interrupts vs polling is why I started searching for something else than the Mausberry script. Also the possibilities of Python in general, to be able to use the script for more functions.
-
@diy_glenn There have been a few developments for me regarding this idea. Aside from the excellent work that was done to make the original BASH script more robust for RetroPie (meaning, it is capable of safely exiting emulators and Emulation Station to save metadata), recent developments in this thread allows us to revert to a simpler version and removes the need to reproduce the new BASH script in python.
What @meleu has built in that thread is basically a service that does nothing while running but stays resident so that upon shutdown (any shutdown) it triggers a script that gracefully exits emulators and ES. He's taken the customization out of the hands of a mausberry script (or any shutdown solution for that matter) whether you are using BASH or python. It no longer matters because his service and its accompanying
killes.sh
script handles the graceful closure automatically.Once setup (and crucially, once you chmod the
killes.sh
as executable) the original mausberry script can be used. Moreover, we can switch to the python script I shared in the original post above and still enjoy the benefits of graceful exit of ES.One tiny fly in the ointment is that the mausberry circuit seems to be blind to software-initiated shutdowns. While this allows reboots to occur unhindered, it is inconvenient when a user selects QUIT>SHUTDOWN from the UI. The circuit does not start watching its GPIO for power drop until AFTER it detects a button push.
My workaround for this is to add a "trigger" command to @meleu's
killes.sh
which "presses" the mausberry switch! Basically, I am exporting a GPIO pin and raising the value to 1. Attached to the pin, I have a 1k-ohm resistor, then the base of an NPN transistor. The collector and emitter of the transistor are connected to the mausberry switch poles (in parallel with the actual switch). SO. . . when the GPIO goes HIGH, that triggers the transistor to short the switch poles (like pressing the button). This in turn gets the mausberry circuit to start watching its own GPIO input to go LOW (because the Pi is shutting down). When it sees the Pi is down, it cuts power.The fallout, for now, is that I cannot differentiate between a shutdown and a software reboot. In either case, the
killes
service ends, the script executes, and my transistor presses the button. That means REBOOT always results in POWEROFF. I am OK with that right now. -
-
One other development for me is that I am running a couple of GPIO-connected pushbuttons on my cabinet already. I use these to trigger volume commands handled by yet another python script. I am using GPIOneer, but I will soon be switching to its replacement software, GPIOnext. This is a great little driver for arcade control input, and even though I have an IPAC, it is very handy to trigger commands, not just keystrokes.
With the
killes
service working, my plan is to drop the use of a mausberry script completely and shift the responsibility over to GPIOnext. A clever observation by the developer revealed that there is no need to script the input for the Mausberry circuit. The script sets up a pin to be monitored, but you could just as easily connect the IN lead to the 3.3v rail (GPIO header pins 1, or 17 for example). When the Pi shuts down, that rail will drop, and the mausberry circuit (if paying attention after a button press) will see that the Pi has shutdown.The other half of the mausberry script is trivial. You can simply define a shutdown command in GPIOnext triggered by the GPIO you connect to the OUT lead the mausberry circuit.
-
@caver01 Please, tell me what would be the commands you want to execute at shutdown and the commands you want to execute at reboot (that
echo "$GPIOpin" > /sys/class/gpio/export
stuff).[BTW, I'm getting crazy with we discussing the same issue in several different threads! :-) ]
-
@meleu said in Replacing Mausberry Script with a Python script:
[BTW, I'm getting crazy with we discussing the same issue in several different threads! :-) ]
I am with ya. Just trying to respect the original posts in most cases. Anyway. . .
Please, tell me what would be the commands you want to execute at shutdown and the commands you want to execute at reboot (that echo "$GPIOpin" > /sys/class/gpio/export stuff).
This is what has to be executed to trigger the transistor for a soft shutdown:
echo 18 > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio18/direction echo "1" > /sys/class/gpio/gpio18/value
The first two lines can be invoked at startup (which I do). They are necessary because you have to export the pin and set its direction before you can write a value to it. The third line can be placed in a different script and must wait until the moment a shutdown is requested.
For a reboot, NONE. No additional code needed (nor wanted), as you want the circuit to continue to deliver power during a reboot.
-
I also abstracted the Pin# in my actual code with stuff like:
GPIOpin=18
then replacing "18" in the code above with$GPIOpin
. You get the idea. It just makes it easier to change 18 in one place at the beginning of the script than in multiple commands.
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.