Change peripheral io APIs
[apps/native/position-finder-server.git] / src / resource / resource_ultrasonic_sensor.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 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
12  *
13  * http://floralicense.org/license/
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_trig(int trig_pin_num)
32 {
33         if (!resource_get_info(trig_pin_num)->opened) return;
34
35         _I("Ultrasonic sensor's trig is finishing...");
36
37         peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
38         resource_get_info(trig_pin_num)->opened = 0;
39 }
40
41 void resource_close_ultrasonic_sensor_echo(int echo_pin_num)
42 {
43         if (!resource_get_info(echo_pin_num)->opened) return;
44
45         _I("Ultrasonic sensor's echo is finishing...");
46
47         peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
48         resource_get_info(echo_pin_num)->opened = 0;
49 }
50
51 static unsigned long long _get_timestamp(void)
52 {
53         struct timespec t;
54         clock_gettime(CLOCK_REALTIME, &t);
55         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
56 }
57
58 static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
59 {
60         float dist = 0;
61         uint32_t value;
62         static unsigned long long prev = 0;
63         unsigned long long now = _get_timestamp();
64         resource_read_s *resource_read_info = user_data;
65
66         ret_if(!resource_read_info);
67         ret_if(!resource_read_info->cb);
68
69         peripheral_gpio_read(gpio, &value);
70
71         if (prev > 0 && value == 0) {
72                 dist = now - prev;
73                 dist = (dist * 34300) / 2000000;
74                 _I("Measured Distance : %0.2fcm\n", dist);
75
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);
79         }
80
81         prev = now;
82 }
83
84 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
85 {
86         int ret = 0;
87         resource_read_s *resource_read_info = NULL;
88
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;
94
95         if (!resource_get_info(trig_pin_num)->opened) {
96                 _I("Ultrasonic sensor's trig is initializing...");
97
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);
100
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);
103
104                 resource_get_info(trig_pin_num)->opened = 1;
105                 resource_get_info(trig_pin_num)->close = resource_close_ultrasonic_sensor_trig;
106         }
107
108         if (!resource_get_info(echo_pin_num)->opened) {
109                 _I("Ultrasonic sensor's echo is initializing...");
110
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);
113
114                 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
115                 retv_if(ret != 0, -1);
116
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);
119
120                 resource_get_info(echo_pin_num)->opened = 1;
121                 resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
122         }
123
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);
127         }
128
129         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
130         retv_if(ret < 0, -1);
131
132         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
133         retv_if(ret < 0, -1);
134
135         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
136         retv_if(ret < 0, -1);
137
138         return 0;
139 }