${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)
#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_
#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
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;
}
*/
#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, ×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);
}
#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;
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;
_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();
}
peripheral_gpio_unset_interrupted_cb(s_info.gpio);
peripheral_gpio_close(s_info.gpio);
}
-