add servo motor api to resoure module
[apps/native/gear-racing-car.git] / src / app.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Jeonghoon Park <jh1979.park@samsung.com>
5  *
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
9  *
10  * http://floralicense.org/license/
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 <stdio.h>
20 #include <unistd.h>
21 #include <glib.h>
22 #include <service_app.h>
23 #include "log.h"
24 #include "dc_motor.h"
25 #include "resource.h"
26
27 enum {
28         DIR_STATE_S,
29         DIR_STATE_F,
30         DIR_STATE_B,
31 };
32
33 typedef struct app_data_s {
34         unsigned int f_value;
35         unsigned int r_value;
36         unsigned int dir_state;
37 } app_data;
38
39 #define FRONT_PIN 21
40 #define REAR_PIN 4
41
42 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
43 {
44         return;
45 }
46
47 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
48 {
49         return;
50 }
51
52 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
53 {
54         return;
55 }
56
57 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
58 {
59         return;
60 }
61
62 static void ___________control_motor(app_data *ad)
63 {
64         _D("control motor, state(%u), f_val(%u), r_val(%u)",
65                         ad->dir_state, ad->f_value, ad->r_value);
66
67
68         switch (ad->dir_state) {
69                 case DIR_STATE_F:
70                         if (ad->f_value)  {
71                                 if (ad->r_value) {
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);
75                                 } else {
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);
79                                 }
80                         }
81                         break;
82                 case DIR_STATE_B:
83                         if (ad->r_value)  {
84                                 if (ad->f_value) {
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);
88                                 } else {
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);
92                                 }
93                         }
94                         break;
95                 case DIR_STATE_S:
96                         if (!ad->f_value)  {
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);
104                         }
105                         break;
106         }
107
108         return;
109 }
110
111 static void _front_ioa_sensor_changed_cb(unsigned int value, void *data)
112 {
113         app_data *ad = data;
114
115         _D("FRONT has obstacle!");
116
117         ad->f_value = value;
118
119         ___________control_motor(ad);
120
121         return;
122 }
123
124 static void _back_ioa_sensor_changed_cb(unsigned int value, void *data)
125 {
126         app_data *ad = data;
127
128         _D("BACK has obstacle!");
129
130         ad->r_value = value;
131
132         ___________control_motor(ad);
133
134         return;
135 }
136
137 static bool service_app_create(void *data)
138 {
139         int ret = 0;
140
141         ret = dc_motor_init();
142         if (ret) {
143                 _E("failed init motor, terminating this application");
144                 service_app_exit();
145         }
146
147         return true;
148 }
149
150 static void service_app_control(app_control_h app_control, void *data)
151 {
152         app_data *ad = data;
153         int ret;
154
155         resource_read_infrared_obstacle_avoidance_sensor(FRONT_PIN, &ad->f_value);
156         resource_read_infrared_obstacle_avoidance_sensor(REAR_PIN, &ad->r_value);
157
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);
160
161         ___________control_motor(ad);
162
163         return;
164 }
165
166 static void service_app_terminate(void *data)
167 {
168         app_data *ad = data;
169
170         dc_motor_fini();
171         log_file_close();
172
173         _D("Bye ~");
174
175         return;
176 }
177
178 int main(int argc, char* argv[])
179 {
180         app_data *ad = NULL;
181         int ret = 0;
182         service_app_lifecycle_callback_s event_callback;
183         app_event_handler_h handlers[5] = {NULL, };
184
185         log_type_set(LOG_TYPE_DLOG);
186
187         ad = calloc(1, sizeof(app_data));
188         retv_if(!ad, -1);
189
190         event_callback.create = service_app_create;
191         event_callback.terminate = service_app_terminate;
192         event_callback.app_control = service_app_control;
193
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);
202
203         ret = service_app_main(argc, argv, &event_callback, ad);
204         if (ret)
205                 _E("failed to start app");
206
207         return 0;
208 }
209