From 96da3f913a2b6dc561bec9a3d109147230adef83 Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Wed, 2 Mar 2011 07:20:35 +0800 Subject: added more template code --- FantomModule/FantomModule.cpp | 98 +++++++++++++++++++++++++++++++++++++------ FantomModule/FantomModule.h | 8 +++- 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/FantomModule/FantomModule.cpp b/FantomModule/FantomModule.cpp index ff2c6cd..1c5fbcc 100644 --- a/FantomModule/FantomModule.cpp +++ b/FantomModule/FantomModule.cpp @@ -51,24 +51,49 @@ extern "C" PyObject *fantom_finddevices(PyObject *py_self, PyObject *py_args) return list; } + extern "C" PyObject *fantom_socket(PyObject *py_self, PyObject *py_args) { + + // Create a FantomModule object, which has not been connected to an actual NXT yet + FantomModule *fantomObject; + fantomObject = new FantomModule; + + // FIXME: Attach fantomObject to PyObject variable. + fantomObject->socket(py_self,py_args); // Internal object setup + } extern "C" PyObject *fantom_connect(PyObject *py_self, PyObject *py_args) { + FantomModule *fantomObject; + // FIXME: Retrieve FantomObject from PyObject variable + + return fantomObject->connect(py_self,py_args); } extern "C" PyObject *fantom_send(PyObject *py_self, PyObject *py_args) { + FantomModule *fantomObject; + // FIXME: Retrieve FantomObject from PyObject variable + + return fantomObject->send(py_self,py_args); } extern "C" PyObject *fantom_recv(PyObject *py_self, PyObject *py_args) { + FantomModule *fantomObject; + // FIXME: Retrieve FantomObject from PyObject variable + + return fantomObject->recv(py_self,py_args); } extern "C" PyObject *fantom_close(PyObject *py_self, PyObject *py_args) { + FantomModule *fantomObject; + // FIXME: Retrieve FantomObject from PyObject variable + + return fantomObject->close(py_self,py_args); } PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) @@ -80,15 +105,9 @@ PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) ViBoolean useBT; useBT = strcmp(proto, FANTOM_BT) ? true : false; -/* - const char *command; - int sts; + PyObject *list = NULL; - if (!PyArg_ParseTuple(py_args, "s", &command)) - return NULL; - sts = system(command); - return Py_BuildValue("i", sts); -*/ + nFANTOM100::iNXTIterator* nxtIteratorPtr; // Create an NXT iterator object which is used to find all accessible NXT devices. nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(useBT, FANTOM_BT_TIMEOUTSEC, status); @@ -102,23 +121,78 @@ PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) if( status.isNotFatal()) { // FIXME: Append to Python list + /* + return Py_BuildValue("i", sts); + */ } nxtIteratorPtr->advance(status); } // Destroy the NXT iterator object which we no longer need nFANTOM100::iNXT::destroyNXTIterator( nxtIteratorPtr ); + return list; } 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 ); + // Internal class object setup + iNXT = NULL; + +} + +PyObject *FantomModule::connect(PyObject *py_self, PyObject *py_args) +{ + // 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. + // FIXME: Retrieve resource String + ViConstString resourceName[FANTOM_NXTNAME_LEN]; + ViConstString passkey[FANTOM_NXTNAME_LEN] = { FANTOM_NXT_PASSKEY }; + + // FIXME: Retrieve PyObject's proto setting + ViBoolean useBT; + useBT = strcmp(proto, FANTOM_BT) ? true : false; + if (useBT) + nFANTOM100::iNXT::pairBluetooth((ViConstString) resourceName, (ViConstString) passkey, (ViChar *) pairedResourceName, status); + + if (status.isNotFatal()) + { + nxtPtr = nFANTOM100::iNXT::createNXT((ViConstString) resourceName, status, false); + } +} + +PyObject *FantomModule::send(PyObject *py_self, PyObject *py_args) +{ + ViByte bufferPtr[FANTOM_DATA_BUFLEN]; + ViUInt32 numberOfBytes; + + nxtPtr->write(bufferPtr, numberOfBytes, status); +} + +PyObject *FantomModule::recv(PyObject *py_self, PyObject *py_args) +{ + ViByte bufferPtr[FANTOM_DATA_BUFLEN]; + ViUInt32 numberOfBytes; + ViUInt32 bytesRead; + + bytesRead = nxtPtr->read(bufferPtr, numberOfBytes, status); +} + +PyObject *FantomModule::close(PyObject *py_self, PyObject *py_args) +{ + nFANTOM100::iNXT::destroyNXT(nxtPtr); + // FIXME: Retrieve resource String + ViConstString resourceName[FANTOM_NXTNAME_LEN]; + + // FIXME: Retrieve PyObject's proto setting + ViBoolean useBT; + useBT = strcmp(proto, FANTOM_BT) ? true : false; + if (useBT) + nFANTOM100::iNXT::unpairBluetooth((ViConstString) resourceName, status); // No Effect on Mac OSX + // FIXME: Set PyObject socket to None } +// Skeleton functions from Xcode void FantomModule::HelloWorld(const char * s) { FantomModulePriv *theObj = new FantomModulePriv; diff --git a/FantomModule/FantomModule.h b/FantomModule/FantomModule.h index 4266d04..16818be 100644 --- a/FantomModule/FantomModule.h +++ b/FantomModule/FantomModule.h @@ -19,6 +19,8 @@ #define FANTOM_USB "USB" #define FANTOM_BT_TIMEOUTSEC 2 #define FANTOM_NXTNAME_LEN 256 +#define FANTOM_DATA_BUFLEN 256 +#define FANTOM_NXT_PASSKEY "1234" /* The classes below are exported */ #pragma GCC visibility push(default) @@ -35,12 +37,16 @@ extern "C" PyObject *fantom_close(PyObject *py_self, PyObject *py_args); class FantomModule { nFANTOM100::tStatus status; - nFANTOM100::iNXTIterator* nxtIteratorPtr; nFANTOM100::iNXT* nxtPtr; + ViChar pairedResourceName[FANTOM_NXTNAME_LEN]; public: PyObject *finddevices(PyObject *py_self, PyObject *py_args); PyObject *socket(PyObject *py_self, PyObject *py_args); + PyObject *connect(PyObject *py_self, PyObject *py_args); + PyObject *send(PyObject *py_self, PyObject *py_args); + PyObject *recv(PyObject *py_self, PyObject *py_args); + PyObject *close(PyObject *py_self, PyObject *py_args); void HelloWorld(const char *); }; -- cgit v1.2.3 From 1b81593dc78daf9eab6acc02255f6f9e14b93d09 Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Wed, 2 Mar 2011 08:17:05 +0800 Subject: wip Template module for pyFantom with some cleanup --- FantomModule/FantomModule.cpp | 113 +++++++++++++++++++++++++++++++++++------- FantomModule/FantomModule.h | 11 ++-- 2 files changed, 100 insertions(+), 24 deletions(-) diff --git a/FantomModule/FantomModule.cpp b/FantomModule/FantomModule.cpp index 1c5fbcc..456caa7 100644 --- a/FantomModule/FantomModule.cpp +++ b/FantomModule/FantomModule.cpp @@ -6,11 +6,8 @@ * 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 + * © Copyright 2006, National Instruments Corporation. + * All rights reserved. Originated: 10 March 2006 * */ @@ -21,7 +18,9 @@ static PyMethodDef FantomMethods[] = { {"finddevices", fantom_finddevices, METH_VARARGS, - "Find a NXT Device"}, + "Discover NXT Devices (BT Discovery)"}, + {"find_bricks", fantom_find_bricks, METH_VARARGS, + "Find and Create NXT Devices (BT & USB)"}, {"socket", fantom_socket, METH_VARARGS, "Create a Socket for a NXT Brick"}, {"connect", fantom_connect, METH_VARARGS, @@ -43,13 +42,18 @@ initfantom(void) 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; + PyObject *list = FantomModule::finddevices(py_self,py_args); - return list; + return list; // of resource names +} + +extern "C" PyObject *fantom_find_bricks(PyObject *py_self, PyObject *py_args) +{ + + PyObject *list = FantomModule::find_bricks(py_self,py_args); + + return list; // of FantomModule objects } extern "C" PyObject *fantom_socket(PyObject *py_self, PyObject *py_args) @@ -93,7 +97,10 @@ extern "C" PyObject *fantom_close(PyObject *py_self, PyObject *py_args) FantomModule *fantomObject; // FIXME: Retrieve FantomObject from PyObject variable - return fantomObject->close(py_self,py_args); + PyObject *status; + status = fantomObject->close(py_self,py_args); + delete fantomObject; + return status; } PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) @@ -108,18 +115,69 @@ PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) PyObject *list = NULL; nFANTOM100::iNXTIterator* nxtIteratorPtr; + nFANTOM100::tStatus status; // Create an NXT iterator object which is used to find all accessible NXT devices. - nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(useBT, FANTOM_BT_TIMEOUTSEC, status); + nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(useBT, (useBT ? FANTOM_BT_TIMEOUTSEC : 0), status); // Creating the NXT iterator object could fail, better check status before dereferencing a // potentially NULL pointer. - if( status.isNotFatal()) + while (status.isNotFatal()) { ViChar nxtName[FANTOM_NXTNAME_LEN]; nxtIteratorPtr->getName(nxtName, status); - if( status.isNotFatal()) + if (status.isNotFatal()) { + // Split nxtName into NXT ID (h) and NXT Name (n) + // return as [(h,n),...] + // FIXME: Append to Python list + /* + return Py_BuildValue("i", sts); + */ + } + nxtIteratorPtr->advance(status); + + } + // Destroy the NXT iterator object which we no longer need + nFANTOM100::iNXT::destroyNXTIterator( nxtIteratorPtr ); + return list; + +} + +PyObject *FantomModule::find_bricks(PyObject *py_self, PyObject *py_args) +{ + const char *host, *name; + if (!PyArg_ParseTuple(py_args, "ss", &host, &name)) + return NULL; + + ViBoolean useBT; + useBT = false; + + PyObject *list = NULL; + + + nFANTOM100::iNXTIterator* nxtIteratorPtr; + nFANTOM100::tStatus status; + + // Create an NXT iterator object which is used to find all accessible NXT devices. + nxtIteratorPtr = nFANTOM100::iNXT::createNXTIterator(useBT, (useBT ? FANTOM_BT_TIMEOUTSEC : 0), status); + + // Creating the NXT iterator object could fail, better check status before dereferencing a + // potentially NULL pointer. + while (status.isNotFatal()) + { + ViChar nxtName[FANTOM_NXTNAME_LEN]; + nFANTOM100::iNXT* aNXT = NULL; + nxtIteratorPtr->getName(nxtName, status); + aNXT = nxtIteratorPtr->getNXT(status); + if (status.isNotFatal()) + { + FantomModule *aFantomObject = new FantomModule; + aFantomObject->nxtPtr = aNXT; + if (strlcpy(aFantomObject->pairedResourceName, nxtName,FANTOM_NXTNAME_LEN) >= FANTOM_NXTNAME_LEN) + // Exceeded Name Length + exit; + // FIXME: Append to Python list /* return Py_BuildValue("i", sts); @@ -137,7 +195,7 @@ PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args) PyObject *FantomModule::socket(PyObject *py_self, PyObject *py_args) { // Internal class object setup - iNXT = NULL; + nxtPtr = NULL; } @@ -150,9 +208,11 @@ PyObject *FantomModule::connect(PyObject *py_self, PyObject *py_args) ViConstString passkey[FANTOM_NXTNAME_LEN] = { FANTOM_NXT_PASSKEY }; // FIXME: Retrieve PyObject's proto setting + nFANTOM100::tStatus status; + const char *proto; ViBoolean useBT; useBT = strcmp(proto, FANTOM_BT) ? true : false; - if (useBT) + if (useBT and !nFANTOM100::iNXT::isPaired((ViConstString)resourceName,status)) nFANTOM100::iNXT::pairBluetooth((ViConstString) resourceName, (ViConstString) passkey, (ViChar *) pairedResourceName, status); if (status.isNotFatal()) @@ -165,6 +225,7 @@ PyObject *FantomModule::send(PyObject *py_self, PyObject *py_args) { ViByte bufferPtr[FANTOM_DATA_BUFLEN]; ViUInt32 numberOfBytes; + nFANTOM100::tStatus status; nxtPtr->write(bufferPtr, numberOfBytes, status); } @@ -174,24 +235,38 @@ PyObject *FantomModule::recv(PyObject *py_self, PyObject *py_args) ViByte bufferPtr[FANTOM_DATA_BUFLEN]; ViUInt32 numberOfBytes; ViUInt32 bytesRead; + nFANTOM100::tStatus status; bytesRead = nxtPtr->read(bufferPtr, numberOfBytes, status); } PyObject *FantomModule::close(PyObject *py_self, PyObject *py_args) { - nFANTOM100::iNXT::destroyNXT(nxtPtr); + if (nxtPtr) + nFANTOM100::iNXT::destroyNXT(nxtPtr); + nxtPtr = NULL; + // FIXME: Retrieve resource String ViConstString resourceName[FANTOM_NXTNAME_LEN]; // FIXME: Retrieve PyObject's proto setting + nFANTOM100::tStatus status; + const char *proto; ViBoolean useBT; useBT = strcmp(proto, FANTOM_BT) ? true : false; - if (useBT) + if (useBT and nFANTOM100::iNXT::isPaired((ViConstString)resourceName,status)) nFANTOM100::iNXT::unpairBluetooth((ViConstString) resourceName, status); // No Effect on Mac OSX // FIXME: Set PyObject socket to None } +FantomModule::~FantomModule() +{ + if (nxtPtr) + nFANTOM100::iNXT::destroyNXT(nxtPtr); + nxtPtr = NULL; + +} + // Skeleton functions from Xcode void FantomModule::HelloWorld(const char * s) { diff --git a/FantomModule/FantomModule.h b/FantomModule/FantomModule.h index 16818be..ff3a1de 100644 --- a/FantomModule/FantomModule.h +++ b/FantomModule/FantomModule.h @@ -17,7 +17,7 @@ #define FANTOM_BT "BT" #define FANTOM_USB "USB" -#define FANTOM_BT_TIMEOUTSEC 2 +#define FANTOM_BT_TIMEOUTSEC 5 #define FANTOM_NXTNAME_LEN 256 #define FANTOM_DATA_BUFLEN 256 #define FANTOM_NXT_PASSKEY "1234" @@ -27,7 +27,8 @@ extern "C" PyMODINIT_FUNC initfantom(void); -extern "C" PyObject *fantom_finddevices(PyObject *py_self, PyObject *py_args); +extern "C" PyObject *fantom_finddevices(PyObject *py_self, PyObject *py_args); // BT Discovery function +extern "C" PyObject *fantom_find_bricks(PyObject *py_self, PyObject *py_args); // BT & USB function 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); @@ -36,18 +37,18 @@ extern "C" PyObject *fantom_close(PyObject *py_self, PyObject *py_args); class FantomModule { - nFANTOM100::tStatus status; nFANTOM100::iNXT* nxtPtr; ViChar pairedResourceName[FANTOM_NXTNAME_LEN]; public: - PyObject *finddevices(PyObject *py_self, PyObject *py_args); + static PyObject *finddevices(PyObject *py_self, PyObject *py_args); + static PyObject *find_bricks(PyObject *py_self, PyObject *py_args); PyObject *socket(PyObject *py_self, PyObject *py_args); PyObject *connect(PyObject *py_self, PyObject *py_args); PyObject *send(PyObject *py_self, PyObject *py_args); PyObject *recv(PyObject *py_self, PyObject *py_args); PyObject *close(PyObject *py_self, PyObject *py_args); - + ~FantomModule(); void HelloWorld(const char *); }; -- cgit v1.2.3