From: Han Minsu Date: Tue, 17 Sep 2019 08:35:28 +0000 (+0900) Subject: Resource : JSN-SR04T(Distance/GPIO) X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cb2d66301da68046b1f9a6ade972d39b104e3b72;p=apps%2Fnative%2Frcc.git Resource : JSN-SR04T(Distance/GPIO) Change-Id: I72f31c1ffaa393fbd16b75ab7849e2d7c3b8db2b --- diff --git a/inc/resource.h b/inc/resource.h index db2f1e9..765b7aa 100755 --- a/inc/resource.h +++ b/inc/resource.h @@ -39,5 +39,6 @@ #include "resource/resource_gas_detection_sensor.h" #include "resource/resource_sound_level_sensor.h" #include "resource/resource_mcu90615.h" +#include "resource/resource_ultrasonic_jsn_sr04t.h" #endif /* __RCC_RESOURCE_H__ */ diff --git a/inc/resource/resource_ultrasonic_jsn_sr04t.h b/inc/resource/resource_ultrasonic_jsn_sr04t.h new file mode 100644 index 0000000..5cd18c5 --- /dev/null +++ b/inc/resource/resource_ultrasonic_jsn_sr04t.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 SINO TECH Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef __RESOURCE_ULTRASONIC_JSN_SR04T_H__ +#define __RESOURCE_ULTRASONIC_JSN_SR04T_H__ + +#if defined (__cplusplus) +extern "C" { +#endif + +/** + * @brief Reads the value of gpio connected ultrasonic sensor(JSN_SR04T). + * @param[in] trig_pin_num The number of the gpio pin connected to the trig of the ultrasonic sensor + * @param[in] echo_pin_num The number of the gpio pin connected to the echo of the ultrasonic sensor + * @param[in] cb A callback function to be invoked when the gpio interrupt is triggered + * @param[in] data The data to be passed to the callback function + * @return 0 on success, otherwise a negative error value + * @see If the gpio pin is not open, creates gpio handle before reading the value of gpio. + */ +extern int resource_read_ultrasonic_jsn_sr04t(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data); + +#if defined (__cplusplus) +} +#endif + +#endif /* __RESOURCE_ULTRASONIC_JSN_SR04T_H__ */ diff --git a/inc/resource/resource_ultrasonic_jsn_sr04t_internal.h b/inc/resource/resource_ultrasonic_jsn_sr04t_internal.h new file mode 100644 index 0000000..f8ca47e --- /dev/null +++ b/inc/resource/resource_ultrasonic_jsn_sr04t_internal.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 SINO TECH Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef __RESOURCE_ULTRASONIC_JSN_SR04T_INTERNAL_H__ +#define __RESOURCE_ULTRASONIC_JSN_SR04T_INTERNAL_H__ + +/** + * @brief Releases the gpio handle and changes the gpio pin state to the close(0). + * @param[in] trig_pin_num The number of the gpio pin connected to the trig of the ultrasonic sensor + */ +extern void resource_close_ultrasonic_jsn_sr04t_trig(int trig_pin_num); + +/** + * @brief Releases the gpio handle and changes the gpio pin state to the close(0). + * @param[in] echo_pin_num The number of the gpio pin connected to the echo of the ultrasonic sensor + */ +extern void resource_close_ultrasonic_jsn_sr04t_echo(int echo_pin_num); + +#endif /* __RESOURCE_ULTRASONIC_JSN_SR04T_INTERNAL_H__ */ diff --git a/inc/resource_internal.h b/inc/resource_internal.h index 4a6b4b0..a75e036 100755 --- a/inc/resource_internal.h +++ b/inc/resource_internal.h @@ -38,6 +38,7 @@ #include "resource/resource_gas_detection_sensor_internal.h" #include "resource/resource_sound_level_sensor_internal.h" #include "resource/resource_mcu90615_internal.h" +#include "resource/resource_ultrasonic_jsn_sr04t_internal.h" #define PIN_MAX 40 diff --git a/src/resource/resource_ultrasonic_jsn_sr04t.c b/src/resource/resource_ultrasonic_jsn_sr04t.c new file mode 100644 index 0000000..5e40914 --- /dev/null +++ b/src/resource/resource_ultrasonic_jsn_sr04t.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2019 SINO TECH Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "resource_internal.h" + +static resource_read_s *resource_read_info = NULL; + +void resource_close_ultrasonic_jsn_sr04t_trig(int trig_pin_num) +{ + _I("Ultrasonic sensor's trig is finishing..."); + + if (resource_get_info(trig_pin_num)->opened) { + peripheral_gpio_close(resource_get_info(trig_pin_num)->sensor_h); + resource_get_info(trig_pin_num)->opened = 0; + } +} + +void resource_close_ultrasonic_jsn_sr04t_echo(int echo_pin_num) +{ + int ret = PERIPHERAL_ERROR_NONE; + + _I("Ultrasonic sensor's echo is finishing..."); + + if (resource_get_info(echo_pin_num)->opened) { + ret = peripheral_gpio_unset_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h); + if (ret != PERIPHERAL_ERROR_NONE) { + _E("It fails to unset the interrupted callback"); + } + + peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h); + resource_get_info(echo_pin_num)->opened = 0; + } + + free(resource_read_info); + resource_read_info = NULL; +} + +static unsigned long long _get_timestamp(void) +{ + struct timespec t; + clock_gettime(CLOCK_REALTIME, &t); + return ((unsigned long long) t.tv_sec * 1000000000LL + t.tv_nsec) / 1000; +} + +static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data) +{ + float dist = 0; + static uint32_t value = 0; + static unsigned long long triggered_time = 0; + static unsigned long long returned_time = 0; + resource_read_s *resource_read_info = user_data; + int ret = PERIPHERAL_ERROR_NONE; + + ret_if(!resource_read_info); + ret_if(!resource_read_info->cb); + + ret = peripheral_gpio_read(gpio, &value); + ret_if(ret != PERIPHERAL_ERROR_NONE); + + if (value) { + triggered_time = _get_timestamp(); + } else { + returned_time = _get_timestamp(); + } + + if (triggered_time > 0 && value == 0) { +#ifdef DEBUG + _D("value:%d, returned_time:%lld", value, returned_time); +#endif + + dist = returned_time - triggered_time; + dist = (dist * 34300) / 2000000; + + resource_read_info->cb(dist, resource_read_info->data); + + triggered_time = 0l; +#ifdef DEBUG + } else { + _D("value:%d, triggered_time:%lld", value, triggered_time); +#endif + } +} + +int resource_read_ultrasonic_jsn_sr04t(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data) +{ + int ret = PERIPHERAL_ERROR_NONE; + + 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(ret != PERIPHERAL_ERROR_NONE, -1); + + ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + resource_get_info(trig_pin_num)->opened = 1; + resource_get_info(trig_pin_num)->close = resource_close_ultrasonic_jsn_sr04t_trig; + } + + if (!resource_get_info(echo_pin_num)->opened) { + _I("Ultrasonic sensor's echo is initializing..."); + + resource_read_info = calloc(1, sizeof(resource_read_s)); + goto_if(!resource_read_info, error); + + resource_read_info->cb = cb; + resource_read_info->data = data; + resource_read_info->pin_num = echo_pin_num; + + ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + ret = peripheral_gpio_set_edge_mode(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_EDGE_BOTH); + /* WARNING : Setting edge mode returns an error at the first time. */ + //goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + ret = peripheral_gpio_set_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + resource_get_info(echo_pin_num)->opened = 1; + resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_jsn_sr04t_echo; + } + + ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + /* Requirement : JSN-SR04T Specification */ + usleep(10); + + ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + /* Requirement : JSN-SR04T Specification */ + usleep(10); + + ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0); + goto_if(ret != PERIPHERAL_ERROR_NONE, error); + + return 0; + +error: + resource_close_ultrasonic_jsn_sr04t_trig(trig_pin_num); + resource_close_ultrasonic_jsn_sr04t_echo(echo_pin_num); + return -1; +}