From edf4a348de0ddaf3bd0429d3cbb7dacc3b748954 Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Tue, 1 Mar 2011 15:49:21 +0800 Subject: added pyfantom python module, renamed .cp to .cpp, add setup.py --- FantomModule/FantomModule.cp | 133 --------------------- FantomModule/FantomModule.cpp | 133 +++++++++++++++++++++ FantomModule/FantomModule.h | 4 +- .../FantomModule.xcodeproj/project.pbxproj | 10 +- FantomModule/pyfantom/__init__.py | 0 FantomModule/setup.py | 35 ++++++ 6 files changed, 176 insertions(+), 139 deletions(-) delete mode 100644 FantomModule/FantomModule.cp create mode 100644 FantomModule/FantomModule.cpp create mode 100644 FantomModule/pyfantom/__init__.py create mode 100644 FantomModule/setup.py diff --git a/FantomModule/FantomModule.cp b/FantomModule/FantomModule.cp deleted file mode 100644 index 60bbe80..0000000 --- a/FantomModule/FantomModule.cp +++ /dev/null @@ -1,133 +0,0 @@ -/* - * FantomModule.cp - * FantomModule - * - * Created by tcmac on 01/03/2011. - * Copyright 2011 TC Wan. All rights reserved. - * - * Based on code from Fantom Driver 1.0.2f0 Example - * © Copyright 2006, - * National Instruments Corporation. - * All rights reserved. - * - * Originated: 10 March 2006 - * - */ - -#include -#include -#include "FantomModule.h" -#include "FantomModulePriv.h" - -static PyMethodDef FantomMethods[] = { - {"finddevices", fantom_finddevices, METH_VARARGS, - "Find a NXT Device"}, - {"socket", fantom_socket, METH_VARARGS, - "Create a Socket for a NXT Brick"}, - {"connect", fantom_connect, METH_VARARGS, - "Connect the Socket to a NXT Brick"}, - {"send", fantom_send, METH_VARARGS, - "Send Data via the Socket to a NXT Brick"}, - {"recv", fantom_recv, METH_VARARGS, - "Receive Data via the Socket from a NXT Brick"}, - {"close", fantom_close, METH_VARARGS, - "Close the Socket to a NXT Brick"}, - {NULL, NULL, 0, NULL} /* Sentinel */ -}; - -extern "C" PyMODINIT_FUNC -initspam(void) -{ - (void) Py_InitModule("fantom", FantomMethods); -} - -extern "C" PyObject *fantom_finddevices(PyObject *py_self, PyObject *py_args) -{ - FantomModule *fantomObject; - fantomObject = new FantomModule; - - PyObject *list = fantomObject->finddevices(py_self,py_args); - delete fantomObject; - - return list; -} -extern "C" PyObject *fantom_socket(PyObject *py_self, PyObject *py_args) -{ -} - -extern "C" PyObject *fantom_connect(PyObject *py_self, PyObject *py_args) -{ -} - -extern "C" PyObject *fantom_send(PyObject *py_self, PyObject *py_args) -{ -} - -extern "C" PyObject *fantom_recv(PyObject *py_self, PyObject *py_args) -{ -} - -extern "C" PyObject *fantom_close(PyObject *py_self, PyObject *py_args) -{ -} - -PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) -{ - const char *proto; - if (!PyArg_ParseTuple(py_args, "s", &proto)) - return NULL; - - ViBoolean useBT; - useBT = strcmp(proto, FANTOM_BT) ? true : false; - -/* - const char *command; - int sts; - - if (!PyArg_ParseTuple(py_args, "s", &command)) - return NULL; - sts = system(command); - return Py_BuildValue("i", sts); -*/ - - // Create an NXT iterator object which is used to find all accessible NXT devices. - nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(useBT, FANTOM_BT_TIMEOUTSEC, status); - - // Creating the NXT iterator object could fail, better check status before dereferencing a - // potentially NULL pointer. - if( status.isNotFatal()) - { - ViChar nxtName[FANTOM_NXTNAME_LEN]; - nxtIteratorPtr->getName(nxtName, status); - if( status.isNotFatal()) - { - // FIXME: Append to Python list - } - nxtIteratorPtr->advance(status); - - } - // Destroy the NXT iterator object which we no longer need - nFANTOM100::iNXT::destroyNXTIterator( nxtIteratorPtr ); - -} - -PyObject *FantomModule::socket(PyObject *py_self, PyObject *py_args) -{ - // Create an NXT object for the first NXT that was found. Note that if a NXT is found - // over BT, the computer and the NXT must be paired before an NXT object can be - // created. This can be done programatically using the iNXT::pairBluetooth method. - // nxtPtr = nxtIteratorPtr->getNXT( status ); -} - -void FantomModule::HelloWorld(const char * s) -{ - FantomModulePriv *theObj = new FantomModulePriv; - theObj->HelloWorldPriv(s); - delete theObj; -}; - -void FantomModulePriv::HelloWorldPriv(const char * s) -{ - std::cout << s << std::endl; -}; - diff --git a/FantomModule/FantomModule.cpp b/FantomModule/FantomModule.cpp new file mode 100644 index 0000000..ff2c6cd --- /dev/null +++ b/FantomModule/FantomModule.cpp @@ -0,0 +1,133 @@ +/* + * FantomModule.cp + * FantomModule + * + * Created by tcmac on 01/03/2011. + * Copyright 2011 TC Wan. All rights reserved. + * + * Based on code from Fantom Driver 1.0.2f0 Example + * © Copyright 2006, + * National Instruments Corporation. + * All rights reserved. + * + * Originated: 10 March 2006 + * + */ + +#include +#include +#include "FantomModule.h" +#include "FantomModulePriv.h" + +static PyMethodDef FantomMethods[] = { + {"finddevices", fantom_finddevices, METH_VARARGS, + "Find a NXT Device"}, + {"socket", fantom_socket, METH_VARARGS, + "Create a Socket for a NXT Brick"}, + {"connect", fantom_connect, METH_VARARGS, + "Connect the Socket to a NXT Brick"}, + {"send", fantom_send, METH_VARARGS, + "Send Data via the Socket to a NXT Brick"}, + {"recv", fantom_recv, METH_VARARGS, + "Receive Data via the Socket from a NXT Brick"}, + {"close", fantom_close, METH_VARARGS, + "Close the Socket to a NXT Brick"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +extern "C" PyMODINIT_FUNC +initfantom(void) +{ + (void) Py_InitModule("fantom", FantomMethods); +} + +extern "C" PyObject *fantom_finddevices(PyObject *py_self, PyObject *py_args) +{ + FantomModule *fantomObject; + fantomObject = new FantomModule; + + PyObject *list = fantomObject->finddevices(py_self,py_args); + delete fantomObject; + + return list; +} +extern "C" PyObject *fantom_socket(PyObject *py_self, PyObject *py_args) +{ +} + +extern "C" PyObject *fantom_connect(PyObject *py_self, PyObject *py_args) +{ +} + +extern "C" PyObject *fantom_send(PyObject *py_self, PyObject *py_args) +{ +} + +extern "C" PyObject *fantom_recv(PyObject *py_self, PyObject *py_args) +{ +} + +extern "C" PyObject *fantom_close(PyObject *py_self, PyObject *py_args) +{ +} + +PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) +{ + const char *proto; + if (!PyArg_ParseTuple(py_args, "s", &proto)) + return NULL; + + ViBoolean useBT; + useBT = strcmp(proto, FANTOM_BT) ? true : false; + +/* + const char *command; + int sts; + + if (!PyArg_ParseTuple(py_args, "s", &command)) + return NULL; + sts = system(command); + return Py_BuildValue("i", sts); +*/ + + // Create an NXT iterator object which is used to find all accessible NXT devices. + nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(useBT, FANTOM_BT_TIMEOUTSEC, status); + + // Creating the NXT iterator object could fail, better check status before dereferencing a + // potentially NULL pointer. + if( status.isNotFatal()) + { + ViChar nxtName[FANTOM_NXTNAME_LEN]; + nxtIteratorPtr->getName(nxtName, status); + if( status.isNotFatal()) + { + // FIXME: Append to Python list + } + nxtIteratorPtr->advance(status); + + } + // Destroy the NXT iterator object which we no longer need + nFANTOM100::iNXT::destroyNXTIterator( nxtIteratorPtr ); + +} + +PyObject *FantomModule::socket(PyObject *py_self, PyObject *py_args) +{ + // Create an NXT object for the first NXT that was found. Note that if a NXT is found + // over BT, the computer and the NXT must be paired before an NXT object can be + // created. This can be done programatically using the iNXT::pairBluetooth method. + // nxtPtr = nxtIteratorPtr->getNXT( status ); +} + +void FantomModule::HelloWorld(const char * s) +{ + FantomModulePriv *theObj = new FantomModulePriv; + theObj->HelloWorldPriv(s); + delete theObj; +}; + +void FantomModulePriv::HelloWorldPriv(const char * s) +{ + std::cout << s << std::endl; +}; + diff --git a/FantomModule/FantomModule.h b/FantomModule/FantomModule.h index 91e7575..4266d04 100644 --- a/FantomModule/FantomModule.h +++ b/FantomModule/FantomModule.h @@ -10,7 +10,7 @@ #ifndef FantomModule_ #define FantomModule_ -#include +#include "Python.h" #include "fantom/iNXT.h" #include "fantom/iNXTIterator.h" #include "fantom/tStatus.h" @@ -23,7 +23,7 @@ /* The classes below are exported */ #pragma GCC visibility push(default) -extern "C" PyMODINIT_FUNC initspam(void); +extern "C" PyMODINIT_FUNC initfantom(void); extern "C" PyObject *fantom_finddevices(PyObject *py_self, PyObject *py_args); extern "C" PyObject *fantom_socket(PyObject *py_self, PyObject *py_args); diff --git a/FantomModule/FantomModule.xcodeproj/project.pbxproj b/FantomModule/FantomModule.xcodeproj/project.pbxproj index c85a049..f4a5ec8 100644 --- a/FantomModule/FantomModule.xcodeproj/project.pbxproj +++ b/FantomModule/FantomModule.xcodeproj/project.pbxproj @@ -19,7 +19,7 @@ 43B531FB131C963A0016BF14 /* visatype.h in Headers */ = {isa = PBXBuildFile; fileRef = 43B531FA131C963A0016BF14 /* visatype.h */; }; 43B5320A131C975B0016BF14 /* Fantom in Frameworks */ = {isa = PBXBuildFile; fileRef = 43B53209131C975B0016BF14 /* Fantom */; }; 50149BDA09E781A5002DEE6A /* FantomModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 50149BD909E781A5002DEE6A /* FantomModule.h */; }; - 5073E0CB09E734E500EC74B6 /* FantomModule.cp in Sources */ = {isa = PBXBuildFile; fileRef = 5073E0C409E734A800EC74B6 /* FantomModule.cp */; }; + 5073E0CB09E734E500EC74B6 /* FantomModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5073E0C409E734A800EC74B6 /* FantomModule.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -35,8 +35,9 @@ 43B531F1131C962E0016BF14 /* tStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tStatus.h; sourceTree = ""; }; 43B531FA131C963A0016BF14 /* visatype.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = visatype.h; sourceTree = ""; }; 43B53209131C975B0016BF14 /* Fantom */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = Fantom; path = /Library/Frameworks/Fantom.framework/Fantom; sourceTree = ""; }; + 43DB2163131CBF18001443AF /* setup.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = setup.py; sourceTree = ""; }; 50149BD909E781A5002DEE6A /* FantomModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FantomModule.h; sourceTree = ""; }; - 5073E0C409E734A800EC74B6 /* FantomModule.cp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = FantomModule.cp; sourceTree = ""; }; + 5073E0C409E734A800EC74B6 /* FantomModule.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = FantomModule.cpp; sourceTree = ""; }; 5073E0C609E734A800EC74B6 /* FantomModuleProj.xcconfig */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.xcconfig; path = FantomModuleProj.xcconfig; sourceTree = ""; }; 5073E0C709E734A800EC74B6 /* FantomModuleTarget.xcconfig */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.xcconfig; path = FantomModuleTarget.xcconfig; sourceTree = ""; }; 50B2938909F016FC00694E55 /* FantomModulePriv.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FantomModulePriv.h; sourceTree = ""; }; @@ -73,6 +74,7 @@ 08FB77ACFE841707C02AAC07 /* Source */, 0867D69AFE84028FC02AAC07 /* External Frameworks and Libraries */, 034768DDFF38A45A11DB9C8B /* Products */, + 43DB2163131CBF18001443AF /* setup.py */, ); name = FantomModule; sourceTree = ""; @@ -92,7 +94,7 @@ 32BAE0B70371A74B00C91783 /* FantomModule_Prefix.pch */, 50149BD909E781A5002DEE6A /* FantomModule.h */, 50B2938909F016FC00694E55 /* FantomModulePriv.h */, - 5073E0C409E734A800EC74B6 /* FantomModule.cp */, + 5073E0C409E734A800EC74B6 /* FantomModule.cpp */, ); name = Source; sourceTree = ""; @@ -191,7 +193,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 5073E0CB09E734E500EC74B6 /* FantomModule.cp in Sources */, + 5073E0CB09E734E500EC74B6 /* FantomModule.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FantomModule/pyfantom/__init__.py b/FantomModule/pyfantom/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/FantomModule/setup.py b/FantomModule/setup.py new file mode 100644 index 0000000..25dd152 --- /dev/null +++ b/FantomModule/setup.py @@ -0,0 +1,35 @@ +from distutils.core import setup, Extension +import sys + +def getextensions(): + mac_ext = Extension("FantomModule", + define_macros=[('PYFANTOM_DEBUG', '0')], # set to '1' to print debug messges + include_dirs=['.'], + extra_compile_args=['-arch i386'], + libraries=["Fantom"], + sources=["FantomModule.cpp"] + ) + return [mac_ext] + +# install the main library +setup(name="pyfantom", + version="0.1", + author="Tat-Chee Wan", + author_email="tcwan@cs.usm.my", + url="", + description="Python Extension to call Fantom Driver", + long_description="Python Wrapper for Fantom Driver on Mac OS X.", + license="GPL", + packages=["pyfantom"], + ext_modules=getextensions(), + classifiers = [ "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + # Need to confirm Fantom / NI-VISA license compatibility + # "License :: OSI Approved :: GNU General Public License (GPL)", + # "License :: OSI Approved :: GNU General Public License v2", + "Programming Language :: C++", + "Topic :: Software Development :: Libraries", + "Topic :: System :: Networking", + "Topic :: Communications", + "Operating System :: MacOS :: MacOS X" ] + ) -- cgit v1.2.3