aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/armdebug/FantomModule/FantomModule.cpp
blob: b3380f11363ab0131b4818741f4d34e6763d2013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 *  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 <iostream>
#include <string.h>
#include "FantomModule.h"
#include "FantomModulePriv.h"

static PyMethodDef FantomMethods[] = {
    {"finddevices",  fantom_finddevices, METH_VARARGS,
		"Discover NXT Devices (BT Discovery)"},
    {"find_bricks",  fantom_find_bricks, METH_VARARGS,
		"Find and Create NXT Devices (USB)"},
    {"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)
{
	
	PyObject *list = FantomModule::finddevices(py_self,py_args);
	
	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)
{
	
	ViChar  newPasskey[FANTOM_PASSKEY_LEN] = FANTOM_NXT_PASSKEY;	// Default Passkey

	// FIXME: Retrieve PyObject's proto setting
	const char *proto;
	if (!PyArg_ParseTuple(py_args, "s", &proto))
        return NULL;
	ViBoolean enableBT = strcmp(proto, FANTOM_BT) ? true : false;
	
	// Get new passkey from Python args if specified
	
	// Create a FantomModule object, which has not been connected to an actual NXT yet
	FantomModule *fantomObject;
	fantomObject = new FantomModule;
	
	fantomObject->socket(enableBT, newPasskey);			// Internal object setup

	// FIXME: Convert fantomObject to PyObject and return.

}

extern "C"  PyObject *fantom_connect(PyObject *py_self, PyObject *py_args)
{
	FantomModule *fantomObject;
	// FIXME: Retrieve FantomObject from PyObject variable
	ViChar resourceName[FANTOM_NXTNAME_LEN];
	ViBoolean success;
	
	success = fantomObject->connect(resourceName);
}

extern "C"  PyObject *fantom_send(PyObject *py_self, PyObject *py_args)
{
	FantomModule *fantomObject;
	// FIXME: Retrieve FantomObject from PyObject variable

	ViByte bufferPtr[FANTOM_DATA_BUFLEN];
	ViUInt32 numberOfBytes;
	ViUInt32 bytesSent;
	
	bytesSent = fantomObject->send(bufferPtr,numberOfBytes);
}

extern "C"  PyObject *fantom_recv(PyObject *py_self, PyObject *py_args)
{
	FantomModule *fantomObject;
	// FIXME: Retrieve FantomObject from PyObject variable

	ViByte bufferPtr[FANTOM_DATA_BUFLEN];
	ViUInt32 numberOfBytes;
	ViUInt32 bytesReceived;
	
	bytesReceived = fantomObject->recv(bufferPtr,numberOfBytes);
}

extern "C"  PyObject *fantom_close(PyObject *py_self, PyObject *py_args)
{
	FantomModule *fantomObject;
	// FIXME: Retrieve FantomObject from PyObject variable
	
	ViBoolean success = fantomObject->close();
	delete fantomObject;
	
	// Return success/failure
}


// Static method called directly from C
PyObject *FantomModule::finddevices(PyObject *py_self, PyObject *py_args)
{
	const char *proto;
	if (!PyArg_ParseTuple(py_args, "s", &proto))
        return NULL;

	ViBoolean useBT = strcmp(proto, FANTOM_BT) ? true : 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];
		nxtIteratorPtr->getName(nxtName, status);
		
		std::cout << "Found: " << nxtName << std::endl;

		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;
	
}

// Static method called directly from C
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 = 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);
		std::cout << "Found: " << nxtName << std::endl;
		aNXT = nxtIteratorPtr->getNXT(status);
		if (status.isNotFatal())
		{
			FantomModule *aFantomObject = new FantomModule;
			aFantomObject->nxtPtr = aNXT;
			aFantomObject->status.assign(status);
			if (strlcpy(aFantomObject->pairedResourceName, nxtName, FANTOM_NXTNAME_LEN) >= FANTOM_NXTNAME_LEN)
			{
				// Exceeded Name Length
				std::cout << "NXTName Length Exceeded: " << nxtName << std::endl;
				delete aFantomObject;
			}
			else 
			{
				// 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;
	
}

ViBoolean FantomModule::socket(ViBoolean enableBT, ViConstString BTkey)
{
	// Internal class object setup
	nxtPtr = NULL;
	useBT = enableBT;
	if (strlcpy(passkey, BTkey, FANTOM_PASSKEY_LEN) >= FANTOM_PASSKEY_LEN)
		return false;
	return true;
	
}

ViBoolean FantomModule::connect(ViConstString resourceName)
{
	// 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
	

	if (useBT and !nFANTOM100::iNXT::isPaired((ViConstString)resourceName,status))
		nFANTOM100::iNXT::pairBluetooth((ViConstString) resourceName, (ViConstString) passkey, (ViChar *) pairedResourceName, status);
	
	if (status.isNotFatal())
		{
			nxtPtr = nFANTOM100::iNXT::createNXT((ViConstString) resourceName, status, false);
		}
}

ViUInt32 FantomModule::send(const ViByte *bufferPtr, ViUInt32 numberOfBytes)
{
	nFANTOM100::tStatus status;
	
	return nxtPtr->write(bufferPtr, numberOfBytes, status);
}

ViUInt32 FantomModule::recv(ViByte *bufferPtr, ViUInt32 numberOfBytes)
{
	nFANTOM100::tStatus status;
	
	return nxtPtr->read(bufferPtr, numberOfBytes, status);
}	

ViBoolean FantomModule::close()
{
	
	const char *proto;
	useBT = strcmp(proto, FANTOM_BT) ? true : false;
	if (useBT and nFANTOM100::iNXT::isPaired((ViConstString)pairedResourceName,status))
		nFANTOM100::iNXT::unpairBluetooth((ViConstString) pairedResourceName, status);			// No Effect on Mac OSX

	if (nxtPtr)
		nFANTOM100::iNXT::destroyNXT(nxtPtr);
	nxtPtr = NULL;
	
	return true;
}

FantomModule::~FantomModule()
{
	if (nxtPtr)
		nFANTOM100::iNXT::destroyNXT(nxtPtr);
	nxtPtr = NULL;

}

// Skeleton functions from Xcode
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;
};