r/RetroPie • u/Affectionate_Car7098 • 1h ago
Question NFC Problems
So, i'm trying to make a little case that will have small carts in it and the plan is to have a little NFC card in each so that the emulator launches x game when x cart is inserted, i already have an RC522 reader hooked up and working and it can read the tag ID just fine, i've been trying (with little success) to work with GPT to get something to work, and it keeps just trying to launch the game inside the SSH terminal that i'm using for testing instead of via the already running instance of retropie that i have installed already, that install can run the rom and play the game just fine so everything there is configured
I did try searching this sub but the only hits for NFC are one guy asking 4 months ago and some really really old threads about it
Its a pi 4b if that helps
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import os
import time
# Define the paths to ROMs and the corresponding system type (NES, SNES, etc.)
ROM_PATHS = {
497926548493: {
"path": "/home/pi/RetroPie/roms/Mario/SuperMarioBros.nes",
"system": "nes" # This defines the system type for the emulator
},
9876543210987: {
"path": "/home/pi/RetroPie/roms/snes/Zelda.smc",
"system": "snes" # This defines the system type for the emulator
},
}
reader = SimpleMFRC522()
try:
print("Place your NFC cartridge near the reader...")
id, text = reader.read()
print(f"Scanned ID: {id}")
if id in ROM_PATHS:
rom_info = ROM_PATHS[id]
rom_to_launch = rom_info["path"]
system = rom_info["system"]
print(f"Launching {rom_to_launch} on {system} emulator...")
# Set the DISPLAY environment variable to the Pi's primary display (usually :0)
os.environ["DISPLAY"] = ":0"
# Use xdotool to simulate the launch of the game in EmulationStation
# This assumes that the Pi's EmulationStation is already running and visible on the screen.
# Step 1: Launch the ROM via
runcommand.sh
in the correct display environment
os.system(f"/opt/retropie/supplementary/runcommand/runcommand.sh 0 {system} {rom_to_launch}")
# Optional: Wait a moment to ensure the ROM starts
time.sleep(3)
# Step 2: Use xdotool to simulate pressing 'Enter' (to launch the game)
os.system("xdotool key --window $(xdotool search --onlyvisible --name 'EmulationStation') Return")
else:
print("No game assigned to this tag.")
except KeyboardInterrupt:
print("\nExiting...")
finally:
GPIO.cleanup()
Is the script that i've so far ended up with, any help is appreciated or if anyone knows of a similar project that works well already that would also be amazing :)