Commits

Ron Huang committed 46549c7

Read in the Motion sensor data.

Comments (0)

Files changed (5)

WhereIsMakkah/App.xaml.cs

 using System.Windows.Shapes;
 using Microsoft.Phone.Controls;
 using Microsoft.Phone.Shell;
+using GalaSoft.MvvmLight.Threading;
 using WhereIsMakkah.ViewModel;
 using WhereIsMakkah.Model;
 
             // Standard Silverlight initialization
             InitializeComponent();
 
+            //Readies the DispatcherHelper for usage throughout application
+            DispatcherHelper.Initialize();
+            
             // Phone-specific initialization
             InitializePhoneApplication();
 

WhereIsMakkah/Lang/AppResources.Designer.cs

 //------------------------------------------------------------------------------
 // <auto-generated>
 //     This code was generated by a tool.
-//     Runtime Version:4.0.30319.235
+//     Runtime Version:4.0.30319.237
 //
 //     Changes to this file may cause incorrect behavior and will be lost if
 //     the code is regenerated.
         }
         
         /// <summary>
+        ///   Looks up a localized string similar to Motion is not supported on this device..
+        /// </summary>
+        public static string FeedbackMotionNotSupportedLabel {
+            get {
+                return ResourceManager.GetString("FeedbackMotionNotSupportedLabel", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Motion is unable to start..
+        /// </summary>
+        public static string FeedbackMotionUnableToStartLabel {
+            get {
+                return ResourceManager.GetString("FeedbackMotionUnableToStartLabel", resourceCulture);
+            }
+        }
+        
+        /// <summary>
         ///   Looks up a localized string similar to Application does not have permission to access location service..
         /// </summary>
         public static string FeedbackNoAccessLabel {

WhereIsMakkah/Lang/AppResources.resx

   <data name="DonateLabel" xml:space="preserve">
     <value>Please click the ad if you like this application.</value>
   </data>
+  <data name="FeedbackMotionNotSupportedLabel" xml:space="preserve">
+    <value>Motion is not supported on this device.</value>
+  </data>
+  <data name="FeedbackMotionUnableToStartLabel" xml:space="preserve">
+    <value>Motion is unable to start.</value>
+  </data>
   <data name="FeedbackNoAccessLabel" xml:space="preserve">
     <value>Application does not have permission to access location service.</value>
   </data>

WhereIsMakkah/ViewModel/MainViewModel.cs

 using System;
 using System.Windows;
 using System.Device.Location;
+using System.Windows.Threading;
+using Microsoft.Devices.Sensors;
+using Microsoft.Xna.Framework;
 using Microsoft.Phone.Shell;
 using GalaSoft.MvvmLight;
 using GalaSoft.MvvmLight.Threading;
 using GalaSoft.MvvmLight.Command;
+using GalaSoft.MvvmLight.Messaging;
 using WhereIsMakkah.Util;
-using GalaSoft.MvvmLight.Messaging;
 using WhereIsMakkah.Lang;
 
 namespace WhereIsMakkah.ViewModel
 
         private static readonly GeoCoordinate Makkah = new GeoCoordinate(21.4166666666666667, 39.8166666666666667);
         private GeoCoordinateWatcher _watcher;
+        private Motion _motion;
 
         private void StartSensor()
         {
+            StartLocationSensor();
+            StartMotionSensor();
+        }
+
+        private void StopSensor()
+        {
+            StopLocationSensor();
+            StopMotionSensor();
+        }
+
+        private void StartLocationSensor()
+        {
             if (!LocationServiceSetting)
             {
                 return;
                         Feedback = AppResources.FeedbackNoFunctionLabel;
                     }
 
-                    StopSensor();
+                    StopLocationSensor();
                     break;
 
                 case GeoPositionStatus.Initializing:
                 case GeoPositionStatus.NoData:
                     Feedback = AppResources.FeedbackNotAvailableLabel;
 
-                    StopSensor();
+                    StopLocationSensor();
                     break;
 
                 case GeoPositionStatus.Ready:
 
                     var destZ = 360.0 - GeoDistanceCalculator.InitialBearing(loc.Latitude, loc.Longitude, Makkah.Latitude, Makkah.Longitude); // counter-clockwise
 
-                    StopSensor();
+                    StopLocationSensor();
                     var msg = new AnimateArrowMessage() { Run = true, Indeterminate = false, DestinationZ = destZ };
                     Messenger.Default.Send<AnimateArrowMessage>(msg);
                     break;
             }
         }
 
-        private void StopSensor()
+        private void StopLocationSensor()
         {
             if (_watcher == null)
             {
             _watcher.Stop();
         }
 
+        private void StartMotionSensor()
+        {
+            // Check to see whether the Motion API is supported on the device.
+            if (!Motion.IsSupported)
+            {
+                Feedback = AppResources.FeedbackMotionNotSupportedLabel;
+                return;
+            }
+
+            // If the Motion object is null, initialize it and add a CurrentValueChanged
+            // event handler.
+            if (_motion == null)
+            {
+                _motion = new Motion();
+                _motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
+                _motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(_motion_CurrentValueChanged);
+            }
+
+            // Try to start the Motion API.
+            try
+            {
+                _motion.Start();
+            }
+            catch (Exception ex)
+            {
+                Feedback = AppResources.FeedbackMotionUnableToStartLabel;
+            }
+        }
+
+        private void _motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
+        {
+            // This event arrives on a background thread. Use BeginInvoke to call
+            // CurrentValueChanged on the UI thread.
+            DispatcherHelper.CheckBeginInvokeOnUI(() => CurrentValueChanged(e.SensorReading));
+        }
+
+        private void CurrentValueChanged(MotionReading e)
+        {
+            AttitudeReading ar = e.Attitude;
+            Feedback = String.Format("Pitch: {0}, Yaw: {1}, Roll: {2}", ar.Pitch, ar.Yaw, ar.Roll);
+        }
+
+        private void StopMotionSensor()
+        {
+            if (_motion == null)
+            {
+                return;
+            }
+
+            _motion.Stop();
+        }
+
         private void GoToSettingsPage()
         {
             var msg = new GoToPageMessage() { PageName = "SettingsPage" };

WhereIsMakkah/WhereIsMakkah.csproj

       <HintPath>..\packages\MvvmLight.3.0.3\lib\sl4-windowsphone71\GalaSoft.MvvmLight.WP71.dll</HintPath>
     </Reference>
     <Reference Include="Microsoft.Advertising.Mobile.UI, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+    <Reference Include="Microsoft.Devices.Sensors" />
     <Reference Include="Microsoft.Expression.Drawing" />
     <Reference Include="Microsoft.Phone" />
     <Reference Include="Microsoft.Phone.Controls, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e, processorArchitecture=MSIL" />
       <HintPath>..\packages\SilverlightToolkitWP.4.2011.8.17\lib\sl4-windowsphone71\Microsoft.Phone.Controls.Toolkit.dll</HintPath>
     </Reference>
     <Reference Include="Microsoft.Phone.Interop" />
+    <Reference Include="Microsoft.Xna.Framework" />
     <Reference Include="System.Device" />
     <Reference Include="System.Windows" />
     <Reference Include="system" />