The SensorApp sample application demonstrates how you can retrieve data from various Tizen sensor types, such as acceleration, device orientation, and gravity sensors, using the Tizen Sensor API.
The following figures illustrate the main screens of the SensorApp.
- Include the Sensor API:
#include <sensor.h>
- Retrieve the sensor handle and hardware information.
The sensor_list_init() function in the sensor-list.c file retrieves the sensor handle and hardware information and stores them in the sensor_info array. The retrieved information, such as sensor range and resolution, is later used to display the sensor data properly.
void sensor_list_init()
{
sensor_info *info = sensors;
sensor_info *end = info + sensor_count;
for (; info != end; ++info)
{
float resolution = 1.0;
// Retrieve sensor handle using sensor type
sensor_get_default_sensor(info->type, &info->sensor);
// Retrieve sensor minimal and maximal values
sensor_get_min_range(info->sensor, &info->value_min);
sensor_get_max_range(info->sensor, &info->value_max);
// Retrieve sensor resolution
sensor_get_resolution(info->sensor, &resolution);
}
}
The <sensor-info.h> header file defines a structure for storing information about the sensor that is used to display the sensor list and sensor data.
struct _sensor_info
{
sensor_h sensor; // Sensor handle
sensor_type_e type; // Sensor type
sensor_unit_e units; // Value measurement units
const char *name; // Sensor display name
const char **value_names; // Value names array of value_count size
int value_count; // Values count
float value_min; // Minimal value
float value_max; // Maximal value
};
The sensor-list.c file defines the sensor list array for storing information about each sensor.
static sensor_info sensors[] =
{
{
.type = SENSOR_ACCELEROMETER,
.units = SENSOR_UNIT_METRE_PER_SECOND_SQUARED,
.name = "Acceleration",
.value_names = axes,
.value_count = 3,
.axes = {-1, 1, 1}
},
}
- Check sensor availability.
The sensor-list-view.c file creates and fills elm_list using the sensor list provided by the sensor-list.c file. While filling the sensor list, the _list_view_fill() function checks whether each sensor in the list is available on the device using the sensor_is_supported() function.
static void _list_view_fill(list_view *view)
{
unsigned count = 0;
const sensor_info *item = sensor_list_get(&count);
const sensor_info *end = item + count;
RETM_IF(!item, "item is NULL");
for (; item != end; ++item)
{
bool is_supported = false;
// Check whether sensor is supported by device
sensor_is_supported(item->type, &is_supported);
if (is_supported)
{
elm_list_item_append(view->list, item->name, NULL, NULL, _list_view_sel_cb, item);
}
}
}
When a sensor is selected, the _list_view_sel_cb() function navigates to the sensor data view passing the selected sensor information.
static void _list_view_sel_cb(void *data, Evas_Object *obj, void *event_info)
{
sensor_info *item = data;
sensor_data_view_create(view->navi, item);
}
- Receive sensor data.
To start receiving sensor data, the _data_view_sensor_start() function registers a sensor listener, sets the sensor event callback, and starts the sensor.
static void _data_view_sensor_start(data_view *view)
{
sensor_error_e err = SENSOR_ERROR_NONE;
err = sensor_create_listener(view->sensor_info->sensor, &view->sensor_listener);
RETM_IF(err != SENSOR_ERROR_NONE, "sensor_create_listener() failed(%d)", err);
sensor_listener_set_event_cb(view->sensor_listener, SENSOR_INTERVAL, _data_view_sensor_cb, view);
sensor_listener_start(view->sensor_listener);
}
When the _data_view_sensor_cb() sensor event callback is called, the _data_view_value_items_update() function displays the received sensor data in genlist items and updates the chart, if necessary.
static void _data_view_sensor_cb(sensor_h sensor, sensor_event_s *sensor_data, void *user_data)
{
data_view *view = user_data;
_data_view_value_items_update(view, sensor_data->values);
_data_view_extra_items_update(view, sensor_data->values);
}
static void _data_view_value_items_update(data_view *view, float *values)
{
bool update_chart = false;
// Update genlist items with values received from the sensor
for (; item != end; ++item, ++value)
{
if (item->value != *value)
{
// Chart MUST be updated if any value has changed
update_chart = true;
item->value = *value;
// Update genlist item part that displays value
elm_genlist_item_fields_update(item->obj_item, PART_VALUE, ELM_GENLIST_ITEM_FIELD_TEXT);
}
}
// Update chart if necessary
if (view->chart && update_chart)
{
sensor_data_chart_update(view->chart, view->sensor_info->value_range,
view->sensor_info->axes, values, data_view_item_colors,
view->sensor_info->value_count);
}
}
- Stop the sensor.
To stop receiving sensor data when the sensor data view is destroyed, the _data_view_destroy_cb() function calls the sensor_listener_stop() function and then destroys the listener using the sensor_destroy_listener() function.
static void _data_view_destroy_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
sensor_listener_stop(view->sensor_listener);
sensor_destroy_listener(view->sensor_listener);
}