Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #for usb joystick support
- import RPi.GPIO as GPIO
- import time
- import uinput
- import pygame
- pygame.init()
- buttons = (uinput.KEY_DOWN, uinput.KEY_UP, uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_ENTER, uinput.KEY_BACK)
- device = uinput.Device(buttons)
- time.sleep(2)
- joystick = pygame.joystick.Joystick(1)
- joystick.init()
- name = joystick.get_name()
- #print(name)
- #GPIO.setmode(GPIO.BCM)
- #GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) #RST Button
- #GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP) #PWR Button
- def leftButton_call():
- print "LEFT Button pressed"
- device.emit_click(uinput.KEY_LEFT)
- def rightButton_call():
- print "RIGHT Button pressed"
- device.emit_click(uinput.KEY_RIGHT)
- def upButton_call():
- print "UP Button pressed"
- device.emit_click(uinput.KEY_UP)
- def downButton_call():
- print "DOWN Button pressed"
- device.emit_click(uinput.KEY_DOWN)
- def AButton_call():
- print "A Button pressed"
- device.emit_click(uinput.KEY_ENTER)
- def BButton_call():
- print "B Button pressed"
- device.emit_click(uinput.KEY_BACK)
- #GPIO.add_event_detect(22, GPIO.RISING, callback=RstButton_call, bouncetime=300)
- #GPIO.add_event_detect(25, GPIO.RISING, callback=PwrButton_call, bouncetime=300)
- while (1==1):
- for event in pygame.event.get():
- if event.type == pygame.JOYBUTTONDOWN:
- buttonAfull = str(joystick.get_button(1))
- buttonBfull = str(joystick.get_button(3))
- buttonAVal = buttonAfull[-1:]
- buttonBVal = buttonBfull[-1:]
- if buttonAVal == "1":
- AButton_call()
- if buttonBVal == "1":
- BButton_call()
- # print(buttonAVal)
- # print(buttonBVal)
- if event.type == pygame.JOYAXISMOTION:
- buttonRIGHTfull = joystick.get_axis(0)
- buttonDOWNfull = joystick.get_axis(1)
- if buttonRIGHTfull > 0:
- rightButton_call()
- if buttonRIGHTfull < 0:
- leftButton_call()
- if buttonDOWNfull > 0:
- downButton_call()
- if buttonDOWNfull < 0:
- upButton_call()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement