1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Flora License, Version 1.1 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lap_counter/lap_counter.h"
#include <log.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include "cloud/cloud_communication.h"
#include "resource/resource_led.h"
#define MIN_LAP_TIME 5
#define MAX_NAME_LENGTH 256
typedef struct lap_counter_data {
char *user_name;
struct timespec last_timestamp;
} lap_counter_data_t;
static lap_counter_data_t s_info = {0, };
void lap_counter_init()
{
char buf[MAX_NAME_LENGTH];
snprintf(buf, MAX_NAME_LENGTH, "Default: %s %s", __DATE__, __TIME__); //
s_info.user_name = strdup(buf);
}
void lap_counter_set_user_name(const char *user_name)
{
retm_if(!user_name, "New user name is NULL");
free((void*)s_info.user_name);
s_info.user_name = strdup(user_name);
_D("User name set to %s", s_info.user_name);
}
const char *lap_counter_get_user_name(void)
{
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("----------------------------------------------");
cloud_communication_post_lap(lap.tv_sec * 1e3 + lap.tv_nsec / 1e6, s_info.user_name);
return lap;
}
void lap_counter_get_lap_time()
{
struct timespec timestamp;
int ret = clock_gettime(CLOCK_MONOTONIC, ×tamp);
ret_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");
resource_bi_led_blink(LED_COLOR_RED, 1000);
}
s_info.last_timestamp.tv_sec = timestamp.tv_sec;
s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
}
void lap_counter_set_start_lap()
{
s_info.last_timestamp.tv_nsec = 0;
s_info.last_timestamp.tv_sec = 0;
}
void lap_counter_shutdown()
{
free(s_info.user_name);
}
|