Skip to content

Commit 90daa44

Browse files
author
Darcy Liu
committed
add Teslameter
1 parent eb4ebcf commit 90daa44

23 files changed

+1861
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,4 +1236,14 @@ AccelerometerGraph sample application graphs the motion of the device. It demons
12361236

12371237
[URL](https://developer.apple.com/library/ios/#samplecode/AccelerometerGraph/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007410)
12381238

1239-
Last Revision: Version 2.5, 2010-06-28
1239+
Last Revision: Version 2.5, 2010-06-28
1240+
1241+
#Teslameter#
1242+
1243+
This application implements a Teslameter, a magnetic field detector. It displays the raw x, y, and z magnetometer values, a plotted history of those values, and a computed magnitude (size or strength) of the magnetic field.
1244+
1245+
The use of the Core Location API for getting "heading" data is contained in the TeslameterViewController class. It creates a CLLocationManager object and uses it to get heading by invoking -[CLLocationManager startUpdatingHeading]. It implements the CLLocationManagerDelegate APIs for receiving heading and updates its user interface accordingly.
1246+
1247+
[URL](https://developer.apple.com/library/ios/#samplecode/Teslameter/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008931)
1248+
1249+
Last Revision: Version 1.2, 2010-06-28

Teslameter.zip

58.3 KB
Binary file not shown.

Teslameter/Classes/AppDelegate.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
File: AppDelegate.h
3+
Abstract: The application delegate, adds the main view to the window and displays the window.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import <UIKit/UIKit.h>
49+
50+
@class TeslameterViewController;
51+
52+
@interface AppDelegate : NSObject <UIApplicationDelegate> {
53+
UIWindow *window;
54+
TeslameterViewController *viewController;
55+
}
56+
57+
@property (nonatomic, retain) IBOutlet UIWindow *window;
58+
@property (nonatomic, retain) IBOutlet TeslameterViewController *viewController;
59+
60+
@end

Teslameter/Classes/AppDelegate.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
File: AppDelegate.m
3+
Abstract: The application delegate, adds the main view to the window and displays the window.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import "AppDelegate.h"
49+
#import "TeslameterViewController.h"
50+
51+
@implementation AppDelegate
52+
53+
@synthesize window;
54+
@synthesize viewController;
55+
56+
- (void)applicationDidFinishLaunching:(UIApplication *)application {
57+
[window addSubview:viewController.view];
58+
[window makeKeyAndVisible];
59+
}
60+
61+
62+
- (void)dealloc {
63+
[viewController release];
64+
[window release];
65+
[super dealloc];
66+
}
67+
68+
69+
@end

Teslameter/Classes/GraphView.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
File: GraphView.h
3+
Abstract: A custom view for plotting history of x, y, and z magnetic values.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import "TeslameterViewController.h"
49+
50+
@interface GraphView : UIView {
51+
NSUInteger nextIndex;
52+
CLHeadingComponentValue history[150][3];
53+
}
54+
55+
- (void)updateHistoryWithX:(CLHeadingComponentValue)x y:(CLHeadingComponentValue)y z:(CLHeadingComponentValue)z;
56+
57+
@end
58+

Teslameter/Classes/GraphView.m

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
File: GraphView.m
3+
Abstract: A custom view for plotting history of x, y, and z magnetic values.
4+
Version: 1.2
5+
6+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7+
Inc. ("Apple") in consideration of your agreement to the following
8+
terms, and your use, installation, modification or redistribution of
9+
this Apple software constitutes acceptance of these terms. If you do
10+
not agree with these terms, please do not use, install, modify or
11+
redistribute this Apple software.
12+
13+
In consideration of your agreement to abide by the following terms, and
14+
subject to these terms, Apple grants you a personal, non-exclusive
15+
license, under Apple's copyrights in this original Apple software (the
16+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
17+
Software, with or without modifications, in source and/or binary forms;
18+
provided that if you redistribute the Apple Software in its entirety and
19+
without modifications, you must retain this notice and the following
20+
text and disclaimers in all such redistributions of the Apple Software.
21+
Neither the name, trademarks, service marks or logos of Apple Inc. may
22+
be used to endorse or promote products derived from the Apple Software
23+
without specific prior written permission from Apple. Except as
24+
expressly stated in this notice, no other rights or licenses, express or
25+
implied, are granted by Apple herein, including but not limited to any
26+
patent rights that may be infringed by your derivative works or by other
27+
works in which the Apple Software may be incorporated.
28+
29+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34+
35+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42+
POSSIBILITY OF SUCH DAMAGE.
43+
44+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
45+
46+
*/
47+
48+
#import "GraphView.h"
49+
50+
@implementation GraphView
51+
52+
- (void)updateHistoryWithX:(CLHeadingComponentValue)x y:(CLHeadingComponentValue)y z:(CLHeadingComponentValue)z {
53+
54+
// Add to history.
55+
history[nextIndex][0] = x;
56+
history[nextIndex][1] = y;
57+
history[nextIndex][2] = z;
58+
59+
// Advance the index counter.
60+
nextIndex = (nextIndex + 1) % 150;
61+
62+
// Mark itself as needing to be redrawn.
63+
[self setNeedsDisplay];
64+
}
65+
66+
- (void)drawGraphInContext:(CGContextRef)context withBounds:(CGRect)bounds {
67+
CGFloat value, temp;
68+
69+
// Save any previous graphics state settings before setting the color and line width for the current draw.
70+
CGContextSaveGState(context);
71+
CGContextSetLineWidth(context, 1.0);
72+
73+
// Draw the intermediate lines
74+
CGContextSetGrayStrokeColor(context, 0.6, 1.0);
75+
CGContextBeginPath(context);
76+
for (value = -5 + 1.0; value <= 5 - 1.0; value += 1.0) {
77+
78+
if (value == 0.0) {
79+
continue;
80+
}
81+
temp = 0.5 + roundf(bounds.origin.y + bounds.size.height / 2 + value / (2 * 5) * bounds.size.height);
82+
CGContextMoveToPoint(context, bounds.origin.x, temp);
83+
CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, temp);
84+
}
85+
CGContextStrokePath(context);
86+
87+
// Draw the center line
88+
CGContextSetGrayStrokeColor(context, 0.25, 1.0);
89+
CGContextBeginPath(context);
90+
temp = 0.5 + roundf(bounds.origin.y + bounds.size.height / 2);
91+
CGContextMoveToPoint(context, bounds.origin.x, temp);
92+
CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, temp);
93+
CGContextStrokePath(context);
94+
95+
// Restore previous graphics state.
96+
CGContextRestoreGState(context);
97+
}
98+
99+
- (void)drawHistory:(NSUInteger)axis fromIndex:(NSUInteger)index inContext:(CGContextRef)context bounds:(CGRect)bounds {
100+
CGFloat value;
101+
102+
CGContextBeginPath(context);
103+
for (NSUInteger counter = 0; counter < 150; ++counter) {
104+
// UIView referential has the Y axis going down, so we need to draw upside-down.
105+
value = history[(index + counter) % 150][axis] / -128;
106+
if (counter > 0) {
107+
CGContextAddLineToPoint(context, bounds.origin.x + (float)counter / (float)(150 - 1) * bounds.size.width, bounds.origin.y + bounds.size.height / 2 + value * bounds.size.height / 2);
108+
} else {
109+
CGContextMoveToPoint(context, bounds.origin.x + (float)counter / (float)(150 - 1) * bounds.size.width, bounds.origin.y + bounds.size.height / 2 + value * bounds.size.height / 2);
110+
}
111+
}
112+
// Save any previous graphics state settings before setting the color and line width for the current draw.
113+
CGContextSaveGState(context);
114+
CGContextSetRGBStrokeColor(context, (axis == 0 ? 1.0 : 0.0), (axis == 1 ? 1.0 : 0.0), (axis == 2 ? 1.0 : 0.0), 1.0);
115+
CGContextSetLineWidth(context, 2.0);
116+
CGContextStrokePath(context);
117+
// Restore previous graphics state.
118+
CGContextRestoreGState(context);
119+
}
120+
121+
- (void)drawRect:(CGRect)clip {
122+
NSUInteger index = nextIndex;
123+
124+
CGContextRef context = UIGraphicsGetCurrentContext();
125+
CGRect bounds = CGRectMake(0, 0, [self bounds].size.width, [self bounds].size.height);
126+
127+
// create the graph
128+
[self drawGraphInContext:context withBounds:bounds];
129+
130+
// plot x,y,z with anti-aliasing turned off
131+
CGContextSetAllowsAntialiasing(context, false);
132+
for (NSUInteger i = 0; i < 3; ++i) {
133+
[self drawHistory:i fromIndex:index inContext:context bounds:bounds];
134+
}
135+
CGContextSetAllowsAntialiasing(context, true);
136+
}
137+
138+
139+
@end

0 commit comments

Comments
 (0)