fd54e22e9971cce8ce1c1c5bd0cb9ffaac167559
[apps/native/gear-racing-car.git] / src / resource / resource_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 <stdio.h>
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <peripheral_io.h>
21 #include <time.h>
22 #include "log.h"
23 #include "lap_counter/lap_counter.h"
24
25 #define GPIO_PIN 26
26
27
28 typedef struct _lap_counter_s {
29         peripheral_gpio_h gpio;
30         uint32_t gpio_initial_value;
31         uint32_t gpio_prev_value;
32 } lap_counter_s;
33
34 static lap_counter_s s_info = { 0, };
35
36 #define  CHECK_GPIO_ERROR(pin, ret) \
37         if (ret != PERIPHERAL_ERROR_NONE) { \
38                 peripheral_gpio_close(pin); \
39                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
40                 return; \
41         }
42
43 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
44 {
45         uint32_t gpio_value = 0;
46         int ret = peripheral_gpio_read(s_info.gpio, &gpio_value);
47         CHECK_GPIO_ERROR(s_info.gpio, ret);
48
49         _D("GPIO_%d value: %d initial: %d, prev: %d", GPIO_PIN, gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
50
51         if (gpio_value != s_info.gpio_initial_value && s_info.gpio_prev_value == s_info.gpio_initial_value) {
52                 lap_counter_get_lap_time();
53         }
54
55
56         s_info.gpio_prev_value = gpio_value;
57 }
58
59 void resource_lap_counter_init(void)
60 {
61         int ret = PERIPHERAL_ERROR_NONE;
62
63         ret = peripheral_gpio_open(GPIO_PIN, &s_info.gpio);
64         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
65
66         ret = peripheral_gpio_set_direction(s_info.gpio, PERIPHERAL_GPIO_DIRECTION_IN);
67         CHECK_GPIO_ERROR(s_info.gpio, ret);
68
69         ret = peripheral_gpio_set_edge_mode(s_info.gpio, PERIPHERAL_GPIO_EDGE_BOTH);
70         CHECK_GPIO_ERROR(s_info.gpio, ret);
71
72         ret = peripheral_gpio_set_interrupted_cb(s_info.gpio, _gpio_interrupted_cb, NULL);
73         CHECK_GPIO_ERROR(s_info.gpio, ret);
74
75         ret = peripheral_gpio_read(s_info.gpio, &s_info.gpio_initial_value);
76         CHECK_GPIO_ERROR(s_info.gpio, ret);
77
78         _D("GPIO_%d initial value: %d", GPIO_PIN, s_info.gpio_initial_value);
79         s_info.gpio_prev_value = s_info.gpio_initial_value;
80 }
81
82 void resource_lap_counter_destroy(void)
83 {
84         peripheral_gpio_unset_interrupted_cb(s_info.gpio);
85         peripheral_gpio_close(s_info.gpio);
86 }