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