Created
April 5, 2025 17:28
-
-
Save andreer/fece840cfa68dfe5ac83aee2cae23d4d to your computer and use it in GitHub Desktop.
A simple python script to read raspberry pi GPIO pins connected to a 4-way joystick / 3 pushbuttons, and turn this into keyboard presses/releases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| import RPi.GPIO as g | |
| import time | |
| import signal | |
| import uinput | |
| import sys | |
| #### | |
| #### KEY SETTINGS | |
| #### | |
| keyA = uinput.KEY_Z | |
| keyB = uinput.KEY_X | |
| keyC = uinput.KEY_ESC | |
| #### | |
| #### | |
| #### | |
| # remember to "modprobe uinput" before running! | |
| # if you don't have permission to the device, | |
| # run this script with sudo | |
| def signal_handler(sig, frame): | |
| print('You pressed Ctrl+C! Cleaning up GPIOs ...') | |
| g.cleanup() | |
| print('... Done.') | |
| sys.exit(0) | |
| signal.signal(signal.SIGINT, signal_handler) | |
| hi = g.HIGH | |
| lo = g.LOW | |
| print("pretending to be a keyboard ...") | |
| g.setmode(g.BOARD) | |
| pins = [ 7, 11, 13, 15, 38, 32, 24 ] | |
| up = 7 | |
| left = 13 | |
| right = 11 | |
| down = 15 | |
| for pin in pins: | |
| g.setup(pin, g.IN, pull_up_down=g.PUD_UP) | |
| def read(ch): | |
| return g.input(ch) | |
| def write(ch, val): | |
| g.setup(ch, g.OUT, initial=val) | |
| # make ground for buttons | |
| write(36, lo) | |
| write(31, lo) | |
| write(23, lo) | |
| lastup = 1 | |
| lastleft = 1 | |
| lastright = 1 | |
| lastdown = 1 | |
| lastA = 1 | |
| lastB = 1 | |
| lastC = 1 | |
| with uinput.Device([uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_UP, uinput.KEY_DOWN, uinput.KEY_1, uinput.KEY_2, uinput.KEY_3, uinput.KEY_ESC, uinput.KEY_SPACE, uinput.KEY_ENTER, uinput.KEY_Z, uinput.KEY_X]) as keyb: | |
| while True: | |
| vup = read(up) | |
| vleft = read(left) | |
| vdown = read(down) | |
| vright = read(right) | |
| vA = read(38) | |
| vB = read(32) | |
| vC = read(24) | |
| if vleft != lastleft: | |
| keyb.emit(uinput.KEY_LEFT, lastleft) | |
| event = "press" | |
| if lastleft == 0: | |
| event = "release" | |
| #print("emit", event, "left") | |
| lastleft = vleft | |
| if vright != lastright: | |
| keyb.emit(uinput.KEY_RIGHT, lastright) | |
| event = "press" | |
| if lastright == 0: | |
| event = "release" | |
| #print("emit", event, "right") | |
| lastright = vright | |
| if vup != lastup: | |
| keyb.emit(uinput.KEY_UP, lastup) | |
| event = "press" | |
| if lastup == 0: | |
| event = "release" | |
| #print("emit", event, "up") | |
| lastup = vup | |
| if vdown != lastdown: | |
| keyb.emit(uinput.KEY_DOWN, lastdown) | |
| event = "press" | |
| if lastdown == 0: | |
| event = "release" | |
| #print("emit", event, "down") | |
| lastdown = vdown | |
| if vA != lastA: | |
| keyb.emit(keyA, lastA) | |
| event = "press" | |
| if lastA == 0: | |
| event = "release" | |
| #print("emit", event, "A") | |
| lastA = vA | |
| if vB != lastB: | |
| keyb.emit(keyB, lastB) | |
| event = "press" | |
| if lastB == 0: | |
| event = "release" | |
| #print("emit", event, "B") | |
| lastB = vB | |
| if vC != lastC: | |
| keyb.emit(keyC, lastC) | |
| event = "press" | |
| if lastC == 0: | |
| event = "release" | |
| #print("emit", event, "C") | |
| lastC = vC | |
| time.sleep(0.01) | |
| g.cleanup() | |
| print(g.RPI_INFO) | |
| print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment