How to use Light Sensor in Tizen

How to use Light Sensor in Tizen

The light sensor detects the brightness of ambient light. It can be used to measure the brightness level of the screen, apps to measure light illuminance in lux using ambient light sensor of your device for many different purpose like (Photography, IoT device control)
As an example use case, the light sensor can be used to control the brightness of the screen. In a dark environment, the light sensor detects the brightness of the environment and can be used to increase the device screen backlight brightness level. In a brighter environment, the backlight brightness level is lowered to save battery power.
Or the light sensor can be used to control the indoor lighting (IoT device control); the light sensor detects the illuminance of the room environment and can be used to suggest increase/decrease the room lighting level. It can also be used for photographic environment purpose.

 

Steps to do

Considering you are working on simple light sensor app, here is the brief summary of steps that we need to do to make things work:

 

Step 1: Add sensor library (header)

#include <sensor.h>

 

Step 2: Check if your sensor is supported

int error;
bool supported;
sensor_type_e type = SENSOR_LIGHT;
error = sensor_is_supported(type, &supported);

 

Step 3: Create an event listener for your sensor

sensor_h sensor;
sensor_listener_h listener;
error = sensor_create_listener(sensor, &listener);

 

Step 4: Then you have to register a callback for the listener

void
on_sensor_event(sensor_h sensor, sensor_event_s *event, void *user_data)
{
   // Select a specific sensor with a sensor handle
   // This example uses the sensor type, assuming there is only 1 sensor for each type
   sensor_type_e type;
   sensor_get_type(sensor, &type);
   switch (type)
   {
      case SENSOR_LIGHT:
        // Use sensor information
   }
}
error = sensor_listener_set_event_cb(listener, 100, on_sensor_event, user_data);

 

Step 5: Set optional keep on/off

error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);

 

Step 6: Start listening (from you event/button click/action)

error = sensor_listener_start(listener);

 

Step 7: Read sensor data after starting

sensor_event_s event;
error = sensor_listener_read_data(listener, &event);

 

Step 8: When your task is done close sensor

error = sensor_listener_unset_event_cb(listener);
error = sensor_listener_stop(listener);
error = sensor_destroy_listener(listener);

 

 

Tizen API for further references:

[1] https://developer.tizen.org/dev-guide/2.3.1/org.tizen.web.apireference/html/device_api/mobile/tizen/sensor.html#LightSensor
[2] https://developer.tizen.org/development/api-guides/native-application/system/sensor
[3] https://developer.tizen.org/development/api-guides/native-application/system/sensor#light

 

Here we have created a simple sample app using Tizen light sensor.

 

文件附件: