*/
#include <tizen.h>
+#include <Ecore.h>
#include <service_app.h>
#include <unistd.h>
#include <glib.h>
#include "thing_toggler.h"
-#define GPIO_NUM 25
+#define GPIO_NUM 20
+
+static Eina_Bool _toggle_led(void *data);
+
+typedef struct app_data_s {
+ int current_status;
+ peripheral_gpio_h gpio;
+ Ecore_Timer *on_off_timer;
+} app_data;
bool service_app_create(void *data)
{
+ app_data *ad = (app_data *)data;
+ int ret = 0;
+
// Todo: add your code here.
+ peripheral_gpio_open(GPIO_NUM, &ad->gpio);
+ if (!ad->gpio) {
+ dlog_print(DLOG_ERROR, LOG_TAG, "The return value of peripheral_gpio_open is null.");
+ return false;
+ }
+
+ ret = peripheral_gpio_set_direction(ad->gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
+ if (ret != 0) {
+ dlog_print(DLOG_ERROR, LOG_TAG, "Cannot set direction error.");
+ return false;
+ }
+
+ // Return value : the ID (greater than 0) of the event source.
+ ad->on_off_timer = ecore_timer_add(1, _toggle_led, ad);
+ if (!ad->on_off_timer) {
+ dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add ON/OFF timer");
+ return false;
+ }
+
return true;
}
void service_app_terminate(void *data)
{
+ app_data *ad = (app_data *)data;
// Todo: add your code here.
+ ecore_timer_del(ad->on_off_timer);
+ peripheral_gpio_close(ad->gpio);
+
+ free(ad);
}
void service_app_control(app_control_h app_control, void *data)
/*APP_EVENT_LOW_MEMORY*/
}
-static void _control_led(peripheral_gpio_h gpio, int power_on)
-{
- dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO.");
- peripheral_gpio_write(gpio, power_on);
-}
-
-static gboolean _toggle_led(gpointer user_data)
-{
- static int current_status = 0;
-
- if (current_status) current_status = 0;
- else current_status = 1;
-
- _control_led(user_data, current_status);
-
- return TRUE;
-}
-
int main(int argc, char* argv[])
{
- char ad[50] = {0,};
+ app_data *ad = NULL;
+ int ret = 0;
service_app_lifecycle_callback_s event_callback;
app_event_handler_h handlers[5] = {NULL, };
- peripheral_gpio_h gpio = NULL;
- guint timer_id = 0;
- int ret = 0;
+
+ ad = (app_data *)calloc(1, sizeof(app_data));
event_callback.create = service_app_create;
event_callback.terminate = service_app_terminate;
service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
- peripheral_gpio_open(GPIO_NUM, &gpio);
- if (!gpio) {
- dlog_print(DLOG_INFO, LOG_TAG, "The return value of peripheral_gpio_open is null.");
- return 1;
- }
+ ret = service_app_main(argc, argv, &event_callback, ad);
- ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
- if (ret != 0) {
- dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
- return 1;
- }
+ return ret;
+}
- // Return value : the ID (greater than 0) of the event source.
- timer_id = g_timeout_add_seconds(1, _toggle_led, gpio);
- if (timer_id <= 0) {
- dlog_print(DLOG_INFO, LOG_TAG, "Timer id : %d", timer_id);
- return 1;
- }
+static Eina_Bool _toggle_led(void *data)
+{
+ app_data *ad = (app_data *)data;
+ int on_off = 0;
- ret = service_app_main(argc, argv, &event_callback, ad);
+ on_off = ad->current_status % 2;
- g_source_remove(timer_id);
- peripheral_gpio_close(gpio);
+ if (peripheral_gpio_write(ad->gpio, on_off) < 0) {
+ dlog_print(DLOG_ERROR, LOG_TAG, "Failed to write to GPIO");
+ return EINA_FALSE;
+ }
- return ret;
+ ad->current_status++;
+
+ return TRUE;
}