using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//Sensoren
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
namespace SLGyroscope
{
    public partial class MainPage : PhoneApplicationPage
    {
        //Gyroscope Objekt
        Gyroscope gyroSensor;
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            //Prüfen ob Gyroscope existiert
            if (!Gyroscope.IsSupported)
            {
                MessageBox.Show("Ihr Gerät unterstützt kein Gyroscope");
                gyro.IsEnabled = false;
            }
        }

        private void gyro_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //Gyroscope anlegen
            if (gyroSensor == null)
            {
                gyroSensor = new Gyroscope();
                gyroSensor.TimeBetweenUpdates = TimeSpan.FromMilliseconds(60);
                gyroSensor.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<GyroscopeReading>>(gyroSensor_CurrentValueChanged);
            }
            //Starten oder stoppen
            if (gyroSensor.IsDataValid)
                gyroSensor.Stop();
            else
                gyroSensor.Start();
        }

        //Gyroscope Daten auslenen und in UI schreiben
        void gyroSensor_CurrentValueChanged(object sender, SensorReadingEventArgs<GyroscopeReading> e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                timestamp.Text = e.SensorReading.Timestamp.ToString("HH:mm:ss:fff dd.MM.yyyy");
                valX.Text = e.SensorReading.RotationRate.X.ToString();
                valY.Text = e.SensorReading.RotationRate.Y.ToString();
                valZ.Text = e.SensorReading.RotationRate.Z.ToString();
            });
        }
    }
}