Function naming refactor
[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 "resource/resource_led.h"
25 #include "config.h"
26
27 #define DEFAULT_LAP_GPIO 26
28 #define CONFIG_KEY_RPI_PIN_LAP_COUNTER "lap.counter"
29 #define CONFIG_GRP_RPI "Rpi"
30
31 typedef struct _lap_counter_s {
32         peripheral_gpio_h gpio;
33         uint32_t gpio_initial_value;
34         uint32_t gpio_prev_value;
35 } lap_counter_s;
36
37 static lap_counter_s s_info = { 0, };
38
39 #define  CHECK_GPIO_ERROR(pin, ret) \
40         if (ret != PERIPHERAL_ERROR_NONE) { \
41                 peripheral_gpio_close(pin); \
42                 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
43                 return; \
44         }
45
46 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
47 {
48         uint32_t gpio_value = 0;
49         int ret = peripheral_gpio_read(s_info.gpio, &gpio_value);
50         CHECK_GPIO_ERROR(s_info.gpio, ret);
51
52         _D("value: %d initial: %d, prev: %d", gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
53
54         if (gpio_value != s_info.gpio_initial_value && s_info.gpio_prev_value == s_info.gpio_initial_value) {
55                 lap_counter_get_lap_time();
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_or_set_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         ret_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 }