도전2022
How to: Get Data from the Accelerometer Sensor for Windows Phone 본문
http://msdn.microsoft.com/en-us/library/ff431810(v=VS.92).aspx
How to: Get Data from the Accelerometer Sensor for Windows Phone
June 24, 2011
This topic walks you through creating an accelerometer application that gives you a numeric display and a graphical representation of the accelerometer data.
The accelerometer measures the forces applied to the device at a moment in time. These forces can be used to determine in which direction the user is moving the device. The acceleration value is expressed as a 3-dimensional vector representing the acceleration components in the X, Y, and Z axes in gravitational units. The orientation of the acceleration is relative to the device such that -1g is applied in the Z-axis when the device is face up on a level table and -1g is applied to the Y-axis when the device is placed perpendicular to the table top.
The Accelerometer sensor detects the force of gravity along with any forces resulting from the movement of the phone. The combined motion API, accessed using the Motion class, uses multiple device sensors to separate the gravity vector from the device acceleration and allows you to easily determine the current attitude (yaw, pitch, and roll) of the device.
The following steps show you how to create an accelerometer application.
To create an accelerometer application
-
In Visual Studio, create a new Windows Phone Application project. This template is in the Silverlight for Windows Phone category.
-
This application requires references to the assemblies containing the sensor APIs and the XNA Framework because accelerometer data is passed in the form of an XNA Framework Vector3 object. From the Project menu, click Add Reference…, select Microsoft.Devices.Sensors and Microsoft.Xna.Framework, and then click OK.
-
In the MainPage.xaml file, place the following XAML code in the Grid element named “ContentPanel”. This code creates two buttons, one to start acquisition of data from the accelerometer and one to stop it. Also created are three TextBlock elements that will be used to display the numeric readings and three Line elements that will be used to graphically represent the reading data. Finally, this code adds a status TextBlock element that will be used to display the current status of the application.
<Button Content="Start" Height="72" HorizontalAlignment="Left" Margin="20,10,0,0" Name="startButton" VerticalAlignment="Top" Width="160" Click="startButton_Click" /> <Button Content="Stop" Height="72" HorizontalAlignment="Right" Margin="0,10,20,0" Name="stopButton" VerticalAlignment="Top" Width="160" Click="stopButton_Click"/> <TextBlock Height="30" HorizontalAlignment="Left" Margin="20,100,0,0" Name="xTextBlock" Text="X: 1.0" VerticalAlignment="Top" Foreground="Red" FontSize="28" FontWeight="Bold"/> <TextBlock Height="30" HorizontalAlignment="Center" Margin="0,100,0,0" Name="yTextBlock" Text="Y: 1.0" VerticalAlignment="Top" Foreground="Green" FontSize="28" FontWeight="Bold"/> <TextBlock Height="30" HorizontalAlignment="Right" Margin="0,100,20,0" Name="zTextBlock" Text="Z: 1.0" VerticalAlignment="Top" Foreground="Blue" FontSize="28" FontWeight="Bold"/> <Line x:Name="xLine" X1="240" Y1="350" X2="340" Y2="350" Stroke="Red" StrokeThickness="4"></Line> <Line x:Name="yLine" X1="240" Y1="350" X2="240" Y2="270" Stroke="Green" StrokeThickness="4"></Line> <Line x:Name="zLine" X1="240" Y1="350" X2="190" Y2="400" Stroke="Blue" StrokeThickness="4"></Line> <TextBlock Height="30" HorizontalAlignment="Center" Margin="6,571,6,0" Name="statusTextBlock" Text="TextBlock" VerticalAlignment="Top" Width="444" />
-
Now open the MainPage.xaml.cs code-behind page and add using directives for the sensors and the XNA Framework framework namespaces to other using directives at the top of the page.
-
Declare a variable of type Accelerometer at the top of the MainPage class definition.
-
In the page’s constructor, check to see whether the device on which the application is running supports the accelerometer sensor. Not all devices support all sensors, so you should always check before you use the sensor. Replace the existing page constructor with the following code.
// Constructor public MainPage() { InitializeComponent(); if (!Accelerometer.IsSupported) { // The device on which the application is running does not support // the accelerometer sensor. Alert the user and disable the // Start and Stop buttons. statusTextBlock.Text = "device does not support accelerometer"; startButton.IsEnabled = false; stopButton.IsEnabled = false; } }
-
Add a handler for the click events for the Start button. Depending on how you added the XAML code above, Visual Studio may have added this handler for you. If so, remove any code from inside the handler. If the handler was not added automatically, copy and paste the following empty function into the MainPage class definition.
-
In the Start button click handler, check to see whether the accelerometer object is null, which it will be until it is initialized. If the accelerometer is null, initialize it using the constructor. Next, set how fast you would like to receive data from the accelerometer by setting the TimeBetweenUpdates property. The default value is 2 milliseconds. Next, set a handler for the CurrentValueChanged event. Paste the following code inside the empty Start button click handler..
if (accelerometer == null) { // Instantiate the Accelerometer. accelerometer = new Accelerometer(); accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20); accelerometer.CurrentValueChanged+=new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged); }
-
Now, start the accelerometer using the Start() method. It is possible for the call to Start to fail, so you should put this call in a try block. In the catch block, you can alert the user that the accelerometer could not be started. Paste this code into the Start button click handler after the previous code section.
-
Now, implement the CurrentValueChanged event handler. This method will be called by the system with new accelerometer data at the frequency you specified with TimeBetweenUpdates. The handler receives an AccelerometerReading object containing the accelerometer data. This handler is called on a background thread that does not have access to the UI. So, this event handler uses the Dispatcher.Invoke method, which calls the specified code on the UI thread. Dispatcher.Invoke is used to call the UpdateUI, which will be defined in the next step, and passes the AccelerometerReading object.
-
Implement the UpdateUI method that will show the accelerometer data to the user. This method first updates the status TextBlock to indicate that data is being received. Next, the three TextBlock objects are updated to display the numeric values of the acceleration on each of the sensor’s axes. Finally, the Line objects are updated to graphically illustrate the acceleration.
private void UpdateUI(AccelerometerReading accelerometerReading) { statusTextBlock.Text = "getting data"; Vector3 acceleration = accelerometerReading.Acceleration; // Show the numeric values. xTextBlock.Text = "X: " + acceleration.X.ToString("0.00"); yTextBlock.Text = "Y: " + acceleration.Y.ToString("0.00"); zTextBlock.Text = "Z: " + acceleration.Z.ToString("0.00"); // Show the values graphically. xLine.X2 = xLine.X1 + acceleration.X * 200; yLine.Y2 = yLine.Y1 - acceleration.Y * 200; zLine.X2 = zLine.X1 - acceleration.Z * 100; zLine.Y2 = zLine.Y1 + acceleration.Z * 100; }
-
The final step is to implement the Stop button click handler that will allow the user to stop acquisition of data from the accelerometer. Once again, if the editor added this handler automatically, replace its contents with the following.
If you have a physical Windows Phone device, you can simply build the application, deploy it to the device, and wiggle the device around to watch the accelerometer data change. If you are using Windows Phone Emulator, you will need to its accelerometer emulator tool to test the application .
To Use the Accelerometer Emulator
-
Choose Windows Phone Emulator from the Select Target for Windows Phone projects list in the Standard toolbar in Visual Studio.
-
Press F5 or click Start Debugging on the Debug menu.
-
Wait for the emulator to start and for the accelerometer application to load.
-
In the emulator toolbar, click the Additional Tools button, which has the icon of two arrows pointing to the right.
-
Select the Accelerometer tab in the Additional Tools window.
-
Click the Start button in the accelerometer application that you created in the main emulator window.
-
Move the pink dot around the Additional Tools window to change the orientation of the virtual phone and observe the changing data in the main emulator window.
'작업 > 윈도우폰7' 카테고리의 다른 글
센서 (0) | 2011.07.06 |
---|---|
Execution Model for Windows Phone (0) | 2011.07.06 |
카메라 관련 내용. (0) | 2011.07.06 |
saverington에 IsoStorePath API가 제거되었다. (0) | 2011.07.06 |
teched 2011 동영상 모음 (0) | 2011.07.05 |