4 * Copyright 2009 Parrot SA. All rights reserved.
5 * @author D HAEYER Frederic
8 #import "MainViewController.h"
9 #import "GLViewController.h"
10 #import "InternalProtocols.h"
14 //#define DEBUG_ENNEMIES_DETECTON
15 //#define DEBUG_NAVIGATION_DATA
16 //#define DEBUG_DETECTION_CAMERA
17 //#define DEBUG_DRONE_CAMERA
19 extern navdata_unpacked_t ctrlnavdata;
20 extern char drone_address[];
21 extern ControlData ctrldata;
22 static bool_t threadStarted = false;
25 static void ARDroneCallback(ARDRONE_ENGINE_MESSAGE msg)
29 case ARDRONE_ENGINE_INIT_OK:
38 @interface ARDrone () <NavdataProtocol>
39 GLViewController *glviewctrl;
40 MainViewController *mainviewctrl;
43 id <ARDroneProtocolOut> _uidelegate;
44 ARDroneHUDConfiguration *hudconfig;
46 - (void) parrotNavdata:(navdata_unpacked_t*)data;
47 - (void) checkThreadStatus;
51 * Define a few methods to make it possible for the game engine to control the Parrot drone
53 @implementation ARDrone
58 * Initialize the Parrot library.<br/>
59 * Note: the library will clean-up everything it allocates by itself, when being destroyed (i.e. when its retain count reaches 0).
61 * @param frame Frame of parent view, used to create the library view (which shall cover the whole screen).
62 * @param inGame Initial state of the game at startup.
63 * @param uidelegate Pointer to the object that implements the Parrot protocol ("ARDroneProtocol"), which will be called whenever the library needs the game engine to change its state.
64 * @return Pointer to the newly initialized Parrot library instance.
66 - (id) initWithFrame:(CGRect)frame withState:(BOOL)inGame withDelegate:(id <ARDroneProtocolOut>)uidelegate withHUDConfiguration:(ARDroneHUDConfiguration*)hudconfiguration
68 if((self = [super init]) != nil)
70 NSLog(@"Frame ARDrone Engine : %f, %f", frame.size.width, frame.size.height);
72 #ifdef ENABLE_AUTO_TVOUT
73 [[[TVOut alloc] init] setupScreenMirroringWithFramesPerSecond:10];
77 inGameOnDemand = inGame;
78 threadStarted = false;
79 _uidelegate = uidelegate;
82 [[NSFileManager defaultManager]changeCurrentDirectoryPath:[[NSBundle mainBundle]resourcePath]];
83 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //creates paths so that you can pull the app's path from it
84 strcpy(root_dir, [[paths objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding]);
86 // Create View Controller
87 glviewctrl = [[GLViewController alloc] initWithFrame:frame withDelegate:self];
89 // Create main view controller
91 mainviewctrl = [[MainViewController alloc] initWithFrame:frame withState:inGame withDelegate:uidelegate withNavdataDelegate:self withControlData:&ctrldata withHUDConfiguration:hudconfiguration];
92 view = mainviewctrl.view;
94 NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
95 NSString *username = [NSString stringWithFormat:@".%@:%@", [[UIDevice currentDevice] model], [[UIDevice currentDevice] uniqueIdentifier]];
96 ardroneEngineStart(ARDroneCallback, [bundleName cStringUsingEncoding:NSUTF8StringEncoding], [username cStringUsingEncoding:NSUTF8StringEncoding]);
97 [self checkThreadStatus];
103 - (void) checkThreadStatus
105 NSLog(@"%s", __FUNCTION__);
107 [mainviewctrl setWifiReachabled:threadStarted];
112 [_uidelegate executeCommandOut:ARDRONE_COMMAND_RUN withParameter:(void*)drone_address fromSender:self];
113 [self changeState:inGameOnDemand];
117 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkThreadStatus) userInfo:nil repeats:NO];
122 * Render a frame. Basically, the Parrot Library renders:<ul>
123 * <li> A full screen textured quad in the background (= the video sent by the drone);</li>
124 * <li> A set of elements in the foreground (=HUD).</li>
129 // Make sure the library is "running"
130 [glviewctrl drawView];
135 [self changeState:NO];
137 [mainviewctrl release];
138 [glviewctrl release];
144 * Set what shall be the orientation of the screen when rendering a frame.
146 * @param right Orientation of the screen: FALSE for "landscape left", TRUE for "landscape right".
148 - (void)setScreenOrientationRight:(BOOL)right
150 NSLog(@"Screen orientation right : %d", right);
151 if(mainviewctrl != nil)
152 [mainviewctrl setScreenOrientationRight:right];
154 if(glviewctrl != nil)
155 [glviewctrl setScreenOrientationRight:right];
158 - (void)parrotNavdata:(navdata_unpacked_t*)data
160 ardrone_navdata_get_data(data);
164 * Get the latest drone's navigation data.
166 * @param data Pointer to a navigation data structure.
168 - (void)navigationData:(ARDroneNavigationData*)data
172 vp_os_memcpy(data, [mainviewctrl navigationData], sizeof(ARDroneNavigationData));
173 #ifdef DEBUG_NAVIGATION_DATA
174 static int numsamples = 0;
175 if(numsamples++ > 64)
177 NSLog(@"x : %f, y : %f, z : %f, flying state : %d, navdata video num frame : %d, video num frames : %d", data->angularPosition.x, data->angularPosition.y, data->angularPosition.z, data->flyingState, data->navVideoNumFrames, data->videoNumFrames);
185 * Get the latest detection camera structure (rotation and translation).
187 * @param data Pointer to a detection camera structure.
189 - (void)detectionCamera:(ARDroneDetectionCamera*)camera
193 vp_os_memcpy(camera, [mainviewctrl detectionCamera], sizeof(ARDroneDetectionCamera));
194 #ifdef DEBUG_DETECTION_CAMERA
195 static int numsamples = 0;
196 if(numsamples++ > 64)
198 NSLog(@"Detection Camera Rotation : %f %f %f %f %f %f %f %f %f",
199 camera->rotation[0][0], camera->rotation[0][1], camera->rotation[0][2],
200 camera->rotation[1][0], camera->rotation[1][1], camera->rotation[1][2],
201 camera->rotation[2][0], camera->rotation[2][1], camera->rotation[2][2]);
202 NSLog(@"Detection Camera Translation : %f %f %f", camera->translation[0], camera->translation[1], camera->translation[2]);
203 NSLog(@"Detection Camera Tag Index : %d", camera->tag_index);
212 * Get the latest drone camera structure (rotation and translation).
214 * @param data Pointer to a drone camera structure.
216 - (void)droneCamera:(ARDroneCamera*)camera
220 vp_os_memcpy(camera, [mainviewctrl droneCamera], sizeof(ARDroneCamera));
221 #ifdef DEBUG_DRONE_CAMERA
222 static int numsamples = 0;
223 if(numsamples++ > 64)
225 NSLog(@"Drone Camera Rotation : %f %f %f %f %f %f %f %f %f",
226 camera->rotation[0][0], camera->rotation[0][1], camera->rotation[0][2],
227 camera->rotation[1][0], camera->rotation[1][1], camera->rotation[1][2],
228 camera->rotation[2][0], camera->rotation[2][1], camera->rotation[2][2]);
229 NSLog(@"Drone Camera Translation : %f %f %f", camera->translation[0], camera->translation[1], camera->translation[2]);
237 * Exchange enemies data.<br/>
238 * Note: basically, data should be provided by the Parrot library when in multiplayer mode (enemy type = "HUMAN"), and by the game controller when in single player mode (enemy type = "AI").
240 * @param data Pointer to an enemies data structure.
242 - (void)humanEnemiesData:(ARDroneEnemiesData*)data
246 vp_os_memcpy(data, [mainviewctrl humanEnemiesData], sizeof(ARDroneEnemiesData));
247 #ifdef DEBUG_ENNEMIES_DETECTON
248 static int old_value = 0;
249 if(old_value != data->count)
250 NSLog(@"enemies detected : %d", data->count);
251 old_value = data->count;
256 - (void)changeState:(BOOL)inGame
258 // Check whether there is a change of state
261 // Change the state of the library
263 ardroneEngineResume();
265 ardroneEnginePause();
271 inGameOnDemand = inGame;
274 // Change state of view
275 [mainviewctrl changeState:inGame];
279 - (void)executeCommandIn:(ARDRONE_COMMAND_IN_WITH_PARAM)commandIn fromSender:(id)sender refreshSettings:(BOOL)refresh
281 [mainviewctrl executeCommandIn:commandIn fromSender:sender refreshSettings:refresh];
284 - (void)executeCommandIn:(ARDRONE_COMMAND_IN)commandId withParameter:(void*)parameter fromSender:(id)sender
286 [mainviewctrl executeCommandIn:commandId withParameter:parameter fromSender:sender];
289 - (void)setDefaultConfigurationForKey:(ARDRONE_CONFIG_KEYS)key withValue:(void *)value
291 [mainviewctrl setDefaultConfigurationForKey:key withValue:value];