Skip to content

Instantly share code, notes, and snippets.

@robsears
Created October 24, 2012 02:33
Show Gist options
  • Save robsears/3943376 to your computer and use it in GitHub Desktop.
Save robsears/3943376 to your computer and use it in GitHub Desktop.
wxPython Joystick Example 4
# 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 in the GUI window
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
# The main GUI window:
class Example(wx.Frame):
def __init__(self, *args, **kw):
super(Example, self).__init__(*args, **kw)
global Y
# Simple function 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 function 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 product information to the screen:
joystick_status = {True: 'Good', False: 'Fail'}[self.stick.IsOk() == True] # Translate Boolean value to text. For more info, 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 (if applicable):
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')
# Bind joystick actions to an updater function:
self.Bind(wx.EVT_JOYSTICK_EVENTS, self.OnJoystick) # If anything happens w/joystick, call OnJoystick
self.stick.SetMovementThreshold(1) # Sets the movement threshold, the number of steps outside which the joystick is deemed to have moved.
# Create some simple gauge widgets for the joystick positions
NewColumn()
wx.StaticText(self, -1, 'X-axis Gauge: ', pos=(X_LABEL,Y))
self.xpos = wx.Gauge(self, range=self.stick.GetXMax(), pos=(X_QUANT, Y), size=(150, 5))
wx.StaticText(self, -1, 'Y-axis Gauge: ', pos=(X_LABEL,Y+35))
self.ypos = wx.Gauge(self, range=self.stick.GetYMax(), pos=(X_QUANT, Y+35), size=(150, 5))
wx.StaticText(self, -1, 'Z-axis Gauge: ', pos=(X_LABEL,Y+70))
self.zpos = wx.Gauge(self, range=self.stick.GetZMax(), pos=(X_QUANT, Y+70), size=(150, 5))
wx.StaticText(self, -1, 'Rudder Gauge: ', pos=(X_LABEL,Y+105))
self.rpos = wx.Gauge(self, range=self.stick.GetRudderMax(), pos=(X_QUANT, Y+105), size=(150, 5))
self.SetSize((750, 575))
self.SetTitle('Joystick Info')
self.Centre()
self.Show(True)
def OnJoystick(self, e):
# Update the values for xy position, rudder position, and z-axis position.
# These are all values that can change. The number of buttons, axes, etc - values that are
# fixed - are not updated for that reason. You can add more values to update by simply adding
# a new line of the format self.stick.ATTRNS.SetLabel(str(self.stick.METHOD())) where ATTRNS
# is the same one defined for the value during initialization, and METHOD is the wxPython
# method for collecting the value.
self.stick.xyposition.SetLabel(str(self.stick.GetPosition()))
self.stick.rudderpos.SetLabel( str(self.stick.GetRudderPosition()))
self.stick.zpos.SetLabel( str(self.stick.GetZPosition()))
# Update the widgets:
newx, newy = self.stick.GetPosition()
self.xpos.SetValue(newx)
self.ypos.SetValue(newy)
self.zpos.SetValue(self.stick.GetZPosition())
self.rpos.SetValue(self.stick.GetRudderPosition())
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