Integration with the lap_counter module 53/191653/3
authorMichal Skorupinski <m.skorupinsk@samsung.com>
Thu, 18 Oct 2018 15:01:25 +0000 (17:01 +0200)
committerMichal Skorupinski <m.skorupinsk@samsung.com>
Thu, 25 Oct 2018 18:11:07 +0000 (20:11 +0200)
Change-Id: Ie1843ccd6fc72e1819dc9f17aca3f836b4d7293b
Signed-off-by: Michal Skorupinski <m.skorupinsk@samsung.com>
CMakeLists.txt
inc/lap_counter/lap_counter.h
src/app.c
src/lap_counter/lap_counter.c
src/resource/resource_lap_counter.c

index 4db9490..c5fcd12 100644 (file)
@@ -64,6 +64,7 @@ ADD_EXECUTABLE(${PROJECT_NAME}
        ${PROJECT_ROOT_DIR}/src/cloud/http_request.c
        ${PROJECT_ROOT_DIR}/src/net-util.c
        ${PROJECT_ROOT_DIR}/src/cloud/cloud_communication.c
+       ${PROJECT_ROOT_DIR}/src/lap_counter/lap_counter.c
 )
 
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -lm)
index 6a4f1a2..9904c93 100644 (file)
@@ -17,8 +17,9 @@
 #ifndef LAP_COUNTER_H_
 #define LAP_COUNTER_H_
 
-int lap_counter_init();
-int lap_counter_set_user_name(const char *user_name);
-int lap_counter_shutdown();
+void lap_counter_init();
+void lap_counter_set_user_name(const char *user_name);
+void lap_counter_get_lap_time();
+void lap_counter_shutdown();
 
 #endif //LAP_COUNTER_H_
index 0092caf..b645682 100644 (file)
--- a/src/app.c
+++ b/src/app.c
@@ -31,6 +31,7 @@
 #include "controller_connection_manager.h"
 #include "lap_counter/lap_counter.h"
 #include "command.h"
+#include "lap_counter/lap_counter.h"
 
 #define ENABLE_MOTOR 1
 #define STERING_SERVO_CENTER 340
@@ -244,6 +245,7 @@ static bool service_app_create(void *data)
        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;
 }
 
index 800e193..dfbd299 100644 (file)
  */
 
 #include "lap_counter/lap_counter.h"
-#include <stdlib.h>
+#include <log.h>
 #include <string.h>
-#include "log.h"
+#include <stdio.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define MIN_LAP_TIME 5
+#define MAX_NAME_LENGTH 256
 
 typedef struct lap_counter_data {
        const char *user_name;
+       struct timespec last_timestamp;
 } lap_counter_data_t;
 
 static lap_counter_data_t s_info = {0, };
 
-int lap_counter_init()
+void lap_counter_init()
 {
-       s_info.user_name = NULL;
-       return 0;
+       char buf[MAX_NAME_LENGTH];
+       snprintf(buf, MAX_NAME_LENGTH, "Default: %s %s", __DATE__, __TIME__); //
+
+       s_info.user_name = strdup(buf);
 }
 
-int lap_counter_set_user_name(const char *user_name)
+void lap_counter_set_user_name(const char *user_name)
 {
        free((void*)s_info.user_name);
        s_info.user_name = strdup(user_name);
        if(!s_info.user_name) {
-               return -1;
+               return;
        }
        _D("User name set to %s", s_info.user_name);
-       return 0;
 }
 
