Code fixes. Some logs added
[apps/native/gear-racing-car.git] / src / app.c
index 0de4182..0330a21 100644 (file)
--- a/src/app.c
+++ b/src/app.c
 #define AZIMUTH_MIN 200
 #define AZIMUTH_MAX 700
 
+#define CONFIG_LED_STATE_KEY_INIT "init"
+#define CONFIG_DEFAULT_LED_3BIT_INIT 1, 1, 0
+#define CONFIG_DEFAULT_LED_24BIT_INIT 255, 32, 0
+
+#define CONFIG_LED_STATE_KEY_OFF "off"
+#define CONFIG_DEFAULT_LED_3BIT_OFF 0, 0, 0
+#define CONFIG_DEFAULT_LED_24BIT_OFF 0, 0, 0
+
 enum {
        DIR_STATE_S,
        DIR_STATE_F,
@@ -131,11 +139,11 @@ static app_data s_info = {
                .elevation_pin = 14,
                .azimuth_pin = 15,
 
-               .engine_1_forward_pin = 19,
-               .engine_1_back_pin = 16,
+               .engine_1_forward_pin = 6,
+               .engine_1_back_pin = 5,
                .engine_1_channel = 5,
 
-               .engine_2_forward_pin = 24,
+               .engine_2_forward_pin = 21,
                .engine_2_back_pin = 20,
                .engine_2_channel = 4,
 };
@@ -276,7 +284,7 @@ static void _initialize_config()
        modified |= config_get_string_or_set_default(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid, &id);
        g_free(uuid);
 
-       modified |= config_get_string_or_set_default(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati", &name);
+       modified |= config_get_string_or_set_default(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Pink Car", &name);
 
        modified |= config_get_int_with_default(CONFIG_GRP_STEERING, CONFIG_KEY_STEERING_CENTER, s_info.stering_center, &s_info.stering_center);
        modified |= config_get_int_with_default(CONFIG_GRP_STEERING, CONFIG_KEY_STEERING_RANGE, s_info.stering_range, &s_info.stering_range);
@@ -311,12 +319,16 @@ static void _initialize_config()
 
 static void _initialize_components(app_data *ad)
 {
+       FUNCTION_START;
+
        net_util_init();
        _initialize_config();
        cloud_communication_init();
        message_manager_init();
        controller_connection_manager_listen();
        lap_counter_init();
+       resource_led_init();
+
 
        _D("Car settings: Stering[%d +/- %d] Engine[%d - %d], CamAzimuth[%d +/- %d], CamElev[%d - %d]",
                        s_info.stering_center, s_info.stering_range,
@@ -328,10 +340,14 @@ static void _initialize_components(app_data *ad)
                        s_info.stering_pin, s_info.azimuth_pin, s_info.elevation_pin,
                        s_info.engine_1_forward_pin, s_info.engine_1_back_pin, s_info.engine_1_channel,
                        s_info.engine_2_forward_pin, s_info.engine_2_back_pin, s_info.engine_2_channel);
+
+       FUNCTION_END;
 }
 
 static bool service_app_create(void *data)
 {
+       FUNCTION_START;
+
        int ret = 0;
        app_data *ad = data;
 
@@ -341,6 +357,8 @@ static bool service_app_create(void *data)
        _D("-----------------------=======================================================================-----------------------");
        _D("-----------------------=======================================================================-----------------------");
 
+       _initialize_components(ad);
+
        /*
         * if you want to use default configuration,
         * Do not need to call resource_set_motor_driver_L298N_configuration(),
@@ -359,40 +377,52 @@ static bool service_app_create(void *data)
        }
 #endif
 
-       _initialize_components(ad);
        resource_lap_counter_init();
        cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
 
        controller_connection_manager_set_command_received_cb(__command_received_cb);
        controller_connection_manager_set_user_name_received_cb(__user_name_received_cb);
 
+       resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_INIT,
+                       CONFIG_DEFAULT_LED_3BIT_INIT,
+                       CONFIG_DEFAULT_LED_24BIT_INIT,
+                       LED_COLOR_RED);
+
+       FUNCTION_END;
        return true;
 }
 
 static void service_app_control(app_control_h app_control, void *data)
 {
+       FUNCTION_START;
 
 #if ENABLE_MOTOR
        /* set speed 0, to reduce delay of initializing motor driver */
        resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
        resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
-       resource_set_servo_motor_value(s_info.stering_pin, STERING_SERVO_CENTER);
-       resource_set_servo_motor_value(s_info.elevation_pin, ELEVATION_MIN);
-       resource_set_servo_motor_value(s_info.azimuth_pin, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
+       resource_set_servo_motor_value(s_info.stering_pin, s_info.stering_center);
+       resource_set_servo_motor_value(s_info.elevation_pin, s_info.camera_elevation_min);
+       resource_set_servo_motor_value(s_info.azimuth_pin, s_info.camera_azimuth_center);
 #endif
 
+       FUNCTION_END;
        return;
 }
 
 static void service_app_terminate(void *data)
 {
        app_data *ad = data;
+       resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_OFF,
+                       CONFIG_DEFAULT_LED_3BIT_OFF,
+                       CONFIG_DEFAULT_LED_24BIT_OFF,
+                       LED_COLOR_NONE);
 
-       resource_set_servo_motor_value(s_info.stering_pin, STERING_SERVO_CENTER);
-       resource_set_servo_motor_value(s_info.elevation_pin, ELEVATION_MIN);
-       resource_set_servo_motor_value(s_info.azimuth_pin, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
+       resource_set_servo_motor_value(s_info.stering_pin, s_info.stering_center);
+       resource_set_servo_motor_value(s_info.elevation_pin, s_info.camera_elevation_min);
+       resource_set_servo_motor_value(s_info.azimuth_pin, s_info.camera_azimuth_center);
 
        resource_lap_counter_destroy();
+       resource_led_destroy();
 
        if (ad->idle_h)
                g_source_remove(ad->idle_h);