Config for different led colors displaying the car states
[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 #define CONFIG_LED_STATE_KEY_LAP "lap"
31 #define CONFIG_DEFAULT_LED_3BIT_LAP 0, 1, 0
32 #define CONFIG_DEFAULT_LED_24BIT_LAP 0, 255, 0
33
34 #define CONFIG_LED_STATE_KEY_FIRST_LAP "first.lap"
35 #define CONFIG_DEFAULT_LED_3BIT_FIRST_LAP 0, 0, 1
36 #define CONFIG_DEFAULT_LED_24BIT_FIRST_LAP 0, 0, 255
37
38 typedef struct lap_counter_data {
39         char *user_name;
40         struct timespec last_timestamp;
41 } lap_counter_data_t;
42
43 static lap_counter_data_t s_info = {0, };
44
45 void lap_counter_init()
46 {
47         char buf[MAX_NAME_LENGTH];
48         snprintf(buf, MAX_NAME_LENGTH, "Default: %s %s", __DATE__, __TIME__); //
49
50         s_info.user_name = strdup(buf);
51 }
52
53 void lap_counter_set_user_name(const char *user_name)
54 {
55         retm_if(!user_name, "New user name is NULL");
56
57         free((void*)s_info.user_name);
58         s_info.user_name = strdup(user_name);
59
60         _D("User name set to %s", s_info.user_name);
61 }
62
63 const char *lap_counter_get_user_name(void)
64 {
65         return s_info.user_name;
66 }
67
68 void _print_time(const char *title, struct timespec *ts)
69 {
70         char buf[PATH_MAX];
71         struct tm *_tm = localtime(&(ts->tv_sec));
72         strftime(&buf[0], PATH_MAX, "%H:%M:%S", _tm);
73
74         snprintf(&buf[strlen(buf)], PATH_MAX, ".%03ld", ts->tv_nsec / (long)1e6);
75
76         _D ("%s %s", title, buf);
77 }
78
79 static inline struct timespec _calculate_lap_time(struct timespec *prev, struct timespec *now)
80 {
81         struct timespec lap;
82
83         lap.tv_sec = now->tv_sec - prev->tv_sec;
84         lap.tv_nsec = now->tv_nsec - prev->tv_nsec;
85
86
87         _D("----------------------------------------------");
88         _print_time("PREV:\t", prev);
89         _print_time("NOW:\t", now);
90
91         if (lap.tv_sec < MIN_LAP_TIME) {
92                 lap.tv_sec = 0;
93                 lap.tv_nsec = 0;
94                 _D ("TOO SHORT LAP");
95                 return lap;
96         }
97
98
99         _print_time("LAP:\t", &lap);
100
101         if (lap.tv_nsec < 0) {
102                 lap.tv_sec--;
103                 lap.tv_nsec = 1e9 + lap.tv_nsec;
104
105                 _print_time("LAP_MOD:\t", &lap);
106         }
107         _D("----------------------------------------------");
108
109         cloud_communication_post_lap(lap.tv_sec * 1e3 + lap.tv_nsec / 1e6, s_info.user_name);
110         resource_led_blink_rgb_colors(CONFIG_LED_STATE_KEY_LAP,
111                         CONFIG_DEFAULT_LED_3BIT_LAP,
112                         CONFIG_DEFAULT_LED_24BIT_LAP,
113                         LED_COLOR_NONE,
114                         1000);
115
116         return lap;
117 }
118
119 void lap_counter_get_lap_time()
120 {
121         struct timespec timestamp;
122         int ret = clock_gettime(CLOCK_MONOTONIC, &timestamp);
123         ret_error_message(ret != 0, ret);
124
125         if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) {
126                 _calculate_lap_time(&s_info.last_timestamp, &timestamp);
127         } else {
128                 _D("Initial lap");
129
130                 resource_led_blink_rgb_colors(CONFIG_LED_STATE_KEY_FIRST_LAP,
131                                 CONFIG_DEFAULT_LED_3BIT_FIRST_LAP,
132                                 CONFIG_DEFAULT_LED_24BIT_FIRST_LAP,
133                                 LED_COLOR_NONE,
134                                 1000);
135         }
136
137         s_info.last_timestamp.tv_sec = timestamp.tv_sec;
138         s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
139 }
140
141 void lap_counter_set_start_lap()
142 {
143         s_info.last_timestamp.tv_nsec = 0;
144         s_info.last_timestamp.tv_sec = 0;
145 }
146
147 void lap_counter_shutdown()
148 {
149         free(s_info.user_name);
150 }