Implement multiple sensor logic
[apps/native/rcc.git] / src / controller.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *          Geunsun Lee <gs86.lee@samsung.com>
6  *          Eunyoung Lee <ey928.lee@samsung.com>
7  *          Junkyu Han <junkyu.han@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22
23 #include <tizen.h>
24 #include <Ecore.h>
25 #include <service_app.h>
26 #include <unistd.h>
27 #include <glib.h>
28
29 #include "log.h"
30 #include "controller.h"
31 #include "resource.h"
32
33 #define I2C_BUS_1 0x1
34 #define GPIO_NOT_USED -1
35 #define GPIO_ULTRASONIC_TRIG_NUM_1 20
36 #define GPIO_ULTRASONIC_ECHO_NUM_1 21
37 #define GPIO_INFRARED_MOTION_NUM_1 4
38 #define I2C_ILLUMINANCE_FIRST_PIN_1 3
39
40 #define USE_MULTIPLE_SENSOR 1
41
42 typedef struct app_data_s {
43         Ecore_Timer *getter_timer[PIN_MAX];
44 } app_data;
45
46 static Eina_Bool _infrared_motion_getter_timer(void *data)
47 {
48 #if USE_MULTIPLE_SENSOR
49         int gpio_num[3] = { 16, 23, 26 };
50         int i = 0;
51         int value[3] = { 0, };
52
53         for (i = 0; i < 3; i++) {
54                 if (model_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
55                         _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
56                         continue;
57                 }
58                 _I("[GPIO:%d] Infrared Motion Value is [%d]", gpio_num[i], value[i]);
59         }
60
61
62         //@TODO: Send the data to Analyzer using connectivity APIs
63
64 #else
65         int value = 0;
66
67         retv_if(resource_read_infrared_motion_sensor(GPIO_INFRARED_MOTION_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
68         _I("Infrared Motion Value is [%d]", value);
69 #endif
70
71         return ECORE_CALLBACK_RENEW;
72 }
73
74 static Eina_Bool _ultrasonic_getter_timer(void *data)
75 {
76         double value = 0;
77
78         retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
79         _I("Ultra Sonic Distance is [%d cm]", value);
80
81         return ECORE_CALLBACK_RENEW;
82 }
83
84 static Eina_Bool _illuminance_getter_timer(void *data)
85 {
86         int value = 0;
87
88         retv_if(resource_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL);
89         _I("Ultra Sonic Distance is [%d lux]", value);
90
91         return ECORE_CALLBACK_RENEW;
92 }
93
94 static bool service_app_create(void *data)
95 {
96         app_data *ad = data;
97
98 #if USE_MULTIPLE_SENSOR
99         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0, _infrared_motion_getter_timer, ad);
100         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
101                 _E("Failed to add infrared motion getter timer");
102                 return false;
103         }
104 #else
105         /* One Pin Sensor */
106         ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1] = ecore_timer_add(3.0, _infrared_motion_getter_timer, ad);
107         if (!ad->getter_timer[GPIO_INFRARED_MOTION_NUM_1]) {
108                 _D("Failed to add infrared motion getter timer");
109                 return false;
110         }
111
112         /* Two Pins Sensor */
113         ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1] = ecore_timer_add(1.0, _ultrasonic_getter_timer, ad);
114         if (!ad->getter_timer[GPIO_ULTRASONIC_TRIG_NUM_1]) {
115                 _D("Failed to add ultra sonic getter timer");
116                 return false;
117         }
118
119         /* I2C Protocol Sensor */
120         ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1] = ecore_timer_add(1.0, _illuminance_getter_timer, ad);
121         if (!ad->getter_timer[I2C_ILLUMINANCE_FIRST_PIN_1]) {
122                 _D("Failed to add ultra sonic getter timer");
123                 return false;
124         }
125 #endif
126
127     return true;
128 }
129
130 static void service_app_terminate(void *data)
131 {
132         app_data *ad = (app_data *)data;
133
134         for (int i = 0; i < PIN_MAX; i++) {
135                 if (ad->getter_timer[i]) {
136                         ecore_timer_del(ad->getter_timer[i]);
137                 }
138         }
139         resource_close_all();
140         free(ad);
141 }
142
143 static void service_app_control(app_control_h app_control, void *data)
144 {
145     // Todo: add your code here.
146 }
147
148 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
149 {
150         /*APP_EVENT_LANGUAGE_CHANGED*/
151 }
152
153 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
154 {
155         /*APP_EVENT_REGION_FORMAT_CHANGED*/
156 }
157
158 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
159 {
160         /*APP_EVENT_LOW_BATTERY*/
161 }
162
163 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
164 {
165         /*APP_EVENT_LOW_MEMORY*/
166 }
167
168 int main(int argc, char* argv[])
169 {
170         app_data *ad = NULL;
171         int ret = 0;
172         service_app_lifecycle_callback_s event_callback;
173         app_event_handler_h handlers[5] = {NULL, };
174
175         ad = calloc(1, sizeof(app_data));
176
177         event_callback.create = service_app_create;
178         event_callback.terminate = service_app_terminate;
179         event_callback.app_control = service_app_control;
180
181         service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
182         service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
183         service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
184         service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
185
186         ret = service_app_main(argc, argv, &event_callback, ad);
187
188         return ret;
189 }