c39792079389499c3dce3845b58478ba756352e9
[apps/native/position-finder-server.git] / src / resource / resource_ultrasonic_sensor.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <peripheral_io.h>
25 #include <sys/time.h>
26
27 #include "log.h"
28 #include "resource_internal.h"
29
30 void resource_close_ultrasonic_sensor(int echo_pin_num, int trig_pin_num)
31 {
32         ret_if(!resource_get_info(echo_pin_num)->opened);
33         ret_if(!resource_get_info(trig_pin_num)->opened);
34
35         _I("Ultrasonic sensor is finishing...");
36
37         peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
38         peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
39
40         resource_get_info(echo_pin_num)->opened = 0;
41         resource_get_info(trig_pin_num)->opened = 0;
42 }
43
44 static int _get_echo_value(int echo_pin_num)
45 {
46         int ret = 0;
47         int value = 0;
48
49         ret = peripheral_gpio_read(resource_get_info(echo_pin_num)->sensor_h, &value);
50         retv_if(ret < 0, -1);
51
52         return value;
53 }
54
55 int resource_read_ultrasonic_sensor(int echo_pin_num, int trig_pin_num, double *out_value)
56 {
57         int ret = 0;
58         double duration = 0.0;
59         struct timeval start_time, end_time, temp_start_time, temp_end_time;
60
61         if (!resource_get_info(echo_pin_num)->opened) {
62                 _I("Ultrasonic sensor is initializing...");
63
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);
66
67                 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
68                 retv_if(ret != 0, -1);
69
70                 resource_get_info(echo_pin_num)->opened = 1;
71         }
72
73         if (!resource_get_info(trig_pin_num)->opened) {
74                 _I("Ultrasonic sensor is initializing...");
75
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);
78
79                 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
80                 retv_if(ret != 0, -1);
81
82                 resource_get_info(trig_pin_num)->opened = 1;
83         }
84
85         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
86         retv_if(ret < 0, -1);
87
88         sleep(1);
89
90         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
91         retv_if(ret < 0, -1);
92
93         usleep(10);
94
95         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
96         retv_if(ret < 0, -1);
97
98         _D("Count the distance");
99         gettimeofday(&temp_start_time, NULL);
100
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.");
107                         return -1;
108                 }
109         }
110         gettimeofday(&start_time, NULL);
111
112         _D("After checking #1");
113
114         while (_get_echo_value(echo_pin_num) == 1);
115         gettimeofday(&end_time, NULL);
116
117         _D("After checking #2");
118
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;
122
123         _I("Ultrasonic Sensor Value : %f", *out_value);
124
125         return 0;
126 }