2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Jeonghoon Park <jh1979.park@samsung.com>
6 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
22 #include <service_app.h>
33 typedef struct app_data_s {
36 unsigned int dir_state;
42 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
47 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
52 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
57 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
62 static void ___________control_motor(app_data *ad)
64 _D("control motor, state(%u), f_val(%u), r_val(%u)",
65 ad->dir_state, ad->f_value, ad->r_value);
68 switch (ad->dir_state) {
72 ad->dir_state = DIR_STATE_S;
73 dc_motor_speed_set(DC_MOTOR_ID_L, 0);
74 dc_motor_speed_set(DC_MOTOR_ID_R, 0);
76 ad->dir_state = DIR_STATE_B;
77 dc_motor_speed_set(DC_MOTOR_ID_L, -2000);
78 dc_motor_speed_set(DC_MOTOR_ID_R, -2000);
85 ad->dir_state = DIR_STATE_S;
86 dc_motor_speed_set(DC_MOTOR_ID_L, 0);
87 dc_motor_speed_set(DC_MOTOR_ID_R, 0);
89 ad->dir_state = DIR_STATE_F;
90 dc_motor_speed_set(DC_MOTOR_ID_L, 2000);
91 dc_motor_speed_set(DC_MOTOR_ID_R, 2000);
97 ad->dir_state = DIR_STATE_F;
98 dc_motor_speed_set(DC_MOTOR_ID_L, 2000);
99 dc_motor_speed_set(DC_MOTOR_ID_R, 2000);
100 } else if (!ad->r_value) {
101 ad->dir_state = DIR_STATE_B;
102 dc_motor_speed_set(DC_MOTOR_ID_L, -2000);
103 dc_motor_speed_set(DC_MOTOR_ID_R, -2000);
111 static void _front_ioa_sensor_changed_cb(unsigned int value, void *data)
115 _D("FRONT has obstacle!");
119 ___________control_motor(ad);
124 static void _back_ioa_sensor_changed_cb(unsigned int value, void *data)
128 _D("BACK has obstacle!");
132 ___________control_motor(ad);
137 static bool service_app_create(void *data)
141 ret = dc_motor_init();
143 _E("failed init motor, terminating this application");
150 static void service_app_control(app_control_h app_control, void *data)
155 resource_read_infrared_obstacle_avoidance_sensor(FRONT_PIN, &ad->f_value);
156 resource_read_infrared_obstacle_avoidance_sensor(REAR_PIN, &ad->r_value);
158 resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(FRONT_PIN, _front_ioa_sensor_changed_cb, ad);
159 resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(REAR_PIN, _back_ioa_sensor_changed_cb, ad);
161 ___________control_motor(ad);
166 static void service_app_terminate(void *data)
178 int main(int argc, char* argv[])
182 service_app_lifecycle_callback_s event_callback;
183 app_event_handler_h handlers[5] = {NULL, };
185 log_type_set(LOG_TYPE_DLOG);
187 ad = calloc(1, sizeof(app_data));
190 event_callback.create = service_app_create;
191 event_callback.terminate = service_app_terminate;
192 event_callback.app_control = service_app_control;
194 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
195 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
196 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
197 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
198 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
199 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
200 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
201 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
203 ret = service_app_main(argc, argv, &event_callback, ad);
205 _E("failed to start app");