summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEunyoung Lee <ey928.lee@samsung.com>2017-07-21 17:41:39 +0900
committerEunyoung Lee <ey928.lee@samsung.com>2017-07-21 17:43:48 +0900
commit5782ff3c7f400a0dba54c8fa2ac857d5d768674f (patch)
treebb57847e724cc6d7d707203167ad101e1fbf5e14 /src
parent642dd73da4b7803e54891eb30251f6e52f44cbc4 (diff)
downloadrcc-5782ff3c7f400a0dba54c8fa2ac857d5d768674f.tar.gz
rcc-5782ff3c7f400a0dba54c8fa2ac857d5d768674f.tar.bz2
rcc-5782ff3c7f400a0dba54c8fa2ac857d5d768674f.zip
Changed the method of reading the sensor value from the ultrasonic sensor
Change-Id: I2ca69857c66db8c2b3ae3bbd2867cc33734d59c0
Diffstat (limited to 'src')
-rw-r--r--src/controller.c10
-rw-r--r--src/resource/resource_ultrasonic_sensor.c74
2 files changed, 41 insertions, 43 deletions
diff --git a/src/controller.c b/src/controller.c
index 886f712..cee20d9 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -39,7 +39,7 @@
#define GPIO_ULTRASONIC_ECHO_NUM_1 21
#define GPIO_INFRARED_MOTION_NUM_1 4
#define I2C_ILLUMINANCE_FIRST_PIN_1 3
-#define USE_MULTIPLE_SENSOR 1
+#define USE_MULTIPLE_SENSOR 0
#define MULTIPLE_SENSOR_NUMBER 5
static void _start_internal_function(void);
@@ -81,12 +81,16 @@ static Eina_Bool _infrared_motion_getter_timer(void *data)
}
#if (!USE_MULTIPLE_SENSOR)
+static void ultrasonic_sensor_read_cb(double value, void *data)
+{
+ _I("Distance : %.2fcm", value);
+}
+
static Eina_Bool _ultrasonic_getter_timer(void *data)
{
double value = 0;
- retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, &value) == -1, ECORE_CALLBACK_CANCEL);
- _I("Ultra Sonic Distance is [%d cm]", value);
+ retv_if(resource_read_ultrasonic_sensor(GPIO_ULTRASONIC_TRIG_NUM_1, GPIO_ULTRASONIC_ECHO_NUM_1, ultrasonic_sensor_read_cb, NULL) == -1, ECORE_CALLBACK_CANCEL);
return ECORE_CALLBACK_RENEW;
}
diff --git a/src/resource/resource_ultrasonic_sensor.c b/src/resource/resource_ultrasonic_sensor.c
index 2dd2b62..a186c11 100644
--- a/src/resource/resource_ultrasonic_sensor.c
+++ b/src/resource/resource_ultrasonic_sensor.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <peripheral_io.h>
#include <sys/time.h>
+#include <gio/gio.h>
#include "log.h"
#include "resource_internal.h"
@@ -41,22 +42,38 @@ void resource_close_ultrasonic_sensor(int echo_pin_num, int trig_pin_num)
resource_get_info(trig_pin_num)->opened = 0;
}
-static int _get_echo_value(int echo_pin_num)
+static void _resource_read_ultrasonic_sensor_cb(gpio_isr_cb_s *data, void *user_data)
{
- int ret = 0;
- int value = 0;
+ float dist = 0;
+ static unsigned long long timestamp = 0;
+ resource_read_s *resource_read_info = user_data;
- ret = peripheral_gpio_read(resource_get_info(echo_pin_num)->sensor_h, &value);
- retv_if(ret < 0, -1);
+ 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);
+ }
- return value;
+ timestamp = data->timestamp;
}
-int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, double *out_value)
+int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource_read_cb cb, void *data)
{
int ret = 0;
- double duration = 0.0;
- struct timeval start_time, end_time, temp_start_time, temp_end_time;
+ 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...");
@@ -79,48 +96,25 @@ int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, double *
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;
}
+ 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);
- sleep(1);
-
ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
retv_if(ret < 0, -1);
- usleep(10);
-
ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
retv_if(ret < 0, -1);
- _D("Count the distance");
- gettimeofday(&temp_start_time, NULL);
-
- while (_get_echo_value(echo_pin_num) == 0) {
- gettimeofday(&temp_end_time, NULL);
- duration = (double)temp_end_time.tv_sec + (double)(temp_end_time.tv_usec / 1000000.0)
- - (double)temp_start_time.tv_sec - (double)(temp_start_time.tv_usec / 1000000.0);
- if (duration > 1.0f) {
- _E("Cannot get the echo value.");
- return -1;
- }
- }
- gettimeofday(&start_time, NULL);
-
- _D("After checking #1");
-
- while (_get_echo_value(echo_pin_num) == 1);
- gettimeofday(&end_time, NULL);
-
- _D("After checking #2");
-
- duration = (double)end_time.tv_sec + (double)(end_time.tv_usec / 1000000.0)
- - (double)start_time.tv_sec - (double)(start_time.tv_usec / 1000000.0);
- *out_value = duration / 2 * 340.0;
-
- _I("Ultrasonic Sensor Value : %f", *out_value);
-
return 0;
}