Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import pygame
  4. from pygame import joystick
  5. from pygame import event
  6. from pygame.locals import *
  7. import sys
  8. import os
  9.  
  10. rotate_script= "~/rotate.sh"
  11.  
  12. def rotate(orientation):
  13.     print "Rotate " + orientation
  14.     os.system(rotate_script + ' ' + orientation)
  15.     return orientation
  16.  
  17. pygame.init()
  18. joystick.init()
  19. num_dev = joystick.get_count()
  20. accel = None
  21. i = 0
  22. while i < num_dev:
  23.     accel = joystick.Joystick(i)
  24.     try:
  25.         accel.init()
  26.     except pygame.error:
  27.         print "Error initializing accelerator!"
  28.         sys.exit(1)
  29.     i += 1
  30.     print "Device:  " + accel.get_name()
  31.     print "Buttons: " + str(accel.get_numbuttons())
  32.     print "Axes:    " + str(accel.get_numaxes())
  33.     print "Hats:    " + str(accel.get_numhats())
  34.     print "Balls:   " + str(accel.get_numballs())
  35.     if accel.get_numaxes() == 3:
  36.         break
  37.  
  38. if not accel:
  39.     print "No accelerator device found. Exiting..."
  40.     sys.exit(1)
  41.  
  42. orientation = rotate("normal") #"normal"
  43. while True:
  44.     for ev in event.get():
  45.         print "Event: " + str(event.type)
  46.         if ev.type == QUIT:
  47.             joystick.quit()
  48.             pygame.quit()
  49.             sys.exit(0)
  50.  
  51.         if ev.type == JOYAXISMOTION:
  52.             axis_x = accel.get_axis(0)
  53.             axis_y = accel.get_axis(1)
  54.             axis_z = accel.get_axis(2)
  55.             print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
  56.             if (axis_x > 0.5) and (orientation != "left"):
  57.                 print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
  58.                 orientation = rotate("left")
  59.             elif (axis_x < -0.2) and (orientation != "right"):
  60.                 print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
  61.                 orientation = rotate("right")
  62.             elif (axis_x < 0.3) and (axis_x > -0.2) and (axis_y < -0.1) and (orientation != "normal"):
  63.                 print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
  64.                 orientation = rotate("normal")
  65.             elif (axis_x < 0.3) and (axis_x > -0.2) and (axis_y > 0.3) and (orientation != "inverted"):
  66.                 print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
  67.                 orientation = rotate("inverted")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement