Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Devices.Sensors;
- using Microsoft.Xna.Framework;
- namespace GlowArkanoid.Input
- {
- public class AccelerometerInput
- {
- public Vector3 RawValue = Vector3.Zero;
- public Vector3 Value = Vector3.Zero;
- public Vector3 DeviceAcceleration = Vector3.Zero; // Na Androidu bude nulová...
- Accelerometer accel;
- #if WP7
- Motion motion;
- #endif
- // Vyhlazování vstupu akcelerometru
- const int samplesCount = 10;
- Vector3[] accelMemory = new Vector3[samplesCount];
- Vector3 accelSum = Vector3.Zero;
- bool memoryInitialized = false;
- int bufferIndex = 0;
- public void StartAccelerometerInput()
- {
- if (Accelerometer.IsSupported)
- {
- accel = new Accelerometer();
- accel.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accel_CurrentValueChanged);
- accel.Start();
- }
- #if WP7
- if (Motion.IsSupported)
- {
- motion = new Motion();
- motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
- motion.Start();
- }
- #endif
- }
- #if WP7
- void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
- {
- DeviceAcceleration = e.SensorReading.DeviceAcceleration;
- }
- #endif
- void accel_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
- {
- RawValue = e.SensorReading.Acceleration;
- lock (accelMemory)
- {
- if (!memoryInitialized)
- {
- for (int i = 0; i < samplesCount; i++)
- accelMemory[i] = RawValue;
- accelSum = RawValue * samplesCount;
- memoryInitialized = true;
- }
- bufferIndex++;
- if (bufferIndex >= samplesCount)
- bufferIndex = 0;
- // Nízké hodnoty
- if (Math.Abs(accelMemory[bufferIndex].X - RawValue.X) < 0.03f)
- RawValue.X = accelMemory[bufferIndex].X;
- if (Math.Abs(accelMemory[bufferIndex].Y - RawValue.Y) < 0.03f)
- RawValue.Y = accelMemory[bufferIndex].Y;
- accelSum += RawValue;
- accelSum -= accelMemory[bufferIndex];
- accelMemory[bufferIndex] = RawValue;
- //Value = -1 * (accelSum / samplesCount); //TODO test převrácení osy...
- Value = accelSum / samplesCount;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment