2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
20 #include <peripheral_io.h>
26 #define MIN_LAP_TIME 5
28 typedef struct _lap_counter_s {
29 peripheral_gpio_h gpio;
30 struct timespec last_timestamp;
31 uint32_t gpio_initial_value;
32 uint32_t gpio_prev_value;
35 static lap_counter_s s_info = { 0, };
37 #define CHECK_GPIO_ERROR(pin, ret) \
38 if (ret != PERIPHERAL_ERROR_NONE) { \
39 peripheral_gpio_close(pin); \
40 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
44 void _print_time(const char *title, struct timespec *ts)
47 struct tm *_tm = localtime(&(ts->tv_sec));
48 strftime(&buf[0], PATH_MAX, "%H:%M:%S", _tm);
50 snprintf(&buf[strlen(buf)], PATH_MAX, ".%03ld", ts->tv_nsec / (long)1e6);
52 _D ("%s %s", title, buf);
55 static struct timespec _calculate_lap_time(struct timespec *prev, struct timespec *now)
59 lap.tv_sec = now->tv_sec - prev->tv_sec;
60 lap.tv_nsec = now->tv_nsec - prev->tv_nsec;
63 _D("----------------------------------------------");
64 _print_time("PREV:\t", prev);
65 _print_time("NOW:\t", now);
67 if (lap.tv_sec < MIN_LAP_TIME) {
75 _print_time("LAP:\t", &lap);
77 if (lap.tv_nsec < 0) {
79 lap.tv_nsec = 1e9 + lap.tv_nsec;
81 _print_time("LAP_MOD:\t", &lap);
83 _D("----------------------------------------------");
88 static inline void _get_lap_time()
90 struct timespec timestamp;
91 int ret = clock_gettime(CLOCK_MONOTONIC, ×tamp);
92 retv_error_message(ret != 0, ret);
94 if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) {
95 _calculate_lap_time(&s_info.last_timestamp, ×tamp);
97 _D("Starting first lap");
100 s_info.last_timestamp.tv_sec = timestamp.tv_sec;
101 s_info.last_timestamp.tv_nsec = timestamp.tv_nsec;
104 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
106 uint32_t gpio_value = 0;
107 int ret = peripheral_gpio_read(s_info.gpio, &gpio_value);
108 CHECK_GPIO_ERROR(s_info.gpio, ret);
110 _D("GPIO_%d value: %d initial: %d, prev: %d", GPIO_PIN, gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
112 if (gpio_value != s_info.gpio_initial_value && s_info.gpio_prev_value == s_info.gpio_initial_value) {
117 s_info.gpio_prev_value = gpio_value;
120 void resource_lap_counter_init(void)
122 int ret = PERIPHERAL_ERROR_NONE;
124 ret = peripheral_gpio_open(GPIO_PIN, &s_info.gpio);
125 retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
127 ret = peripheral_gpio_set_direction(s_info.gpio, PERIPHERAL_GPIO_DIRECTION_IN);
128 CHECK_GPIO_ERROR(s_info.gpio, ret);
130 ret = peripheral_gpio_set_edge_mode(s_info.gpio, PERIPHERAL_GPIO_EDGE_BOTH);
131 CHECK_GPIO_ERROR(s_info.gpio, ret);
133 ret = peripheral_gpio_set_interrupted_cb(s_info.gpio, _gpio_interrupted_cb, NULL);
134 CHECK_GPIO_ERROR(s_info.gpio, ret);
136 ret = peripheral_gpio_read(s_info.gpio, &s_info.gpio_initial_value);
137 CHECK_GPIO_ERROR(s_info.gpio, ret);
139 _D("GPIO_%d initial value: %d", GPIO_PIN, s_info.gpio_initial_value);
140 s_info.gpio_prev_value = s_info.gpio_initial_value;
143 void resource_lap_counter_destroy(void)
145 peripheral_gpio_unset_interrupted_cb(s_info.gpio);
146 peripheral_gpio_close(s_info.gpio);