2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Jin Yoon <jinny.yoon@samsung.com>
5 * Geunsun Lee <gs86.lee@samsung.com>
6 * Eunyoung Lee <ey928.lee@samsung.com>
7 * Junkyu Han <junkyu.han@samsung.com>
9 * Licensed under the Flora License, Version 1.1 (the License);
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://floralicense.org/license/
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an AS IS BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 #include <peripheral_io.h>
25 #include "resource_internal.h"
27 void resource_close_infrared_obstacle_avoidance_sensor(int pin_num)
29 if (!resource_get_info(pin_num)->opened) return;
31 _I("Infrared Obstacle Avoidance Sensor is finishing...");
32 if (resource_get_info(pin_num)->resource_changed_info) {
33 free(resource_get_info(pin_num)->resource_changed_info);
34 resource_get_info(pin_num)->resource_changed_info = NULL;
36 peripheral_gpio_unset_interrupted_cb(resource_get_info(pin_num)->sensor_h);
37 peripheral_gpio_close(resource_get_info(pin_num)->sensor_h);
38 resource_get_info(pin_num)->sensor_h = NULL;
39 resource_get_info(pin_num)->opened = 0;
44 static int _init_pin(int pin_num)
46 int ret = PERIPHERAL_ERROR_NONE;
48 ret = peripheral_gpio_open(pin_num, &resource_get_info(pin_num)->sensor_h);
49 retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
51 ret = peripheral_gpio_set_direction(resource_get_info(pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
52 if (ret != PERIPHERAL_ERROR_NONE) {
53 peripheral_gpio_close(resource_get_info(pin_num)->sensor_h);
54 resource_get_info(pin_num)->sensor_h = NULL;
58 resource_get_info(pin_num)->opened = 1;
59 resource_get_info(pin_num)->close = resource_close_infrared_obstacle_avoidance_sensor;
64 int resource_read_infrared_obstacle_avoidance_sensor(int pin_num, unsigned int *out_value)
66 int ret = PERIPHERAL_ERROR_NONE;
68 if (!resource_get_info(pin_num)->opened) {
69 ret = _init_pin(pin_num);
73 ret = peripheral_gpio_read(resource_get_info(pin_num)->sensor_h, out_value);
74 retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
76 *out_value = !*out_value;
78 _I("Infrared Obstacle Avoidance Sensor Value : %d", *out_value);
84 static void __ioa_sensor_interrupt_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
87 resource_changed_s *resource_changed_info = (resource_changed_s *)user_data;
89 /* Detected : 0, Non-detected : 1 */
90 peripheral_gpio_read(gpio, &value);
91 _D("interrupt value = %d", !value);
93 resource_changed_info->cb(!value, resource_changed_info->data);
98 int resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(int pin_num, resource_changed_cb cb, void *data)
100 int ret = PERIPHERAL_ERROR_NONE;
102 if (resource_get_info(pin_num)->resource_changed_info == NULL) {
103 resource_get_info(pin_num)->resource_changed_info = calloc(1, sizeof(resource_changed_s));
104 retv_if(!resource_get_info(pin_num)->resource_changed_info, -1);
106 if (resource_get_info(pin_num)->sensor_h) {
107 peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_get_info(pin_num)->resource_changed_info->pin_num)->sensor_h);
111 resource_get_info(pin_num)->resource_changed_info->cb = cb;
112 resource_get_info(pin_num)->resource_changed_info->data = data;
113 resource_get_info(pin_num)->resource_changed_info->pin_num = pin_num;
115 if (!resource_get_info(pin_num)->opened) {
116 ret = _init_pin(pin_num);
117 goto_if(ret < 0, FREE_RESOURCE);
120 if (resource_get_info(pin_num)->sensor_h) {
121 ret = peripheral_gpio_set_edge_mode(resource_get_info(pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
122 goto_if(ret != PERIPHERAL_ERROR_NONE, FREE_RESOURCE);
124 ret = peripheral_gpio_set_interrupted_cb(resource_get_info(pin_num)->sensor_h, __ioa_sensor_interrupt_cb, resource_get_info(pin_num)->resource_changed_info);
125 goto_if(ret != PERIPHERAL_ERROR_NONE, FREE_RESOURCE);
131 if (resource_get_info(pin_num)->resource_changed_info) {
132 free(resource_get_info(pin_num)->resource_changed_info);
133 resource_get_info(pin_num)->resource_changed_info = NULL;
136 if (resource_get_info(pin_num)->sensor_h) {
137 peripheral_gpio_unset_interrupted_cb(resource_get_info(pin_num)->sensor_h);
138 peripheral_gpio_close(resource_get_info(pin_num)->sensor_h);
139 resource_get_info(pin_num)->sensor_h = NULL;
140 resource_get_info(pin_num)->opened = 0;