Initial version
[apps/native/thing-illumination.git] / src / thing_illumination.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <tizen.h>
20 #include <service_app.h>
21 #include <unistd.h>
22 #include <glib.h>
23 #include <peripheral_io.h>
24
25 #include "thing_illumination.h"
26
27 #define TOGGLE_FUNCTION 0 /* 0 : Toggle, 1 : Sensoring */
28 #define THRESHOLD_OF_DARKNESS 100
29 #define DURATION 500 /* msec */
30
31 /* I2C */
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)
36
37 /* GPIO */
38 #define GPIO_NUM 25
39
40 struct _peripheral {
41         peripheral_i2c_context_h i2c_dev;
42         peripheral_gpio_context_h gpio_dev;
43 };
44 typedef struct _peripheral peripheral_s;
45
46 bool service_app_create(void *data)
47 {
48     // Todo: add your code here.
49     return true;
50 }
51
52 void service_app_terminate(void *data)
53 {
54     // Todo: add your code here.
55 }
56
57 void service_app_control(app_control_h app_control, void *data)
58 {
59     // Todo: add your code here.
60 }
61
62 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
63 {
64         /*APP_EVENT_LANGUAGE_CHANGED*/
65 }
66
67 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
68 {
69         /*APP_EVENT_REGION_FORMAT_CHANGED*/
70 }
71
72 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
73 {
74         /*APP_EVENT_LOW_BATTERY*/
75 }
76
77 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
78 {
79         /*APP_EVENT_LOW_MEMORY*/
80 }
81
82 static void _control_led(peripheral_s *info, int power_on)
83 {
84         dlog_print(DLOG_INFO, LOG_TAG, "Write [%d] into GPIO...", power_on);
85         peripheral_gpio_write(info->gpio_dev, power_on);
86 }
87
88 static gboolean _check_and_turn_on_light(gpointer user_data)
89 {
90         unsigned char buf[10];
91         static int status = 0;
92         peripheral_s *info = user_data;
93         int result = -1;
94
95         dlog_print(DLOG_INFO, LOG_TAG, "Check the sensor...");
96
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.");
100                 return FALSE;
101         }
102
103         peripheral_i2c_read(info->i2c_dev, buf, 2);
104         result = GY30_READ_INTENSITY(buf);
105
106         dlog_print(DLOG_INFO, LOG_TAG, "The result is [%d].", result);
107         if (result > THRESHOLD_OF_DARKNESS) {
108                 if (status != 0) {
109                         _control_led(info, 0);
110                         status = 0;
111                 }
112         } else {
113                 if (status != 1) {
114                         _control_led(info, 1);
115                         status = 1;
116                 }
117         }
118
119         return TRUE;
120 }
121
122 int main(int argc, char* argv[])
123 {
124     char ad[50] = {0, };
125         peripheral_s info = {0, };
126         service_app_lifecycle_callback_s event_callback;
127         app_event_handler_h handlers[5] = {NULL, };
128         guint timer_id = 0;
129         int ret = 0;
130
131         event_callback.create = service_app_create;
132         event_callback.terminate = service_app_terminate;
133         event_callback.app_control = service_app_control;
134
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);
139
140         info.i2c_dev = peripheral_i2c_init(I2C_BUS);
141         if (!info.i2c_dev) {
142                 dlog_print(DLOG_INFO, LOG_TAG, "Cannot initialize the peripheral i2c.");
143                 return 1;
144         }
145
146         if (peripheral_i2c_set_address(info.i2c_dev, GY30_ADDR) != 0) {
147                 dlog_print(DLOG_INFO, LOG_TAG, "Cannot set the address.");
148                 return 1;
149         }
150
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.");
154                 return 1;
155         }
156
157         ret = peripheral_gpio_set_direction(info.gpio_dev, PERIPHERAL_GPIO_DIRECTION_OUT);
158         if (ret != 0) {
159                 dlog_print(DLOG_INFO, LOG_TAG, "Cannot set direction error.");
160                 return 1;
161         }
162
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);
165         if (timer_id <= 0) {
166                 dlog_print(DLOG_INFO, LOG_TAG, "Timer id : %d", timer_id);
167                 return 1;
168         }
169
170         ret = service_app_main(argc, argv, &event_callback, ad);
171
172         g_source_remove(timer_id);
173         peripheral_i2c_stop(info.i2c_dev);
174         peripheral_gpio_close(info.gpio_dev);
175
176         return ret;
177 }
178