tomasslavicek

WP7 - Accelerometer and Motion API

Oct 16th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using Microsoft.Devices.Sensors;
  3. using Microsoft.Xna.Framework;
  4.  
  5. namespace GlowArkanoid.Input
  6. {
  7.     public class AccelerometerInput
  8.     {
  9.         public Vector3 RawValue = Vector3.Zero;
  10.         public Vector3 Value = Vector3.Zero;
  11.         public Vector3 DeviceAcceleration = Vector3.Zero; // Na Androidu bude nulová...
  12.  
  13.         Accelerometer accel;        
  14. #if WP7
  15.         Motion motion;
  16. #endif
  17.  
  18.         // Vyhlazování vstupu akcelerometru
  19.         const int samplesCount = 10;
  20.         Vector3[] accelMemory = new Vector3[samplesCount];
  21.         Vector3 accelSum = Vector3.Zero;
  22.         bool memoryInitialized = false;
  23.         int bufferIndex = 0;
  24.  
  25.         public void StartAccelerometerInput()
  26.         {
  27.             if (Accelerometer.IsSupported)
  28.             {
  29.                 accel = new Accelerometer();
  30.                 accel.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accel_CurrentValueChanged);
  31.                 accel.Start();
  32.             }
  33.  
  34. #if WP7
  35.             if (Motion.IsSupported)
  36.             {
  37.                 motion = new Motion();
  38.                 motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
  39.                 motion.Start();
  40.             }
  41. #endif
  42.         }
  43.  
  44. #if WP7
  45.         void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
  46.         {
  47.             DeviceAcceleration = e.SensorReading.DeviceAcceleration;
  48.         }
  49. #endif
  50.  
  51.         void accel_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
  52.         {
  53.             RawValue = e.SensorReading.Acceleration;
  54.  
  55.             lock (accelMemory)
  56.             {
  57.                 if (!memoryInitialized)
  58.                 {
  59.                     for (int i = 0; i < samplesCount; i++)
  60.                         accelMemory[i] = RawValue;
  61.  
  62.                     accelSum = RawValue * samplesCount;
  63.                     memoryInitialized = true;
  64.                 }
  65.  
  66.                 bufferIndex++;
  67.                 if (bufferIndex >= samplesCount)
  68.                     bufferIndex = 0;
  69.  
  70.                 // Nízké hodnoty
  71.                 if (Math.Abs(accelMemory[bufferIndex].X - RawValue.X) < 0.03f)
  72.                     RawValue.X = accelMemory[bufferIndex].X;
  73.                 if (Math.Abs(accelMemory[bufferIndex].Y - RawValue.Y) < 0.03f)
  74.                     RawValue.Y = accelMemory[bufferIndex].Y;
  75.  
  76.                 accelSum += RawValue;
  77.                 accelSum -= accelMemory[bufferIndex];
  78.                 accelMemory[bufferIndex] = RawValue;
  79.  
  80.                 //Value = -1 * (accelSum / samplesCount); //TODO test převrácení osy...
  81.                 Value = accelSum / samplesCount;
  82.             }
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment