Created
October 24, 2012 02:32
-
-
Save robsears/3943374 to your computer and use it in GitHub Desktop.
wxPython Joystick Example 3
This file contains 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
# Import wx objects | |
import wx | |
# Taken from wxPython demos: | |
haveJoystick = True | |
if wx.Platform == "__WXMAC__": | |
haveJoystick = False | |
# Create a few global variables so we can tweak alignment, etc | |
X_LABEL = 10 | |
X_QUANT = 120 | |
Y = 10 | |
LINE_HEIGHT = 15 | |
def PrintJoystickMethod(self, label, val, attrns): | |
# Create a function to condense and clarify the code. This function takes a | |
# label and a value then positions them in the frame. The 'attrns' input | |
# makes the wx.StaticText object an attribute of the joystick object. For example, | |
# calling PrintJoystickMethod(self, 'Status: ', self.stick.IsOk(), 'status') | |
# is equivalent to running: | |
# 1. wx.StaticText(self, -1, 'Status: ', pos=(X_LABEL,Y)) | |
# 2. self.stick.status=wx.StaticText(self, -1, str(self.stick.IsOk()), pos=(X_QUANT, Y)) | |
# 3. Y = Y + LINE_HEIGHT | |
# This way we can condense three lines of code down to just one, and do it in a way that | |
# allows us to access and change the values later (like if the joystick is moved, button | |
# pressed, etc). Since this example covers around 40 methods, this function allows us to | |
# reduce the code from 120 lines to just 40, while maintaining a consistent visual layout. | |
global X_LABEL, X_QUANT, Y | |
wx.StaticText(self, -1, str(label), pos=(X_LABEL,Y)) | |
setattr(self.stick, str(attrns), wx.StaticText(self, -1, str(val), pos=(X_QUANT,Y))) | |
Y = Y + LINE_HEIGHT | |
class Example(wx.Frame): | |
def __init__(self, *args, **kw): | |
super(Example, self).__init__(*args, **kw) | |
# Simple method to create a new column of data | |
def NewColumn(): | |
global X_LABEL, X_QUANT, Y | |
Y = 10 | |
X_LABEL = X_QUANT + 75 | |
X_QUANT = X_LABEL + 110 | |
# Simple method to create a new row of data: | |
def NewRow(): | |
global Y | |
Y = Y + LINE_HEIGHT | |
# Create Joystick object | |
self.stick = wx.Joystick() | |
self.stick.SetCapture(self) | |
# Print the information to the screen: | |
joystick_status = {True: 'Good', False: 'Fail'}[self.stick.IsOk() == True] # See: http://en.wikipedia.org/wiki/%3F:#Python | |
PrintJoystickMethod(self, 'Joystick Status:', joystick_status, 'status') | |
PrintJoystickMethod(self, 'Manufacturer ID:', self.stick.GetManufacturerId(), 'mid') | |
PrintJoystickMethod(self, 'Product ID:', self.stick.GetProductId(), 'pid') | |
# Get the joystick name: | |
joystick_name = "<None>" | |
try: | |
# This is placed in a try...except block to catch errors that sometimes occur. | |
# Depends on the joystick, drivers, registry, etc. May or may not work as expected. | |
# If it throws errors during testing, just comment out this line and uncomment the line below it | |
joystick_name = self.stick.GetProductName() | |
#joystick_name = 'YOUR JOYSTICK NAME' | |
except: | |
pass | |
PrintJoystickMethod(self, 'Product Name:', joystick_name, 'name') | |
# Print some physical properties of the joystick: | |
NewRow() | |
PrintJoystickMethod(self, 'Movement Threshold:', self.stick.GetMovementThreshold(), 'movethresh') | |
PrintJoystickMethod(self, 'Number of Axes:', self.stick.GetNumberAxes(), 'numaxes') | |
PrintJoystickMethod(self, 'Number of Buttons:', self.stick.GetNumberButtons(), 'numbuttons') | |
PrintJoystickMethod(self, 'Number of Joysticks:', self.stick.GetNumberJoysticks(), 'numjoysticks') | |
# Translate boolean results to Yes/No answers for some joystick properties: | |
has_rudder = {True: 'Yes', False: 'No'}[self.stick.HasRudder() == True] | |
has_u = {True: 'Yes', False: 'No'}[self.stick.HasU() == True] | |
has_v = {True: 'Yes', False: 'No'}[self.stick.HasV() == True] | |
has_z = {True: 'Yes', False: 'No'}[self.stick.HasZ() == True] | |
has_pov = {True: 'Yes', False: 'No'}[self.stick.HasPOV() == True] | |
has_pov4dir = {True: 'Yes', False: 'No'}[self.stick.HasPOV4Dir() == True] | |
has_povcts = {True: 'Yes', False: 'No'}[self.stick.HasPOVCTS() == True] | |
# Print details about the rudder: | |
NewRow() | |
PrintJoystickMethod(self, 'Has Rudder?:', has_rudder, 'hasrudder') | |
if self.stick.HasRudder() == True: | |
PrintJoystickMethod(self, 'Rudder Max:', self.stick.GetRudderMax(), 'ruddermax') | |
PrintJoystickMethod(self, 'Rudder Min:', self.stick.GetRudderMin(), 'ruddermin') | |
PrintJoystickMethod(self, 'Rudder Position:', self.stick.GetRudderPosition(), 'rudderpos') | |
# Print details about the joystick's U-axis (if applicable): | |
NewRow() | |
PrintJoystickMethod(self, 'Has U?:', has_u, 'hasu') | |
if self.stick.HasU() == True: | |
PrintJoystickMethod(self, 'U Max:', self.stick.GetUMax(), 'umax') | |
PrintJoystickMethod(self, 'U Min:', self.stick.GetUMin(), 'umin') | |
PrintJoystickMethod(self, 'U Position:', self.stick.GetUPosition(), 'upos') | |
# Print details about the joystick's V-axis (if applicable): | |
NewRow() | |
PrintJoystickMethod(self, 'Has V?:', has_v, 'hasv') | |
if self.stick.HasV() == True: | |
PrintJoystickMethod(self, 'V Max:', self.stick.GetVMax(), 'vmax') | |
PrintJoystickMethod(self, 'V Min:', self.stick.GetVMin(), 'vmin') | |
PrintJoystickMethod(self, 'V Position:', self.stick.GetVPosition(), 'vpos') | |
# Print details about the joystick's Z-axis (if applicable): | |
NewRow() | |
PrintJoystickMethod(self, 'Has Z?:', has_z, 'hasz') | |
if self.stick.HasZ() == True: | |
PrintJoystickMethod(self, 'Z Max:', self.stick.GetZMax(), 'zmax') | |
PrintJoystickMethod(self, 'Z Min:', self.stick.GetZMin(), 'zmin') | |
PrintJoystickMethod(self, 'Z Position:', self.stick.GetZPosition(), 'zpos') | |
# Print details about the joystick's POV switch (if applicable): | |
NewColumn() | |
PrintJoystickMethod(self, 'Has POV?:', has_pov, 'haspov') | |
if self.stick.HasPOV() == True: | |
PrintJoystickMethod(self, 'POV Position:', self.stick.GetPOVPosition(), 'povpos') | |
# Does the joystick offer 4-directional POV switch? | |
# (forward, backward, left, and right): | |
NewRow() | |
PrintJoystickMethod(self, 'Has POV4Dir?:', has_pov4dir, 'haspov4dir') | |
# Print details about the joystick's POV switch's | |
# continuous degree bearings (if applicable): | |
NewRow() | |
PrintJoystickMethod(self, 'Has POVCTS?:', has_povcts, 'haspovcts') | |
if self.stick.HasPOVCTS() == True: | |
PrintJoystickMethod(self, 'POVCTS Position:', self.stick.GetPOVCTSPosition(), 'povctspos') | |
# Print details about the joystick's polling: | |
NewRow() | |
PrintJoystickMethod(self, 'Polling (max):', self.stick.GetPollingMax(), 'pollingmin') | |
PrintJoystickMethod(self, 'Polling (min):', self.stick.GetPollingMin(), 'pollingmax') | |
# Print details about the joystick's X-Y postion: | |
NewRow() | |
PrintJoystickMethod(self, '(X,Y) Position:', self.stick.GetPosition(), 'xyposition') | |
# Print details about the joystick's X-axis: | |
NewRow(); | |
PrintJoystickMethod(self, 'X (max):', self.stick.GetXMax(), 'xmax') | |
PrintJoystickMethod(self, 'X (min):', self.stick.GetXMin(), 'xmin') | |
# Print details about the joystick's Y-axis: | |
NewRow(); | |
PrintJoystickMethod(self, 'Y (max):', self.stick.GetYMax(), 'ymax') | |
PrintJoystickMethod(self, 'Y (min):', self.stick.GetYMin(), 'ymin') | |
# Set the frame size and title, center to the screen and load | |
self.SetSize((500, 500)) | |
self.SetTitle('Joystick Info') | |
self.Centre() | |
self.Show(True) | |
def main(): | |
if haveJoystick == False: | |
print 'wx.Joystick is not available on this platform.' | |
return | |
else: | |
ex = wx.App() | |
Example(None) | |
ex.MainLoop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment