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(int trig_pin_num, int echo_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(trig_pin_num)->sensor_h);
39         peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
40
41         resource_get_info(trig_pin_num)->opened = 0;
42         resource_get_info(echo_pin_num)->opened = 0;
43 }
44
45 static unsigned long long _get_timestamp(void)
46 {
47         struct timespec t;
48         clock_gettime(CLOCK_REALTIME, &t);
49         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
50 }
51
52 static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
53 {
54         float dist = 0;
55         uint32_t value;
56         static unsigned long long prev = 0;
57         unsigned long long now = _get_timestamp();
58         resource_read_s *resource_read_info = user_data;
59
60         ret_if(!resource_read_info);
61         ret_if(!resource_read_info->cb);
62
63         peripheral_gpio_read(gpio, &value);
64
65         if (prev > 0 && value == 0) {
66                 dist = now - prev;
67                 dist = (dist * 34300) / 2000000;
68                 _I("Measured Distance : %0.2fcm\n", dist);
69
70                 resource_read_info->cb(dist, resource_read_info->data);
71                 peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
72                 free(resource_read_info);
73         }
74
75         prev = now;
76 }
77
78 int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
79 {
80         int ret = 0;
81         resource_read_s *resource_read_info = NULL;
82
83         resource_read_info = calloc(1, sizeof(resource_read_s));
84         retv_if(!resource_read_info, -1);
85         resource_read_info->cb = cb;
86         resource_read_info->data = data;
87         resource_read_info->pin_num = echo_pin_num;
88
89         if (!resource_get_info(trig_pin_num)->opened) {
90                 _I("Ultrasonic sensor's trig is initializing...");
91
92                 ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
93                 retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);
94
95                 ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
96                 retv_if(ret != 0, -1);
97
98                 resource_get_info(trig_pin_num)->opened = 1;
99         }
100
101         if (!resource_get_info(echo_pin_num)->opened) {
102                 _I("Ultrasonic sensor's echo is initializing...");
103
104                 ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
105                 retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
106
107                 ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
108                 retv_if(ret != 0, -1);
109
110                 ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
111                 retv_if(ret != 0, -1);
112
113                 resource_get_info(echo_pin_num)->opened = 1;
114         }
115
116         if (resource_get_info(echo_pin_num)->sensor_h) {
117                 ret = peripheral_gpio_set_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
118                 retv_if(ret != 0, -1);
119         }
120
121         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
122         retv_if(ret < 0, -1);
123
124         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
125         retv_if(ret < 0, -1);
126
127         ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
128         retv_if(ret < 0, -1);
129
130         return 0;
131 }