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>
23 #include "lap_counter/lap_counter.h"
24 #include "resource/resource_led.h"
27 #define DEFAULT_LAP_GPIO 26
28 #define CONFIG_KEY_RPI_PIN_LAP_COUNTER "lap.counter"
29 #define CONFIG_GRP_RPI "Rpi"
31 typedef struct _lap_counter_s {
32 peripheral_gpio_h gpio;
33 uint32_t gpio_initial_value;
34 uint32_t gpio_prev_value;
37 static lap_counter_s s_info = { 0, };
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__); \
46 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
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);
52 _D("value: %d initial: %d, prev: %d", gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
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();
58 s_info.gpio_prev_value = gpio_value;
61 void resource_lap_counter_init(void)
63 int ret = PERIPHERAL_ERROR_NONE;
64 int pin = DEFAULT_LAP_GPIO;
66 bool modified = config_get_int_or_set_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_PIN_LAP_COUNTER, DEFAULT_LAP_GPIO, &pin);
68 _D("Lap gpio: %d", pin);
74 ret = peripheral_gpio_open(pin, &s_info.gpio);
75 ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
77 ret = peripheral_gpio_set_direction(s_info.gpio, PERIPHERAL_GPIO_DIRECTION_IN);
78 CHECK_GPIO_ERROR(s_info.gpio, ret);
80 ret = peripheral_gpio_set_edge_mode(s_info.gpio, PERIPHERAL_GPIO_EDGE_BOTH);
81 CHECK_GPIO_ERROR(s_info.gpio, ret);
83 ret = peripheral_gpio_set_interrupted_cb(s_info.gpio, _gpio_interrupted_cb, NULL);
84 CHECK_GPIO_ERROR(s_info.gpio, ret);
86 ret = peripheral_gpio_read(s_info.gpio, &s_info.gpio_initial_value);
87 CHECK_GPIO_ERROR(s_info.gpio, ret);
89 _D("GPIO_%d initial value: %d", pin, s_info.gpio_initial_value);
90 s_info.gpio_prev_value = s_info.gpio_initial_value;
93 void resource_lap_counter_destroy(void)
95 peripheral_gpio_unset_interrupted_cb(s_info.gpio);
96 peripheral_gpio_close(s_info.gpio);