e6d5029528badeaa70f55ddc7eb2ef58dd859b5f
[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 #include "config.h"
25
26 #define DEFAULT_LAP_GPIO 26
27 #define CONFIG_KEY_RPI_PIN_LAP_COUNTER "lap.counter"
28 #define CONFIG_GRP_RPI "Rpi"
29
30 typedef struct _lap_counter_s {
31         peripheral_gpio_h gpio;
32         uint32_t gpio_initial_value;
33         uint32_t gpio_prev_value;
34 } lap_counter_s;
35
36 static lap_counter_s s_info = { 0, };
37
38 #define  CHECK_GPIO_ERROR(pin, ret) \
39         if (ret != PERIPHERAL_ERROR_NONE) { \
40                 peripheral_gpio_close(pin); \
41                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
42                 return; \
43         }
44
45 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
46 {
47         uint32_t gpio_value = 0;
48         int ret = peripheral_gpio_read(s_info.gpio, &gpio_value);
49         CHECK_GPIO_ERROR(s_info.gpio, ret);
50
51         _D("value: %d initial: %d, prev: %d", gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
52
53         if (gpio_value != s_info.gpio_initial_value && s_info.gpio_prev_value == s_info.gpio_initial_value) {
54                 lap_counter_get_lap_time();
55         }
56
57
58         s_info.gpio_prev_value = gpio_value;
59 }
60
61 void resource_lap_counter_init(void)
62 {
63         int ret = PERIPHERAL_ERROR_NONE;
64         int pin = DEFAULT_LAP_GPIO;
65
66         bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_PIN_LAP_COUNTER, DEFAULT_LAP_GPIO, &pin);
67
68         _D("Lap gpio: %d", pin);
69
70         if (modified) {
71                 config_save();
72         }
73
74         ret = peripheral_gpio_open(pin, &s_info.gpio);
75         retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
76
77         ret = peripheral_gpio_set_direction(s_info.gpio, PERIPHERAL_GPIO_DIRECTION_IN);
78         CHECK_GPIO_ERROR(s_info.gpio, ret);
79
80         ret = peripheral_gpio_set_edge_mode(s_info.gpio, PERIPHERAL_GPIO_EDGE_BOTH);
81         CHECK_GPIO_ERROR(s_info.gpio, ret);
82
83         ret = peripheral_gpio_set_interrupted_cb(s_info.gpio, _gpio_interrupted_cb, NULL);
84         CHECK_GPIO_ERROR(s_info.gpio, ret);
85
86         ret = peripheral_gpio_read(s_info.gpio, &s_info.gpio_initial_value);
87         CHECK_GPIO_ERROR(s_info.gpio, ret);
88
89         _D("GPIO_%d initial value: %d", pin, s_info.gpio_initial_value);
90         s_info.gpio_prev_value = s_info.gpio_initial_value;
91 }
92
93 void resource_lap_counter_destroy(void)
94 {
95         peripheral_gpio_unset_interrupted_cb(s_info.gpio);
96         peripheral_gpio_close(s_info.gpio);
97 }