Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import pygame
- from pygame import joystick
- from pygame import event
- from pygame.locals import *
- import sys
- import os
- rotate_script= "~/rotate.sh"
- def rotate(orientation):
- print "Rotate " + orientation
- os.system(rotate_script + ' ' + orientation)
- return orientation
- pygame.init()
- joystick.init()
- num_dev = joystick.get_count()
- accel = None
- i = 0
- while i < num_dev:
- accel = joystick.Joystick(i)
- try:
- accel.init()
- except pygame.error:
- print "Error initializing accelerator!"
- sys.exit(1)
- i += 1
- print "Device: " + accel.get_name()
- print "Buttons: " + str(accel.get_numbuttons())
- print "Axes: " + str(accel.get_numaxes())
- print "Hats: " + str(accel.get_numhats())
- print "Balls: " + str(accel.get_numballs())
- if accel.get_numaxes() == 3:
- break
- if not accel:
- print "No accelerator device found. Exiting..."
- sys.exit(1)
- orientation = rotate("normal") #"normal"
- while True:
- for ev in event.get():
- print "Event: " + str(event.type)
- if ev.type == QUIT:
- joystick.quit()
- pygame.quit()
- sys.exit(0)
- if ev.type == JOYAXISMOTION:
- axis_x = accel.get_axis(0)
- axis_y = accel.get_axis(1)
- axis_z = accel.get_axis(2)
- print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
- if (axis_x > 0.5) and (orientation != "left"):
- print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
- orientation = rotate("left")
- elif (axis_x < -0.2) and (orientation != "right"):
- print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
- orientation = rotate("right")
- elif (axis_x < 0.3) and (axis_x > -0.2) and (axis_y < -0.1) and (orientation != "normal"):
- print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
- orientation = rotate("normal")
- elif (axis_x < 0.3) and (axis_x > -0.2) and (axis_y > 0.3) and (orientation != "inverted"):
- print "X " + str(axis_x) + ", Y " + str(axis_y) + ", Z " + str(axis_z)
- orientation = rotate("inverted")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement