summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJin Yoon <jinny.yoon@samsung.com>2019-10-07 08:37:56 +0000
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>2019-10-07 08:37:56 +0000
commit7c4d790dfa28dbd30221f8ea4e6c56297158de84 (patch)
tree02983015163f9ed9d0039398b9719b415b8eb2e8
parentab51bfd00079c148b6d1f867225b399333531047 (diff)
parent5f9a34155fe571eb81c8dab5916af4dcb7d8c3f9 (diff)
downloadrcc-tizen_4.0.tar.gz
rcc-tizen_4.0.tar.bz2
rcc-tizen_4.0.zip
Merge "Update Resource : Ultrasonic, JSN-SR04T"tizen_4.0
-rw-r--r--src/resource/resource_ultrasonic_jsn_sr04t.c6
-rw-r--r--src/resource/resource_ultrasonic_sensor.c88
2 files changed, 56 insertions, 38 deletions
diff --git a/src/resource/resource_ultrasonic_jsn_sr04t.c b/src/resource/resource_ultrasonic_jsn_sr04t.c
index 5e40914..ab2c893 100644
--- a/src/resource/resource_ultrasonic_jsn_sr04t.c
+++ b/src/resource/resource_ultrasonic_jsn_sr04t.c
@@ -112,10 +112,11 @@ int resource_read_ultrasonic_jsn_sr04t(int trig_pin_num, int echo_pin_num, resou
ret = peripheral_gpio_open(trig_pin_num, &resource_get_info(trig_pin_num)->sensor_h);
retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
+ resource_get_info(trig_pin_num)->opened = 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;
}
@@ -132,6 +133,8 @@ int resource_read_ultrasonic_jsn_sr04t(int trig_pin_num, int echo_pin_num, resou
ret = peripheral_gpio_open(echo_pin_num, &resource_get_info(echo_pin_num)->sensor_h);
goto_if(ret != PERIPHERAL_ERROR_NONE, error);
+ resource_get_info(echo_pin_num)->opened = 1;
+
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);
@@ -145,7 +148,6 @@ int resource_read_ultrasonic_jsn_sr04t(int trig_pin_num, int echo_pin_num, resou
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;
}
diff --git a/src/resource/resource_ultrasonic_sensor.c b/src/resource/resource_ultrasonic_sensor.c
index 336e623..1807ed3 100644
--- a/src/resource/resource_ultrasonic_sensor.c
+++ b/src/resource/resource_ultrasonic_sensor.c
@@ -33,22 +33,30 @@ static unsigned long long triggered_time = 0;
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;
+ 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_sensor_echo(int echo_pin_num)
{
- if (!resource_get_info(echo_pin_num)->opened) return;
+ int ret = PERIPHERAL_ERROR_NONE;
_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;
+ 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;
}
@@ -63,16 +71,19 @@ static unsigned long long _get_timestamp(void)
static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
{
float dist = 0;
- uint32_t value;
- unsigned long long returned_time = 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);
- peripheral_gpio_read(gpio, &value);
+ ret = peripheral_gpio_read(gpio, &value);
+ ret_if(ret != PERIPHERAL_ERROR_NONE);
- if (value == 1) {
+ if (value) {
triggered_time = _get_timestamp();
} else if (value == 0) {
returned_time = _get_timestamp();
@@ -87,6 +98,7 @@ static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripher
}
resource_read_info->cb(dist, resource_read_info->data);
+ triggered_time = 0l;
}
}
@@ -96,62 +108,66 @@ int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource
triggered_time = 0;
- if (resource_read_info == NULL) {
- resource_read_info = calloc(1, sizeof(resource_read_s));
- retv_if(!resource_read_info, -1);
- } else {
- peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
- }
- 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);
+ retv_if(ret != PERIPHERAL_ERROR_NONE, -1);
+
+ resource_get_info(trig_pin_num)->opened = 1;
ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
- retv_if(ret != 0, -1);
+ 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_sensor_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);
- retv_if(!resource_get_info(echo_pin_num)->sensor_h, -1);
+ goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
+
+ resource_get_info(echo_pin_num)->opened = 1;
ret = peripheral_gpio_set_direction(resource_get_info(echo_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_IN);
- retv_if(ret != 0, -1);
+ 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);
- 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;
- }
+ /* WARNING : Setting edge mode returns an error at the first time. */
+ //goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
- if (resource_get_info(echo_pin_num)->sensor_h) {
ret = peripheral_gpio_set_interrupted_cb(resource_get_info(echo_pin_num)->sensor_h, _resource_read_ultrasonic_sensor_cb, resource_read_info);
- retv_if(ret != 0, -1);
+ goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
+
+ resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
}
ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
- retv_if(ret < 0, -1);
+ goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
usleep(20000);
ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
- retv_if(ret < 0, -1);
+ goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
usleep(20000);
ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
- retv_if(ret < 0, -1);
+ goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR);
return 0;
+
+ERROR:
+ resource_close_ultrasonic_sensor_trig(trig_pin_num);
+ resource_close_ultrasonic_sensor_echo(echo_pin_num);
+
+ return -1;
}