Enabling the Ambient Light Sensor(ALS) in Desktop Applications for Ultrabook™ on Windows* 8

Current laptops might not be best suited for use under certain circumstances such as in cars or outdoors in direct sunlight as laptop screens usually have some readability and legibility constraints with different lighting conditions. This limits the mobility aspects of current laptops to an extent. The Ambient Light Sensors present in Ultrabook™ devices enable them to adapt to the surrounding environment and dynamically deliver the most optimized user experience. Windows* 8 fully supports automatic brightness control with ambient light sensors.

Microsoft has done the heavy lifting in Windows 8 by adding class driver support, which means developers do not have to worry about writing custom drivers to support ambient light sensors. Using the Ambient Light Sensor APIs available on Windows 8, developers can create both light-aware and energy efficient applications. For example, consider an IM or Email client type of application. When an Ultrabook user moves to an outdoor environment with direct sunlight, the application could use a different theme that might use brighter colors, larger font size, increase screen brightness, etc. Similarly, when a darker environment is detected, the application could reduce the brightness to conserve battery. Code snippets in this article demonstrate how to read and display data using the Ambient Light Sensor API on Ultrabooks running Windows 8.

Implementation

Light sensor is one type of environmental sensor that allows applications to respond to changes in the user’s environment. LightSensor class of Windows Runtime provides different events and methods to access light sensor data. ReadingChanged event is raised when light sensor value is changed. It provides current light sensor value of luminance as a LUX value.

Step: 1

Declare a private variable of LightSensor type into MainWindowViewModel class and also declare _ambientLightValue string variable to store a string representation of the light sensor value.

 // ViewModelBase is the base class for all view models. It provides INotifyPropertyChanged // implementation to all its derived classes. class MainWindowViewModel : ViewModelBase { //Member variables //declare variable of LightSensor class private LightSensor lightSensor; //String variable to store light sensor value private string _ambientLightValue; ..... } 

Step: 2

In the MainWindowViewModel() constructor, get the default ambient light sensor of the device and register for ReadingChanged event. Then, get the current value of the sensor by calling GetCurrentReading().

 class MainWindowViewModel : ViewModelBase { public MainWindowViewModel() { //Get default Ambient Light sensor. lightSensor = LightSensor.GetDefault(); More Info: //Register for the event to get the sensor value changed notification. lightSensor.ReadingChanged += lightSensor_ReadingChanged; //Get the current reading. LightSensorReading = lightSensor.GetCurrentReading().IlluminanceInLux; ..... } ..... } 

Step: 3

When the device sensor detects a new light value, it notifies the event handler with the new value. We have already registered for this ReadingChanged event into MainWindowViewModel() constructor. Fetch the new reading from the event arguments, e.g., LightSensorReadingChangedEventArgs args and assign it to the LightSensorReading property. LightSensorReading will convert the float value into string type and assign it to the AmbientLightValue property as described in Step: 4.

 class MainWindowViewModel : ViewModelBase { //Event handler notified when sensor value is changed void lightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args) { LightSensorReading = args.Reading.IlluminanceInLux; } ..... } 

Step: 4

The LightSensor class returns the sensor value as float type. In this example, we are displaying it as a string. Below is the code to convert it into string type. New private property is created which will convert the float value into a string and assign it to AmbientLightValue property.

 class MainWindowViewModel : ViewModelBase { private float LightSensorReading { set { //Convert float into string and assign to AmbientLightValue AmbientLightValue = string.Format("{0} Lux", value); } } ..... } 

Step: 5

Declare public property to provide access to the light sensor value. Implement INotifyPropertyChanged interface to provide auto-notification when a new value is set to the underlying _ambientLightValue variable.

 class MainWindowViewModel : ViewModelBase { //Create public property public string AmbientLightValue { get { return _ambientLightValue; } set { if (_ambientLightValue != value) { _ambientLightValue = value; //Raises notification when new value is set for this variable. OnNotifyPropertyChanged("AmbientLightValue"); } } } ..... } 

Additional Resources

Ultrabook and Tablet Windows* 8 Sensors Development Guide

UltrabookTM Desktop Applications Development on Windows* 8: Photo Application with Touch and Sensors

Notices

INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.

UNLESS OTHERWISE AGREED IN WRITING BY INTEL, THE INTEL PRODUCTS ARE NOT DESIGNED NOR INTENDED FOR ANY APPLICATION IN WHICH THE FAILURE OF THE INTEL PRODUCT COULD CREATE A SITUATION WHERE PERSONAL INJURY OR DEATH MAY OCCUR.

Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information.

The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request.

Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order.

Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations, and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you infully evaluating your contemplated purchases, including the performance of that product when combined with other products.

Any software source code reprinted in this document is furnished under a software license and may only be used or copied in accordance with the terms of that license.

Intel, Ultrabook, and the Intel logo are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione