Resetting the lap counter when a new name is recived
[apps/native/gear-racing-car.git] / src / app.c
index 01d8c73..5c6a593 100644 (file)
--- a/src/app.c
+++ b/src/app.c
 #include "cloud/cloud_communication.h"
 #include "messages/message_manager.h"
 #include "controller_connection_manager.h"
+#include "lap_counter/lap_counter.h"
 #include "command.h"
 
 #define ENABLE_MOTOR 1
+#define STERING_SERVO_CENTER 340
+#define STERING_SERVO_RANGE 125
+
+#define MAX_UDP_INPUT 10000
 
 #define CONFIG_GRP_CAR "Car"
 #define CONFIG_KEY_ID "Id"
 #define CONFIG_KEY_NAME "Name"
 #define CLOUD_REQUESTS_FREQUENCY 15
+#define AZIMUTH_SERVO_PIN 15
+#define ELEVATION_SERVO_PIN 14
+
+#define ELEVATION_MIN 200
+#define ELEVATION_MAX 400
+#define AZIMUTH_MIN 200
+#define AZIMUTH_MAX 700
 
 enum {
        DIR_STATE_S,
@@ -48,6 +60,7 @@ typedef struct app_data_s {
        unsigned int f_value;
        unsigned int r_value;
        unsigned int dir_state;
+       const char *user_name;
        guint idle_h;
 } app_data;
 
@@ -82,7 +95,7 @@ static inline double __map_round(double val)
        return floor(val + 0.5);
 }
 
-static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
+static int __map_range_val(int v_min, int v_max, int d_min, int d_max, int val)
 {
        int rval = 0;
        double slope = 0;
@@ -93,26 +106,14 @@ static int __map_range_val(int d_max, int d_min, int v_max, int v_min, int val)
        return rval;
 }
 
-static int ___map_speed_val(int speed)
+static inline int ___map_speed_val(int speed)
 {
-       static const int motor_max = 4095;
-       static const int motor_min = -4095;
-       static const int speed_max = 1000;
-       static const int speed_min = -1000;
-
-       return __map_range_val(motor_max, motor_min,
-               speed_max, speed_min, speed);
+       return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, -4095, 4095, speed);
 }
 
-static int ___map_servo_val(int servo)
+static inline int ___map_servo_val(int servo)
 {
-       static const int motor_max = 500;
-       static const int motor_min = 400;
-       static const int servo_max = 1000;
-       static const int servo_min = -1000;
-
-       return __map_range_val(motor_max, motor_min,
-               servo_max, servo_min, servo);
+       return __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, STERING_SERVO_CENTER - STERING_SERVO_RANGE, STERING_SERVO_CENTER + STERING_SERVO_RANGE, servo);
 }
 
 static int __driving_motors(int servo, int speed)
@@ -136,20 +137,13 @@ static int __driving_motors(int servo, int speed)
 
 static void __camera(int azimuth, int elevation)
 {
-       static const int camer_input_min = -1000;
-       static const int camer_input_max = 1000;
-       static const int camer_azimuth_min = 200;
-       static const int camer_azimuth_max = 700;
-       static const int camer_elevation_min = 200;
-       static const int camer_elevation_max = 400;
-
-       int val_azimuth = __map_range_val(camer_input_min, camer_input_max, camer_azimuth_min, camer_azimuth_max, azimuth);
-       int val_elevation = __map_range_val(0, camer_input_max, camer_elevation_min, camer_elevation_max, elevation); // No need to look upside down
+       int val_azimuth = __map_range_val(-MAX_UDP_INPUT, MAX_UDP_INPUT, AZIMUTH_MIN, AZIMUTH_MAX, azimuth);
+       int val_elevation = __map_range_val(0, MAX_UDP_INPUT, ELEVATION_MIN, ELEVATION_MAX, elevation); // No need to look upside down
 
        _D("camera - azimuth[%4d : %4d], elevation[%4d : %4d]", azimuth, val_azimuth, elevation, val_elevation);
 #if ENABLE_MOTOR
-       resource_set_servo_motor_value(14, val_elevation);
-       resource_set_servo_motor_value(15, val_azimuth);
+       resource_set_servo_motor_value(ELEVATION_SERVO_PIN, val_elevation);
+       resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, val_azimuth);
 #endif
 }
 
@@ -173,6 +167,13 @@ static void __command_received_cb(command_s command) {
        }
 }
 
+static void __user_name_received_cb(const char *name)
+{
+       _D("User name received: %s", name);
+       lap_counter_set_user_name(name);
+       lap_counter_set_start_lap();
+}
+
 static void _initialize_config()
 {
        net_util_init();
@@ -206,6 +207,7 @@ static void _initialize_components(app_data *ad)
        cloud_communication_init();
        message_manager_init();
        controller_connection_manager_listen();
+       lap_counter_init();
 }
 
 static bool service_app_create(void *data)
@@ -213,6 +215,12 @@ static bool service_app_create(void *data)
        int ret = 0;
        app_data *ad = data;
 
+       _D("-----------------------=======================================================================-----------------------");
+       _D("-----------------------=======================================================================-----------------------");
+       _D("-----------------------============================== APP %s %s ==============================-----------------------", __DATE__, __TIME__);
+       _D("-----------------------=======================================================================-----------------------");
+       _D("-----------------------=======================================================================-----------------------");
+
        /*
         * if you want to use default configuration,
         * Do not need to call resource_set_motor_driver_L298N_configuration(),
@@ -231,11 +239,15 @@ static bool service_app_create(void *data)
        }
 #endif
 
+       resource_lap_counter_init();
+
        _initialize_components(ad);
        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);
 
+       lap_counter_init();
        return true;
 }
 
@@ -247,6 +259,8 @@ static void service_app_control(app_control_h app_control, void *data)
        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(0, 450);
+       resource_set_servo_motor_value(ELEVATION_SERVO_PIN, ELEVATION_MIN);
+       resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
 #endif
 
        return;
@@ -256,10 +270,16 @@ static void service_app_terminate(void *data)
 {
        app_data *ad = data;
 
+       resource_set_servo_motor_value(0, STERING_SERVO_CENTER);
+       resource_set_servo_motor_value(ELEVATION_SERVO_PIN, ELEVATION_MIN);
+       resource_set_servo_motor_value(AZIMUTH_SERVO_PIN, (AZIMUTH_MIN + AZIMUTH_MAX) / 2);
+
+       resource_lap_counter_destroy();
+
        if (ad->idle_h)
                g_source_remove(ad->idle_h);
 
-
+       lap_counter_shutdown();
        controller_connection_manager_release();
        message_manager_shutdown();