-int lap_counter_shutdown()
+const char *lap_counter_get_user_name(void)
 {
-       free((void*)s_info.user_name);
-       return 0;
+       return s_info.user_name;
+}
+
+void _print_time(const char *title, struct timespec *ts)
+{
+       char buf[PATH_MAX];
+       struct tm *_tm = localtime(&(ts->tv_sec));
+       strftime(&buf[0], PATH_MAX, "%H:%M:%S", _tm);
+
+       snprintf(&buf[strlen(buf)], PATH_MAX, ".%03ld", ts->tv_nsec / (long)1e6);
+
+       _D ("%s %s", title, buf);
+}
+
+static inline struct timespec _calculate_lap_time(struct timespec *prev, struct timespec *now)
+{
+       struct timespec lap;
+
+       lap.tv_sec = now->tv_sec - prev->tv_sec;
+       lap.tv_nsec = now->tv_nsec - prev->tv_nsec;
+
+
+       _D("----------------------------------------------");
+       _print_time("PREV:\t", prev);
+       _print_time("NOW:\t", now);
+
+       if (lap.tv_sec < MIN_LAP_TIME) {
+               lap.tv_sec = 0;
+               lap.tv_nsec = 0;
+               _D ("TOO SHORT LAP");
+               return lap;
+       }
+
+
+       _print_time("LAP:\t", &lap);
+
+       if (lap.tv_nsec < 0) {
+               lap.tv_sec--;
+               lap.tv_nsec = 1e9 + lap.tv_nsec;
+
+               _print_time("LAP_MOD:\t", &lap);
+       }
+       _D("----------------------------------------------");
+
+       return lap;
+}
+
+void lap_counter_get_lap_time()
+{
+       struct timespec timestamp;
+       int ret = clock_gettime(CLOCK_MONOTONIC, &timestamp);
+       retv_error_message(ret != 0, ret);
+
+       if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) {
+               _calculate_lap_time(&s_info.last_timestamp, &timestamp);
+       } else {
+               _D("Initial lap");
+       }
+
+       s_info.last_timestamp.tv_sec = timestamp.tv_sec;
+       s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
+}
+
+void lap_counter_shutdown()
+{
+       free(s_info.user_name);
 }
index bde429c..fd54e22 100644 (file)
 #include <peripheral_io.h>
 #include <time.h>
 #include "log.h"
-
+#include "lap_counter/lap_counter.h"
 
 #define GPIO_PIN 26
-#define MIN_LAP_TIME 5
+
 
 typedef struct _lap_counter_s {
        peripheral_gpio_h gpio;
-       struct timespec last_timestamp;
        uint32_t gpio_initial_value;
        uint32_t gpio_prev_value;
 } lap_counter_s;
@@ -41,66 +40,6 @@ static lap_counter_s s_info = { 0, };
                return; \
        }
 
-void _print_time(const char *title, struct timespec *ts)
-{
-       char buf[PATH_MAX];
-       struct tm *_tm = localtime(&(ts->tv_sec));
-       strftime(&buf[0], PATH_MAX, "%H:%M:%S", _tm);
-
-       snprintf(&buf[strlen(buf)], PATH_MAX, ".%03ld", ts->tv_nsec / (long)1e6);
-
-       _D ("%s %s", title, buf);
-}
-
-static struct timespec _calculate_lap_time(struct timespec *prev, struct timespec *now)
-{
-       struct timespec lap;
-
-       lap.tv_sec = now->tv_sec - prev->tv_sec;
-       lap.tv_nsec = now->tv_nsec - prev->tv_nsec;
-
-
-       _D("----------------------------------------------");
-       _print_time("PREV:\t", prev);
-       _print_time("NOW:\t", now);
-
-       if (lap.tv_sec < MIN_LAP_TIME) {
-               lap.tv_sec = 0;
-               lap.tv_nsec = 0;
-               _D ("TOO SHORT LAP");
-               return lap;
-       }
-
-
-       _print_time("LAP:\t", &lap);
-
-       if (lap.tv_nsec < 0) {
-               lap.tv_sec--;
-               lap.tv_nsec = 1e9 + lap.tv_nsec;
-
-               _print_time("LAP_MOD:\t", &lap);
-       }
-       _D("----------------------------------------------");
-
-       return lap;
-}
-
-static inline void _get_lap_time()
-{
-       struct timespec timestamp;
-       int ret = clock_gettime(CLOCK_MONOTONIC, &timestamp);
-       retv_error_message(ret != 0, ret);
-
-       if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) {
-               _calculate_lap_time(&s_info.last_timestamp, &timestamp);
-       } else {
-               _D("Starting first lap");
-       }
-
-       s_info.last_timestamp.tv_sec = timestamp.tv_sec;
-       s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
-}
-
 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
 {
        uint32_t gpio_value = 0;
@@ -110,7 +49,7 @@ static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e erro
        _D("GPIO_%d value: %d initial: %d, prev: %d", GPIO_PIN, gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
 
        if (gpio_value != s_info.gpio_initial_value && s_info.gpio_prev_value == s_info.gpio_initial_value) {
-               _get_lap_time();
+               lap_counter_get_lap_time();
        }
 
 
@@ -145,4 +84,3 @@ void resource_lap_counter_destroy(void)
        peripheral_gpio_unset_interrupted_cb(s_info.gpio);
        peripheral_gpio_close(s_info.gpio);
 }
-