*/
#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);
}