Picade X HAT USB-C with Python not RetroPie

Hello, I have just bought the Picade X HAT and I am using it with a Raspberry 3. With RetroPie all is great, but I want to get key presses in Python programs (same machine so keys bound etc).

I can’t find a simply Python example of how to detect PICADE X HAT keys e.g. joystick moves, in a Python program, so could someone point me in the right direction please?

Thanks, Nick

I do stuff like this to detect a button press to do a shutdown. My button momentarily grounds GPIO 24 when pressed.

import RPi.GPIO as GPIO

def Shutdown(channel):  
    os.system("sudo shutdown now -P")
    time.sleep(30)


GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(24, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

This pinout will tell you what button uses what GPIO pin.
Picade X HAT at Raspberry Pi GPIO Pinout

That’s helpful thanks. I was thinking of using the PICADE python drivers and getting stuck with that, but this is simpler.

There’s always one more question ;)

I can control the PICADE HAT with GPIO as above but since I cannot find a legal Space Invaders ROM for RetroPie, I need to develop this more.

I have found a very good Python Space Invaders program on github but it uses key.get_pressed() and event.get(), KEYDOWN, etc to detect keys. That’s fine for the keyboard but I also want detect the PICADE HAT joystick and Fire button.

So I think I either need to:

  1. Be able to mix get events with GPIO (first attempt at a GPIO detector to stuff the keyboard didn’t work)
  2. Ignore RetroPie and just install the PICADE HAT and use dtoverlay to map the joystick etc - that works when RetroPie is installed but not otherwise.
  3. Experiment another way…

The essentials are that I have a Raspberry Pi 3 with PICADE HAT, and the joystick, buttons and cables. I also have a Python 3 program which is expecting space to fire and left/right keys to move, and I want to have an alternative of the joystick and a button for those.

I will continue to experiment but if anyone has any helpful insights, I would be grateful.

Thanks, Nick