Arcade – How to switch from MagicMirror to RetroPie using a push button connected to a GPIO pin

Arcade – How to switch from MagicMirror to RetroPie using a push button connected to a GPIO pin

Recently I have shown how to launch MagicMirror from RetroPie. You can check it out here! I also have shown how to switch back to RetroPie from MagicMirror using USB joystick buttons. Because I wanted to turn off the LEDs from the buttons while using MagicMirror I needed to disable power to the USB ports. I could do that by disconnecting one push button from the joystick hardware driver and connecting directly to a GPIO pin from Raspberry Pi. When pressed this button quits MagicMirror application, ends LXDE desktop, enables the power to the USB ports and goes back to RetroPie.

Before all that, If you want to install RetroPie in your Raspberry Pi just follow this link. You can also install MagicMirror by following the installation manual.

First you have to create a bash script called push_button.sh, to verify if the button was pressed, and save it in /home/pi/Documents. At the code, first the pin 11 (GPIO 17) is configured as input. After that there is an infinity loop where it is checked if the button was pressed. If the button was pressed at least 1 second, the loop is broken, the power to the USB ports is restored, the LXDE desktop ends and the RetroPie is launched again.

#!/bin/bash

# Set the pin for input
gpio -1 mode 11 in

while true; do
        # Set the variable through command substitution
        b=$(gpio -1 read 11)
        if (( b == 1 )); then
                sleep 1
                # Set the variable through command substitution
                b=$(gpio -1 read 11)
                if (( b == 1 )); then
                        break
                fi
        fi
done

echo '1-1' | sudo tee /sys/bus/usb/drivers/usb/bind
killall xinit

Note that I have used WiringPi library to read an input pin from Raspberry Pi. But there is no more official development of this repository. So you ask me why did I use it? Just because four years ago I bought a 50 book of Raspberry Pi and, finally when I used the book for the first time, I saw that this library was recommended there. So I had to justify my investment, but you can choose other way to manipulate GPIO pins.

To install WiringPi just type the following code at command line. The gpio command will be installed at /usr/local/bin.

$ sudo apt-get install git-core
$ git clone https://github.com/WiringPi/WiringPi.git
$ cd WiringPi
$ ./build

The gpio readall command shows the 40-pin GPIO header with description of the pins. You can see bellow the header of my Raspberry Pi 3 Model B that I’m using as hardware for my Arcade.

 $ gpio readall
 +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+

Check it out again how to launch MagicMirror automatically from RetroPie here. Just remember to write mirror.desktop at /home/pi/.config/autostart (to run at every boot) and to write mirror.sh at home/pi/Documents (it’s called by mirror.desktop and it calls MagicMirror). Now we just have to modify mirror.sh to disable power to the USB ports before calling MagicMirror as shown bellow.

#!/bin/bash

echo 1-1 | sudo tee /sys/bus/usb/drivers/usb/unbind
cd /home/pi/MagicMirror
DISPLAY=:0 npm start

Now it’s missing the part where the push_button.sh script is called. Save the code bellow as push_button.desktop. This file is going call the bash script push_button.sh saved in /home/pi/Documents.

[Desktop Entry]
Type=Application
Name=push_button
Exec=/home/pi/Documents/push_button.sh

Don’t worry! We are almost finishing. The Pi screen will eventually go into sleep mode and we want to prevent that now that the USB joystick is disable. To do that type the following code.

$ sudo nano ~/.config/lxsession/LXDE-pi/autostart

And save autostart file as bellow.

@xset s 0 0
@xset s noblank
@xset s noexpose
@xset dpms 0 0 0

Finally our journey has come to an end!! You can use push buttons connected to GPIO pins together with USB joystick buttons to implement a variety of tasks. Sky’s the limit!

I hope you enjoyed and learned something new! See you around!! 🙂


Arcade – How to switch from MagicMirror to RetroPie using Joystick Buttons

Recently I have shown how to launch MagicMirror from RetroPie. You can check it out here! Now I want to show the opposite: How to access RetroPie from MagicMirror (running on LXDE desktop) using USB joystick buttons (by replacing mouse and keyboard). It’s possible to quit MagicMirror and to end LXDE desktop and go back to RetroPie by creating custom keyboard shortcuts and mapping them to the joystick buttons.

Before that, If you want to install RetroPie in your Raspberry Pi just follow this link. You can also install MagicMirror by following the installation manual.

You have to modify lxde-pi-rc.xml, a file that holds shortcuts settings, saved in /etc/xdg/openbox/. I recommend to create a folder called openbox at /home/pi/.config/ and save a copy there. This way the configuration file will run locally instead of globally and you can modify it completely. You can go back to the original configuration by deleting the local file so the untouched global file can be used.

$ cd /home/pi/.config
$ mkdir openbox
$ cd openbox
$ cp /etc/xdg/openbox/lxde-pi-rc.xml
$ nano lxde-pi-rc.xml

