Search code examples
c#.netmotion-detection

Gyroscope and accelerometer data from Windows?


The Microsoft Surface Pro has a gyroscope and accelerometer, Windows 8, and the full .NET framework.

Most articles I find that talk about the motion API point to the Windows Phone 8 API.

What .NET Framework namespaces and classes should I be using to get gyroscope and accelerometer data from?


Solution

  • I just worked based off the documentation - http://msdn.microsoft.com/en-us/library/ie/windows.devices.sensors

    using Windows.Devices.Sensors;
    
    private Accelerometer _accelerometer;
    
    private void DoStuffWithAccel()
    {
       _accelerometer = Accelerometer.GetDefault();
       if (_accelerometer != null)
       {
          AccelerometerReading reading = _accelerometer.GetCurrentReading();
          if (reading != null)
          double xreading = reading.AccelerationX;
          ... etc.
       }
    }
    

    Haven't tested it, but it should work for any Windows Store App - If you're trying to make it run as a console/windows forms app, you need to change the targetplatform by:

    1. Right Click your project -> Unload Project
    2. Follow the rest of this https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications