ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / Examples / Android / ardrone / project / src / com / parrot / ARDrone / DemoActivity.java
1 /*
2  * AR Drone demo
3  * This file is based on the NDK sample app "San Angeles"
4  */
5 package com.parrot.ARDrone;
6
7
8
9 import android.app.Activity;
10 import android.content.Context;
11 import android.opengl.GLSurfaceView;
12 import android.os.Bundle;
13 import android.text.Layout;
14 import android.util.Log;
15 import android.view.MotionEvent;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.view.WindowManager;
19 import android.hardware.Sensor;
20 import android.hardware.SensorEvent;
21 import android.hardware.SensorEventListener;
22 import android.hardware.SensorManager;
23 import android.widget.Button;
24
25 public class DemoActivity extends Activity implements SensorEventListener {
26         private static final  String LOG_TAG = "ARDrone Activity"; 
27         private SensorManager mSensorManager;
28     private DemoGLSurfaceView mGLView;
29         private Sensor mSensor;
30         private Button mMoveUpButton;
31         private Button mMoveDownButton;
32         private Button mTakeOffButton;
33         private Button mEmergLandButton;
34         private ViewGroup mLayoutView;
35         // private GLSurfaceView mLayoutView;
36         
37         // Used for toggle the takeoff/landing button text
38         private boolean mFlyingState = false;
39         
40         public final static int  COMMAND_TAKEOFF                        = 1;
41         public final static int  COMMAND_EMERGLAND      = 2;
42         public final static int  COMMAND_MOVEUP                         = 3;
43         public final static int  COMMAND_MOVEDOWN       = 4;    
44         public final static int  COMMAND_USE_ACCELEROMETER      = 5;    
45         
46     @Override
47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49        
50         setContentView( R.layout.main );
51         
52         mGLView =  (DemoGLSurfaceView) findViewById(R.id.glsurfaceview);
53
54         mGLView.initialize( this );
55
56         // Map initialize control buttons
57                 mTakeOffButton = (Button) findViewById( R.id.takeOffButton );
58                 mTakeOffButton.setOnTouchListener( new  View.OnTouchListener() {
59                         @Override
60                         public boolean onTouch(View v, MotionEvent event) {
61                         //      Log.d(LOG_TAG, event.toString() );
62                                 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
63                                         DemoActivity.nativeCommand(COMMAND_TAKEOFF, 0, 0, 0, 0, 0);
64                                         if( mFlyingState == false ) {
65                                                 mTakeOffButton.setText( "Land");
66                                         }
67                                         else {
68                                                 mTakeOffButton.setText( "Take off");
69                                         }
70                                         mFlyingState = !mFlyingState;
71                                 }
72
73                                 return false;
74                         }
75                 });
76                 
77                 mEmergLandButton = (Button) findViewById( R.id.emergLandingButton );
78                 mEmergLandButton.setOnTouchListener( new  View.OnTouchListener() {
79                         @Override
80                         public boolean onTouch(View v, MotionEvent event) {
81                 //              Log.d(LOG_TAG, event.toString() );
82                                 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
83                                         DemoActivity.nativeCommand(COMMAND_EMERGLAND, 0, 0, 0, 0, 0);
84                                 }
85
86                                 return false;
87                         }
88                 });
89                 
90                 mMoveUpButton = (Button) findViewById(  R.id.moveUpButton );
91                 mMoveUpButton.setOnTouchListener( new  View.OnTouchListener() {
92                         @Override
93                         public boolean onTouch(View v, MotionEvent event) {
94                         //      Log.d(LOG_TAG, event.toString() );
95                                 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
96                                         DemoActivity.nativeCommand(COMMAND_MOVEUP, 1, 0, 0, 0, 0);
97                                         DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER,  1, 0, 0, 0, 0);
98                                 }
99                                 else if( event.getAction() == MotionEvent.ACTION_UP) {
100                                         DemoActivity.nativeCommand(COMMAND_MOVEUP, 0, 0, 0, 0, 0);
101                                         // Enable accelerometer use for steering
102                                         DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER,  0, 0, 0, 0, 0);
103                                 }
104
105                                 return false;
106                         }
107                 });
108                 
109                 mMoveDownButton = (Button) findViewById( R.id.moveDownButton  );
110                 mMoveDownButton.setOnTouchListener( new  View.OnTouchListener() {
111                         @Override
112                         public boolean onTouch(View v, MotionEvent event) {
113                 //              Log.d(LOG_TAG, event.toString() );
114                                 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
115                                         DemoActivity.nativeCommand(COMMAND_MOVEDOWN, 1, 0, 0, 0, 0);
116                                         DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER,  1, 0, 0, 0, 0);
117                                 }
118                                 else if( event.getAction() == MotionEvent.ACTION_UP) {
119                                         DemoActivity.nativeCommand(COMMAND_MOVEDOWN, 0, 0, 0, 0, 0);
120                                         // Enable accelerometer use for steering
121                                         DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER,  0, 0, 0, 0, 0);
122                                 }
123
124                                 return false;
125                         }
126                 });
127
128
129                 mLayoutView = (ViewGroup) findViewById(  R.id.layoutView );
130                 mLayoutView.setOnTouchListener( new  View.OnTouchListener() {
131                         @Override
132                         public boolean onTouch(View v, MotionEvent event) {
133                         //      Log.d(LOG_TAG, event.toString() );
134                                 if( event.getAction() == MotionEvent.ACTION_DOWN ) {
135                                         // Disable accelerometer use for steering
136                                         DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER,  1, 0, 0, 0, 0);
137                                 }
138                                 else if( event.getAction() == MotionEvent.ACTION_UP) {
139                                         // Enable accelerometer use for steering
140                                         DemoActivity.nativeCommand(COMMAND_USE_ACCELEROMETER,  0, 0, 0, 0, 0);
141                                 }
142                                 return true;
143                         }
144                 });
145                 
146         // Set flags to keep screen from dimming
147         getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
148                              WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
149                 
150         
151         // Get Sensor Manager
152                 mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
153                 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
154     }
155
156     @Override
157     protected void onPause() {
158         super.onPause();
159         mGLView.onPause();
160                 mSensorManager.unregisterListener(this);
161     }
162
163     @Override
164     protected void onResume() {
165         super.onResume();
166         mGLView.onResume();
167                 // update every 200 ms (NORMAL), 60 ms (UI) or 20 ms (GAME)
168         mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
169     }
170
171     @Override
172     protected void onStop() {
173         super.onStop();
174         nativeStop();
175     }
176
177         public void onAccuracyChanged(Sensor sensor, int accuracy) {
178                 /* do nothing ? */
179     }
180
181     public void onSensorChanged(final SensorEvent ev) {
182                 
183         //Log.d(LOG_TAG, "azimuth= " + ev.values[0] +  " pitch= " + ev.values[1] +  "roll= " + ev.values[2] );
184         nativeSensorEvent(ev.values[0],ev.values[1],ev.values[2]);
185     }
186
187     static {
188         System.loadLibrary("ardrone");
189     }
190         
191    
192     /**
193      * Method for pass enumerated commands to native layer
194      * 
195      * @param commandId
196      * @param iparam1
197      * @param fparam1
198      * @param fparam2
199      * @param fparam3
200      * @param fparam4
201      */
202     private static native void nativeCommand(int commandId, int iparam1, float fparam1, float fparam2, float fparam3, float fparam4);
203     
204     private static native void nativeStop();
205         private static native void nativeSensorEvent(float x, float y, float z);
206 }
207
208
209