Init master branch with timer code
[apps/native/st-things-light.git] / src / controller.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <tizen.h>
18 #include <service_app.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <Ecore.h>
23
24 #include "log.h"
25 #include "resource/resource_infrared_motion.h"
26 #include "resource/resource_led.h"
27
28 // Timer duration
29 #define TIMER_GATHER_INTERVAL (2.0f)
30
31 // Motion sensor information
32 #define SENSOR_MOTION_GPIO_NUMBER (18)
33
34 // LED sensor information
35 #define SENSOR_LED_GPIO_NUMBER (24)
36
37 Ecore_Timer *getter_timer = NULL;
38
39 static int _change_led_state(int state) {
40         int ret = 0;
41
42         // Write state to LED light
43         ret = resource_write_led(SENSOR_LED_GPIO_NUMBER, state);
44         if (ret != 0) {
45                 _E("cannot write led data");
46                 return -1;
47         }
48
49         _I("LED State : %s", state ? "ON":"OFF");
50
51         return 0;
52 }
53
54 static Eina_Bool _get_motion_set_led(void *user_data)
55 {
56         int ret = 0;
57         uint32_t value = 0;
58
59         // Get value from motion sensor
60         ret = resource_read_infrared_motion(SENSOR_MOTION_GPIO_NUMBER, &value);
61         if (ret != 0) {
62                 _E("cannot read data from the infrared motion sensor");
63                 return ECORE_CALLBACK_CANCEL;
64         }
65
66         _I("Detected motion value : %u", value);
67
68         // Set LED light with value
69         _change_led_state(value);
70
71         return ECORE_CALLBACK_RENEW;
72 }
73
74 static bool service_app_create(void *user_data)
75 {
76         return true;
77 }
78
79 static void service_app_control(app_control_h app_control, void *user_data)
80 {
81         // Delete old timer if there is one
82         if (getter_timer) {
83                 ecore_timer_del(getter_timer);
84                 getter_timer = NULL;
85         }
86
87         // Create a timer to call the given function in given period of time
88         getter_timer = ecore_timer_add(TIMER_GATHER_INTERVAL, _get_motion_set_led, user_data);
89         if (!getter_timer) {
90                 _E("Failed to add getter timer");
91                 return;
92         }
93 }
94
95 static void service_app_terminate(void *user_data)
96 {
97         // Delete timer
98         if (getter_timer) {
99                 ecore_timer_del(getter_timer);
100                 getter_timer = NULL;
101         }
102
103         // Turn off LED light with __set_led()
104         _change_led_state(0);
105
106         // Close Motion and LED resources
107         resource_close_infrared_motion();
108         resource_close_led();
109
110         FN_END;
111 }
112
113 int main(int argc, char *argv[])
114 {
115         service_app_lifecycle_callback_s event_callback;
116
117         event_callback.create = service_app_create;
118         event_callback.terminate = service_app_terminate;
119         event_callback.app_control = service_app_control;
120
121         return service_app_main(argc, argv, &event_callback, NULL);
122 }