2012 Rocking the Windows 8 Sensors on the Intel IvyBridge Ultrabook
2012 年在英特尔 IvyBridge 超级本上玩转 Windows 8 传感器。
优秀的传感器和使用 Windows 8 传感器 API 的简便性相结合,必将为一些出色的应用程序打开大门。在这最后的评测中,我将介绍核心传感器、它们的表现以及如何使用它们。
多年来,我收到过一些用于评测的硬件和软件。不幸的是,我收到的一些硬件远未达到我的期望,几乎从未再次使用过。但对于我收到的这款运行 Windows 8 Pro 的英特尔 IvyBridge 超级本,情况绝非如此。在这段时间内,我的使用体验令我在速度、便携性、触摸屏以及各种传感器方面印象深刻。
据我所知,这款机器本身可能不会在商店里销售,而是英特尔用来展示 OEM 厂商可以为 Windows 8 创造出何种产品的模型。我的这款没有品牌,底部贴满了“非零售”的标签。然而,这款设备中的英特尔组件将出现在您可以购买的设备中。
您可以在 这里 阅读我对 IvyBridge 超级本的初步印象,以及 我两周后的后续反馈。这款超级本非常便携,内置了许多端口,因此我无需携带大量转接器。这对我这样经常出差和演示的人来说至关重要。我可以轻松地将其放入包中,随身携带进行演示,并在上面完成大量工作。
它速度很快。我主要使用 Visual Studio 2012 和其他几个开发工具。速度和性能对我来说不是“锦上添花”,而是“必需品”。尽管这款超级本的 RAM 和 CPU 比我通常的配置要低,但它在这方面表现出色。我喜欢 i7 处理器,但不得不承认,对于这款超级本来说,i5 已经绰绰有余(也许是 Windows 8 及其声称的高效性,也许只是超快的 180 GB SSD)。无论如何,我对这款 IvyBridge 超级本的速度非常满意。
去年 10 月,我使用这款设备测试了我和 Dan Wahlin 为 Pluralsight 编写的一些 Windows 8 课程材料。我同时使用了不少程序,包括 Visual Studio 2012、Powerpoint 和 OneNote。在这款设备上一切都很顺利。您可以通过点击下方的图片了解更多关于这个 Windows 8 入门课程的信息。
传感器
Windows 8 提供了各种 API 来访问设备传感器。Windows 团队提供了几个示例应用程序,以便您可以测试传感器。您可以找到包括(但不限于)以下传感器在内的 API:
- 加速度计(沿 3 个轴的加速度)
- 指南针(方向和位置)
- 陀螺仪(角速度)
- 倾斜仪(倾斜角度)
- 光线传感器(环境光照)
- 方向传感器(结合加速度计、指南针、陀螺仪以获得更灵敏的运动检测)
- 简单方向(设备的方向,包括正面朝上或朝下)
我使用这些示例应用程序测试了传感器。
下面所有的代码示例均直接来自 Windows 8 示例应用程序,您可以通过此链接下载。
加速度计
private Accelerometer _accelerometer;
private uint _desiredReportInterval;
public Scenario1()
{
this.InitializeComponent();
_accelerometer = Accelerometer.GetDefault();
if (_accelerometer != null)
{
// Select a report interval that is both suitable for the purposes of the app and supported by the sensor.
// This value will be used later to activate the sensor.
uint minReportInterval = _accelerometer.MinimumReportInterval;
_desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16;
}
else
{
rootPage.NotifyUser("No accelerometer found", NotifyType.StatusMessage);
}
}
_accelerometer.ReadingChanged +=
new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);
然后,获取数据就像在事件参数中读取 `AccelerometerReading` 对象一样简单(使用异步处理程序)。数据逐一推送到屏幕的 TextBlocks 中。
async private void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
AccelerometerReading reading = e.Reading;
ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
});
}
我们还可以通过连接事件处理程序来检查设备是否被摇动。
_accelerometer.Shaken +=
new TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs>(Shaken);
async private void Shaken(object sender, AccelerometerShakenEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
_shakeCount++;
ScenarioOutputText.Text = _shakeCount.ToString();
});
}
指南针
指南针传感器会报告设备的方向和位置信息。与我拥有的其他指南针相比,英特尔 IvyBridge 超级本上的传感器通过指南针很好地检测到了位置。您可以使用 Windows 8 示例应用程序“Compass sensor sample”(指南针传感器示例)来检测方向(北、南、东、西),以度为单位进行测量。首先,您获取指南针对象。
private Compass _compass;
_compass.ReadingChanged +=
new TypedEventHandler(ReadingChanged);
CompassReading reading = e.Reading;
ScenarioOutput_MagneticNorth.Text = String.Format("{0,5:0.00}", reading.HeadingMagneticNorth);
陀螺仪
private Gyrometer _gyrometer;
_gyrometer.ReadingChanged +=
new TypedEventHandler(ReadingChanged);
async private void ReadingChanged(object sender, GyrometerReadingChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
GyrometerReading reading = e.Reading;
ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX);
ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY);
ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ);
});
}
倾斜仪
private Inclinometer _inclinometer;
_inclinometer.ReadingChanged +=
new TypedEventHandler(ReadingChanged);
async private void ReadingChanged(object sender, InclinometerReadingChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
InclinometerReading reading = e.Reading;
ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.PitchDegrees);
ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.RollDegrees);
ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.YawDegrees);
});
}
光线传感器
private LightSensor _sensor;
_sensor.ReadingChanged +=
new TypedEventHandler(ReadingChanged);
最后,在事件处理程序中读取照度数据(以勒克斯为单位)。
async private void ReadingChanged(object sender, LightSensorReadingChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
LightSensorReading reading = e.Reading;
ScenarioOutput_LUX.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
});
}
方向传感器
首先获取 `OrientationSensor` 对象
private OrientationSensor _sensor;
_sensor.ReadingChanged +=
new TypedEventHandler(ReadingChanged);
async private void ReadingChanged(object sender, OrientationSensorReadingChangedEventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OrientationSensorReading reading = e.Reading;
// Quaternion values
SensorQuaternion quaternion = reading.Quaternion; // get a reference to the object to avoid re-creating it for each access
ScenarioOutput_X.Text = String.Format("{0,8:0.00000}", quaternion.X);
ScenarioOutput_Y.Text = String.Format("{0,8:0.00000}", quaternion.Y);
ScenarioOutput_Z.Text = String.Format("{0,8:0.00000}", quaternion.Z);
ScenarioOutput_W.Text = String.Format("{0,8:0.00000}", quaternion.W);
// Rotation Matrix values
SensorRotationMatrix rotationMatrix = reading.RotationMatrix;
ScenarioOutput_M11.Text = String.Format("{0,8:0.00000}", rotationMatrix.M11);
ScenarioOutput_M12.Text = String.Format("{0,8:0.00000}", rotationMatrix.M12);
ScenarioOutput_M13.Text = String.Format("{0,8:0.00000}", rotationMatrix.M13);
ScenarioOutput_M21.Text = String.Format("{0,8:0.00000}", rotationMatrix.M21);
ScenarioOutput_M22.Text = String.Format("{0,8:0.00000}", rotationMatrix.M22);
ScenarioOutput_M23.Text = String.Format("{0,8:0.00000}", rotationMatrix.M23);
ScenarioOutput_M31.Text = String.Format("{0,8:0.00000}", rotationMatrix.M31);
ScenarioOutput_M32.Text = String.Format("{0,8:0.00000}", rotationMatrix.M32);
ScenarioOutput_M33.Text = String.Format("{0,8:0.00000}", rotationMatrix.M33);
});
}
总结
Windows 8 传感器 API 使得访问设备传感器变得容易。这使得设备实现传感器并以密集且准确的间隔报告数据变得至关重要。我发现我测试过的英特尔 IvyBridge 超级本设备在所有这些传感器方面表现都非常出色。我很高兴看到会有哪些类型的应用程序能够利用这些传感器和 Windows 8。
披露物质联系:我免费收到了一项或多项上述产品或服务,希望能在我的博客上提及。无论如何,我只推荐我个人使用并且认为我的读者会喜欢的产品或服务。我正在根据《美国联邦贸易委员会》的 16 CFR 第 255 部分:“关于在广告中使用认可和推荐的指南”披露这一点。