a186c118255662225bea008e95bcf7ba044bfa83
[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 #include <gio/gio.h>
27
28 #include "log.h"
29 #include "resource_internal.h"
30
31 void resource_close_ultrasonic_sensor(int echo_pin_num, int trig_pin_num)
32 {
33         if (!resource_get_info(echo_pin_num)->opened) return;
34         if (!resource_get_info(trig_pin_num)->opened) return;
35
36         _I("Ultrasonic sensor is finishing...");
37
38         peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
39         peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
40
41         resource_get_info(echo_pin_num)->opened = 0;
42         resource_get_info(trig_pin_num)->opened = 0;
43 }
44
45 static void _resource_read_ultrasonic_sensor_cb(gpio_isr_cb_s *data, void *user_data)
46 {
47         float dist = 0;
48         static unsigned long long timestamp = 0;
49         resource_read_s *resource_read_info = user_data;
50
51         ret_if(!resource_read_info);
52         ret_if(!resource_read_info->cb);
53
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);
58
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);
62         }
63
64         timestamp = data->timestamp;
65 }
66
67 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
68 {
69         int ret = 0;
70         resource_read_s *resource_read_info = NULL;
71
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;
77
78         if (!resource_get_info(trig_pin_num)->opened) {
79                 _I("Ultrasonic sensor's trig is initializing...");
80
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);
83
84                 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT);
85                 retv_if(ret != 0, -1);
86
87                 resource_get_info(trig_pin_num)->opened = 1;
88         }
89
90         if (!resource_get_info(echo_pin_num)->opened) {
91                 _I("Ultrasonic sensor's echo is initializing...");
92
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);
95
96                 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
97                 retv_if(ret != 0, -1);
98
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);
101
102                 resource_get_info(echo_pin_num)->opened = 1;
103         }
104
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);
108         }
109
110         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
111         retv_if(ret < 0, -1);
112
113         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
114         retv_if(ret < 0, -1);
115
116         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
117         retv_if(ret < 0, -1);
118
119         return 0;
120 }