Now you can edit the file. First you search for keybindings for running applications (you can get there by typing CTRL+W and writing “Keybindings”), then you can add code for other shortcuts. At the example I added a shortcut for CTRL+A (key=”C-a”), to execute the mirror.sh script that starts MagicMirror, and for CTRL+C (key=”C-c”), to end LXDE desktop and go back to RetroPie. It’s good to remember that these shortcuts only work on the LXDE desktop. If you are logging into the command prompt via SSH, for example, it will not work.

<!-- Keybindings for running applications -->
<keybind key="C-c">
	<action name="Execute">
    	<command>killall xinit</command>
	</action>
</keybind>
<keybind key="C-a">
	<action name="Execute">
		<command>bash /home/pi/Documents/mirror.sh</command>
	</action>
</keybind>

You can write mirror.sh code saved in /home/pi/Documents as described bellow.

#!/bin/bash

cd /home/pi/MagicMirror
DISPLAY=:0 npm start

Don’t forget to make mirror.sh executable by tipping the following code at command line.

$ sudo chmod +x /home/pi/Documents/mirror.sh

To use your USB joystick as a keyboard you have to install the joystick input driver and the joystick configuration package. To test and calibrate the gamepad you can run jstest-gtk.

$ sudo apt-get install joystick
$ sudo apt-get install jstest-gtk
$ jstest-gtk

The next step is to install QJoyPad. This way you can map some keys of the keyboard to your joystick buttons.

$ sudo apt-get install qjoypad

Note that I saved my settings as Arcade and I mapped the keys CTRL, Q, A and C. So now I can use my joystick to start MagicMirror (CTRL+A), to quit an application (CTRL+Q) and to quit the desktop and go back to RetroPie (CTRL+C). You can map other keys to run any application that you want.

Now you have to guarantee that QJoyPad is executed automatically as soon as the desktop starts. First you have to create a file to call this application. Let’s call the file qpad.desktop and save it in /home/pi/.config/autostart to run at every boot. You’ll have to create the autostart folder if it doesn’t exist.

$ cd /home/pi/.config/
$ mkdir autostart
$ cd autostart
$ nano qpad.desktop

Save the code bellow as qpad.desktop. Note the last code line that you have to pass your saved settings as parameter (Arcade).

[Desktop Entry]
Type=Application
Name=qpad
Exec=qjoypad "Arcade"

Finally you can go back to RetroPie from MagicMirror!! If you want to do the same thing but using instead a push button connected to a GPIO pin from the Raspberry Pi, click here! This way you can also disable the USB ports to save energy.

I hope you enjoyed and learned something new! See you around!! 🙂


Arcade – How to launch MagicMirror from RetroPie

I’m new to Raspberry Pi and I have recently built an Arcade with it, using an old monitor and 2 Player Joystick Buttons. Now I’m trying to explore the full potential of the Raspberry Pi and I wanted to launch the MagicMirror application from RetroPie.

My version of RetroPie is comit f90600 (Date: Jul 30 2019) and I can see that by typing this at the command line:

$ cd ~/RetroPie-Setup
$ git show

If you want to install RetroPie in your Raspberry Pi just follow this link. You can also install MagicMirror by following the installation manual.

Getting back to the subject, first you have to install PIXEL desktop environment (to run MagicMirror from there) by accessing RetroPie menu -> RetroPie SETUP -> Configuration / tools -> raspbiantools – Raspbian related tools -> Install Pixel desktop environment. After that you can access desktop from the ports menu.

To start MagicMirror automatically first you have to create a file to call a bash script that executes MagicMirror (you must have installed it before). Let’s call the file mirror.desktop and save it in /home/pi/.config/autostart to run at every boot. You’ll have to create the autostart folder if it doesn’t exist.

$ cd /home/pi/.config/
$ mkdir autostart
$ cd autostart
$ nano mirror.desktop

Save the code bellow as mirror.desktop. This file is going call the bash script mirror.sh saved in /home/pi/Documents.

[Desktop Entry] 
Type=Application
Name=mirror
Exec=/home/pi/Documents/mirror.sh

You can write mirror.sh code as described bellow.

#!/bin/bash

cd /home/pi/MagicMirror
DISPLAY=:0 npm start

Don’t forget to make mirror.sh executable by tipping the following code at command line.

$ sudo chmod +x /home/pi/Documents/mirror.sh

Yes!!! Finally the MagicMirror starts automatically from the RetroPie!!!! But… What about going back to RetroPie from MagicMirror, without a mouse or a keyboard, just using the joystick controller. I’ve tried two different approaches to end the user session and you can check it out by clicking the following links: using USB joystick buttons and using a push button connected to GPIO.

I hope you enjoyed and learned something new! See you around!! 🙂