2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jin Yoon <jinny.yoon@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <service_app.h>
23 #include <peripheral_io.h>
25 #include "thing_illumination.h"
27 #define TOGGLE_FUNCTION 0 /* 0 : Toggle, 1 : Sensoring */
28 #define THRESHOLD_OF_DARKNESS 100
29 #define DURATION 500 /* msec */
32 #define I2C_BUS 0x1 /* I2C Bus number */
33 #define GY30_ADDR 0x23 /* Address of GY30 light sensor */
34 #define GY30_CONT_HIGH_RES_MODE 0x10 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
35 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
41 peripheral_i2c_context_h i2c_dev;
42 peripheral_gpio_context_h gpio_dev;
44 typedef struct _peripheral peripheral_s;
46 bool service_app_create(void *data)
48 // Todo: add your code here.
52 void service_app_terminate(void *data)
54 // Todo: add your code here.
57 void service_app_control(app_control_h app_control, void *data)
59 // Todo: add your code here.
62 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
64 /*APP_EVENT_LANGUAGE_CHANGED*/
67 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
69 /*APP_EVENT_REGION_FORMAT_CHANGED*/
72 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
74 /*APP_EVENT_LOW_BATTERY*/
77 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
79 /*APP_EVENT_LOW_MEMORY*/
82 static void _control_led(peripheral_s *info, int power_on)
84 dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO...", power_on);
85 peripheral_gpio_write(info->gpio_dev, power_on);
88 static gboolean _check_and_turn_on_light(gpointer user_data)
90 unsigned char buf[10];
91 static int status = 0;
92 peripheral_s *info = user_data;
95 dlog_print(DLOG_INFO, LOG_TAG, "Check the sensor...");
97 buf[0] = GY30_CONT_HIGH_RES_MODE;
98 if (peripheral_i2c_write(info->i2c_dev, buf, 1) != 0) {
99 dlog_print(DLOG_INFO, LOG_TAG, "Cannot write into the buffer.");
103 peripheral_i2c_read(info->i2c_dev, buf, 2);
104 result = GY30_READ_INTENSITY(buf);
106 dlog_print(DLOG_INFO, LOG_TAG, "The result is [%d].", result);
107 if (result > THRESHOLD_OF_DARKNESS) {
109 _control_led(info, 0);
114 _control_led(info, 1);
122 int main(int argc, char* argv[])
125 peripheral_s info = {0, };
126 service_app_lifecycle_callback_s event_callback;
127 app_event_handler_h handlers[5] = {NULL, };
131 event_callback.create = service_app_create;
132 event_callback.terminate = service_app_terminate;
133 event_callback.app_control = service_app_control;
135 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
136 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
137 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
138 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
140 info.i2c_dev = peripheral_i2c_init(I2C_BUS);
142 dlog_print(DLOG_INFO, LOG_TAG, "Cannot initialize the peripheral i2c.");
146 if (peripheral_i2c_set_address(info.i2c_dev, GY30_ADDR) != 0) {
147 dlog_print(DLOG_INFO, LOG_TAG, "Cannot set the address.");
151 info.gpio_dev = peripheral_gpio_open(GPIO_NUM);
152 if (!info.gpio_dev) {
153 dlog_print(DLOG_INFO, LOG_TAG, "The return value of peripheral_gpio_open is null.");
157 ret = peripheral_gpio_set_direction(info.gpio_dev, PERIPHERAL_GPIO_DIRECTION_OUT);
159 dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
163 // Return value : the ID (greater than 0) of the event source.
164 timer_id = g_timeout_add(DURATION, _check_and_turn_on_light, &info);
166 dlog_print(DLOG_INFO, LOG_TAG, "Timer id : %d", timer_id);
170 ret = service_app_main(argc, argv, &event_callback, ad);
172 g_source_remove(timer_id);
173 peripheral_i2c_stop(info.i2c_dev);
174 peripheral_gpio_close(info.gpio_dev);