Re: [Pyobjc-dev] Using OS-X frameworks from Python
Brought to you by:
ronaldoussoren
|
From: Arnar B. <ar...@gm...> - 2008-02-10 17:17:58
|
Hi there,
On Feb 10, 2008 5:10 PM, Ronald Oussoren <ron...@ma...> wrote:
> > Can I use it from a custom extension module (written in C)?
> > Can I write a Python extension module in Objective-C?
>
> Yes to both, but that's not really necessary. Both PyObjC 1.x and
> PyObjC 2.x (shipping with Leopard) have tools to automaticly create a
> python wrapper from header files. Those are a bit tuned to the way
> Apple writes headers, but with some luck work with the WiiRemote
> headers as well.
Thanks Ronald. I figured out how to talk with the framework and
specify delegates in C with the help of another member of this list
(John Stoner). Didn't know about this tool, will look at that.
Currently I'm dealing with "Bus errors" which I don't know if they are
my fault (i.e. incorrect specification of the informal interfaces) or
something with the WiiRemote framework. I suspect the former.
I'm pasting the code I have now below if anyone's interested.
thanks,
Arnar
import objc
objc.loadBundle("WiiRemote", globals(), bundle_path="WiiRemote.framework")
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
wd = WiiRemoteDiscovery.new()
IRData = objc.createStructType("IRData", "iii", ["x", "y", "s"])
WiiRemoteDiscoveryDelegate = objc.informal_protocol(
"WiiRemoteDiscoveryDelegate",
[
objc.selector(None,
selector="WiiRemoteDiscovered:",signature="v@:@",isRequired=0),
objc.selector(None, selector="WiiRemoteDiscoveryError:",
signature="v@:i",isRequired=0)
])
WiiRemoteDelegate = objc.informal_protocol(
"WiiRemoteDelegate",
[
objc.selector(None, selector="irPointMovedX:",
signature="v@:ff@", isRequired=False),
objc.selector(None, selector="rawIRData:",
signature="v@:[4{IRData=iii}]@", isRequired=False),
objc.selector(None, selector="buttonChanged:",
signature="v@:Sc@", isRequired=False),
objc.selector(None, selector="accelerationChanged:",
signature="v@:SCCC@", isRequired=False),
objc.selector(None, selector="joyStickChanged:",
signature="v@:SCC@", isRequired=False),
objc.selector(None, selector="analogButtonChanged:",
signature="v@:SI@", isRequired=False),
objc.selector(None, selector="wiiRemoteDisconnected:",
signature="v@:@", isRequired=False),
])
wii = None
class wii_remote_discovery_delegate(NSObject):
def WiiRemoteDiscovered_(self, wiiRemote):
self.wiiRemote = wiiRemote
global wii
wii = wiiRemote.retain()
print "discovered"
delegate = wii_remote_delegate.new()
wii.setDelegate_(delegate)
print "set delegate"
wii.setLEDEnabled1_enabled2_enabled3_enabled4_(True, False, True, False)
#wii.setMotionSensorEnabled_(True)
def WiiRemoteDiscoveryError_(self, returnCode):
print "not discovered, error ", returnCode
AppHelper.stopEventLoop()
class wii_remote_delegate(NSObject):
def irPointMovedX_Y_wiiRemote_(self, px, py, wiiRemote):
print 'irPointMovedX:Y:wiiRemote:'
def rawIRData_wiiRemote_(self, irData, wiiRemote):
print 'rawIRData:wiiRemote:'
def buttonChanged_isPressed_wiiRemote_(self, type, isPressed, wiiRemote):
print 'buttonChanged:isPressed:wiiRemote:'
def accelerationChanged_accX_accY_accZ_wiiRemote_(self, type,
accX, accY, accZ, wiiRemote):
print 'accelerationChanged:accX:accY:accZ:wiiRemote:'
def joyStickChanged_tiltX_tiltY_wiiRemote_(self, type, tiltX,
tiltY, wiiRemote):
print 'joyStickChanged:tiltX:tiltY:wiiRemote:'
def analogButtonChanged_amount_wiiRemote_(self, type, press, wiiRemote):
print 'analogButtonChanged:amount:wiiRemote:'
def wiiRemoteDisconnected_(self, device):
print 'wiiRemoteDisconnected:'
x = wii_remote_discovery_delegate.new()
wd.setDelegate_(x)
wd.start()
try:
AppHelper.runConsoleEventLoop(installInterrupt=True)
except KeyboardInterrupt:
print "Ctrl-C received, quitting."
AppHelper.stopEventLoop()
"""
The informal interfaces from WiiRemote's headers:
@interface NSObject( WiiRemoteDiscoveryDelegate )
- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote;
- (void) WiiRemoteDiscoveryError:(int)code;
@end;
@interface NSObject( WiiRemoteDelegate )
- (void) irPointMovedX:(float)px Y:(float)py wiiRemote:(WiiRemote*)wiiRemote;
- (void) rawIRData: (IRData[4])irData wiiRemote:(WiiRemote*)wiiRemote;
- (void) buttonChanged:(WiiButtonType)type isPressed:(BOOL)isPressed
wiiRemote:(WiiRemote*)wiiRemote;
- (void) accelerationChanged:(WiiAccelerationSensorType)type
accX:(unsigned char)accX accY:(unsigned char)accY accZ:(unsigned
char)accZ wiiRemote:(WiiRemote*)wiiRemote;
- (void) joyStickChanged:(WiiJoyStickType)type tiltX:(unsigned
char)tiltX tiltY:(unsigned char)tiltY wiiRemote:(WiiRemote*)wiiRemote;
- (void) analogButtonChanged:(WiiButtonType)type
amount:(unsigned)press wiiRemote:(WiiRemote*)wiiRemote;
- (void) wiiRemoteDisconnected:(IOBluetoothDevice*)device;
//- (void) dataChanged:(unsigned short)buttonData accX:(unsigned
char)accX accY:(unsigned char)accY accZ:(unsigned char)accZ
mouseX:(float)mx mouseY:(float)my;
@end
"""
|