4 * code originally nased on:"San Angeles" Android demo app
10 #include <android/log.h>
13 #include <ardrone_tool/UI/ardrone_input.h>
20 * Command passed down from Java UI layer
23 #define COMMAND_TAKEOFF_LAND 1
24 #define COMMAND_EMERGLAND 2
25 #define COMMAND_MOVEUP 3
26 #define COMMAND_MOVEDOWN 4
27 #define COMMAND_USE_ACCELEROMETER 5
29 typedef struct _private_data
38 static private_data data = { 0 };
39 static int sWindowWidth = 800;
40 static int sWindowHeight = 850;
41 static int sDemoStopped = 0;
42 static long sTimeOffset = 0;
43 static int sTimeOffsetInit = 0;
44 static long sTimeStopped = 0;
45 static instance_navdata_t nav;
48 * Returns time in milliseconds
55 gettimeofday(&now, NULL);
56 return (long)(now.tv_sec*1000 + now.tv_usec/1000);
59 /* Call to initialize the graphics state */
61 Java_com_parrot_ARDrone_DemoRenderer_nativeInit( JNIEnv* env )
65 ardrone_navdata_reset_data( &nav );
71 Java_com_parrot_ARDrone_DemoRenderer_nativeResize( JNIEnv* env, jobject thiz, jint w, jint h )
75 __android_log_print(ANDROID_LOG_INFO, "ARDrone", "resize w=%d h=%d", w, h);
78 /* Call to finalize the graphics state */
80 Java_com_parrot_ARDrone_DemoRenderer_nativeDone( JNIEnv* env )
86 /* Call to render the next GL frame */
88 Java_com_parrot_ARDrone_DemoRenderer_nativeRender( JNIEnv* env )
92 /* NOTE: if sDemoStopped is TRUE, then we re-render the same frame
96 curTime = sTimeStopped + sTimeOffset;
98 curTime = _getTime() + sTimeOffset;
99 if (sTimeOffsetInit == 0) {
101 sTimeOffset = -curTime;
105 ardrone_navdata_get_data( &nav );
109 ardrone_at_set_progress_cmd( 1,
122 ardrone_at_set_progress_cmd( 1,
130 INFO( "roll=%f, pitch=%f, gaz=%f, yaw=%f\n",
138 appRender(curTime, sWindowWidth, sWindowHeight);
141 /* This is called to indicate to the render loop that it should
142 * stop as soon as possible.
145 Java_com_parrot_ARDrone_DemoGLSurfaceView_nativePause( JNIEnv* env )
147 sDemoStopped = !sDemoStopped;
149 /* we paused the animation, so store the current
150 * time in sTimeStopped for future nativeRender calls */
151 sTimeStopped = _getTime();
153 /* we resumed the animation, so adjust the time offset
154 * to take care of the pause interval. */
155 sTimeOffset -= _getTime() - sTimeStopped;
159 #define TRACKBALL_THRESHOLD 32
162 Java_com_parrot_ARDrone_DemoGLSurfaceView_nativeTrackballEvent(JNIEnv *env,
172 data.isRefresh = FALSE;
176 /* horizontal gestures = YAW control
178 * x = 1: idle position
181 nx += (int)(100.0*x);
182 INFO( "nx=%d\n", nx );
184 // FIXME: use trackball to control yaw in a better way
185 if (nx <= -TRACKBALL_THRESHOLD) {
189 else if (nx >= TRACKBALL_THRESHOLD) {
193 data.isRefresh = TRUE;
199 Java_com_parrot_ARDrone_DemoGLSurfaceView_nativeMotionEvent(JNIEnv *env,
206 if (motion_info.state == 1) {
207 INFO("touch %ld @(%d,%d)\n", (long)eventTime,(int)x, (int)y);
213 Java_com_parrot_ARDrone_DemoGLSurfaceView_nativeKeyEvent(JNIEnv *env,
216 //FIXME: thread synchronization required here !!
217 trackball_info.state = (int)action;
218 INFO("KEY %d\n", (int)action);
223 Java_com_parrot_ARDrone_DemoActivity_nativeSensorEvent(JNIEnv *env,
229 struct orientation_info orientation;
233 orientation.values[0] = (int)x;
234 orientation.values[1] = (int)y;
235 orientation.values[2] = (int)z;
236 // orientation values in degrees
238 data.pitch = -(orientation.values[2]+20)*600;
239 if (data.pitch >= 25000) {
242 if (data.pitch <= -25000) {
245 data.roll = orientation.values[1]*600;
246 if (data.roll >= 25000) {
249 if (data.roll <= -25000) {
256 Java_com_parrot_ARDrone_DemoActivity_nativeStop( JNIEnv* env )
260 void Java_com_parrot_ARDrone_DemoActivity_nativeCommand( JNIEnv* env, jobject thiz, jint commandId, jint iparam1, jfloat fparam1, jfloat fparam2, jfloat fparam3, jfloat fparam4 )
262 static uint8_t select = 0;
264 __android_log_print(ANDROID_LOG_INFO, "ARDrone", "command received %d with iparam1=%d\n", commandId, iparam1 );
266 switch( (int)commandId )
268 case COMMAND_TAKEOFF_LAND:
269 if( !nav.startPressed )
271 ardrone_tool_set_ui_pad_start( 1 );
275 ardrone_tool_set_ui_pad_start( 0 );
278 case COMMAND_EMERGLAND:
280 ardrone_tool_set_ui_pad_select( select );
286 data.isRefresh = TRUE;
290 data.isRefresh = FALSE;
293 case COMMAND_MOVEDOWN:
297 data.isRefresh = TRUE;
301 data.isRefresh = FALSE;
304 case COMMAND_USE_ACCELEROMETER:
307 data.isRefresh = TRUE;
311 data.isRefresh = FALSE;
315 __android_log_print(ANDROID_LOG_INFO, "ARDrone", "unrecognized command received %d", commandId );