Enabling the Accelerometer Sensor in Desktop Applications for Ultrabook™ on Windows* 8

An accelerometer is one of the mandatory sensors that all Ultrabook™ devices must possess. This sensor can detect motion and gravity. Use cases for this sensor are innumerable. Hard drive data protection is one such example. An accelerometer sensor gives an Ultrabook device the ability to sense that it is falling. An application could use this ability to detect the fall and lock the hard drive to prevent loss of data. Another example is application developers can use this sensor to provide a special input mechanism to their applications for a better user experience. Car racing applications on a convertible Ultrabook (tablet mode) can use this sensor to allow the user to navigate left/right just by tilting the device without having to provide a touch input. This sensor when combined with GPS can be used for accurate speed detection, etc.

In this article, we discuss the Windows* RT APIs that are available to access the accelerometer sensor. Windows RT provides an the ”Accelerometer” class that exposes different methods and events to monitor and recognize shaking. In the sample photo application, we demonstrate the use of this sensor by displaying a random image upon shake detection when the application is in image viewing mode.

Implementation

Accelerometer sensors are one type of device sensor that allows applications to respond to a device’s movement. Accelerometer class of Windows Runtime provides different events and methods to access device movement data. For example, shaken event is raised when the device shakes.

Step: 1

Declare a private variable of Accelerometer type into MainWindowViewModel class.

 //ViewModelBase is the base class for all view models. It provides INotifyPropertyChanged implementation to all its derived classes. class MainWindowViewModel : ViewModelBase { //Member variables //declare variable of Accelerometer class private Accelerometer accelerometer; ..... } 

Step: 2

In MainWindowViewModel() constructor, get the default accelerometer sensor of the device and register for "Shaken" event.

 class MainWindowViewModel : ViewModelBase { public MainWindowViewModel() { //Get default Accelerometer sensor. accelerometer = Accelerometer.GetDefault(); //Register for the event to get the device shake notification. accelerometer.Shaken += accelerometer_Shaken; ..... } ..... } 

Step: 3

When the device sensor detects shake, it notifies the event handler. We have already registered for this Shake event in the MainWindowViewModel() constructor. In the sample photo application, we display a random image when a device shakes in either direction. Calculating the random number and image display logic is handled by our “ShowRandomImage” function.

 class MainWindowViewModel : ViewModelBase { //Event handler notified when device shake void accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args) { //Show random image. ShowRandomImage(); } ..... } 

Step: 4

The “ShowRandomImage” function finds a random number from the available number range and makes it the selected index for the list view. This results in displaying the random image into the image view element.

 Class MainWindowViewModel : ViewModelBase { //Show random image. ShowRandomImage() { int count = vm.ImageList.count; if(count > 0) { Random randNum = new Random(); int newNum = randNum.Next(0, count – 1); imageListBox.SelectedIndex = newNum; } } ..... } 

Additional Resources

UltrabookTM Desktop Applications Development on Windows* 8: Photo Application with Touch and Sensors

Notices

INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.

UNLESS OTHERWISE AGREED IN WRITING BY INTEL, THE INTEL PRODUCTS ARE NOT DESIGNED NOR INTENDED FOR ANY APPLICATION IN WHICH THE FAILURE OF THE INTEL PRODUCT COULD CREATE A SITUATION WHERE PERSONAL INJURY OR DEATH MAY OCCUR.

Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information.

The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request.

Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order.

Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations, and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you infully evaluating your contemplated purchases, including the performance of that product when combined with other products.

Any software source code reprinted in this document is furnished under a software license and may only be used or copied in accordance with the terms of that license.

Intel, Ultrabook, and the Intel logo are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Per informazioni più dettagliate sulle ottimizzazioni basate su compilatore, vedere il nostro Avviso sull'ottimizzazione.