小时候玩儿任天堂的游戏的时候,如果想退出游戏,直接按reset键就出来了,可以重新选择,可是我现在用的是retropie模拟任天堂,那么问题来了,如果要映射热键去跳出游戏,默认官方已经设置了select+start的热键是针对LR系列的模拟器,如果是LR系列的模拟器,这样一起按就可以退出游戏回到es界面,但是对于其他的模拟器:如DOSBox、Mupen64plus、DGen、PPSSPP、MAME4ALL、AdvanceMAME等其他非LR系列的模拟器,就不适用于RetroArch的的热键了,这时候要回到ES系統首页,就非常麻烦,所以就可以做一款通过硬件的reset回到ES首页的功能。
硬件需求:
-------------------
1 x 非自锁开关1个
杜邦线若干
树莓派 2B/3B/3B+/3A+/zero /zero w 均可
1x 16GB class 10 SD卡
1x HDMI 线
1x 5v/2.5A电源
---------------------
然后,烧录好retropie系统,并且联网更新,打开ssh, 基本配置搞定了再来接下来的操作。
利用了GPIO的37 pin和39pin ,分别代表 GPIO26和GND
然后软件配置更简单:
1. ssh远程连接到树莓派。
2. 建立一个目录
sudo mkdir scripts
cd scripts/
sudo touch ExitEmu.py
3. 编辑这个文件然后填写:
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # 使用BCM的命名方式
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def exitEmulator(channel):
print('exitEmulator')
os.system('killall retroarch')
pids = [pid for pid in os.listdir('/proc') if pid.isdigit() ]
for pid in pids:
try:
commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
if commandpath[0:24] == '/opt/retropie/emulators/':
os.system('kill -QUIT %s' % pid)
print('kill -QUIT %s' % pid)
except IOError:
continue
GPIO.add_event_detect(26, GPIO.RISING, callback=exitEmulator, bouncetime=500)
while True:
sleep(10)
保存退出后直接执行测试一下:
sudo python ExitEmu.py
然后按下按钮,如果看到屏幕上出现: exitEmulator 及 retroarch两行字符串代表指令成功了。
接下来让它能够开机自动启动就好了。
直接编辑:/etc/rc.local,添加刚才的脚本。
sudo vim.tiny /etc/rc.local
在exit 0之前填写:
sudo python /home/pi/scripts/ExitEmu.py &
然后保存退出后重启你的树莓派,就可以尝试使用你的按键了。是不是感觉非常方便呢?
参考:
* https://forum.recalbox.com/topic/3504/emulator-exit-button-gpio/10
* https://www.element14.com/community/docs/DOC-78055/l/adding-a-shutdown-button-to-the-raspberry-pi-b
* https://learn.adafruit.com/retro-gaming-with-raspberry-pi/adding-controls-software?view=all
* https://www.reddit.com/r/raspberry_pi/comments/2yw4fn/finally_set_up_retropie_complete_with_a_gpio/
* http://raspberrypi.stackexchange.com/questions/40311/how-can-i-add-an-on-off-switch-to-my-raspberry-pi-2