The "new" Joystick Service

I am writing here beacause i think it's more long lasting than the shoutbox and could be helptful for others:

The script with the new build:
 

joy = Runtime.createAndStart("joy","Joystick")
 
joy.setController(3)
 
joy.addInputListener(python)
 
def onJoystickInput(data):
 print data

prints data in form of: [A] 1.0000
                                   [A] 0.0000

How can i handle this data in an if-code for example. Do i have to handle it like a string:  
if(data == '[A] 1.0000'):
    .......    

(didnt really work for me) or is it an array and i have to handle it with a for-code for example?

 

Alessandruino's picture

Hey Kakadu31... modified the

Hey Kakadu31... modified the python example in the joystick service page ! Hope it's more clear now !


#file : /service/Joystick.py edit raw
#########################################
# Joystick.py
# categories: input sensor joystick
# more info @: http://myrobotlab.org/service/Joystick
#########################################
# start the services
joy = Runtime.start("joy","Joystick")
python = Runtime.start("python","Python")
#this set which kind of controller you want to poll data from
#it is the number you can see in the Joystick GUI when you open the list of devices
joy.setController(5)

#tell joystick service to send data to python as a message only when new data is aviable
joy.addInputListener(python)

#this is the method in python which receive the data from joystick service
#it is triggered only when new data arrive, it's not a loop !
def onJoystickInput(data):
 #this print the name of the key/button you pressed (it's a String)
 #this print the value of the key/button (it's a Float)
 print data.id, data.value
 if (data.id == "3"):
     print("button 3 was pressed its value is", data.value)
 elif (data.id == "x"):
     print("stick x", data.value)