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.
24 #include <service_app.h>
29 #include "connection_manager.h"
32 #define ENABLE_MOTOR 1
40 typedef struct app_data_s {
43 unsigned int dir_state;
47 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
52 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
57 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
59 _E("low battery! exit app");
65 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
70 static inline double __map_round(double val)
72 return floor(val + 0.5);
75 static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
79 slope = 1.0 * (d_max - d_min) / (v_max - v_min);
81 rval = d_min + __map_round(slope * (val - v_min));
86 static int ___map_speed_val(int speed)
88 static const int motor_max = 4095;
89 static const int motor_min = -4095;
90 static const int speed_max = 1000;
91 static const int speed_min = -1000;
93 return __map_range_val(motor_max, motor_min,
94 speed_max, speed_min, speed);
97 static int ___map_servo_val(int servo)
99 static const int motor_max = 500;
100 static const int motor_min = 400;
101 static const int servo_max = 1000;
102 static const int servo_min = -1000;
104 return __map_range_val(motor_max, motor_min,
105 servo_max, servo_min, servo);
109 static int __driving_motors(int servo, int speed)
114 val_servo = ___map_servo_val(servo);
115 val_speed = ___map_speed_val(speed);
117 _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
118 servo, val_servo, speed, val_speed);
120 resource_set_servo_motor_value(0, val_servo);
121 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, val_speed);
122 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, val_speed);
128 static gboolean __message_dispatcher(gpointer user_data)
130 message_s *msg = NULL;
133 msg = message_pop_from_inqueue();
136 case MESSAGE_CMD_HELLO:
137 /* TODO : say hello to sender */
139 case MESSAGE_CMD_CALIBRATION:
140 /* TODO : set calibration mode */
142 case MESSAGE_CMD_DRIVE:
143 /* TODO : driving car */
144 __driving_motors(msg->servo, msg->speed);
146 case MESSAGE_CMD_BYE:
147 __driving_motors(0, 0);
157 static void __recv_state_change(receiver_type_e type,
158 receiver_state_e state, void* user_data)
160 app_data *ad = user_data;
163 _D("receiver type[%d] state changed[%d]", type, state);
165 if (state == RECEIVER_STATE_CONNECTED) {
167 ad->idle_h = g_idle_add(__message_dispatcher, ad);
170 g_source_remove(ad->idle_h);
173 __driving_motors(0, 0);
179 static void __conn_state_changed_cb(connection_state_e state,
180 const char *ip, void* user_data)
182 app_data *ad = user_data;
184 _D("connection state changed : %d", state);
186 if (state == CONNECTION_STATE_CONNECTED) {
187 receiver_start(RECEIVER_TYPE_UDP);
190 receiver_stop(RECEIVER_TYPE_UDP);
193 g_source_remove(ad->idle_h);
197 __driving_motors(0, 0);
202 static bool service_app_create(void *data)
208 * if you want to use default configuration,
209 * Do not need to call resource_set_motor_driver_L298N_configuration(),
213 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
215 _E("resource_set_motor_driver_L298N_configuration()");
218 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
220 _E("resource_set_motor_driver_L298N_configuration()");
225 receiver_init(RECEIVER_TYPE_UDP);
226 receiver_set_state_changed_cb(RECEIVER_TYPE_UDP, __recv_state_change, ad);
228 connection_manager_init();
229 connection_manager_set_state_changed_cb(__conn_state_changed_cb, ad);
237 static void service_app_control(app_control_h app_control, void *data)
241 /* set speed 0, to reduce delay of initializing motor driver */
242 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
243 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
244 resource_set_servo_motor_value(0, 450);
250 static void service_app_terminate(void *data)
255 g_source_remove(ad->idle_h);
258 connection_manager_fini();
260 receiver_fini(RECEIVER_TYPE_UDP);
262 resource_close_all();
270 int main(int argc, char* argv[])
274 service_app_lifecycle_callback_s event_callback;
275 app_event_handler_h handlers[5] = {NULL, };
277 log_type_set(LOG_TYPE_DLOG);
279 ad = calloc(1, sizeof(app_data));
282 event_callback.create = service_app_create;
283 event_callback.terminate = service_app_terminate;
284 event_callback.app_control = service_app_control;
286 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
287 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
288 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
289 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
290 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
291 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
292 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
293 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
295 ret = service_app_main(argc, argv, &event_callback, ad);
297 _E("failed to start app");