Arcade – How to switch from MagicMirror to RetroPie using a push button connected to a GPIO pin
Category : Arcade Raspberry Pi
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!! 🙂
Recent Comments