+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Jin Yoon <jinny.yoon@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <tizen.h>
+#include <service_app.h>
+#include <unistd.h>
+#include <glib.h>
+#include <peripheral_io.h>
+
+#include "thing_illumination.h"
+
+#define TOGGLE_FUNCTION 0 /* 0 : Toggle, 1 : Sensoring */
+#define THRESHOLD_OF_DARKNESS 100
+#define DURATION 500 /* msec */
+
+/* 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)
+
+/* GPIO */
+#define GPIO_NUM 25
+
+struct _peripheral {
+ peripheral_i2c_context_h i2c_dev;
+ peripheral_gpio_context_h gpio_dev;
+};
+typedef struct _peripheral peripheral_s;
+
+bool service_app_create(void *data)
+{
+ // Todo: add your code here.
+ return true;
+}
+
+void service_app_terminate(void *data)
+{
+ // Todo: add your code here.
+}
+
+void service_app_control(app_control_h app_control, void *data)
+{
+ // Todo: add your code here.
+}
+
+static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
+{
+ /*APP_EVENT_LANGUAGE_CHANGED*/
+}
+
+static void service_app_region_changed(app_event_info_h event_info, void *user_data)
+{
+ /*APP_EVENT_REGION_FORMAT_CHANGED*/
+}
+
+static void service_app_low_battery(app_event_info_h event_info, void *user_data)
+{
+ /*APP_EVENT_LOW_BATTERY*/
+}
+
+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_dev, power_on);
+}
+
+static gboolean _check_and_turn_on_light(gpointer user_data)
+{
+ 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...");
+
+ buf[0] = GY30_CONT_HIGH_RES_MODE;
+ if (peripheral_i2c_write(info->i2c_dev, buf, 1) != 0) {
+ dlog_print(DLOG_INFO, LOG_TAG, "Cannot write into the buffer.");
+ return FALSE;
+ }
+
+ peripheral_i2c_read(info->i2c_dev, 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;
+}
+
+int main(int argc, char* argv[])
+{
+ char ad[50] = {0, };
+ peripheral_s info = {0, };
+ service_app_lifecycle_callback_s event_callback;
+ app_event_handler_h handlers[5] = {NULL, };
+ guint timer_id = 0;
+ int ret = 0;
+
+ event_callback.create = service_app_create;
+ event_callback.terminate = service_app_terminate;
+ event_callback.app_control = service_app_control;
+
+ service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
+ service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
+ 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);
+
+ info.i2c_dev = peripheral_i2c_init(I2C_BUS);
+ if (!info.i2c_dev) {
+ dlog_print(DLOG_INFO, LOG_TAG, "Cannot initialize the peripheral i2c.");
+ return 1;
+ }
+
+ if (peripheral_i2c_set_address(info.i2c_dev, GY30_ADDR) != 0) {
+ dlog_print(DLOG_INFO, LOG_TAG, "Cannot set the address.");
+ return 1;
+ }
+
+ info.gpio_dev = peripheral_gpio_open(GPIO_NUM);
+ if (!info.gpio_dev) {
+ dlog_print(DLOG_INFO, LOG_TAG, "The return value of peripheral_gpio_open is null.");
+ return 1;
+ }
+
+ ret = peripheral_gpio_set_direction(info.gpio_dev, PERIPHERAL_GPIO_DIRECTION_OUT);
+ if (ret != 0) {
+ dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
+ return 1;
+ }
+
+ // 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 = service_app_main(argc, argv, &event_callback, ad);
+
+ g_source_remove(timer_id);
+ peripheral_i2c_stop(info.i2c_dev);
+ peripheral_gpio_close(info.gpio_dev);
+
+ return ret;
+}
+