Click here to Skip to main content
15,880,364 members
Articles / Desktop Programming / WPF

Windows 8 : Fun with sensors

Rate me:
Please Sign up or sign in to vote.
4.96/5 (11 votes)
29 Jan 2013CPOL7 min read 45.3K   373   25  
Simple Windows 8 app that tries to have some fun with sensors
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cinch;
using MEFedMVVM.ViewModelLocator;
using System.ComponentModel.Composition;
using SimpleWin8App.Services;
using System.Diagnostics;

namespace SimpleWin8App.ViewModels
{
    [ExportViewModel("MainWindowViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class MainWindowViewModel : ViewModelBase
    {
        private IViewAwareStatusWindow viewAwareStatusService;
        private IMessageBoxService messageBoxService;
        private IDisposable lightSensorSubscripton;
        private IDisposable gyrometerSensorSubscription;
        private GyrometerReading previousGyrometerReading = null;

        [ImportingConstructor]
        public MainWindowViewModel(
            IViewAwareStatusWindow viewAwareStatusService, 
            IMessageBoxService messageBoxService, 
            ILightSensorService lightSensorService,
            IGyrometerSensorService gyrometerSensorService
            )
        {
            this.viewAwareStatusService = viewAwareStatusService;
            this.viewAwareStatusService.ViewWindowClosing += viewAwareStatusService_ViewWindowClosing;
            this.messageBoxService = messageBoxService;
            lightSensorSubscripton = lightSensorService.LightObservable.Subscribe(LightChanged, LightException);
            gyrometerSensorSubscription = gyrometerSensorService.GyrometerObservable.Subscribe(GyrometerChanged, GyrometerException);
        }

        void viewAwareStatusService_ViewWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            lightSensorSubscripton.Dispose();
            gyrometerSensorSubscription.Dispose();
        }

        private void LightChanged(float reading)
        {
            SensorLightMode mode = reading > GlobalSettings.ValueToBeConsideredDark ? SensorLightMode.Light : SensorLightMode.Dark;
            ((ISupportBrowserChanges)this.viewAwareStatusService.View).ApplyChangeBasedOnLightReading(mode);
        }
        
        private void LightException(Exception lightReadingEx)
        {
            Console.WriteLine("Oh no the light is not reading any more");
        }

        private void GyrometerChanged(GyrometerReading reading)
        {
            if(previousGyrometerReading != null)
            {
                ISupportBrowserChanges supportBrowserChanges  = (ISupportBrowserChanges)this.viewAwareStatusService.View;

                double diffLongitude = reading.VelocityX - previousGyrometerReading.VelocityX;
                double diffLatitude = reading.VelocityY - previousGyrometerReading.VelocityY;

                if (Math.Abs(diffLongitude) > GlobalSettings.MinLongitudeChange)
                {
                    supportBrowserChanges.IncreaseLongitude();
                }
                else
                {
                    supportBrowserChanges.DecreaseLongitude();
                }

                if (Math.Abs(diffLatitude) > GlobalSettings.MinLatitudeChange)
                {
                    supportBrowserChanges.IncreaseLatitude();
                }
                else
                {
                    supportBrowserChanges.DecreaseLatitude();
                }
            }

            previousGyrometerReading = reading;
        }
        
        private void GyrometerException(Exception gyrometerReadingEx)
        {
            Console.WriteLine("Oh no the gyrometer is not reading any more");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions