Thread: [Pyobjc-dev] Using OS-X frameworks from Python
Brought to you by:
ronaldoussoren
|
From: Arnar B. <ar...@gm...> - 2008-02-08 19:06:45
|
Hello there, Forgive me if I'm in the wrong place asking this. I have an OS-X Framework (with source code) and I need to use the functionality of it from Python. The framework in question is WiiRemote.framework that provides events from a Wii remote through delegates (unfortunately I'm not familiar with Objective-C). After digging around for a while, I'm having trouble finding answers to the following: What are my options here? Is it possible to use the compiled framework directly with ctypes? Can I use it from a custom extension module (written in C)? Can I write a Python extension module in Objective-C? thanks, Arnar |
|
From: Ronald O. <ron...@ma...> - 2008-02-10 17:10:43
Attachments:
smime.p7s
|
On 8 Feb, 2008, at 20:06, Arnar Birgisson wrote: > Hello there, > > Forgive me if I'm in the wrong place asking this. > > I have an OS-X Framework (with source code) and I need to use the > functionality of it from Python. The framework in question is > WiiRemote.framework that provides events from a Wii remote through > delegates (unfortunately I'm not familiar with Objective-C). > > After digging around for a while, I'm having trouble finding answers > to the following: > > What are my options here? You're on the right list of this, PyObjC is specificly designed for this task. > > Is it possible to use the compiled framework directly with ctypes? Possibly, but using PyObjC would be better. > > 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. Ronald > > > thanks, > Arnar > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Pyobjc-dev mailing list > Pyo...@li... > https://lists.sourceforge.net/lists/listinfo/pyobjc-dev |
|
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
"""
|
|
From: Arnar B. <ar...@gm...> - 2008-02-10 17:23:19
|
One more thing, On Feb 10, 2008 5:10 PM, Ronald Oussoren <ron...@ma...> wrote: > 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. Where can I find these tools? I'm running Leopard (and still using the bundled Python distribution). I'd also appreciate links to documentation. cheers, Arnar |
|
From: Ronald O. <ron...@ma...> - 2008-02-11 09:11:56
|
On Sunday, February 10, 2008, at 06:23PM, "Arnar Birgisson" <ar...@gm...> wrote: >One more thing, > >On Feb 10, 2008 5:10 PM, Ronald Oussoren <ron...@ma...> wrote: >> 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. > >Where can I find these tools? I'm running Leopard (and still using the >bundled Python distribution). I'd also appreciate links to >documentation. The tool for this is pyobjc-metadata in the pyobjc repository. There is little to no documentation at the moment. I have to wrap another 3th party framework in the near future, and will use that opportunity to write some documentation and push pyobjc-metadata to pypi (making it easier to install). In theory, a working wrapper is just one script invocation away, although the tool uses a regex-based parser to scan header files and that parser might be easy to confuse. Ronald |
|
From: Arnar B. <ar...@gm...> - 2008-02-11 09:29:48
|
Thank you Ronald, On Feb 11, 2008 9:11 AM, Ronald Oussoren <ron...@ma...> wrote: > The tool for this is pyobjc-metadata in the pyobjc repository. There is little to no documentation at the moment. > > I have to wrap another 3th party framework in the near future, and will use that opportunity to write some documentation and push pyobjc-metadata to pypi (making it easier to install). In theory, a working wrapper is just one script invocation away, although the tool uses a regex-based parser to scan header files and that parser might be easy to confuse. Very good. Have you thought about using a more sophisticated parser, perhaps based on ANTLR or similar? In the meantime, I figured out the bus errors. My selectors were wrong, I was missing the names of the parameters (which I guess in Obj-C are part of the method name). Now I'm dealing with some commnication errors that I'm sure are more related to the Wiimote library than the objc-bridge. thanks, Arnar |