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.
24 #include <peripheral_io.h>
29 #include "resource_internal.h"
31 void resource_close_ultrasonic_sensor_trig(int trig_pin_num)
33 if (!resource_get_info(trig_pin_num)->opened) return;
35 _I("Ultrasonic sensor's trig is finishing...");
37 peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
38 resource_get_info(trig_pin_num)->opened = 0;
41 void resource_close_ultrasonic_sensor_echo(int echo_pin_num)
43 if (!resource_get_info(echo_pin_num)->opened) return;
45 _I("Ultrasonic sensor's echo is finishing...");
47 peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
48 resource_get_info(echo_pin_num)->opened = 0;
51 static unsigned long long _get_timestamp(void)
54 clock_gettime(CLOCK_REALTIME, &t);
55 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
58 static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
62 static unsigned long long prev = 0;
63 unsigned long long now = _get_timestamp();
64 resource_read_s *resource_read_info = user_data;
66 ret_if(!resource_read_info);
67 ret_if(!resource_read_info->cb);
69 peripheral_gpio_read(gpio, &value);
71 if (prev > 0 && value == 0) {
73 dist = (dist * 34300) / 2000000;
74 _I("Measured Distance : %0.2fcm\n", dist);
76 resource_read_info->cb(dist, resource_read_info->data);
77 peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
78 free(resource_read_info);
84 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
87 resource_read_s *resource_read_info = NULL;
89 resource_read_info = calloc(1, sizeof(resource_read_s));
90 retv_if(!resource_read_info, -1);
91 resource_read_info->cb = cb;
92 resource_read_info->data = data;
93 resource_read_info->pin_num = echo_pin_num;
95 if (!resource_get_info(trig_pin_num)->opened) {
96 _I("Ultrasonic sensor's trig is initializing...");
98 ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
99 retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);
101 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
102 retv_if(ret != 0, -1);
104 resource_get_info(trig_pin_num)->opened = 1;
105 resource_get_info(trig_pin_num)->close = resource_close_ultrasonic_sensor_trig;
108 if (!resource_get_info(echo_pin_num)->opened) {
109 _I("Ultrasonic sensor's echo is initializing...");
111 ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
112 retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
114 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
115 retv_if(ret != 0, -1);
117 ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
118 retv_if(ret != 0, -1);
120 resource_get_info(echo_pin_num)->opened = 1;
121 resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
124 if (resource_get_info(echo_pin_num)->sensor_h) {
125 ret = peripheral_gpio_set_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
126 retv_if(ret != 0, -1);
129 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
130 retv_if(ret < 0, -1);
132 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
133 retv_if(ret < 0, -1);
135 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
136 retv_if(ret < 0, -1);