llcong 发表于 2015-5-12 11:07:26

Windows phone 7 31天学习笔记 11:加速器

  原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-11-Accelerometer.aspx
  上篇讨论了手机上的键盘。这次将会讨论硬件中的加速器,以及如何利用它提供的信息。
  什么是加速器
  由于缺少更好的定一个加速器是手机中能够获取3维(x,y,z)加速度以及感知坠落的设备。除了时间戳,这些数据都是按照G来计算的(1G=9.81m/s^2)。这意味着如果手机迎面在水平面上无阻力飞行,Z轴获取数据为-1,其他轴为0。示意图如下:

  如何从加速器中获取数据?
  感谢微软,这些很简单。唯一复杂的地方就是需要一个进程管理,但是这也比较简单。先面试我们需要做的。
   
[*]实例化一个Accelerometer   
[*]创建一个ReadingChanged()函数来监视数据变化。   
[*]将数据传给我们的页面进程。   
[*]在程序中使用数据。
  下面是例子中的代码,你看到我已经在页面中创建了文本块,所以我可以写他们的值。同样注意需要引用Microsoft.Devices.Sesors来获取加速器。

   Home Archive ContactSubscribe Twitter LinkedIn
>
31 Days of Windows Phone | Day #11: Accelerometer
By Jeff Blankenburg11. October 2010 02:00
This post is Day #11 in a series called the 31 Days of Windows Phone.
Yesterday, we talked about the software-based keyboard on Windows Phone.Today, we’re going to talk about the hardware-based accelerometer, and how we can take advantage of the information it provides.
What is an accelerometer?
For lack of a better definition, an accelerometer is a sensor in the Windows Phone devices that measures the acceleration on 3 axes (X, Y, Z), relative to freefall.In addition to a timestamp, the values are expressed in G-forces (1G = 9.81 m/s2).What this means is that if the phone is lying face-up on a perfectly flat surface, the Z axis would read –1.0, and the other two axes would read 0.Here’s a quick illustration showing the different values (thanks to WindowsTeamBlog for the image):


How do I get values from the WP7 Accelerometer?
Thankfully, this is pretty simple.The one major complication is that we have some thread management to handle, but it’s really simple stuff.Here’s what we need to do:
Instantiate a new Accelerometer object.
Create a new ReadingChanged() event handler to monitor the changes in data.
Pass the results of that event back to our page’s thread (the event fires on a different thread).
Use the data values in our application.
Here’s the entirety of my MainPage.xaml.cs file for this example.You’ll see that I have created three TextBlocks (XText, YText, and ZText) on my MainPage.xaml file, so that I can write the values to the screen.Also notice that I had to add a new reference to Microsoft.Devices.Sensors to get access to the Accelerometer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
namespace Day11_Accelerometer
{
public partial class MainPage : PhoneApplicationPage
{
Accelerometer acc = new Accelerometer();
// Constructor
public MainPage()
{
InitializeComponent();
acc.ReadingChanged += new EventHandler(acc_ReadingChanged);
acc.Start();
}
void acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => ThreadSafeAccelerometerChanged(e));
}
void ThreadSafeAccelerometerChanged(AccelerometerReadingEventArgs e)
{
XText.Text = e.X.ToString("0.000");
YText.Text = e.Y.ToString("0.000");
ZText.Text = e.Z.ToString("0.000");
}
}
}
  额,模拟器不能够使加速器工作
  的确。如果你下载我的代码并且诧异为什么z轴值为-1,这是因为模拟器认为他在一个水平面上滑动。没有方法来模拟加速器数据。但是一些聪明的家伙发现了解决办法。我正在解密,我的确想写所有的方法,但是有太多的方法,我觉得最好让你知道原理胜过只是写代码。 下面是一些建议,你可以一试:

Reactive Extensions
  交互扩展是一个框架允许你模拟加速器。你没有真实的控制,实际上是他给你产生随机数据,这很简单,比起因为没有手机而停止开发,在MSDN官方网站可以看到更多:http://bit.ly/bdeaft

accelKit
  这是最酷的选项之一。他扩充你的照相头,允许你移动手机像现实中一样。我感到很酷的原因有两点:
  1 允许你测试动作反应
  2 使用扩展实现。很惊人的实现。
  代码详细见:http://bit.ly/9TfqaS

WiimoteLib
  Windows Phone中同样可以使用的类库,工作原理也很惊奇。基本原理就是:如果你有一个任天堂的Wii,你将会通过它来生成数据。Brain Peek是开发者,你可以下载代码:http://bit.ly/aUdEEW

Windows Mobile Unified Sensor API
  在特定的Windows phone 6.5上实现加速器交互鲜果。具体见:http://bit.ly/crHbW9
页: [1]
查看完整版本: Windows phone 7 31天学习笔记 11:加速器