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>
28 #include "resource_internal.h"
30 void resource_close_ultrasonic_sensor(int echo_pin_num, int trig_pin_num)
32 ret_if(!resource_get_info(echo_pin_num)->opened);
33 ret_if(!resource_get_info(trig_pin_num)->opened);
35 _I("Ultrasonic sensor is finishing...");
37 peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
38 peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
40 resource_get_info(echo_pin_num)->opened = 0;
41 resource_get_info(trig_pin_num)->opened = 0;
44 static int _get_echo_value(int echo_pin_num)
49 ret = peripheral_gpio_read(resource_get_info(echo_pin_num)->sensor_h, &value);
55 int resource_read_ultrasonic_sensor(int echo_pin_num, int trig_pin_num, double *out_value)
58 double duration = 0.0;
59 struct timeval start_time, end_time, temp_start_time, temp_end_time;
61 if (!resource_get_info(echo_pin_num)->opened) {
62 _I("Ultrasonic sensor is initializing...");
64 ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
65 retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
67 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
68 retv_if(ret != 0, -1);
70 resource_get_info(echo_pin_num)->opened = 1;
73 if (!resource_get_info(trig_pin_num)->opened) {
74 _I("Ultrasonic sensor is initializing...");
76 ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
77 retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);
79 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
80 retv_if(ret != 0, -1);
82 resource_get_info(trig_pin_num)->opened = 1;
85 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
90 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
95 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
98 _D("Count the distance");
99 gettimeofday(&temp_start_time, NULL);
101 while (_get_echo_value(echo_pin_num) == 0) {
102 gettimeofday(&temp_end_time, NULL);
103 duration = (double)temp_end_time.tv_sec + (double)(temp_end_time.tv_usec / 1000000.0)
104 - (double)temp_start_time.tv_sec - (double)(temp_start_time.tv_usec / 1000000.0);
105 if (duration > 1.0f) {
106 _E("Cannot get the echo value.");
110 gettimeofday(&start_time, NULL);
112 _D("After checking #1");
114 while (_get_echo_value(echo_pin_num) == 1);
115 gettimeofday(&end_time, NULL);
117 _D("After checking #2");
119 duration = (double)end_time.tv_sec + (double)(end_time.tv_usec / 1000000.0)
120 - (double)start_time.tv_sec - (double)(start_time.tv_usec / 1000000.0);
121 *out_value = duration / 2 * 340.0;
123 _I("Ultrasonic Sensor Value : %f", *out_value);