bzr branch
/browse/iphone/tappity
| Line | Revision | Contents |
| 1 | 1 | // |
| 2 | // MarineCompassAppDelegate.m |
|
| 3 | // MarineCompass |
|
| 4 | // |
|
| 5 | // Created by döme on 26.07.2009. |
|
| 6 | // Copyright __MyCompanyName__ 2009. All rights reserved. |
|
| 7 | // |
|
| 8 | ||
| 9 | #import "MarineCompassAppDelegate.h" |
|
| 10 | #import "EAGLView.h" |
|
| 11 | 22 | #import "CompassViewController.h" |
| 12 | 38 | #import "ElAnimation.h" |
| 13 | 49 | #import "TappityServer.h" |
| 14 | 22 | |
| 15 | 38 | #import <UIKit/UIKit.h> |
| 16 | 22 | #import <CoreLocation/CoreLocation.h> |
| 17 | 1 | |
| 18 | 7 | #import <QuartzCore/QuartzCore.h> |
| 19 | #import <OpenGLES/EAGLDrawable.h> |
|
| 20 | #import <CoreGraphics/CoreGraphics.h> |
|
| 21 | ||
| 22 | 1 | @implementation MarineCompassAppDelegate |
| 23 | ||
| 24 | ||
| 25 | - (void)applicationDidFinishLaunching:(UIApplication *)application |
|
| 26 | { |
|
| 27 | 38 | self.view; |
| 28 | } |
|
| 29 | ||
| 30 | - (void) startAnimation |
|
| 31 | { |
|
| 32 | ElCallbackAnimation* anim = [[[ElCallbackAnimation alloc] init] autorelease]; |
|
| 33 | [anim setTarget: glView]; |
|
| 34 | [anim setCallback: @selector(animationCallback:)]; |
|
| 35 | [anim setDuration: HUGE_VALF]; |
|
| 36 | [anim queueForKey: @"glViewAnimation" atBeginning: NO cancelPending: NO]; |
|
| 37 | } |
|
| 38 | ||
| 39 | - (void) stopAnimation |
|
| 40 | { |
|
| 41 | [[[[ElAnimation alloc] init] autorelease] queueForKey: @"glViewAnimation" atBeginning: YES cancelPending: YES]; |
|
| 42 | } |
|
| 43 | ||
| 44 | - (void) viewDidLoad |
|
| 45 | { |
|
| 46 | ||
| 47 | [window addSubview: [self view]]; |
|
| 48 | 49 | |
| 49 | #if TARGET_IPHONE_SIMULATOR |
|
| 50 | TappityServer* tappityServer = [TappityServer sharedServer]; |
|
| 51 | [tappityServer startWithName: @"MarineCompass"]; |
|
| 52 | [tappityServer setHeadingDelegate: self]; |
|
| 53 | #else |
|
| 54 | 1 | lman = [[CLLocationManager alloc] init]; |
| 55 | |
|
| 56 | [lman setDelegate: self]; |
|
| 57 | |
|
| 58 | [lman startUpdatingHeading]; |
|
| 59 | 49 | #endif |
| 60 | 1 | |
| 61 | UIAccelerometer* accm = [UIAccelerometer sharedAccelerometer]; |
|
| 62 | [accm setDelegate: self]; |
|
| 63 | [accm setUpdateInterval: 0.01]; |
|
| 64 | |
|
| 65 | 6 | rawBaseTransform = midentity(); |
| 66 | baseCompassTransform = midentity(); |
|
| 67 | compassDiskTransform = midentity(); |
|
| 68 | 38 | |
| 69 | [self startAnimation]; |
|
| 70 | ||
| 71 | 6 | mag = (v3){0.1,0.2,0.3}; |
| 72 | 7 | |
| 73 | 49 | |
| 74 | ||
| 75 | 7 | /* |
| 76 | unsigned int cp0 = 0; |
|
| 77 | __asm__( |
|
| 78 | "MRC p15, 0, %0, c0, 0, 0" |
|
| 79 | : "=r" (cp0) |
|
| 80 | ); |
|
| 81 | */ |
|
| 82 | 1 | } |
| 83 | ||
| 84 | ||
| 85 | - (void)applicationWillResignActive:(UIApplication *)application { |
|
| 86 | 38 | |
| 87 | 1 | } |
| 88 | ||
| 89 | ||
| 90 | - (void)applicationDidBecomeActive:(UIApplication *)application { |
|
| 91 | 38 | |
| 92 | 1 | } |
| 93 | ||
| 94 | - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager |
|
| 95 | { |
|
| 96 | return YES; |
|
| 97 | } |
|
| 98 | ||
| 99 | - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading |
|
| 100 | { |
|
| 101 | 2 | v3 v = { [heading x], [heading y], [heading z] }; |
| 102 | |
|
| 103 | 3 | v = mtransformdir3(mtranspose(baseCompassTransform), v); |
| 104 | 2 | |
| 105 | // clamp delta |
|
| 106 | v3 dv = vsub(v, prevm); |
|
| 107 | dv = vclamp(dv, -0.2, 0.2); |
|
| 108 | ||
| 109 | v = vadd(prevm, dv); |
|
| 110 | ||
| 111 | mag = v; |
|
| 112 | 1 | |
| 113 | double ffactor = 0.05; |
|
| 114 | 2 | |
| 115 | ||
| 116 | v3 cv = mag; |
|
| 117 | v3 cvflat = cv; |
|
| 118 | cvflat.v.z = 0.0; |
|
| 119 | cvflat = vnormalize(cvflat); |
|
| 120 | |
|
| 121 | cvfp = vnormalize(vadd(vmul(cvfp, 1.0-ffactor), vmul(cvflat, ffactor))); |
|
| 122 | ||
| 123 | v3 Z = {0.0,0.0,1.0}; |
|
| 124 | v3 cright = vnormalize(vcross(Z, cvfp)); |
|
| 125 | |
|
| 126 | compassDiskTransform = mcreatefrombases(vnegate(cvfp), vnegate(cright), Z); |
|
| 127 | |
|
| 128 | prevm = v; |
|
| 129 | ||
| 130 | 8 | [label setText: [NSString stringWithFormat: @"%.1f µT", vlength(mag)]]; |
| 131 | 1 | } |
| 132 | ||
| 133 | - (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration*) acceleration |
|
| 134 | { |
|
| 135 | 2 | v3 v = { [acceleration x], [acceleration y], [acceleration z] }; |
| 136 | 1 | |
| 137 | 2 | |
| 138 | 1 | |
| 139 | double ffactor = 0.02; |
|
| 140 | |
|
| 141 | 2 | acc = vnormalize(vadd(vmul(acc, 1.0-ffactor), vmul(v, ffactor))); |
| 142 | ||
| 143 | ||
| 144 | // v3 up = vnormalize(vnegate(acc)); |
|
| 145 | 3 | |
| 146 | 6 | //v3 oldForward = {baseCompassTransform.a[4], baseCompassTransform.a[5], baseCompassTransform.a[6]}; |
| 147 | v3 oldForward = mtransformdir3(baseCompassTransform, cvfp); |
|
| 148 | 2 | |
| 149 | 3 | m16 newBaseCompassTransform = computeAlignedBasesFromDownVector2(acc, oldForward); |
| 150 | 2 | |
| 151 | 3 | m16 newRawBaseTransform = computeAlignedBasesFromDownVector2(v, oldForward); |
| 152 | 2 | |
| 153 | cvfp = mtransformdir3(baseCompassTransform, cvfp); |
|
| 154 | cvfp = mtransformdir3(mtranspose(newBaseCompassTransform), cvfp); |
|
| 155 | cvfp.v.z = 0.0; |
|
| 156 | cvfp = vnormalize(cvfp); |
|
| 157 | ||
| 158 | baseCompassTransform = newBaseCompassTransform; |
|
| 159 | rawBaseTransform = newRawBaseTransform; |
|
| 160 | 1 | } |
| 161 | ||
| 162 | 7 | - (IBAction) toggleCamera: (id) sender |
| 163 | { |
|
| 164 | UIBarButtonItemStyle style = [sender style]; |
|
| 165 | 8 | |
| 166 | 7 | if (style != UIBarButtonItemStyleDone) |
| 167 | { |
|
| 168 | |
|
| 169 | if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) |
|
| 170 | { |
|
| 171 | 8 | |
| 172 | //[compassViewController.view removeFromSuperview]; |
|
| 173 | 7 | |
| 174 | 38 | |
| 175 | [self.view setBackgroundColor: [UIColor clearColor]]; |
|
| 176 | [glView setBackgroundColor: [UIColor clearColor]]; |
|
| 177 | |
|
| 178 | self.view.layer.opaque = NO; |
|
| 179 | glView.layer.opaque = NO; |
|
| 180 | |
|
| 181 | [glView.layer setOpacity: 0.5f]; |
|
| 182 | 7 | |
| 183 | if (!imagePicker) |
|
| 184 | imagePicker = [[UIImagePickerController alloc] init]; |
|
| 185 | ||
| 186 | [imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera]; |
|
| 187 | [imagePicker setDelegate: self]; |
|
| 188 | [imagePicker setShowsCameraControls: NO]; |
|
| 189 | |
|
| 190 | 38 | [self.view setFrame: CGRectMake(0.0f,0.0f, 320.0f, 480.0f)]; |
| 191 | |
|
| 192 | [imagePicker setCameraOverlayView: self.view]; |
|
| 193 | |
|
| 194 | 8 | |
| 195 | 38 | [self presentModalViewController: imagePicker animated: NO]; |
| 196 | |
|
| 197 | //[self.view setFrame: [[self.view superview] bounds]]; |
|
| 198 | 7 | |
| 199 | 8 | // [viewpointButton setEnabled: NO]; |
| 200 | 7 | |
| 201 | [sender setStyle: UIBarButtonItemStyleDone]; |
|
| 202 | } |
|
| 203 | } |
|
| 204 | else |
|
| 205 | { |
|
| 206 | 38 | CALayer *eaglLayer = glView.layer; |
| 207 | 7 | |
| 208 | 38 | [self.view setBackgroundColor: [UIColor blackColor]]; |
| 209 | [glView setBackgroundColor: [UIColor clearColor]]; |
|
| 210 | ||
| 211 | 7 | eaglLayer.opaque = YES; |
| 212 | |
|
| 213 | 38 | [self.view setAlpha: 1.0f]; |
| 214 | 7 | |
| 215 | 38 | [self dismissModalViewControllerAnimated: NO]; |
| 216 | 7 | |
| 217 | 38 | [window addSubview: [self view]]; |
| 218 | [self.view setFrame: [[self.view superview] bounds]]; |
|
| 219 | 7 | |
| 220 | 8 | // [viewpointButton setEnabled: YES]; |
| 221 | |
|
| 222 | 7 | [sender setStyle: UIBarButtonItemStyleBordered]; |
| 223 | } |
|
| 224 | } |
|
| 225 | ||
| 226 | ||
| 227 | 1 | - (void)dealloc |
| 228 | { |
|
| 229 | [lman release]; |
|
| 230 | [window release]; |
|
| 231 | 38 | [glView release]; |
| 232 | 1 | [super dealloc]; |
| 233 | } |
|
| 234 | ||
| 235 | 2 | @synthesize baseCompassTransform, compassDiskTransform; |
| 236 | ||
| 237 | 1 | @end |
Loggerhead 1.17 is a web-based interface for Bazaar branches