![Page 1: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/1.jpg)
CS193P - Lecture 15iPhone Application Development
iPhone Device APIsLocation, Accelerometer & Camera
Battery Life & Power Management
1Friday, February 26, 2010
![Page 2: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/2.jpg)
Announcements• Paparazzi 4 due Friday night at 11:59PM
■ Late days: use ’em if you’ve got ’em
• Work on final projects!
2Friday, February 26, 2010
![Page 3: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/3.jpg)
Today’s Topics• Hardware features
■ Image Picker & Camera■ Location■ Accelerometer
• Battery Life & Power Management
3Friday, February 26, 2010
![Page 4: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/4.jpg)
Lots of Cool Features
4Friday, February 26, 2010
![Page 5: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/5.jpg)
Device HardwareCamera
5Friday, February 26, 2010
![Page 6: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/6.jpg)
Device HardwareCore location
6Friday, February 26, 2010
![Page 7: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/7.jpg)
Device HardwareAccelerometers
7Friday, February 26, 2010
![Page 8: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/8.jpg)
Limited Simulator Support
8Friday, February 26, 2010
![Page 9: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/9.jpg)
Image Picker
9Friday, February 26, 2010
![Page 10: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/10.jpg)
The Image Picker InterfaceThe camera
10Friday, February 26, 2010
![Page 11: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/11.jpg)
The Image Picker InterfaceSaved photos
11Friday, February 26, 2010
![Page 12: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/12.jpg)
The Image Picker InterfaceThe photo library
12Friday, February 26, 2010
![Page 13: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/13.jpg)
The Image Picker InterfaceDisplaying the interface
• UIImagePickerController class■ Use as-is (no subclassing)■ Handles all user and device interactions■ UIViewController Subclass
• UIImagePickerControllerDelegate protocol■ Implemented by your delegate object
13Friday, February 26, 2010
![Page 14: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/14.jpg)
Displaying the Image PickerSteps for using
• Check the source availability• Assign a delegate object• Present the controller modally
14Friday, February 26, 2010
![Page 15: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/15.jpg)
Displaying the Image PickerCalled from a view controller object
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){ UIImagePickerController* picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self;
[self presentModalViewController:picker animated:YES];}
15Friday, February 26, 2010
![Page 16: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/16.jpg)
Selecting an Image
20Friday, February 26, 2010
![Page 17: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/17.jpg)
Defining Your Delegate ObjectThe UIImagePickerControllerDelegate protocol
• Two methods:
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo;
- (void)imagePickerControllerDidCancel: (UIImagePickerController*)picker;
21Friday, February 26, 2010
![Page 18: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/18.jpg)
Defining Your Delegate ObjectThe accept case
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo { // Save or use the image here.
// Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; [picker release];}
22Friday, February 26, 2010
![Page 19: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/19.jpg)
Defining Your Delegate ObjectThe cancel case
- (void)imagePickerControllerDidCancel: (UIImagePickerController*)picker{ // Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; [picker release];}
26Friday, February 26, 2010
![Page 20: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/20.jpg)
Manipulating the Returned Image Allowing users to edit returned images
• If allowsImageEditing property is YES:■ User allowed to crop the returned image■ Image metadata returned in editingInfo
29Friday, February 26, 2010
![Page 21: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/21.jpg)
Manipulating the Returned Image Allowing users to edit returned images
30Friday, February 26, 2010
![Page 22: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/22.jpg)
The editingInfo dictionaryManipulating the Returned Image
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo { // Save or use the image here.
// Dismiss the image picker. [self dismissModalViewControllerAnimated:YES]; [picker release];}
31Friday, February 26, 2010
![Page 23: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/23.jpg)
Manipulating the Returned Image The editingInfo dictionary
• Original image in UIImagePickerControllerOriginalImage key• Crop rectangle in UIImagePickerControllerCropRect key
32Friday, February 26, 2010
![Page 24: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/24.jpg)
Augmented RealityWalk around looking through a camera.What could possibly go wrong?
@property BOOL showsCameraControls;@property(retain) UIView cameraOverlayView;@property CGAffineTransform cameraViewTransform;
33Friday, February 26, 2010
![Page 25: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/25.jpg)
The Image Picker InterfaceCustom Camera Interface
34Friday, February 26, 2010
![Page 26: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/26.jpg)
Managing Image DataAvoid retaining images
Application
35Friday, February 26, 2010
![Page 27: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/27.jpg)
Saving ImagesWriting to the saved photos album
• UIImageWriteToSavedPhotosAlbum function■ Photos can be downloaded to iPhoto by user■ Optional completion callback
36Friday, February 26, 2010
![Page 28: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/28.jpg)
Saving VideosWriting to the saved photos album
• UIVideoAtPathIsCompatibleWithSavedPhotosAlbum
• UISaveVideoAtPathToSavedPhotosAlbum function■ Videos can be downloaded to iPhoto by user■ Optional completion callback
37Friday, February 26, 2010
![Page 29: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/29.jpg)
Available in the Simulator
38Friday, February 26, 2010
![Page 30: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/30.jpg)
Key TipsUsing UIImagePickerController effectively
• Always check the source availability• Your delegate methods do the cleanup• Be frugal with images• Available in the simulator
39Friday, February 26, 2010
![Page 31: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/31.jpg)
Core Location
40Friday, February 26, 2010
![Page 32: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/32.jpg)
Core LocationWhat is it?
41Friday, February 26, 2010
![Page 33: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/33.jpg)
Core LocationWhat is it?
Location ring
Activate service
41Friday, February 26, 2010
![Page 34: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/34.jpg)
Core LocationHow?
42Friday, February 26, 2010
![Page 35: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/35.jpg)
How?Core Location
43Friday, February 26, 2010
![Page 36: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/36.jpg)
Core LocationHow?
44Friday, February 26, 2010
![Page 37: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/37.jpg)
Core LocationLocation Technologies
Bootstrap
46Friday, February 26, 2010
![Page 38: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/38.jpg)
Core LocationLocation Technologies
Cross-check
47Friday, February 26, 2010
![Page 39: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/39.jpg)
Core LocationLocation Technologies
Complement
48Friday, February 26, 2010
![Page 40: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/40.jpg)
Core Location Framework
49Friday, February 26, 2010
![Page 41: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/41.jpg)
Core Location FrameworkThe core classes and protocols
• Classes■ CLLocationManager■ CLLocation
• Protocol■ CLLocationManagerDelegate
50Friday, February 26, 2010
![Page 42: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/42.jpg)
Core Location FrameworkCLLocationManagerDelegate protocol
• Two optional methods
• Called asynchronously on main thread• Issues movement-based updates
- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation;
- (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error;
51Friday, February 26, 2010
![Page 43: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/43.jpg)
Getting a LocationStarting the location service
CLLocationManager* locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;[locManager startUpdatingLocation];
54Friday, February 26, 2010
![Page 44: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/44.jpg)
- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
NSTimeInterval howRecent = [newLocation.timestamp timeIntervalSinceNow];
if (howRecent < -10) return;
if (newLocation.horizontalAccuracy > 100) return;
// Use the coordinate data. double lat = newLocation.coordinate.latitude; double lon = newLocation.coordinate.longitude;}
Getting a LocationUsing the event data
59Friday, February 26, 2010
![Page 45: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/45.jpg)
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{ // Use the coordinate data. CLLocationDirection heading = newHeading.trueHeading;}
Getting a HeadingUsing the event data
60Friday, February 26, 2010
![Page 46: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/46.jpg)
Power Play (beat Canada again): CLLocationManager Properties
61Friday, February 26, 2010
![Page 47: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/47.jpg)
Desired AccuracyChoosing an appropriate accuracy level
• Choose an appropriate accuracy level■ Higher accuracy impacts power consumption■ Lower accuracy is “good enough” in most cases
• Can change accuracy setting later if needed• Actual accuracy reported in CLLocation object
CLLocationManager* locManager = [[CLLocationManager alloc] init];
locManager.desiredAccuracy = kCLLocationAccuracyBest;
62Friday, February 26, 2010
![Page 48: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/48.jpg)
Distance FilterChoosing an appropriate update threshold
• New events delivered when threshold exceeded
CLLocationManager* locManager = [[CLLocationManager alloc] init];
locManager.distanceFilter = 3000;
63Friday, February 26, 2010
![Page 49: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/49.jpg)
• Restart the service later as needed
Stopping the Service
CLLocationManager* locManager = [[CLLocationManager alloc] init];[locManager startUpdatingLocation];
...
[locManager stopUpdatingLocation];
64Friday, February 26, 2010
![Page 50: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/50.jpg)
Responding to ErrorsUser may deny use of the location service
• Results in a kCLErrorDenied error
• Protects user privacy• Occurs on a per-application basis
65Friday, February 26, 2010
![Page 51: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/51.jpg)
Responding to ErrorsLocation may be unavailable
• Results in a kCLErrorLocationUnknown error
• Likely just temporary• Scan continues in background
66Friday, February 26, 2010
![Page 52: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/52.jpg)
Limited Simulator Support
67Friday, February 26, 2010
![Page 53: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/53.jpg)
Accelerometers
68Friday, February 26, 2010
![Page 54: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/54.jpg)
What Are Accelerometers?Measure changes in force
70Friday, February 26, 2010
![Page 55: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/55.jpg)
What Are Accelerometers?Measure changes in force
71Friday, February 26, 2010
![Page 56: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/56.jpg)
What Are Accelerometers?Measure changes in force
71Friday, February 26, 2010
![Page 57: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/57.jpg)
AccelerometersWhat are the uses?
72Friday, February 26, 2010
![Page 58: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/58.jpg)
AccelerometersWhat are the uses?
73Friday, February 26, 2010
![Page 59: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/59.jpg)
Kinds of OrientationThe physical vs the interface
• Physical Orientation■ How is the device positioned?
• Interface Orientation■ Where is the status bar?
• Examples: Photos & Safari
74Friday, February 26, 2010
![Page 60: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/60.jpg)
75Friday, February 26, 2010
![Page 61: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/61.jpg)
76Friday, February 26, 2010
![Page 62: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/62.jpg)
77Friday, February 26, 2010
![Page 63: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/63.jpg)
78Friday, February 26, 2010
![Page 64: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/64.jpg)
79Friday, February 26, 2010
![Page 65: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/65.jpg)
79Friday, February 26, 2010
![Page 66: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/66.jpg)
80Friday, February 26, 2010
![Page 67: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/67.jpg)
81Friday, February 26, 2010
![Page 68: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/68.jpg)
82Friday, February 26, 2010
![Page 69: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/69.jpg)
82Friday, February 26, 2010
![Page 70: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/70.jpg)
83Friday, February 26, 2010
![Page 71: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/71.jpg)
Orientation-Related ChangesGetting the physical orientation
• UIDevice class■ Start notifications
■ beginGeneratingDeviceOrientationNotifications■ Get Orientation
■ UIDeviceOrientationDidChangeNotification delivered to registered observers
■ orientation property■ Stop notifications
■ endGeneratingDeviceOrientationNotifications
84Friday, February 26, 2010
![Page 72: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/72.jpg)
Orientation-Related ChangesGetting the interface orientation
• UIApplication class■ statusBarOrientation property■ Defines interface orientation, not device orientation
• UIViewController class■ interfaceOrientation property
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
85Friday, February 26, 2010
![Page 73: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/73.jpg)
ShakeUndo!
• UIEvent type■ @property(readonly) UIEventType type;■ @property(readonly) UIEventSubtype subtype;
■ UIEventTypeMotion■ UIEventSubtypeMotionShake
86Friday, February 26, 2010
![Page 74: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/74.jpg)
Orientation changes are nice, but…
87Friday, February 26, 2010
![Page 75: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/75.jpg)
Wii Want Raw Data
0.5g
0.75g
1.0g
™
88Friday, February 26, 2010
![Page 76: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/76.jpg)
Wii Want Raw Data
0.5g
0.75g
1.0g
™
88Friday, February 26, 2010
![Page 77: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/77.jpg)
The Accelerometer InterfaceGetting the raw accelerometer data
• Part of the UIKit framework• Delivers 3-axis data• Configurable update frequency (approx 10–100Hz)• Delegate-based event delivery
89Friday, February 26, 2010
![Page 78: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/78.jpg)
Device Axis Orientation
+X
-X
+Y
-Y
-Z
+Z
90Friday, February 26, 2010
![Page 79: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/79.jpg)
The Accelerometer InterfaceGetting the raw accelerometer data
• Classes■ UIAccelerometer■ UIAcceleration
• Protocol■ UIAccelerometerDelegate
91Friday, February 26, 2010
![Page 80: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/80.jpg)
Configuring the AccelerometerStarting the event delivery
- (void)enableAccelerometerEvents{ UIAccelerometer* theAccel = [UIAccelerometer sharedAccelerometer]; theAccel.updateInterval = 1/50; // 50 Hz theAccel.delegate = self;}
92Friday, February 26, 2010
![Page 81: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/81.jpg)
Event delivery begins as soon asyou assign the delegate
96Friday, February 26, 2010
![Page 82: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/82.jpg)
Defining Your Delegate ObjectProcessing the accelerometer data
• Only one delegate per application• Delivered asynchronously to main thread
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{ // Get the event data UIAccelerationValue x, y, z; x = acceleration.x; y = acceleration.y; z = acceleration.z;
// Process the data... }
99Friday, February 26, 2010
![Page 83: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/83.jpg)
Configuring the AccelerometerChoosing an appropriate update frequency
• System range is approximately 10–100Hz• Frequency should be based on need
■ Determine the minimum frequency for your needs■ Don’t update too frequently
• Target ranges■ Game input: 30–60 Hz■ Orientation detection: 10–20 Hz
100Friday, February 26, 2010
![Page 84: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/84.jpg)
Disabling Event DeliveryStopping the event delivery
- (void)disableAccelerometerEvents{ UIAccelerometer* theAccel = [UIAccelerometer sharedAccelerometer];
theAccel.delegate = nil;}
101Friday, February 26, 2010
![Page 85: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/85.jpg)
Filtering Accelerometer DataUse filters to isolate data components
• Low-pass filter■ Isolates constant acceleration■ Used to find the device orientation
• High-pass filter■ Shows instantaneous movement only■ Used to identify user-initiated movement
102Friday, February 26, 2010
![Page 86: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/86.jpg)
Filtering Accelerometer DataExamining the accelerometer data
-1.0g
f(t)
103Friday, February 26, 2010
![Page 87: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/87.jpg)
Filtering Accelerometer DataBut, to apply a filter…
f(t) => F(ω)Fourier Transform
104Friday, February 26, 2010
![Page 88: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/88.jpg)
Filtering Accelerometer DataChanging to the frequency domain
f(t) F(ω)
Frequency (ω)
105Friday, February 26, 2010
![Page 89: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/89.jpg)
Filtering Accelerometer DataBut if we shake the device…
106Friday, February 26, 2010
![Page 90: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/90.jpg)
We see something more interesting…Filtering Accelerometer Data
f(t)
Frequency (ω)
F(ω)
107Friday, February 26, 2010
![Page 91: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/91.jpg)
We see something more interesting…Filtering Accelerometer Data
f(t)
Frequency (ω)
F(ω)
107Friday, February 26, 2010
![Page 92: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/92.jpg)
Frequency (ω)
F(ω)
Applying a low-pass filterFiltering Accelerometer Data
f(t)108Friday, February 26, 2010
![Page 93: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/93.jpg)
Frequency (ω)
F(ω)
Applying a low-pass filterFiltering Accelerometer Data
f(t)108Friday, February 26, 2010
![Page 94: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/94.jpg)
Frequency (ω)
F(ω)
Applying a low-pass filterFiltering Accelerometer Data
f(t)108Friday, February 26, 2010
![Page 95: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/95.jpg)
Filtering Accelerometer DataApplying a low-pass filter
• Simple low-pass filter example
#define FILTERFACTOR 0.1
value = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 - FILTERFACTOR));
previousValue = value;
109Friday, February 26, 2010
![Page 96: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/96.jpg)
Frequency (ω)
F(ω)
Applying a high-pass filterFiltering Accelerometer Data
f(t)110Friday, February 26, 2010
![Page 97: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/97.jpg)
Frequency (ω)
F(ω)
Applying a high-pass filterFiltering Accelerometer Data
f(t)110Friday, February 26, 2010
![Page 98: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/98.jpg)
Filtering Accelerometer DataApplying a high-pass filter
• Simple high-pass filter example
#define FILTERFACTOR 0.1
lowPassValue = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 - FILTERFACTOR));
previousLowPassValue = lowPassValue;
highPassValue = newAcceleration - lowPassValue;
111Friday, February 26, 2010
![Page 99: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/99.jpg)
Filtering Accelerometer DataBubble Level sample (low-pass filter)
112Friday, February 26, 2010
![Page 100: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/100.jpg)
Demo
113Friday, February 26, 2010
![Page 101: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/101.jpg)
Filtering Accelerometer DataBubble Level sample (low-pass filter)
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{ accelerationX = acceleration.x * kFilteringFactor + accelerationX * (1.0 - kFilteringFactor); accelerationY = acceleration.y * kFilteringFactor + accelerationY * (1.0 - kFilteringFactor);
currentRawReading = atan2(accelerationY,accelerationX); float calibtratedAngle = [self calibratedAngleFromAngle: currentRawReading];
[levelView updateToInclinationInRadians:calibratedAngle]; }
114Friday, February 26, 2010
![Page 102: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/102.jpg)
Demo
115Friday, February 26, 2010
![Page 103: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/103.jpg)
No Simulator Support
116Friday, February 26, 2010
![Page 104: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/104.jpg)
Key TipsUsing the Accelerometers Effectively
• Use UIViewControllers• Use filters to isolate raw data components• Disable accelerometer updates when not needed
■ Set your accelerometer delegate to nil
117Friday, February 26, 2010
![Page 105: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/105.jpg)
Summary• Take advantage of the device APIs, but…• For image picker, always check source availability• For hardware-based features, turn them off when not needed
118Friday, February 26, 2010
![Page 106: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/106.jpg)
Battery Life & Power Management
119Friday, February 26, 2010
![Page 107: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/107.jpg)
Power ManagementSmall devices need advanced power management
• Total power consumption■ Laptops: ~20-60W■ iPhone: 500 mW to 2.5W
• Dynamic clocking• Clock gating and power gating
■ Turning blocks on and off continuously
120Friday, February 26, 2010
![Page 108: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/108.jpg)
Power ConsumptionEverything consumes power
• Radios – up to ~2W■ Baseband, Wi-Fi, Bluetooth, GPS
• CPU/GPU – up to ~800 mW• Display – up to ~200 mW• Hardware modules – ~10s of mWs• Keeping the system awake – enormous impact
121Friday, February 26, 2010
![Page 109: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/109.jpg)
Be aware of power consumptionBattery Life
122Friday, February 26, 2010
![Page 110: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/110.jpg)
Be aware of power consumptionBattery Life
122Friday, February 26, 2010
![Page 111: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/111.jpg)
Power Consumption - RadiosThe network
• Transmitting is the most expensive operation• Minimize the amount of transmitted data• Avoid chatty protocols• Transmit/receive in bursts• Use compact data formats• Core Location
■ Stop the location service once you have a location fix■ Request only the location accuracy that you need
123Friday, February 26, 2010
![Page 112: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/112.jpg)
Power Consumption - CPU/GPUAll about performance
• Reduce CPU usage• Use Sample or Shark• Stress the GPU less – fewer layers, smaller textures, etc.
124Friday, February 26, 2010
![Page 113: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/113.jpg)
Power Consumption - Hardware ModulesAccelerometer, NAND, others
• Turn off what you don’t need• Accelerometer
■ Set the UIAccelerometer delegate to nil■ Support orientation changes only as needed
• NAND■ Access the disk less – use the System Usage instrument
125Friday, February 26, 2010
![Page 114: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/114.jpg)
Power Consumption - StandbyLet the system sleep
• Battery life drops from 250+ hours to <12 hours without sleep• Don’t disable the idle timer• Don’t play audio except when you need to
126Friday, February 26, 2010
![Page 115: CS193P - Lecture 15CS193P - Lecture 15 iPhone Application Development iPhone Device APIs Location, Accelerometer & Camera Battery Life & Power Management Friday, February 26, 2010](https://reader034.vdocuments.pub/reader034/viewer/2022042123/5e9e78bd119939727b2efe79/html5/thumbnails/115.jpg)
Questions?
127Friday, February 26, 2010