From 319900e99b0dab8bcefeddb500540c88800c4e3f Mon Sep 17 00:00:00 2001 From: Michal Skorupinski Date: Thu, 18 Oct 2018 17:01:25 +0200 Subject: [PATCH] Integration with the lap_counter module Change-Id: Ie1843ccd6fc72e1819dc9f17aca3f836b4d7293b Signed-off-by: Michal Skorupinski --- CMakeLists.txt | 1 + inc/lap_counter/lap_counter.h | 7 +-- src/app.c | 2 + src/lap_counter/lap_counter.c | 94 ++++++++++++++++++++++++++++++++----- src/resource/resource_lap_counter.c | 68 ++------------------------- 5 files changed, 93 insertions(+), 79 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4db9490..c5fcd12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/inc/lap_counter/lap_counter.h b/inc/lap_counter/lap_counter.h index 6a4f1a2..9904c93 100644 --- a/inc/lap_counter/lap_counter.h +++ b/inc/lap_counter/lap_counter.h @@ -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_ diff --git a/src/app.c b/src/app.c index 0092caf..b645682 100644 --- 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; } diff --git a/src/lap_counter/lap_counter.c b/src/lap_counter/lap_counter.c index 800e193..dfbd299 100644 --- a/src/lap_counter/lap_counter.c +++ b/src/lap_counter/lap_counter.c @@ -15,35 +15,107 @@ */ #include "lap_counter/lap_counter.h" -#include +#include #include -#include "log.h" +#include +#include +#include +#include + +#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, ×tamp); + 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, ×tamp); + } 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); } diff --git a/src/resource/resource_lap_counter.c b/src/resource/resource_lap_counter.c index bde429c..fd54e22 100644 --- a/src/resource/resource_lap_counter.c +++ b/src/resource/resource_lap_counter.c @@ -20,14 +20,13 @@ #include #include #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, ×tamp); - 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, ×tamp); - } 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); } - -- 2.7.4