void LUA_STOP_COMMAND_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			CSimxSocket* s=allConnections.getConnectionFromPort(p->inputInt[0]);
			if ( (s!=NULL)&&s->getActiveOnlyDuringSimulation() )
			{
				allConnections.removeSocketConnection(s);
				result=1;
			}
			else
				simSetLastError(LUA_STOP_COMMAND,"Invalid port number."); // output an error
		}
		else
			simSetLastError(LUA_STOP_COMMAND,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_STOP_COMMAND,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return
}
VREP_DLLEXPORT void v_repDecimateMesh(void* data)
{
	// Collect info from V-REP:
	void** valPtr=(void**)data;
	float* verticesIn=((float*)valPtr[0]);
	int verticesInLength=((int*)valPtr[1])[0];
	int* indicesIn=((int*)valPtr[2]);
	int indicesInLength=((int*)valPtr[3])[0];
	float decimationPercentage=((float*)valPtr[4])[0];
	int interfaceVersion=((int*)valPtr[5])[0]; // should be zero for the current version

	std::vector<float> verticesOut;
	std::vector<int> indicesOut;
	bool result=compute(verticesIn,verticesInLength,indicesIn,indicesInLength,decimationPercentage,verticesOut,indicesOut);
	((bool*)valPtr[6])[0]=result;
	if (result)
	{
		float* v=(float*)simCreateBuffer(verticesOut.size()*sizeof(float));
		for (size_t i=0;i<verticesOut.size();i++)
			v[i]=verticesOut[i];
		((float**)valPtr[7])[0]=v;
		((int*)valPtr[8])[0]=verticesOut.size();
		int* ind=(int*)simCreateBuffer(indicesOut.size()*sizeof(int));
		for (size_t i=0;i<indicesOut.size();i++)
			ind[i]=indicesOut[i];
		((int**)valPtr[9])[0]=ind;
		((int*)valPtr[10])[0]=indicesOut.size();
	}
}
Exemple #3
0
void LUA_END_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	simLockInterface(1);

	int result=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			if ( (p->inputInt[0]>=0)&&(p->inputInt[0]<4)&&(startCountPerDevice[p->inputInt[0]]>0) )
			{
				startCountOverall--;
				startCountPerDevice[p->inputInt[0]]--;
				if (startCountPerDevice[p->inputInt[0]]==0)
				{ // No one is using this device anymore... we remove it
					EnterCriticalSection(&m_cs);
					_allDevices[p->inputInt[0]]->Disconnect();
					delete _allDevices[p->inputInt[0]];
					_allDevices[p->inputInt[0]]=NULL;
					LeaveCriticalSection(&m_cs);
				}
				if (startCountOverall==0)
					killThread();
				result=1;
			}
			else
				simSetLastError(LUA_END,"Invalid device index."); // output an error
		}
		else
			simSetLastError(LUA_END,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_END,"Not enough arguments."); // output an error


	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return

	simLockInterface(0);
}
VREP_DLLEXPORT simChar* v_repImportUrdf(simChar* filenameOrUrdf, simBool hideCollisionLinks, simBool hideJoints, simBool convexDecomposeNonConvexCollidables, simBool createVisualIfNone, simBool showConvexDecompositionDlg, simBool centerAboveGround, simBool makeModel, simBool noSelfCollision, simBool positionCtrl) {

    robot Robot(filenameOrUrdf, hideCollisionLinks, hideJoints, convexDecomposeNonConvexCollidables, createVisualIfNone, showConvexDecompositionDlg, centerAboveGround, makeModel, noSelfCollision, positionCtrl);

    simChar* result = (simChar*)simCreateBuffer(Robot.name.length()+1);
    memcpy((void*) result, (void*) Robot.name.c_str(), Robot.name.length()+1);

    return result;
} 
void LUA_RESET_COMMAND_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			CSimxSocket* s=allConnections.getConnectionFromPort(p->inputInt[0]);
			if (s!=NULL)
			{
				bool simulOnly=s->getActiveOnlyDuringSimulation();
				bool debug=s->getDebug();
				int maxPacketS=s->getMaxPacketSize();
				bool triggerPreEnabled=s->getWaitForTriggerAuthorized();

				// Kill the thread/connection:
				allConnections.removeSocketConnection(s);
				
				// Now create a similar thread/connection:
				CSimxSocket* oneSocketConnection=new CSimxSocket(p->inputInt[0],simulOnly,debug,maxPacketS,triggerPreEnabled);
				oneSocketConnection->start();
				allConnections.addSocketConnection(oneSocketConnection);
				
				result=1;
			}
			else
				simSetLastError(LUA_RESET_COMMAND,"Invalid port number."); // output an error
		}
		else
			simSetLastError(LUA_RESET_COMMAND,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_RESET_COMMAND,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return
}
void LUA_END_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			if ( (p->inputInt[0]<4)&&(startCountPerDevice[p->inputInt[0]]>0) )
			{
				startCountOverall--;
				startCountPerDevice[p->inputInt[0]]--;
				if (startCountPerDevice[p->inputInt[0]]==0)
				{
					EnterCriticalSection(&m_cs);
					deinitCapture(p->inputInt[0]);
					delete[] captureInfo[p->inputInt[0]].mTargetBuf;
					openCaptureDevices[p->inputInt[0]]=false;
					LeaveCriticalSection(&m_cs);
				}
				if (startCountOverall==0)
					killThread();
				result=1;
			}
			else
				simSetLastError(LUA_END,"Invalid device index."); // output an error
		}
		else
			simSetLastError(LUA_END,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_END,"Not enough arguments."); // output an error


	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return
}
void v_repImportUrdfCallback(SLuaCallBack* p)
{
    if (p->inputArgCount == 10) {
        simChar* name = v_repImportUrdf(p->inputChar,p->inputBool[0],p->inputBool[1],p->inputBool[2],p->inputBool[3],
                    p->inputBool[4],p->inputBool[5],p->inputBool[6],p->inputBool[7],p->inputBool[8]);

        p->outputArgCount = 1;

        p->outputArgTypeAndSize = (simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt));
        p->outputArgTypeAndSize[0] = sim_lua_arg_string;
        p->outputArgTypeAndSize[1] = 1;

        p->outputChar = (simChar*)simCreateBuffer(strlen(name)+1);
        memcpy((void*)p->outputChar, (void*)name, strlen(name)+1);

    } else {
        simSetLastError("simExtImportUrdfCallback","Wrong number of arguments.");
        p->outputArgCount = 0;
    }
}
void LUA_INFO_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	char infoString[200];
	infoString[0]=0;

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) one int as argument
			if ( (countCaptureDevices()>p->inputInt[0])&&(p->inputInt[0]>=0)&&(p->inputInt[0]<4) )
			{
				getCaptureDeviceName(p->inputInt[0],infoString,200);
			}
			else
				simSetLastError(LUA_INFO,"Wrong index."); // output an error
		}
		else
			simSetLastError(LUA_INFO,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_INFO,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	int l=int(strlen(infoString));
	if (l!=0)
	{
		p->outputArgCount=1; // 1 return value
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_string;	// The return value is a string
		p->outputArgTypeAndSize[2*0+1]=1;					// Not used (table size if the return value was a table)
		p->outputChar=(simChar*)simCreateBuffer(l+1); // 1 string return value
		for (int i=0;i<l;i++)
			p->outputChar[i]=infoString[i];
		p->outputChar[l]=0;
	}
	else
		p->outputArgCount=0; // no return values (nil)
}
Exemple #9
0
void LUA_SET_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	simLockInterface(1);

	int result=-1; // error

	if (p->inputArgCount>2)
	{ // Ok, we have at least 3 input argument
		if ( (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)&&(p->inputArgTypeAndSize[1*2+0]==sim_lua_arg_int)&&(p->inputArgTypeAndSize[2*2+0]==sim_lua_arg_bool) )
		{ // Ok, we have (at least) 2 ints and a bool as argument
			if ( (p->inputInt[0]>=0)&&(p->inputInt[0]<4)&&(startCountPerDevice[p->inputInt[0]]>0) )
			{ // Ok, we have an "open" device here
				_allDeviceData[p->inputInt[0]].leds=BYTE(p->inputInt[1]);
				_allDeviceData[p->inputInt[0]].rumble=(p->inputBool[1]!=0);
				result=1;
			}
			else
				simSetLastError(LUA_SET,"Invalid device index."); // output an error
		}
		else
			simSetLastError(LUA_SET,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_SET,"Not enough arguments."); // output an error


	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return

	simLockInterface(0);
}
void LUA_STATUS_COMMAND_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error or no connection at given index
	int info[5];
	int clientVersion=-1;

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			CSimxSocket* s=allConnections.getConnectionFromPort(p->inputInt[0]);
//			CSimxSocket* s=allConnections.getConnectionAtIndex(p->inputInt[0]);
			if (s!=NULL)
			{
				result=s->getStatus();
				s->getInfo(info);
				clientVersion=s->getClientVersion();
			}
		}
		else
			simSetLastError(LUA_STATUS_COMMAND,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_STATUS_COMMAND,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	if (result>-1)
	{
		p->outputArgCount=4; // 4 return values
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*1+0]=sim_lua_arg_int|sim_lua_arg_table;	// The return value is an int table
		p->outputArgTypeAndSize[2*1+1]=5;				// Table size
		p->outputArgTypeAndSize[2*2+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*2+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*3+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*3+1]=1;				// Not used (table size if the return value was a table)
		p->outputInt=(simInt*)simCreateBuffer(8*sizeof(result)); // total of 8 ints
		p->outputInt[0]=result; // This is the int value we want to return (arg 1)
		// Now the table values:
		for (int i=0;i<5;i++)
			p->outputInt[1+i]=info[i];
		p->outputInt[6]=SIMX_VERSION; // This is the server version info (arg 3)
		p->outputInt[7]=clientVersion; // This is the client version info (arg 4)
	}
	else
	{
		p->outputArgCount=4; // 4 return values
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*1+0]=sim_lua_arg_nil;	// The return value is nil
		p->outputArgTypeAndSize[2*1+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*2+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*2+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*3+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*3+1]=1;				// Not used (table size if the return value was a table)
		p->outputInt=(simInt*)simCreateBuffer(3*sizeof(result)); // 3 int return value
		p->outputInt[0]=result; // This is the int value we want to return
		p->outputInt[1]=SIMX_VERSION; // This is the second int value we want to return
		p->outputInt[2]=clientVersion; // This is the third int value we want to return
	}
}
void LUA_START_COMMAND_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			bool err=false;
			int maxPacketSize=1300;
			if (p->inputInt[0]<0)
				maxPacketSize=3200000; // when using shared memory
			bool debug=false;
			bool triggerPreEnabled=false; // 3/3/2014
			if (p->inputArgCount>1)
			{
				if (p->inputArgTypeAndSize[1*2+0]==sim_lua_arg_int)
				{
					maxPacketSize=p->inputInt[1];
					if (p->inputInt[0]<0)
					{ // when using shared memory
						if (maxPacketSize<1000)
							maxPacketSize=1000;
						if (maxPacketSize>32000000)
							maxPacketSize=32000000;
					}
					else
					{ // when using sockets
						if (maxPacketSize<200)
							maxPacketSize=200;
						if (maxPacketSize>30000)
							maxPacketSize=30000;
					}
				}
				else
					err=true;
				if (p->inputArgCount>2)
				{
					if (p->inputArgTypeAndSize[2*2+0]==sim_lua_arg_bool)
						debug=(p->inputBool[0]!=0);
					else
						err=true;

					// 3/3/2014
					if (p->inputArgCount>3)
					{
						if (p->inputArgTypeAndSize[3*2+0]==sim_lua_arg_bool)
							triggerPreEnabled=(p->inputBool[1]!=0);
						else
							err=true;
					}
				}
			}
			if (!err)
			{ 
				CSimxSocket* s=allConnections.getConnectionFromPort(p->inputInt[0]);
				if (s==NULL)
				{
					CSimxSocket* oneSocketConnection=new CSimxSocket(p->inputInt[0],true,debug,maxPacketSize,triggerPreEnabled); // 3/3/2014
					oneSocketConnection->start();
					allConnections.addSocketConnection(oneSocketConnection);
					result=1;
				}
				else
					simSetLastError(LUA_START_COMMAND,"Invalid port number."); // output an error
			}
			else
				simSetLastError(LUA_START_COMMAND,"Wrong argument type/size."); // output an error
		}
		else
			simSetLastError(LUA_START_COMMAND,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_START_COMMAND,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return
}
void LUA_GRAB_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int retVal=0; // Means error

	if (p->inputArgCount>1)
	{ // Ok, we have at least 2 input argument
		if ( (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)&&(p->inputArgTypeAndSize[1*2+0]==sim_lua_arg_int) )
		{ // Ok, we have (at least) 2 ints as argument
			if ( (countCaptureDevices()>p->inputInt[0])&&(p->inputInt[0]>=0)&&(p->inputInt[0]<4) )
			{
				if (startCountPerDevice[p->inputInt[0]]>0)
				{
					if (openCaptureDevices[p->inputInt[0]])
					{
						int errorModeSaved;
						simGetIntegerParameter(sim_intparam_error_report_mode,&errorModeSaved);
						simSetIntegerParameter(sim_intparam_error_report_mode,sim_api_errormessage_ignore);
						int t=simGetObjectType(p->inputInt[1]);
						simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved); // restore previous settings
						if (t==sim_object_visionsensor_type)
						{
							int r[2]={0,0};
							simGetVisionSensorResolution(p->inputInt[1],r);
							if ( (r[0]==captureInfo[p->inputInt[0]].mWidth)&&(r[1]==captureInfo[p->inputInt[0]].mHeight) )
							{
								float* buff=new float[r[0]*r[1]*3];

								for (int i=0;i<r[1];i++)
								{
									int y0=r[0]*i;
									int y1=r[0]*(r[1]-i-1);
									for (int j=0;j<r[0];j++)
									{ // Info is provided as BGR!! (and not RGB)
										buff[3*(y0+j)+0]=float(((BYTE*)captureInfo[p->inputInt[0]].mTargetBuf)[4*(y1+j)+2])/255.0f;
										buff[3*(y0+j)+1]=float(((BYTE*)captureInfo[p->inputInt[0]].mTargetBuf)[4*(y1+j)+1])/255.0f;
										buff[3*(y0+j)+2]=float(((BYTE*)captureInfo[p->inputInt[0]].mTargetBuf)[4*(y1+j)+0])/255.0f;
									}
								}
								simSetVisionSensorImage(p->inputInt[1],buff);
								delete[] buff;
								retVal=1;								
							}
							else
								simSetLastError(LUA_GRAB,"Resolutions do not match."); // output an error
						}
						else
							simSetLastError(LUA_GRAB,"Invalid vision sensor handle."); // output an error
					}
					else
						simSetLastError(LUA_GRAB,"Resolution was not set."); // output an error
				}
				else
					simSetLastError(LUA_GRAB,"simExtCamStart was not called."); // output an error
			}
			else
				simSetLastError(LUA_GRAB,"Wrong index."); // output an error
		}
		else
			simSetLastError(LUA_GRAB,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_GRAB,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(retVal)); // 1 int return value
	p->outputInt[0]=retVal; // This is the int value we want to return
}
void LUA_START_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error
	int returnResolution[2]={0,0};

	if (p->inputArgCount>2)
	{ // Ok, we have at least 3 input argument
		if ( (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)&&(p->inputArgTypeAndSize[1*2+0]==sim_lua_arg_int)&&(p->inputArgTypeAndSize[2*2+0]==sim_lua_arg_int) )
		{ // Ok, we have (at least) 3 ints as argument
			if ( (countCaptureDevices()>p->inputInt[0])&&(p->inputInt[0]>=0)&&(p->inputInt[0]<4) )
			{
				if (!openCaptureDevices[p->inputInt[0]])
				{ // We can set the new resolution
					bool goOn=true;
					if (startCountOverall==0)
					{ // Launch the thread!
						_camThreadLaunched=false;
						CreateThread(NULL,0,_camThread,NULL,THREAD_PRIORITY_NORMAL,NULL);
						while (!_camThreadLaunched)
							Sleep(2);
						if (deviceCount<1)
						{
							simSetLastError(LUA_START,"ESCAPI initialization failure or no devices found."); // output an error
							killThread();
							goOn=false;
						}
					}

					if (goOn)
					{
						captureInfo[p->inputInt[0]].mWidth=p->inputInt[1];
						captureInfo[p->inputInt[0]].mHeight=p->inputInt[2];
						captureInfo[p->inputInt[0]].mTargetBuf=new int[p->inputInt[1]*p->inputInt[2]];
						if (initCapture(p->inputInt[0],&captureInfo[p->inputInt[0]])!=0)
						{
							doCapture(p->inputInt[0]);
							openCaptureDevices[p->inputInt[0]]=true;
							returnResolution[0]=p->inputInt[1];
							returnResolution[1]=p->inputInt[2];
							result=1; // success!
							startCountOverall++;
							startCountPerDevice[p->inputInt[0]]++;
						}
						else
						{
							delete[] captureInfo[p->inputInt[0]].mTargetBuf;
							simSetLastError(LUA_START,"Device may already be in use."); // output an error
						}
					}
				}
				else
				{ // We have to retrieve the current resolution
					returnResolution[0]=captureInfo[p->inputInt[0]].mWidth;
					returnResolution[1]=captureInfo[p->inputInt[0]].mHeight;
					result=0;
					startCountOverall++;
					startCountPerDevice[p->inputInt[0]]++;
				}
			}
			else
				simSetLastError(LUA_START,"Invalid device index."); // output an error
		}
		else
			simSetLastError(LUA_START,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_START,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	if (result>-1)
	{
		p->outputArgCount=3; // 3 return values
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*1+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*1+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*2+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*2+1]=1;				// Not used (table size if the return value was a table)
		p->outputInt=(simInt*)simCreateBuffer(3*sizeof(result)); // 1 int return value
		p->outputInt[0]=result; // This is the int value we want to return
		p->outputInt[1]=returnResolution[0]; // This is the int value we want to return
		p->outputInt[2]=returnResolution[1]; // This is the int value we want to return
	}
	else
	{
		p->outputArgCount=1; // 1 return value
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
		p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
		p->outputInt[0]=result; // This is the int value we want to return
	}
}
Exemple #14
0
void CLuaFunctionData::writeDataToLua_luaFunctionCall(SLuaCallBack* p,const int* expectedArguments)
{ // use this when doing a Lua function call from a plugin
	p->inputArgCount=0;
	p->inputBool=NULL;
	p->inputInt=NULL;
	p->inputFloat=NULL;
	p->inputDouble=NULL;
	p->inputChar=NULL;
	p->inputCharBuff=NULL;
	p->inputArgTypeAndSize=NULL;

	p->outputArgCount=expectedArguments[0];
	p->outputBool=NULL;
	p->outputInt=NULL;
	p->outputFloat=NULL;
	p->outputDouble=NULL;
	p->outputChar=NULL;
	p->outputCharBuff=NULL;
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt));
	for (int i=0;i<p->outputArgCount*2;i++)
		p->outputArgTypeAndSize[i]=expectedArguments[1+i];

	int itemCnt=int(_inData.size());
	if (itemCnt>0)
	{
		p->inputArgCount=itemCnt;
		p->inputArgTypeAndSize=(simInt*)simCreateBuffer(p->inputArgCount*2*sizeof(simInt));

		int boolDataCnt=0;
		int intDataCnt=0;
		int floatDataCnt=0;
        int doubleDataCnt=0;
        int charDataCnt=0;
		int charBuffDataCnt=0;

		for (int i=0;i<itemCnt;i++)
		{
			if (_inData[i].isTable())
			{ // table
				if (_inData[i].getType()==-1)
				{
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_nil|sim_lua_arg_table;
					p->inputArgTypeAndSize[i*2+1]=_inData[i].getNilTableSize();
				}
				if (_inData[i].getType()==0)
				{
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_bool|sim_lua_arg_table;
					p->inputArgTypeAndSize[i*2+1]=int(_inData[i].boolData.size());
					boolDataCnt+=p->inputArgTypeAndSize[i*2+1];
				}
				if (_inData[i].getType()==1)
				{
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_int|sim_lua_arg_table;
					p->inputArgTypeAndSize[i*2+1]=int(_inData[i].intData.size());
					intDataCnt+=p->inputArgTypeAndSize[i*2+1];
				}
				if (_inData[i].getType()==2)
				{
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_float|sim_lua_arg_table;
					p->inputArgTypeAndSize[i*2+1]=int(_inData[i].floatData.size());
					floatDataCnt+=p->inputArgTypeAndSize[i*2+1];
				}
                if (_inData[i].getType()==5)
                {
                    p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_double|sim_lua_arg_table;
                    p->inputArgTypeAndSize[i*2+1]=int(_inData[i].doubleData.size());
                    doubleDataCnt+=p->inputArgTypeAndSize[i*2+1];
                }
                if (_inData[i].getType()==3)
				{
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_string|sim_lua_arg_table;
					p->inputArgTypeAndSize[i*2+1]=int(_inData[i].stringData.size());
					for (int j=0;j<int(_inData[i].stringData.size());j++)
						charDataCnt+=int(_inData[i].stringData[j].length())+1;
				}
			}
			else
			{
				if (_inData[i].getType()==-1)
				{
					p->inputArgTypeAndSize[i*2+1]=0;
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_nil;
				}
				if (_inData[i].getType()==0)
				{
					p->inputArgTypeAndSize[i*2+1]=0;
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_bool;
					boolDataCnt++;
				}
				if (_inData[i].getType()==1)
				{
					p->inputArgTypeAndSize[i*2+1]=0;
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_int;
					intDataCnt++;
				}
				if (_inData[i].getType()==2)
				{
					p->inputArgTypeAndSize[i*2+1]=0;
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_float;
					floatDataCnt++;
				}
                if (_inData[i].getType()==5)
                {
                    p->inputArgTypeAndSize[i*2+1]=0;
                    p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_double;
                    doubleDataCnt++;
                }
                if (_inData[i].getType()==3)
				{
					p->inputArgTypeAndSize[i*2+1]=0;
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_string;
					charDataCnt+=int(_inData[i].stringData[0].length())+1;
				}
				if (_inData[i].getType()==4)
				{
					p->inputArgTypeAndSize[i*2+1]=int(_inData[i].stringData[0].length());
					p->inputArgTypeAndSize[i*2+0]=sim_lua_arg_charbuff;
					charBuffDataCnt+=int(_inData[i].stringData[0].length());
				}
			}
		}

		// Now create the buffers:
		p->inputBool=(simBool*)simCreateBuffer(boolDataCnt*sizeof(simBool));
		p->inputInt=(simInt*)simCreateBuffer(intDataCnt*sizeof(simInt));
		p->inputFloat=(simFloat*)simCreateBuffer(floatDataCnt*sizeof(simFloat));
        p->inputDouble=(simDouble*)simCreateBuffer(doubleDataCnt*sizeof(simDouble));
        p->inputChar=(simChar*)simCreateBuffer(charDataCnt*sizeof(simChar));
		p->inputCharBuff=(simChar*)simCreateBuffer(charBuffDataCnt*sizeof(simChar));
		
		// Now populate the buffers:
		int boolDataOff=0;
		int intDataOff=0;
		int floatDataOff=0;
        int doubleDataOff=0;
        int charDataOff=0;
		int charBuffDataOff=0;

		for (int i=0;i<itemCnt;i++)
		{
			if (_inData[i].isTable())
			{ // table
				if (_inData[i].getType()==0)
				{
					for (int j=0;j<int(_inData[i].boolData.size());j++)
						p->inputBool[boolDataOff++]=_inData[i].boolData[j];
				}
				if (_inData[i].getType()==1)
				{
					for (int j=0;j<int(_inData[i].intData.size());j++)
						p->inputInt[intDataOff++]=_inData[i].intData[j];
				}
				if (_inData[i].getType()==2)
				{
					for (int j=0;j<int(_inData[i].floatData.size());j++)
						p->inputFloat[floatDataOff++]=_inData[i].floatData[j];
				}
                if (_inData[i].getType()==5)
                {
                    for (int j=0;j<int(_inData[i].doubleData.size());j++)
                        p->inputDouble[doubleDataOff++]=_inData[i].doubleData[j];
                }
                if (_inData[i].getType()==3)
				{
					for (int j=0;j<int(_inData[i].stringData.size());j++)
					{
						for (int k=0;k<int(_inData[i].stringData[j].length());k++)
							p->inputChar[charDataOff++]=_inData[i].stringData[j][k];
						p->inputChar[charDataOff++]=0;
					}
				}
			}
			else
			{
				if (_inData[i].getType()==0)
					p->inputBool[boolDataOff++]=_inData[i].boolData[0];
				if (_inData[i].getType()==1)
					p->inputInt[intDataOff++]=_inData[i].intData[0];
				if (_inData[i].getType()==2)
					p->inputFloat[floatDataOff++]=_inData[i].floatData[0];
                if (_inData[i].getType()==5)
                    p->inputDouble[doubleDataOff++]=_inData[i].doubleData[0];
                if (_inData[i].getType()==3)
				{
					for (int j=0;j<int(_inData[i].stringData[0].length());j++)
						p->inputChar[charDataOff++]=_inData[i].stringData[0][j];
					p->inputChar[charDataOff++]=0;
				}
				if (_inData[i].getType()==4)
				{
					for (int j=0;j<int(_inData[i].stringData[0].length());j++)
						p->inputCharBuff[charBuffDataOff++]=_inData[i].stringData[0][j];
				}
			}
		}
	}
}
Exemple #15
0
void LUA_GET_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	simLockInterface(1);

	int result=-1; // error

	struct SWiiData returnData;

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			if ( (p->inputInt[0]>=0)&&(p->inputInt[0]<4)&&(startCountPerDevice[p->inputInt[0]]>0) )
			{ // Ok, we have an "open" device here
				returnData.buttonStates=_allDeviceData[p->inputInt[0]].buttonStates;
				returnData.accelerationX=_allDeviceData[p->inputInt[0]].accelerationX;
				returnData.accelerationY=_allDeviceData[p->inputInt[0]].accelerationY;
				returnData.accelerationZ=_allDeviceData[p->inputInt[0]].accelerationZ;
				returnData.roll=_allDeviceData[p->inputInt[0]].roll;
				returnData.pitch=_allDeviceData[p->inputInt[0]].pitch;
				returnData.batteryState=_allDeviceData[p->inputInt[0]].batteryState;
				result=1;
			}
			else
				simSetLastError(LUA_GET,"Invalid device index."); // output an error
		}
		else
			simSetLastError(LUA_GET,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_GET,"Not enough arguments."); // output an error

	// Now we prepare the return value(s):
	if (result==-1)
	{ // We just return -1 in case of an error
		p->outputArgCount=1; // 1 return value
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
		p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
		p->outputInt[0]=result; // This is the int value we want to return
	}
	else
	{ // We return: result,buttons,acceleration(table_3),orientation(table_2),battery
		p->outputArgCount=5; // 5 return values
		p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*5*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer

		p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*1+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*1+1]=1;				// Not used (table size if the return value was a table)
		p->outputArgTypeAndSize[2*2+0]=sim_lua_arg_float|sim_lua_arg_table;	// The return value is a float table
		p->outputArgTypeAndSize[2*2+1]=3;				// table size is 3
		p->outputArgTypeAndSize[2*3+0]=sim_lua_arg_float|sim_lua_arg_table;	// The return value is a float table
		p->outputArgTypeAndSize[2*3+1]=2;				// table size is 2
		p->outputArgTypeAndSize[2*4+0]=sim_lua_arg_int;	// The return value is an int
		p->outputArgTypeAndSize[2*4+1]=1;				// Not used (table size if the return value was a table)

		p->outputInt=(simInt*)simCreateBuffer(3*sizeof(int)); // 1+1+1 int return value
		p->outputInt[0]=result;
		p->outputInt[1]=int(returnData.buttonStates);
		p->outputInt[2]=int(returnData.batteryState);

		p->outputFloat=(simFloat*)simCreateBuffer(5*sizeof(float)); // 3+2 float return value
		p->outputFloat[0]=returnData.accelerationX;
		p->outputFloat[1]=returnData.accelerationY;
		p->outputFloat[2]=returnData.accelerationZ;

		p->outputFloat[3]=returnData.roll*DEGREE_TO_RAD;
		p->outputFloat[4]=returnData.pitch*DEGREE_TO_RAD;
	}

	simLockInterface(0);
}
Exemple #16
0
void LUA_START_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	simLockInterface(1);

	int deviceIndex=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			int effectiveDeviceIndex=-1;
			int desiredIndex=p->inputInt[0];
			if (desiredIndex==-1)
			{ // We want any free device (not yet being used)
				// We search for a free slot:
				for (int i=0;i<4;i++)
				{
					if (_allDevices[i]==NULL)
					{
						desiredIndex=i;
						break;
					}
				}
			}
			if ( (desiredIndex>=0)&&(desiredIndex<4) )
			{
				if (_allDevices[desiredIndex]==NULL)
				{ // We connect to a new device!
					wiimote* it=new wiimote();
					it->ChangedCallback=on_state_change;
					it->CallbackTriggerFlags=(state_change_flags)(CONNECTED|EXTENSION_CHANGED|MOTIONPLUS_CHANGED);
					if (it->Connect(desiredIndex+1))
					{ // Success!
						_allDeviceData[desiredIndex].buttonStates=0;
						_allDeviceData[desiredIndex].batteryState=0;
						_allDeviceData[desiredIndex].accelerationX=0.0f;
						_allDeviceData[desiredIndex].accelerationY=0.0f;
						_allDeviceData[desiredIndex].accelerationZ=0.0f;
						_allDeviceData[desiredIndex].pitch=0.0f;
						_allDeviceData[desiredIndex].roll=0.0f;
						_allDeviceData[desiredIndex].rumble=0;
						_allDeviceData[desiredIndex].leds=0;
						_allDeviceData[desiredIndex].initialized=false;
						_allDevices[desiredIndex]=it;
					}
					else
						desiredIndex=-1; // No more devices available!
				}
			}
			if ( (desiredIndex>=0)&&(desiredIndex<4) )
			{
				if (startCountOverall==0)
				{ // Launch the thread!
					_wiiThreadLaunched=false;
					CreateThread(NULL,0,_wiiThread,NULL,THREAD_PRIORITY_NORMAL,NULL);
					while (!_wiiThreadLaunched)
						Sleep(2);
				}
				startCountOverall++;
				startCountPerDevice[desiredIndex]++;
				deviceIndex=desiredIndex;
			}
			else
				simSetLastError(LUA_START,"Invalid device index, or no available device at given index. Is device connected?"); // output an error
		}
		else
			simSetLastError(LUA_START,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_START,"Not enough arguments."); // output an error

	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(deviceIndex)); // 1 int return value
	p->outputInt[0]=deviceIndex; // This is the int value we want to return

	simLockInterface(0);
}