""" rpi-gpio-SNES-commands.py by Douglas Gazineu 4 Oct 2012 GPIO Keyboard driver for Raspberry Pi for use with external buttons on SNES mod based on rpi-gpio-jstk.py by Chris Swan based on python-uinput/examples/joystick.py by tuomasjjrasanen https://github.com/tuomasjjrasanen/python-uinput/blob/master/examples/joystick.py requires uinput kernel module (sudo modprobe uinput) requires python-uinput (git clone https://github.com/tuomasjjrasanen/python-uinput) requires python RPi.GPIO (from http://pypi.python.org/pypi/RPi.GPIO/0.3.1a) for detailed usage see http://blog.thestateofme.com/2012/08/10/raspberry-pi-gpio-joystick/ Changes 19 Aug 2012 - inputs set to use internal pull ups rather than external 10k resistors """ import uinput import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) pin_p = 10 pin_h = 11 pin_f2 = 12 pin_f3 = 13 pin_f6 = 15 pin_f7 = 16 pin_esc = 18 # esc, h, f2, f3, f6, f7, p GPIO.setup(pin_esc, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_h, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_f2, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_f3, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_f6, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_f7, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin_p, GPIO.IN, pull_up_down=GPIO.PUD_UP) events = (uinput.KEY_ESC, uinput.KEY_H, uinput.KEY_F2, uinput.KEY_F3, uinput.KEY_F6, uinput.KEY_F7, uinput.KEY_P) device = uinput.Device(events) # Bools to keep track of movement key_esc = False key_h = False key_f2 = False key_f3 = False key_f6 = False key_f7 = False key_p = False while True: if (not key_esc) and (not GPIO.input(pin_esc)): # ESC button pressed key_esc = True device.emit(uinput.KEY_ESC, 1) if key_esc and GPIO.input(pin_esc): # ESC button released key_esc = False device.emit(uinput.KEY_ESC, 0) if (not key_h) and (not GPIO.input(pin_h)): # H button pressed key_h = True device.emit(uinput.KEY_H, 1) if key_h and GPIO.input(pin_h): # H button released key_h = False device.emit(uinput.KEY_H, 0) if (not key_f2) and (not GPIO.input(pin_f2)): # F2 button pressed key_f2 = True device.emit(uinput.KEY_F2, 1) if key_f2 and GPIO.input(pin_f2): # F2 button released key_f2 = False device.emit(uinput.KEY_F2, 0) if (not key_f3) and (not GPIO.input(pin_f3)): # F3 button pressed key_f3 = True device.emit(uinput.KEY_F3, 1) if key_f3 and GPIO.input(pin_f3): # F3 button released key_f3 = False device.emit(uinput.KEY_F3, 0) if (not key_f6) and (not GPIO.input(pin_f6)): # F6 button pressed key_f6 = True device.emit(uinput.KEY_F6, 1) if key_f6 and GPIO.input(pin_f6): # F6 button released key_f6 = False device.emit(uinput.KEY_F6, 0) if (not key_f7) and (not GPIO.input(pin_f7)): # F7 button pressed key_f7 = True device.emit(uinput.KEY_F7, 1) if key_f7 and GPIO.input(pin_f7): # F7 button released key_f7 = False device.emit(uinput.KEY_F7, 0) if (not key_p) and (not GPIO.input(pin_p)): # P button pressed key_p = True device.emit(uinput.KEY_P, 1) if key_p and GPIO.input(pin_p): # P button released key_p = False device.emit(uinput.KEY_P, 0) time.sleep(.03) # Poll every 30ms (otherwise CPU load gets too high)