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 #include "cloud/cloud_communication.h"
34 #define ENABLE_MOTOR 1
36 #define CONFIG_GRP_CAR "Car"
37 #define CONFIG_KEY_ID "Id"
38 #define CONFIG_KEY_NAME "Name"
39 #define CLOUD_REQUESTS_FREQUENCY 15
47 typedef struct app_data_s {
50 unsigned int dir_state;
54 static void _initialize_components(app_data *ad);
55 static void _initialize_config();
57 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
62 static void service_app_region_changed(app_event_info_h event_info, void *user_data)
67 static void service_app_low_battery(app_event_info_h event_info, void *user_data)
69 _E("low battery! exit app");
75 static void service_app_low_memory(app_event_info_h event_info, void *user_data)
80 static inline double __map_round(double val)
82 return floor(val + 0.5);
85 static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
89 slope = 1.0 * (d_max - d_min) / (v_max - v_min);
91 rval = d_min + __map_round(slope * (val - v_min));
96 static int ___map_speed_val(int speed)
98 static const int motor_max = 4095;
99 static const int motor_min = -4095;
100 static const int speed_max = 1000;
101 static const int speed_min = -1000;
103 return __map_range_val(motor_max, motor_min,
104 speed_max, speed_min, speed);
107 static int ___map_servo_val(int servo)
109 static const int motor_max = 500;
110 static const int motor_min = 400;
111 static const int servo_max = 1000;
112 static const int servo_min = -1000;
114 return __map_range_val(motor_max, motor_min,
115 servo_max, servo_min, servo);
119 static int __driving_motors(int servo, int speed)
124 val_servo = ___map_servo_val(servo);
125 val_speed = ___map_speed_val(speed);
127 _D("control motor - servo[%4d : %4d], speed[%4d : %4d]",
128 servo, val_servo, speed, val_speed);
130 resource_set_servo_motor_value(0, val_servo);
131 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, val_speed);
132 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, val_speed);
138 static gboolean __message_dispatcher(gpointer user_data)
140 message_s *msg = NULL;
143 msg = message_pop_from_inqueue();
146 case MESSAGE_CMD_HELLO:
147 /* TODO : say hello to sender */
149 case MESSAGE_CMD_CALIBRATION:
150 /* TODO : set calibration mode */
152 case MESSAGE_CMD_DRIVE:
153 /* TODO : driving car */
154 __driving_motors(msg->servo, msg->speed);
156 case MESSAGE_CMD_BYE:
157 __driving_motors(0, 0);
167 static void __recv_state_change(receiver_type_e type,
168 receiver_state_e state, void* user_data)
170 app_data *ad = user_data;
173 _D("receiver type[%d] state changed[%d]", type, state);
175 if (state == RECEIVER_STATE_CONNECTED) {
177 ad->idle_h = g_idle_add(__message_dispatcher, ad);
180 g_source_remove(ad->idle_h);
183 __driving_motors(0, 0);
189 static void __conn_state_changed_cb(connection_state_e state,
190 const char *ip, void* user_data)
192 app_data *ad = user_data;
194 _D("connection state changed : %d", state);
196 if (state == CONNECTION_STATE_CONNECTED) {
197 receiver_start(RECEIVER_TYPE_UDP);
200 receiver_stop(RECEIVER_TYPE_UDP);
203 g_source_remove(ad->idle_h);
207 __driving_motors(0, 0);
212 static void _initialize_config()
217 gboolean modified = false;
218 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
219 char *uuid = g_uuid_string_random();
220 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
224 if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
225 config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
228 if (modified == true) {
235 static void _initialize_components(app_data *ad)
237 receiver_init(RECEIVER_TYPE_UDP);
238 receiver_set_state_changed_cb(RECEIVER_TYPE_UDP, __recv_state_change, ad);
240 connection_manager_init();
241 connection_manager_set_state_changed_cb(__conn_state_changed_cb, ad);
243 _initialize_config();
244 cloud_communication_init();
247 static bool service_app_create(void *data)
253 * if you want to use default configuration,
254 * Do not need to call resource_set_motor_driver_L298N_configuration(),
258 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
260 _E("resource_set_motor_driver_L298N_configuration()");
263 ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
265 _E("resource_set_motor_driver_L298N_configuration()");
270 _initialize_components(ad);
271 cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
278 static void service_app_control(app_control_h app_control, void *data)
282 /* set speed 0, to reduce delay of initializing motor driver */
283 resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
284 resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
285 resource_set_servo_motor_value(0, 450);
291 static void service_app_terminate(void *data)
296 g_source_remove(ad->idle_h);
299 connection_manager_fini();
300 receiver_fini(RECEIVER_TYPE_UDP);
302 cloud_communication_stop();
303 cloud_communication_fini();
307 resource_close_all();
315 int main(int argc, char* argv[])
319 service_app_lifecycle_callback_s event_callback;
320 app_event_handler_h handlers[5] = {NULL, };
322 log_type_set(LOG_TYPE_DLOG);
324 ad = calloc(1, sizeof(app_data));
327 event_callback.create = service_app_create;
328 event_callback.terminate = service_app_terminate;
329 event_callback.app_control = service_app_control;
331 service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY],
332 APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
333 service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY],
334 APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
335 service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED],
336 APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
337 service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED],
338 APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);
340 ret = service_app_main(argc, argv, &event_callback, ad);
342 _E("failed to start app");