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 static resource_read_s *resource_read_info = NULL;
32 static unsigned long long triggered_time = 0;
34 void resource_close_ultrasonic_sensor_trig(int trig_pin_num)
36 if (!resource_get_info(trig_pin_num)->opened) return;
38 _I("Ultrasonic sensor's trig is finishing...");
40 peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
41 resource_get_info(trig_pin_num)->opened = 0;
44 void resource_close_ultrasonic_sensor_echo(int echo_pin_num)
46 if (!resource_get_info(echo_pin_num)->opened) return;
48 _I("Ultrasonic sensor's echo is finishing...");
50 peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
51 resource_get_info(echo_pin_num)->opened = 0;
52 free(resource_read_info);
53 resource_read_info = NULL;
56 static unsigned long long _get_timestamp(void)
59 clock_gettime(CLOCK_REALTIME, &t);
60 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
63 static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
67 unsigned long long returned_time = 0;
68 resource_read_s *resource_read_info = user_data;
70 ret_if(!resource_read_info);
71 ret_if(!resource_read_info->cb);
73 peripheral_gpio_read(gpio, &value);
76 triggered_time = _get_timestamp();
77 } else if (value == 0) {
78 returned_time = _get_timestamp();
81 if (triggered_time > 0 && value == 0) {
82 dist = returned_time - triggered_time;
83 if (dist < 150 || dist > 25000) {
86 dist = (dist * 34300) / 2000000;
89 resource_read_info->cb(dist, resource_read_info->data);
93 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
99 if (resource_read_info == NULL) {
100 resource_read_info = calloc(1, sizeof(resource_read_s));
101 retv_if(!resource_read_info, -1);
103 peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
105 resource_read_info->cb = cb;
106 resource_read_info->data = data;
107 resource_read_info->pin_num = echo_pin_num;
109 if (!resource_get_info(trig_pin_num)->opened) {
110 _I("Ultrasonic sensor's trig is initializing...");
112 ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
113 retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);
115 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
116 retv_if(ret != 0, -1);
118 resource_get_info(trig_pin_num)->opened = 1;
119 resource_get_info(trig_pin_num)->close = resource_close_ultrasonic_sensor_trig;
122 if (!resource_get_info(echo_pin_num)->opened) {
123 _I("Ultrasonic sensor's echo is initializing...");
125 ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
126 retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
128 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
129 retv_if(ret != 0, -1);
131 ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
132 retv_if(ret != 0, -1);
134 resource_get_info(echo_pin_num)->opened = 1;
135 resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
138 if (resource_get_info(echo_pin_num)->sensor_h) {
139 ret = peripheral_gpio_set_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
140 retv_if(ret != 0, -1);
143 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
144 retv_if(ret < 0, -1);
148 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
149 retv_if(ret < 0, -1);
153 ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
154 retv_if(ret < 0, -1);