Advertisement
Guest User

gamepad_Keyboard.py

a guest
Jan 2nd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #for usb joystick support
  2.  
  3. import RPi.GPIO as GPIO
  4. import time
  5. import uinput
  6. import pygame
  7.  
  8. pygame.init()
  9.  
  10. buttons = (uinput.KEY_DOWN, uinput.KEY_UP, uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_ENTER, uinput.KEY_BACK)
  11. device = uinput.Device(buttons)
  12. time.sleep(2)
  13. joystick = pygame.joystick.Joystick(1)
  14. joystick.init()
  15. name = joystick.get_name()
  16. #print(name)
  17.  
  18.  
  19. #GPIO.setmode(GPIO.BCM)
  20.  
  21. #GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP) #RST Button
  22. #GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP) #PWR Button
  23.  
  24. def leftButton_call():
  25.         print "LEFT Button pressed"
  26.         device.emit_click(uinput.KEY_LEFT)
  27.  
  28. def rightButton_call():
  29.         print "RIGHT Button pressed"
  30.         device.emit_click(uinput.KEY_RIGHT)
  31.  
  32. def upButton_call():
  33.         print "UP Button pressed"
  34.         device.emit_click(uinput.KEY_UP)
  35.  
  36. def downButton_call():
  37.         print "DOWN Button pressed"
  38.         device.emit_click(uinput.KEY_DOWN)
  39.  
  40. def AButton_call():
  41.         print "A Button pressed"
  42.         device.emit_click(uinput.KEY_ENTER)
  43.  
  44. def BButton_call():
  45.         print "B Button pressed"
  46.         device.emit_click(uinput.KEY_BACK)
  47.  
  48. #GPIO.add_event_detect(22, GPIO.RISING, callback=RstButton_call, bouncetime=300)
  49. #GPIO.add_event_detect(25, GPIO.RISING, callback=PwrButton_call, bouncetime=300)
  50.  
  51. while (1==1):
  52.         for event in pygame.event.get():
  53.                 if event.type == pygame.JOYBUTTONDOWN:
  54.                         buttonAfull = str(joystick.get_button(1))
  55.                         buttonBfull = str(joystick.get_button(3))
  56.                         buttonAVal = buttonAfull[-1:]
  57.                         buttonBVal = buttonBfull[-1:]
  58.                         if buttonAVal == "1":
  59.                                 AButton_call()
  60.                         if buttonBVal == "1":
  61.                                 BButton_call()
  62. #                       print(buttonAVal)
  63. #                       print(buttonBVal)
  64.                 if event.type == pygame.JOYAXISMOTION:
  65.                         buttonRIGHTfull = joystick.get_axis(0)
  66.                         buttonDOWNfull = joystick.get_axis(1)
  67.                         if buttonRIGHTfull > 0:
  68.                                 rightButton_call()
  69.                         if buttonRIGHTfull < 0:
  70.                                 leftButton_call()
  71.                         if buttonDOWNfull > 0:
  72.                                 downButton_call()
  73.                         if buttonDOWNfull < 0:
  74.                                 upButton_call()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement