Modify code to loop according to service app life cycle 07/133707/2 tizen
authorjunkyu han <junkyu.han@samsung.com>
Tue, 13 Jun 2017 07:14:47 +0000 (16:14 +0900)
committerjunkyu han <junkyu.han@samsung.com>
Tue, 13 Jun 2017 07:16:04 +0000 (16:16 +0900)
Change-Id: Ic729e7d6c55ebbc09b71c5efb066dc4ce9af69c9

CMakeLists.txt
packaging/org.tizen.thing-illumination.spec
src/thing_illumination.c

index 60f3872..379bdef 100644 (file)
@@ -15,6 +15,7 @@ pkg_check_modules(APP_PKGS REQUIRED
        capi-appfw-application
        capi-appfw-service-application
        capi-system-peripheral-io
+       ecore
 )
 
 FOREACH (flag ${APP_PKGS_CFLAGS})
index 9a0ba34..d8fe2e5 100644 (file)
@@ -14,7 +14,8 @@ BuildRequires:  pkgconfig(capi-appfw-application)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(capi-appfw-service-application)
-BuildRequires: pkgconfig(capi-system-peripheral-io)
+BuildRequires:  pkgconfig(capi-system-peripheral-io)
+BuildRequires:  pkgconfig(ecore)
 
 %description
 Thing Illumination
index 5a38417..7f605fe 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <tizen.h>
+#include <Ecore.h>
 #include <service_app.h>
 #include <unistd.h>
 #include <glib.h>
 
 #include "thing_illumination.h"
 
-#define TOGGLE_FUNCTION 0 /* 0 : Toggle, 1 : Sensoring */
+#define GET_BY_BUFFER 1
+
 #define THRESHOLD_OF_DARKNESS 100
-#define DURATION 500 /* msec */
+#define DURATION (0.5)
+#define LIGHT_OFF 0
+#define LIGHT_ON 1
 
 /* I2C */
 #define I2C_BUS 0x1 /* I2C Bus number */
 #define GY30_ADDR 0x23 /* Address of GY30 light sensor */
 #define GY30_CONT_HIGH_RES_MODE 0x10 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
-#define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
+#define GY30_CONSTANT_NUM (1.2)
 
 /* GPIO */
-#define GPIO_NUM 25
+#define GPIO_NUM 20
 
-struct _peripheral {
+typedef struct app_data_s {
        peripheral_i2c_h i2c;
        peripheral_gpio_h gpio;
-};
-typedef struct _peripheral peripheral_s;
+       Ecore_Timer *check_illum_timer;
+       int light_status;
+} app_data;
+
+static Eina_Bool _prepare_for_light_toggler(app_data *ad);
+static Eina_Bool _check_and_turn_on_light(void *data);
+static int _get_intensity_by_word(app_data *ad);
+static int _get_intensity_by_buffer(app_data *ad);
 
 bool service_app_create(void *data)
 {
     // Todo: add your code here.
+       app_data *ad = (app_data *)data;
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       ret = peripheral_i2c_open(I2C_BUS, GY30_ADDR, &ad->i2c);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot initialize the peripheral i2c.");
+               return false;
+       }
+
+       ret = _prepare_for_light_toggler(ad);
+       if (!ret) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Failed to prepare light toggler");
+               return false;
+       }
+
+       // Return value : the ID (greater than 0) of the event source.
+       ad->check_illum_timer = ecore_timer_add(DURATION, _check_and_turn_on_light, ad);
+       if (!ad->check_illum_timer) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Failed to add check illum timer");
+               return false;
+       }
     return true;
 }
 
 void service_app_terminate(void *data)
 {
     // Todo: add your code here.
+       app_data *ad = (app_data *)data;
+
+       ecore_timer_del(ad->check_illum_timer);
+       peripheral_gpio_close(ad->gpio);
+       peripheral_i2c_close(ad->i2c);
+
+       free(ad);
 }
 
 void service_app_control(app_control_h app_control, void *data)
@@ -79,55 +117,29 @@ static void service_app_low_memory(app_event_info_h event_info, void *user_data)
        /*APP_EVENT_LOW_MEMORY*/
 }
 
-static void _control_led(peripheral_s *info, int power_on)
-{
-       dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO...", power_on);
-       peripheral_gpio_write(info->gpio, power_on);
-}
-
-static gboolean _check_and_turn_on_light(gpointer user_data)
+static Eina_Bool _control_led(app_data *ad, int on_off)
 {
-       unsigned char buf[10];
-       static int status = 0;
-       peripheral_s *info = user_data;
-       int result = -1;
-
-       dlog_print(DLOG_INFO, LOG_TAG, "Check the sensor...");
+       dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO...", on_off);
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       buf[0] = GY30_CONT_HIGH_RES_MODE;
-       if (peripheral_i2c_write(info->i2c, buf, 1) != 0) {
-               dlog_print(DLOG_INFO, LOG_TAG, "Cannot write into the buffer.");
-               return FALSE;
+       ret = peripheral_gpio_write(ad->gpio, on_off);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Failed to write to GPIO");
+               return false;
        }
 
-       peripheral_i2c_read(info->i2c, buf, 2);
-       result = GY30_READ_INTENSITY(buf);
-
-       dlog_print(DLOG_INFO, LOG_TAG, "The result is [%d].", result);
-       if (result > THRESHOLD_OF_DARKNESS) {
-               if (status != 0) {
-                       _control_led(info, 0);
-                       status = 0;
-               }
-       } else {
-               if (status != 1) {
-                       _control_led(info, 1);
-                       status = 1;
-               }
-       }
-
-       return TRUE;
+       return true;
 }
 
 int main(int argc, char* argv[])
 {
-    char ad[50] = {0, };
-       peripheral_s info = {0, };
+       app_data *ad = NULL;
        service_app_lifecycle_callback_s event_callback;
        app_event_handler_h handlers[5] = {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;
        event_callback.app_control = service_app_control;
@@ -137,37 +149,107 @@ int main(int argc, char* argv[])
        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);
 
-       ret = peripheral_i2c_open(I2C_BUS, GY30_ADDR, &info.i2c);
-       if (ret) {
-               dlog_print(DLOG_INFO, LOG_TAG, "Cannot initialize the peripheral i2c.");
-               return 1;
+
+       ret = service_app_main(argc, argv, &event_callback, ad);
+
+       return ret;
+}
+
+static Eina_Bool _prepare_for_light_toggler(app_data *ad)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       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;
        }
 
-       peripheral_gpio_open(GPIO_NUM, &info.gpio);
-       if (!info.gpio) {
-               dlog_print(DLOG_INFO, LOG_TAG, "The return value of peripheral_gpio_open is null.");
-               return 1;
+       ret = peripheral_gpio_set_direction(ad->gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot set direction error.");
+               return false;
        }
 
-       ret = peripheral_gpio_set_direction(info.gpio, PERIPHERAL_GPIO_DIRECTION_OUT);
-       if (ret != 0) {
-               dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
-               return 1;
+       return true;
+}
+
+static int _get_intensity_by_buffer(app_data *ad)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+       unsigned char buf[10] = { 0, };
+       int result = 0;
+
+       ret = peripheral_i2c_write_byte(ad->i2c, GY30_CONT_HIGH_RES_MODE);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot write to the sensor.");
+               return FALSE;
        }
 
-       // Return value : the ID (greater than 0) of the event source.
-       timer_id = g_timeout_add(DURATION, _check_and_turn_on_light, &info);
-       if (timer_id <= 0) {
-               dlog_print(DLOG_INFO, LOG_TAG, "Timer id : %d", timer_id);
-               return 1;
+       ret = peripheral_i2c_read(ad->i2c, buf, 2);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot read from the sensor.");
+               return FALSE;
        }
 
-       ret = service_app_main(argc, argv, &event_callback, ad);
+       result =  (buf[0] << 8 | buf[1]) / GY30_CONSTANT_NUM; // Just Sum High 8bit and Low 8bit
 
-       g_source_remove(timer_id);
-       peripheral_i2c_close(info.i2c);
-       peripheral_gpio_close(info.gpio);
+       return result;
+}
 
-       return ret;
+#ifndef GET_BY_BUFFER
+static int _get_intensity_by_word(app_data *ad)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+       uint16_t data = 0;
+       int result = 0;
+
+       ret = peripheral_i2c_write_byte(ad->i2c, GY30_CONT_HIGH_RES_MODE);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot write to the sensor.");
+               return FALSE;
+       }
+
+       ret = peripheral_i2c_read_register_word(ad->i2c, GY30_CONT_HIGH_RES_MODE, &data);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               dlog_print(DLOG_ERROR, LOG_TAG, "Cannot read from the sensor.");
+               return FALSE;
+       }
+
+       result = (((data & 0xFF00) >> 8) | ((data & 0xFF) << 8)) / GY30_CONSTANT_NUM; // High and Low are exchanged each other
+
+       return result;
+}
+#endif
+
+static Eina_Bool _check_and_turn_on_light(void *data)
+{
+       app_data *ad = (app_data *)data;
+       int lux = -1;
+
+       dlog_print(DLOG_INFO, LOG_TAG, "Check the sensor...");
+
+       lux = _get_intensity_by_buffer(ad);
+#ifndef GET_BY_BUFFER
+       lux = _get_intensity_by_word(ad);
+#endif
+
+       dlog_print(DLOG_INFO, LOG_TAG, "The lux is [%d].", lux);
+
+       if (lux > THRESHOLD_OF_DARKNESS) {
+               if (ad->light_status != LIGHT_OFF) {
+                       if (_control_led(ad, LIGHT_OFF)) {
+                               ad->light_status = LIGHT_OFF;
+                       }
+               }
+       } else {
+               if (ad->light_status != LIGHT_ON) {
+                       if (_control_led(ad, LIGHT_ON)) {
+                               ad->light_status = LIGHT_ON;
+                       }
+               }
+       }
+
+       return TRUE;
 }