summaryrefslogtreecommitdiff
path: root/src/resource/resource_ultrasonic_sensor.c
blob: d74950368adcefc762a0d2253b30b073c483426d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Contact: Jin Yoon <jinny.yoon@samsung.com>
 *          Geunsun Lee <gs86.lee@samsung.com>
 *          Eunyoung Lee <ey928.lee@samsung.com>
 *          Junkyu Han <junkyu.han@samsung.com>
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <unistd.h>
#include <peripheral_io.h>
#include <sys/time.h>
#include <gio/gio.h>

#include "log.h"
#include "resource_internal.h"

void resource_close_ultrasonic_sensor_trig(int trig_pin_num)
{
	if (!resource_get_info(trig_pin_num)->opened) return;

	_I("Ultrasonic sensor's trig is finishing...");

	peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h);
	resource_get_info(trig_pin_num)->opened = 0;
}

void resource_close_ultrasonic_sensor_echo(int echo_pin_num)
{
	if (!resource_get_info(echo_pin_num)->opened) return;

	_I("Ultrasonic sensor's echo is finishing...");

	peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
	resource_get_info(echo_pin_num)->opened = 0;
}

static void _resource_read_ultrasonic_sensor_cb(gpio_isr_cb_s *data, void *user_data)
{
	float dist = 0;
	static unsigned long long timestamp = 0;
	resource_read_s *resource_read_info = user_data;

	ret_if(!resource_read_info);
	ret_if(!resource_read_info->cb);

	if (timestamp > 0 && data->value == 0) {
		dist = data->timestamp - timestamp;
		dist = (dist * 34300) / 2000000;
		_I("Measured Distance : %0.2fcm\n", dist);

		resource_read_info->cb(dist, resource_read_info->data);
		peripheral_gpio_unregister_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
		free(resource_read_info);
	}

	timestamp = data->timestamp;
}

int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
{
	int ret = 0;
	resource_read_s *resource_read_info = NULL;

	resource_read_info = calloc(1, sizeof(resource_read_s));
	retv_if(!resource_read_info, -1);
	resource_read_info->cb = cb;
	resource_read_info->data = data;
	resource_read_info->pin_num = echo_pin_num;

	if (!resource_get_info(trig_pin_num)->opened) {
		_I("Ultrasonic sensor's trig is initializing...");

		ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
		retv_if(!resource_get_info(trig_pin_num)->sensor_h, -1);

		ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT);
		retv_if(ret != 0, -1);

		resource_get_info(trig_pin_num)->opened = 1;
		resource_get_info(trig_pin_num)->close = resource_close_ultrasonic_sensor_trig;
	}

	if (!resource_get_info(echo_pin_num)->opened) {
		_I("Ultrasonic sensor's echo is initializing...");

		ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
		retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);

		ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
		retv_if(ret != 0, -1);

		ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH);
		retv_if(ret != 0, -1);

		resource_get_info(echo_pin_num)->opened = 1;
		resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
	}

	if (resource_get_info(echo_pin_num)->sensor_h) {
		ret = peripheral_gpio_register_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
		retv_if(ret != 0, -1);
	}

	ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
	retv_if(ret < 0, -1);

	ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
	retv_if(ret < 0, -1);

	ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
	retv_if(ret < 0, -1);

	return 0;
}