/*void test(void * null){
	while(1){
		printf("将来在这里处理无人机控制事件");
		//sleep(2);
	}}*/
int main(int argc, char **argv)
{
    C_RESULT res;
    const char* appname = argv[0];
    WSADATA wsaData = {0};
    int iResult = 0;
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        wprintf(L"WSAStartup failed: %d\n", iResult);
        return 1;
    }


#include <VP_Os/vp_os_signal.h>
#if defined USE_PTHREAD_FOR_WIN32
#pragma comment (lib,"pthreadVC2.lib")
#endif

    res = test_drone_connection();//检测WiFi连接
    if(res!=0) {
        printf("%s","Could not detect the drone version ... press <Enter> to try connecting anyway.\n");
        getchar();
        WSACleanup();
        exit(-1);
    }

    init_client();
    res = ardrone_tool_setup_com( NULL );//配置AT命令,视频传输
    if( FAILED(res) ) {
        PRINT("Wifi initialization failed.\n");
        return -1;
    }

    res = ardrone_tool_init(argc, argv);//调用ardrone_tool_init_custom()函数
    while( VP_SUCCEEDED(res) && ardrone_tool_exit() == FALSE ) {
        res = ardrone_tool_update();
    }//循环调用ardrone_too_update()函数

    res = ardrone_tool_shutdown();
    closesocket(ConnectSocket);
    WSACleanup();
    system("cls");
    printf("End of SDK Demo for Windows\n");
    getchar();
    return VP_SUCCEEDED(res) ? 0 : -1;
}
int main(int argc , char** argv)
{
  int cnt=0;
    
	  C_RESULT res;					// functions return value


	  WSADATA wsaData = {0};
	  int iResult = 0;

    
	/* Initializes Windows socket subsystem */
		iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
		if (iResult != 0) {	wprintf(L"WSAStartup failed: %d\n", iResult);	return 1;	}
			
	/* Includes the Pthread for Win32 Library if necessary */
		#include <VP_Os/vp_os_signal.h>
			#if defined USE_PTHREAD_FOR_WIN32
			#pragma comment (lib,"pthreadVC2.lib")
			#endif
#ifdef VIDEO_RECORD
  cv_writer = cvCreateVideoWriter("D:\\out.avi",-1,15,cvSize(320,240),1);
#endif
  cv_image = cvCreateImage(cvSize(320,240),IPL_DEPTH_8U,3);
  //Kinect_Init();
	/* 	Initializes communication sockets	*/	
    hSocketThread = CreateThread(NULL,0,Socket_ProcessThread,NULL,0,0);
   // while (1);
		res = test_drone_connection();
		if(res!=0){
			printf("%s","Could not detect the drone version ... press <Enter> to try connecting anyway.\n");
			getchar();
			//WSACleanup(); exit(-1);
		}

		res = ardrone_tool_setup_com( NULL );
		if( FAILED(res) ){  PRINT("Wifi initialization failed.\n");  return -1;	}

	/* Initialises ARDroneTool */
	   res = ardrone_tool_init(argc, argv);

    /*record video*/
     
     
   /* Keeps sending AT commands to control the drone as long as 
		everything is OK */

      while( VP_SUCCEEDED(res) && ardrone_tool_exit() == FALSE) {
#ifdef VIDEO_RECORD
        if ( cnt++ > 3000 )
          cvReleaseVideoWriter(&cv_writer);
#endif
        res = ardrone_tool_update();     
    
      }
      printf("out\n");
   /**/
      //close Socket Thread
    if ( hSocketThread!=NULL ) {
      WaitForSingleObject(hSocketThread,INFINITE);
      CloseHandle(hSocketThread);
    }
      res = ardrone_tool_shutdown();
    //Kinect_UnInit();
	  WSACleanup();

  /* Bye bye */
  cvReleaseVideoWriter(&cv_writer);
	system("cls");
	  printf("End of SDK Demo for Windows\n");
	  getchar();
	  return VP_SUCCEEDED(res) ? 0 : -1;
}
DEFINE_THREAD_ROUTINE(ihm, data)
{
	C_RESULT res;

	WSADATA wsaData = {0};
	int iResult = 0;


	/* Initializes Windows socket subsystem */
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0)
	{
		wprintf(L"WSAStartup failed: %d\n", iResult);
		return 1;
	}

 
	/* Initializes communication sockets */	
	res = test_drone_connection(); // Nick disabled the press enter (wait)
	if(res!=0)
	{
		printf("%s","Could not detect the drone version ... press <Enter> to try connecting anyway.\n");
		getchar();
		//WSACleanup();
		exit(-1);
	}


	res = ardrone_tool_setup_com( NULL );
	if( FAILED(res) )
	{
		PRINT("Wifi initialization failed.\n");
		return -1;
	}


	START_THREAD(video_stage, 0);

	res = ardrone_tool_init(WIFI_ARDRONE_IP, strlen(WIFI_ARDRONE_IP), NULL, ARDRONE_CLIENT_APPNAME, ARDRONE_CLIENT_USRNAME);

	ardrone_tool_set_refresh_time(20); // 20 ms

	ardrone_at_reset_com_watchdog();


	// config
	ardrone_control_config.euler_angle_max = 0.20943951f; // 12 degrees
	ardrone_control_config.video_channel	= ZAP_CHANNEL_VERT;
	ardrone_control_config.video_codec		= UVLC_CODEC; //P264_CODEC;
	ardrone_control_config.navdata_demo		= FALSE;
	ardrone_control_config.altitude_max		= 10000;
	ardrone_control_config.control_vz_max	= 1000.0f;
	ardrone_control_config.outdoor			= FALSE;
	//ardrone_control_config.flight_without_shell = TRUE;


	ARDRONE_TOOL_CONFIGURATION_ADDEVENT(video_channel, &ardrone_control_config.video_channel, (ardrone_tool_configuration_callback) ardrone_demo_config_callback);
	//ARDRONE_TOOL_CONFIGURATION_ADDEVENT(video_channel, &ardrone_control_config.video_channel, NULL);
	//ARDRONE_TOOL_CONFIGURATION_ADDEVENT(video_codec, &ardrone_control_config.video_codec, NULL);
	ARDRONE_TOOL_CONFIGURATION_ADDEVENT (navdata_demo, &ardrone_control_config.navdata_demo, NULL);
	ARDRONE_TOOL_CONFIGURATION_ADDEVENT (altitude_max, &ardrone_control_config.altitude_max, NULL);
	ARDRONE_TOOL_CONFIGURATION_ADDEVENT (control_vz_max, &ardrone_control_config.control_vz_max, NULL);
	//ARDRONE_TOOL_CONFIGURATION_ADDEVENT (outdoor, &ardrone_control_config.outdoor, NULL);
	//ARDRONE_TOOL_CONFIGURATION_ADDEVENT (flight_without_shell, &ardrone_control_config.flight_without_shell, NULL);
	ARDRONE_TOOL_CONFIGURATION_ADDEVENT (euler_angle_max, &ardrone_control_config.euler_angle_max, NULL);


	// flat trim
	ardrone_at_set_flat_trim();


	//SetEvent(ardrone_ready);
	//ardrone_demo_redirect_to_interface = 1;


	while( VP_SUCCEEDED(res) && ardrone_tool_exit() == FALSE )
	{
		res = ardrone_tool_update();
	}

	JOIN_THREAD(video_stage);

	res = ardrone_tool_shutdown();

	WSACleanup();

	return (THREAD_RET)res;
}
	int _stdcall InitDrone() // TODO : Should SSID be passed in here?
	{
		
	  C_RESULT res;					// functions return value
	  const char* appname = "droneapp";
	  WSADATA wsaData = {0};
	  int iResult = 0;


	/* Initializes Windows socket subsystem */
		iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
		
		if (iResult != 0) 
		{	
			wprintf(L"WSAStartup failed: %d\n", iResult);	
			return -3;	
		}
			
	/* Includes the Pthread for Win32 Library if necessary */
		#include <VP_Os/vp_os_signal.h>
			#if defined USE_PTHREAD_FOR_WIN32
			#pragma comment (lib,"pthreadVC2.lib")
			#endif

		
		res = test_drone_connection();
		
		if( res !=0 )
		{
			// Failed;
			WSACleanup(); 
			return -1;
		}

		res = ardrone_tool_setup_com(NULL); // Can pass in the SSID here
		
		if(FAILED(res))
		{  
			// Wifi init has failed
			return -2;	
		}

		/* Initialises ARDroneTool */
		res = ardrone_tool_init(0, NULL);
		if(FAILED(res))
		{
			return -4;
		}

		// Success
		return 0;

		//-----------------------------------
		//while (VP_SUCCEEDED(res) && ardrone_tool_exit() == FALSE)
		//	res = ardrone_tool_update(); 

	  //res = ardrone_tool_shutdown();
    
	  //WSACleanup();

      /* Bye bye */
	  return VP_SUCCEEDED(res) ? 0 : -1;
	}