2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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 Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <peripheral_io.h>
29 #include "resource_internal.h"
31 void resource_close_ultrasonic_sensor(int echo_pin_num, int trig_pin_num)
33 if (!resource_get_info(echo_pin_num)->opened) return;
34 if (!resource_get_info(trig_pin_num)->opened) return;
36 _I("Ultrasonic sensor is finishing...");
38 peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
39 peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
41 resource_get_info(echo_pin_num)->opened = 0;
42 resource_get_info(trig_pin_num)->opened = 0;
45 static void _resource_read_ultrasonic_sensor_cb(gpio_isr_cb_s *data, void *user_data)
48 static unsigned long long timestamp = 0;
49 resource_read_s *resource_read_info = user_data;
51 ret_if(!resource_read_info);
52 ret_if(!resource_read_info->cb);
54 if (timestamp > 0 && data->value == 0) {
55 dist = data->timestamp - timestamp;
56 dist = (dist * 34300) / 2000000;
57 _I("Measured Distance : %0.2fcm\n", dist);
59 resource_read_info->cb(dist, resource_read_info->data);
60 peripheral_gpio_unregister_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
61 free(resource_read_info);
64 timestamp = data->timestamp;
67 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
70 resource_read_s *resource_read_info = NULL;
72 resource_read_info = calloc(1, sizeof(resource_read_s));
73 retv_if(!resource_read_info, -1);
74 resource_read_info->cb = cb;
75 resource_read_info->data = data;
76 resource_read_info->pin_num = echo_pin_num;
78 if (!resource_get_info(trig_pin_num)->opened) {
79 _I("Ultrasonic sensor's trig is initializing...");
81 ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
82 retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);
84 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT);
85 retv_if(ret != 0, -1);
87 resource_get_info(trig_pin_num)->opened = 1;
90 if (!resource_get_info(echo_pin_num)->opened) {
91 _I("Ultrasonic sensor's echo is initializing...");
93 ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
94 retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
96 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
97 retv_if(ret != 0, -1);
99 ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
100 retv_if(ret != 0, -1);
102 resource_get_info(echo_pin_num)->opened = 1;
105 if (resource_get_info(echo_pin_num)->sensor_h) {
106 ret = peripheral_gpio_register_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
107 retv_if(ret != 0, -1);
110 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
111 retv_if(ret < 0, -1);
113 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
114 retv_if(ret < 0, -1);
116 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
117 retv_if(ret < 0, -1);