from pyglet import event
import sys
if sys.platform == 'linux2':
import struct
from select import select
class Joystick(event.EventDispatcher):
JS_EVENT_BUTTON = 0x01 #/* button pressed/released */
JS_EVENT_AXIS = 0x02 #/* joystick moved */
JS_EVENT_INIT = 0x80 #/* initial state of device */
JS_EVENT = "IhBB"
JS_EVENT_SIZE = struct.calcsize(JS_EVENT)
def __init__(self, device_number):
device = '/dev/input/js%s' % device_number
event.EventDispatcher.__init__(self)
self.dev = open('/dev/input/js0')
def dispatch_events(self):
r,w,e = select([self.dev],[],[], 0)
if self.dev not in r: return
evt = self.dev.read(self.JS_EVENT_SIZE)
time, value, type, number = struct.unpack(self.JS_EVENT, evt)
evt = type & ~self.JS_EVENT_INIT
if evt == self.JS_EVENT_AXIS:
self.dispatch_event('on_axis', number, value)
elif evt == self.JS_EVENT_BUTTON:
self.dispatch_event('on_button', number, value==1)
def on_axis(self, axis, value):
pass
def on_button(self, button, pressed):
pass
Joystick.register_event_type('on_axis')
Joystick.register_event_type('on_button')
else:
raise ImportError('Platform not supported (%s)' % sys.platform)
if __name__ == "__main__":
j = Joystick(0)
@j.event
def on_button(button, pressed):
print 'button', button, pressed
@j.event
def on_axis(axis, value):
print 'axis', axis, value
while True:
j.dispatch_events()
Friday, February 01, 2008
Pyglet + Joystick
I've written a pygletified Joystick class. I'm not sure I'm using dispatch_events correctly, but it seems to work. With some more work it could support OSX and Win32.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
These are the robots I've been working on for the last 12 months. They each weigh about 11 tonnes and have a 17 meter reach. The control...
-
So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...
-
Often, when building a game, you need to test if objects are colliding. The objects could be spaceships, rocks, mouse pointers, laser beams....
-
Summary: NodeJS wins. Test Program ab -n 10000 -c 5 http://localhost/ Gevent Code from gevent import wsgi class WebServer(object): def a...
-
Why would I ask that question? Python 3 has been available for some time now, yet uptake is slow. There aren't a whole lot of packages i...
-
MiddleMan: A Pub/Sub and Request/Response server in Go. This is my first Go project. It is a rewrite of an existing Python server, based o...
-
I've just finished refactoring an awful C# class. I had been delaying the job for a while because I didn't want to do it. Then, whil...
-
Dear Lazyweb. Imagine a nice RESTful interface for working with Tags. The URL: /tags/ will return a list of all the tags. The URL: /tags/fo...
-
It is about 8 degrees C this morning. So cold, especially when last week we had high twenties. To help solve the problem, a friend suggeste...
-
We're almost done with our Giant Robot Project. Realising we had only a few hours before the robots would be deconstructed and sent awa...
6 comments:
Is there a reason you throw away the device variable in Joystick's __init__?
It looks like the device is hard-coded now in init for testing - probably just forgot to change. I would suggest (or request) making the device number a keyword argument defaulting to 0.
Can we get rumble? SDL has been lacking it for years
I would need to get hold a Joystick with force feedback first. Also, I would not write any code unless there is a standard interface for working with all JS models.
Very cool, thanks! Oh, and if anyone ever does get rumble feedback going, I'd be very interested in getting my hands on it for my research: http://memetic.ca
Mike
Handy joystick routine to get me up and running quick! Worked out of the box with an old junky usb joystick I have analog and all. I know folks have mentioned it about the device number, so I just added it in with a try except block:
try:
self.dev = open(device)
except:
print "Error opening %s" % device
sys.exit(0)
Post a Comment