Replacing Mausberry Script with a Python script
-
@barrymossel Well the improvement will also be hardware based. As the mausberry dev rather sends the switch signal to the Pie than expects to be received from the Pie it is a bit of soldering to get a full working switch. But as I'm not a circuit designer ... I can't blame anyone. But I never expected that the Mausberry will not really shutdown if you choose a software powerdown as it is also a "everyday" usecase and not specially for the RetroPie.
So this issue could be solved by automatically shutdown if there is no GPIOPin2 feed from the Pie after 30seconds but therefore the Mausberry designer should be informed or read here in the forum ;)
-
@cyperghost Agreed. I too was surprised that it gets stuck the way it does during a soft shutdown.
I seem to recall reading somewhere that a long press (5 seconds?) of the power switch forces it to cut power either way. It doesn't work for me now, but I swear I was able to do it at one point. In any case, I would guess that the designer chose to do it this way. . . ignore the input signal until the hardware button has been pressed.
It seems like the better design would be wait for the input to become a "1", and and then start waiting for it to become zero, regardless of the power switch. Once it sees a "1", it should know that it was generated by the script, so the Pi must be running. If that value drops, the Pi must have shutdown.
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?
<EDIT> I realize now that this suggested approach would have the unfortunate effect of converting all reboots into shutdowns.
-
@cyperghost Also, regarding the design, it could not have been built just to receive a signal from the Pi. It has to generate the power on, which the Pi can't do if it has no power. Part of what the mausberry circuit solves is that it completely cuts power to the Pi after the save shutdown.
-
I thought switching the power switch to off and then reset would work when you turn off through the menu. Not sure though. I will never use the menu option anyway, but if someone else in my house does, the reset could work I think.
-
@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. -
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.