Click here to Skip to main content
15,895,774 members
Articles / Desktop Programming / WPF

Ultrabook Sensors on Desktop App

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
5 Jan 2013CPOL7 min read 23.2K   827   8  
Using all Ultrabook sensors on Desktop Application
using System;
using Windows.Devices.Sensors;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace SensorsUltrabook
{
    public class AmbientLightSensor : SensorBase
    {
        public static readonly DependencyProperty IlluminanceProperty = DependencyProperty.Register(
            "Illuminance",
            typeof (float),
            typeof (AmbientLightSensor),
            new PropertyMetadata(0.0f));

        private readonly LightSensor _lightSensor;

        public AmbientLightSensor()
        {
            _lightSensor = LightSensor.GetDefault();

            if (_lightSensor != null)
            {
                _lightSensor.ReadingChanged += LightSensorReadingChanged;
            }
        }

        public float Illuminance
        {
            get { return (float) GetValue(IlluminanceProperty); }
            protected set { SetValue(IlluminanceProperty, value); }
        }

        private async void LightSensorReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
        {
           await   this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                                                                         {
                                                                             LightSensorReading reading = args.Reading;

                                                                             Illuminance = reading.IlluminanceInLux;
                                                                         }));
        }

        protected override void OnReportIntervalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            _lightSensor.ReportInterval = (uint) ReportInterval;
        }
    }
}

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) NIC
India India
I have more then 5 years of experience in the software development sectors with more then 15 softwares in C#, WPF, SQL SERVER, WCF, LINQ, intel perceptual computing sdk, digital signature, snmp.

Comments and Discussions