[Android Apps and Games] mcfasa09:
Could someone help me figure out how to display the gyroscope data as text on the watches screen using Native Tizen code (Tizen 3.0)?

:good:Update: I figured this out. Here is a link to download the Tizen Studio project:

https://drive.google.com/file/d/1izEePFnpUAww7Li7RdlyNxX85_1Mso3n/view?usp=sharing

Should this download link get removed at some point in the future here is the code:

/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////SOURCE (.c) FILE///////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////

#include <sensor.h>

#include <stdbool.h>

#include <dlog.h> // for logging purposes

//Define objects to appear on screen

Evas_Object *GLOBAL_DEBUG_BOX;

Evas_Object *start, *stop;

Evas_Object *conform;

sensor_listener_h listener;

Evas_Object *event_label;

Evas_Object *GLOBAL_DEBUG_BOX;

Evas_Object *start, *stop;

Evas_Object *conform;

sensor_listener_h listener;

Evas_Object *event_label;

void on_sensor_event(sensor_h sensor, sensor_event_s *event, void *user_data)

{

// Select a specific sensor with a sensor handle

sensor_type_e type;

sensor_get_type(sensor, &type);

switch (type) {

case SENSOR_GYROSCOPE:

dlog_print(DLOG_INFO, LOG_TAG, "%d" , event->values[0], event->values[1], event->values[2]);

char a[100];

sprintf(a,"X:%.2f Y:%.2f Z:%.2f", event->values[0], event->values[1], event->values[2]);

elm_object_text_set(event_label, a);

break;

default:

dlog_print(DLOG_ERROR, LOG_TAG, "Not a gyroscope event");

}

}

void _sensor_accuracy_changed_cb(sensor_h sensor, unsigned long long timestamp,

sensor_data_accuracy_e accuracy, void *data)

{

dlog_print(DLOG_DEBUG, LOG_TAG, "Sensor accuracy change callback invoked");

}

void _sensor_start_cb(void *data, Evas_Object *obj, void *event_info)

{

void *user_data = NULL;

char out[100];

// Retrieving a Sensor

sensor_type_e type = SENSOR_GYROSCOPE;

sensor_h sensor;

bool supported;

int error = sensor_is_supported(type, &supported);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_is_supported error: %d", error);

return;

}

if(supported)

{

dlog_print(DLOG_DEBUG, LOG_TAG, "Gyroscope is%s supported", supported ? "" : " not");

sprintf(out,"Gyroscope is%s supported", supported ? "" : " not");

elm_object_text_set(event_label, out);

}

// Get sensor list

int count;

sensor_h *list;

error = sensor_get_sensor_list(type, &list, &count);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_sensor_list error: %d", error);

} else {

dlog_print(DLOG_DEBUG, LOG_TAG, "Number of sensors: %d", count);

free(list);

}

error = sensor_get_default_sensor(type, &sensor);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_default_sensor error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_get_default_sensor");

// Registering a Sensor Event

error = sensor_create_listener(sensor, &listener);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_create_listener error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_create_listener");

int min_interval = 0;

error = sensor_get_min_interval(sensor, &min_interval);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_min_interval error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Minimum interval of the sensor: %d", min_interval);

// Callback for sensor value change

error = sensor_listener_set_event_cb(listener, min_interval, on_sensor_event, user_data);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_set_event_cb error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_listener_set_event_cb");

// Registering the Accuracy Changed Callback

error = sensor_listener_set_accuracy_cb(listener, _sensor_accuracy_changed_cb, user_data);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_set_accuracy_cb error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_listener_set_accuracy_cb");

error = sensor_listener_set_interval(listener, 100);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_set_interval error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_listener_set_intervals");

error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_set_option error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_listener_set_option");

error = sensor_listener_start(listener);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_start error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "sensor_listener_start");

sensor_event_s event;

error = sensor_listener_read_data(listener, &event);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_read_data error: %d", error);

return;

}

