2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <service_app.h>
30 #include "controller.h"
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
40 #define USE_MULTIPLE_SENSOR 1
42 typedef struct app_data_s {
43 Ecore_Timer *getter_timer[PIN_MAX];
46 static Eina_Bool _infrared_motion_getter_timer(void *data)
48 #if USE_MULTIPLE_SENSOR
49 int gpio_num[3] = { 16, 23, 26 };
51 int value[3] = { 0, };
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]);
58 _I("[GPIO:%d] Infrared Motion Value is [%d]", gpio_num[i], value[i]);
62 //@TODO: Send the data to Analyzer using connectivity APIs
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);
71 return ECORE_CALLBACK_RENEW;
74 static Eina_Bool _ultrasonic_getter_timer(void *data)
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);
81 return ECORE_CALLBACK_RENEW;
84 static Eina_Bool _illuminance_getter_timer(void *data)
88 retv_if(resource_read_illuminance_sensor(I2C_BUS_1, &value) == -1, ECORE_CALLBACK_CANCEL);
89 _I("Ultra Sonic Distance is [%d lux]", value);
91 return ECORE_CALLBACK_RENEW;
94 static bool service_app_create(void *data)
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");
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");
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");
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");
130 static void service_app_terminate(void *data)
132 app_data *ad = (app_data *)data;
134 for (int i = 0; i < PIN_MAX; i++) {
135 if (ad->getter_timer[i]) {
136 ecore_timer_del(ad->getter_timer[i]);
139 resource_close_all();
143 static void service_app_control(app_control_h app_control, void *data)
145 // Todo: add your code here.
148 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
150 /*APP_EVENT_LANGUAGE_CHANGED*/
153 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
155 /*APP_EVENT_REGION_FORMAT_CHANGED*/
158 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
160 /*APP_EVENT_LOW_BATTERY*/
163 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
165 /*APP_EVENT_LOW_MEMORY*/
168 int main(int argc, char* argv[])
172 service_app_lifecycle_callback_s event_callback;
173 app_event_handler_h handlers[5] = {NULL, };
175 ad = calloc(1, sizeof(app_data));
177 event_callback.create = service_app_create;
178 event_callback.terminate = service_app_terminate;
179 event_callback.app_control = service_app_control;
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);
186 ret = service_app_main(argc, argv, &event_callback, ad);