d567678ec1b00e8c4504060de1ea9a3ca2e083b3
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / Win32 / sdk_demo / Sources / custom_code.c
1 /********************************************************************
2  *                    COPYRIGHT PARROT 2010
3  ********************************************************************
4  *       PARROT - A.R.Drone SDK Windows Client Example
5  *-----------------------------------------------------------------*/
6 /**
7  * @file custom_code.c 
8  * @brief User added code
9  *
10  * @author sylvain.gaeremynck@parrot.com
11  * @date 2009/07/01
12  *
13  * @author Stephane Piskorski <stephane.piskorski.ext@parrot.fr>
14  * @date   Sept, 8. 2010
15  *
16  *******************************************************************/
17
18
19
20 #include <custom_code.h>
21
22 //ARDroneLib
23         #include <ardrone_tool/ardrone_time.h>
24         #include <ardrone_tool/Navdata/ardrone_navdata_client.h>
25         #include <ardrone_tool/Control/ardrone_control.h>
26         #include <ardrone_tool/UI/ardrone_input.h>
27
28 //Common
29         #include <config.h>
30         #include <ardrone_api.h>
31
32 //VP_SDK
33         #include <ATcodec/ATcodec_api.h>
34         #include <VP_Os/vp_os_print.h>
35         #include <VP_Api/vp_api_thread_helper.h>
36         #include <VP_Os/vp_os_signal.h>
37
38 //Local project
39         #include <UI/gamepad.h>
40         #include <Video/video_stage.h>
41         #include <UI/directx_rendering.h>
42
43 //Global variables
44         int32_t exit_ihm_program = 1;
45         vp_os_mutex_t consoleMutex;
46
47
48 /* Implementing Custom methods for the main function of an ARDrone application */
49
50
51
52 /*--------------------------------------------------------------------
53 The delegate object calls this method during initialization of an 
54 ARDrone application 
55 --------------------------------------------------------------------*/
56 C_RESULT ardrone_tool_init_custom(int argc, char **argv)
57 {
58         /* Change the console title */
59                 vp_os_mutex_init(&consoleMutex);
60                 system("cls");
61                 SetConsoleTitle(TEXT("Parrot A.R. Drone SDK Demo for Windows"));
62                 //CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&HmiStart,NULL,0,0);
63                 //CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)&HmiStart2,NULL,0,0);
64
65
66         /* Registering for a new device of game controller */
67                 ardrone_tool_input_add( &dx_keyboard );
68                 ardrone_tool_input_add( &dx_gamepad );
69         
70         /* Start all threads of your application */
71                 START_THREAD( directx_renderer_thread , NULL);
72                 START_THREAD( video_stage, NULL );
73
74                 
75   
76   return C_OK;
77 }
78
79
80
81 /*--------------------------------------------------------------------
82 The delegate object calls this method when the event loop exit     
83 --------------------------------------------------------------------*/
84 C_RESULT ardrone_tool_shutdown_custom()
85 {
86   /* Relinquish all threads of your application */
87   JOIN_THREAD( video_stage );
88
89   /* Unregistering for the current device */
90   ardrone_tool_input_remove( &dx_gamepad );
91   ardrone_tool_input_remove( &dx_keyboard );
92
93   /* Mutex needs to be destroyed but may still be used by navdata thread */
94   /* todo : synchronize threads shutdown */
95   //vp_os_mutex_destroy(&consoleMutex);
96
97   return C_OK;
98 }
99
100 /*--------------------------------------------------------------------
101 The event loop calls this method for the exit condition            
102 --------------------------------------------------------------------*/
103 bool_t ardrone_tool_exit()
104 {
105   return exit_ihm_program == 0;
106 }
107
108 C_RESULT signal_exit()
109 {
110   exit_ihm_program = 0;
111
112   return C_OK;
113 }
114
115 int custom_main(int argc,char**argv) { return 0;};
116
117
118 /* Implementing thread table in which you add routines of your application and those provided by the SDK */
119 BEGIN_THREAD_TABLE
120   THREAD_TABLE_ENTRY( ardrone_control, 20 )
121   THREAD_TABLE_ENTRY( navdata_update, 20 )
122   THREAD_TABLE_ENTRY( video_stage, 20 )
123   THREAD_TABLE_ENTRY( directx_renderer_thread, 20 )
124 END_THREAD_TABLE
125
126
127
128 /* Function to change the cursor place in the console window */
129
130         HANDLE hStdout =  NULL;  /* Handle to the output console */\r
131         CONSOLE_SCREEN_BUFFER_INFO csbiInfo;                            /* Information about the output console */
132
133
134
135 void ARWin32Demo_SetConsoleCursor(int x,int y)
136 {
137         if (hStdout==NULL) hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
138
139         if (hStdout != INVALID_HANDLE_VALUE){\r
140                         GetConsoleScreenBufferInfo(hStdout, &csbiInfo);\r
141                         csbiInfo.dwCursorPosition.X=x;\r
142                         csbiInfo.dwCursorPosition.Y=y;\r
143                         SetConsoleCursorPosition(hStdout,csbiInfo.dwCursorPosition);\r
144         }\r
145 }\r
146 \r
147 void ARWin32Demo_AcquireConsole(int x,int y)\r
148 {\r
149         vp_os_mutex_lock(&consoleMutex);\r
150 }\r
151 void ARWin32Demo_ReleaseConsole(int x,int y)\r
152 {\r
153         vp_os_mutex_unlock(&consoleMutex);\r
154 }