switch (type) {

case SENSOR_GYROSCOPE:

dlog_print(DLOG_INFO, LOG_TAG, "X:%.2f Y:%.2f Z:%.2f" , event.values[0], event.values[1], event.values[2]);

sprintf(out,"X:%.2f Y:%.2f Z:%.2f", event.values[0], event.values[1], event.values[2]);

elm_object_text_set(event_label, out);

break;

default:

dlog_print(DLOG_ERROR, LOG_TAG, "Not a gyroscope event");

}

dlog_print(DLOG_DEBUG, LOG_TAG, out);

char *name = NULL;

char *vendor = NULL;

float min_range = -571.0;

float max_range = 571.0;

float resolution = 0.0;

error = sensor_get_name(sensor, &name);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_name error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Sensor name: %s", name);

free(name);

error = sensor_get_vendor(sensor, &vendor);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_vendor error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Sensor vendor: %s", vendor);

free(vendor);

error = sensor_get_type(sensor, &type);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_type error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Sensor type: %s",

type == SENSOR_ACCELEROMETER ? "Accelerometer"

: type == SENSOR_GRAVITY ? "Gravity sensor"

: type == SENSOR_LINEAR_ACCELERATION ? "Linear acceleration sensor"

: type == SENSOR_MAGNETIC ? "Magnetic sensor"

: type == SENSOR_ROTATION_VECTOR ? "Rotation Vector sensor"

: type == SENSOR_ORIENTATION ? "Orientation sensor"

: type == SENSOR_GYROSCOPE ? "Gyroscope sensor"

: type == SENSOR_LIGHT ? "Light sensor"

: type == SENSOR_PROXIMITY ? "Proximity sensor"

: type == SENSOR_PRESSURE ? "Pressure sensor"

: type == SENSOR_ULTRAVIOLET ? "Ultraviolet sensor"

: type == SENSOR_TEMPERATURE ? "Temperature sensor"

: type == SENSOR_HUMIDITY ? "Humidity sensor"

: type == SENSOR_HRM ? "Heart Rate Monitor sensor (Since Tizen 2.3.1)"

: type == SENSOR_HRM_LED_GREEN ? "HRM (LED Green) sensor (Since Tizen 2.3.1)"

: type == SENSOR_HRM_LED_IR ? "HRM (LED IR) sensor (Since Tizen 2.3.1)"

: type == SENSOR_HRM_LED_RED ? "HRM (LED RED) sensor (Since Tizen 2.3.1)"

: type == SENSOR_LAST ? "End of sensor enum values" : "Custom sensor");

error = sensor_get_min_range(sensor, &min_range);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_min_range error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Minimum range of the sensor: %f", min_range);

error = sensor_get_max_range(sensor, &max_range);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_max_range error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Maximum range of the sensor: %f", max_range);

error = sensor_get_resolution(sensor, &resolution);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_get_resolution error: %d", error);

return;

}

dlog_print(DLOG_DEBUG, LOG_TAG, "Resolution of the sensor: %f", resolution);

elm_object_disabled_set(start, EINA_TRUE);

elm_object_disabled_set(stop, EINA_FALSE);

}

void _sensor_stop_cb(void *data, Evas_Object *obj, void *event_info)

{

int error = sensor_listener_unset_event_cb(listener);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_unset_event_cb error: %d", error);

}

error = sensor_listener_stop(listener);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_listener_stop error: %d", error);

}

error = sensor_destroy_listener(listener);

if (error != SENSOR_ERROR_NONE) {

dlog_print(DLOG_ERROR, LOG_TAG, "sensor_destroy_listener error: %d", error);

}

elm_object_disabled_set(start, EINA_FALSE);

elm_object_disabled_set(stop, EINA_TRUE);

}

static void

win_delete_request_cb(void *data, Evas_Object *obj, void *event_info)

{

ui_app_exit();

}

Eina_Bool _pop_cb(void *data, Elm_Object_Item *item)

{

elm_win_lower(((appdata_s *)data)->win);

return EINA_FALSE;

}

Evas_Object *_new_button(appdata_s *ad, Evas_Object *display, char *name, void *cb)

{

// Create a button

Evas_Object *bt = elm_button_add(display);

elm_object_text_set(bt, name);

evas_object_smart_callback_add(bt, "clicked", (Evas_Smart_Cb) cb, ad);

evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);

evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);

elm_box_pack_end(display, bt);

evas_object_show(bt);

return bt;

}

void _create_new_cd_display(appdata_s *ad, char *name, void *cb)

{

// Create main box

Evas_Object *box = elm_box_add(conform);

elm_object_content_set(conform, box);

elm_box_horizontal_set(box, EINA_FALSE);

evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);

evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

evas_object_show(box);

start = _new_button(ad, box, "Start", _sensor_start_cb);

event_label = elm_label_add(box);

elm_object_text_set(event_label, "Press Start and Wait");

elm_box_pack_end(box, event_label);

evas_object_show(event_label);

stop = _new_button(ad, box, "Stop", _sensor_stop_cb);

}

static void create_base_gui(appdata_s *ad)

{

// Setting the window

ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);

elm_win_conformant_set(ad->win, EINA_TRUE);

elm_win_autodel_set(ad->win, EINA_TRUE);

elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);

elm_win_indicator_opacity_set(ad->win, ELM_WIN_INDICATOR_OPAQUE);

evas_object_smart_callback_add(ad->win, "delete, request", win_delete_request_cb, NULL);

/* Create conformant */

conform = elm_conformant_add(ad->win);

evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

elm_win_resize_object_add(ad->win, conform);

evas_object_show(conform);

// Create a naviframe

ad->navi = elm_naviframe_add(conform);

evas_object_size_hint_align_set(ad->navi, EVAS_HINT_FILL, EVAS_HINT_FILL);

evas_object_size_hint_weight_set(ad->navi, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

elm_object_content_set(conform, ad->navi);

evas_object_show(ad->navi);

// Fill the list with items

//create_buttons_in_main_window(ad);

_create_new_cd_display(ad, "Sensor", _pop_cb);

eext_object_event_callback_add(ad->navi, EEXT_CALLBACK_BACK, eext_naviframe_back_cb, NULL);

// Show the window after base gui is set up

evas_object_show(ad->win);

}

static bool app_create(void *data)

{

/*

* Hook to take necessary actions before main event loop starts

* Initialize UI resources and application's data

* If this function returns true, the main loop of application starts

* If this function returns false, the application is terminated

*/

create_base_gui((appdata_s *)data);

return true;

}

int main(int argc, char *argv[])

{

appdata_s ad;

memset(&ad, 0x00, sizeof(appdata_s));

ui_app_lifecycle_callback_s event_callback;

memset(&event_callback, 0x00, sizeof(ui_app_lifecycle_callback_s));

event_callback.create = app_create;

int ret = ui_app_main(argc, argv, &event_callback, &ad);

if (ret != APP_ERROR_NONE)

dlog_print(DLOG_ERROR, LOG_TAG, "ui_app_main() failed with error: %d", ret);

return ret;

}

/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////HEADER (.h) FILE//////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __charge2silence_H__

#define __charge2silence_H__

#include <app.h>

#include <Elementary.h>

#include <app.h>

#include <dlog.h>

#include <efl_extension.h>

#include <sensor.h>

#define _PRINT_MSG_LOG_BUFFER_SIZE_ 1024

#define PRINT_MSG(fmt, args...) do { char _log_[_PRINT_MSG_LOG_BUFFER_SIZE_]; \

snprintf(_log_, _PRINT_MSG_LOG_BUFFER_SIZE_, fmt, ##args); _add_entry_text(_log_); } while (0)

typedef struct {

Evas_Object *win;

Evas_Object *navi;

} appdata_s;

void _add_entry_text(const char *text);

Evas_Object *_new_button(appdata_s *ad, Evas_Object *display, char *name, void *cb);

void _create_new_cd_display(appdata_s *ad, char *name, void *cb);

Eina_Bool _pop_cb(void *data, Elm_Object_Item *item);

#ifdef LOG_TAG

#undef LOG_TAG

#endif

#define LOG_TAG "charge2silence"

#if !defined(PACKAGE)

#define PACKAGE "org.volume.charge2silence"

#endif

#endif /* __charge2silence_H__ */

//"charge2silence" is just the name of my project//