Change template code for UltraSonic Sensor with Soscon2017 03/157403/2
authorjunkyu han <junkyu.han@samsung.com>
Tue, 24 Oct 2017 10:09:23 +0000 (19:09 +0900)
committerjunkyu han <junkyu.han@samsung.com>
Tue, 24 Oct 2017 10:24:17 +0000 (19:24 +0900)
Change-Id: Icc27a2e0b97c95c96c15384cee53220925014ac4

inc/resource_internal.h
src/controller.c
src/resource/resource_ultrasonic_sensor.c

index ffc6f60..7a6e2d2 100644 (file)
@@ -48,7 +48,7 @@ struct _resource_s {
 };
 typedef struct _resource_s resource_s;
 
-typedef void (*resource_read_cb)(double value, void *data);
+typedef void (*resource_read_cb)(float value, void *data);
 
 struct _resource_read_cb_s {
        resource_read_cb cb;
index 18f3155..a9ce579 100644 (file)
@@ -35,23 +35,41 @@ typedef struct app_data_s {
        connectivity_resource_s *resource_info;
 } app_data;
 
-static Eina_Bool control_sensors_cb(void *data)
+static void _ultrasonic_sensor_read_cb(float value, void *data)
 {
-       double value = 0.0f;
        app_data *ad = data;
 
        /**
-        * Infrared motion sensor outputs 1 if motion is detected, or 0 if motion is not detected.
+        * TODO: Send the value of the ultrasonic sensor to the Client.
         */
-       /*
-       if (resource_read_infrared_motion_sensor(PIN_NUMBER, &value) == -1)
-               _E("Failed to get DATA from Infrared Motion value");
 
-       _D("Sensor Value: %d", value);
+       /*
+       if (value < 0) {
+               _E("OUT OF RANGE");
+       } else {
+               _D("Measured Distance : %0.2fcm", value);
+       }
        */
+}
 
+static Eina_Bool _control_sensors_cb(void *data)
+{
+       app_data *ad = data;
+       int ret = 0;
 
        /**
+        * TODO: Prepare to read the value of the ultrasonic sensor.
+        */
+
+    /*
+       ret = resource_read_ultrasonic_sensor(TRIG_PIN_NUMBER, ECHO_PIN_NUMBER, _ultrasonic_sensor_read_cb, NULL);
+       */
+
+       if (ret < 0) {
+               _E("Failed to read from ultrasonic sensor");
+       }
+
+       /*
         * Notifies specific clients that resource's attributes have changed.
         */
        /*
@@ -59,6 +77,7 @@ static Eina_Bool control_sensors_cb(void *data)
                _E("Cannot notify message");
        */
 
+
        return ECORE_CALLBACK_RENEW;
 }
 
@@ -86,7 +105,7 @@ static bool service_app_create(void *data)
         * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
         */
        /*
-       ad->getter_timer = ecore_timer_add(Duration, control_sensors_cb, ad);
+       ad->getter_timer = ecore_timer_add( Duration , _control_sensors_cb, ad);
        if (!ad->getter_timer) {
                _E("Failed to add infrared motion getter timer");
                return false;
index 550caf6..4a9b035 100644 (file)
@@ -28,6 +28,9 @@
 #include "log.h"
 #include "resource_internal.h"
 
+static resource_read_s *resource_read_info = NULL;
+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;
@@ -46,6 +49,8 @@ void resource_close_ultrasonic_sensor_echo(int echo_pin_num)
 
        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)
@@ -59,8 +64,7 @@ static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripher
 {
        float dist = 0;
        uint32_t value;
-       static unsigned long long prev = 0;
-       unsigned long long now = _get_timestamp();
+       unsigned long long returned_time = 0;
        resource_read_s *resource_read_info = user_data;
 
        ret_if(!resource_read_info);
@@ -68,26 +72,36 @@ static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripher
 
        peripheral_gpio_read(gpio, &value);
 
-       if (prev > 0 && value == 0) {
-               dist = now - prev;
-               dist = (dist * 34300) / 2000000;
-               _I("Measured Distance : %0.2fcm\n", dist);
+       if (value == 1) {
+               triggered_time = _get_timestamp();
+       } else if (value == 0) {
+               returned_time = _get_timestamp();
+       }
+
+       if (triggered_time > 0 && value == 0) {
+               dist = returned_time - triggered_time;
+               if (dist < 150 || dist > 25000) {
+                       dist = -1;
+               } else {
+                       dist = (dist * 34300) / 2000000;
+               }
 
                resource_read_info->cb(dist, resource_read_info->data);
-               peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_read_info->pin_num)->sensor_h);
-               free(resource_read_info);
        }
-
-       prev = now;
 }
 
 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);
+       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;
@@ -129,9 +143,13 @@ int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, resource
        ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
        retv_if(ret < 0, -1);
 
+       usleep(20000);
+
        ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 1);
        retv_if(ret < 0, -1);
 
+       usleep(20000);
+
        ret = peripheral_gpio_write(resource_get_info(trig_pin_num)->sensor_h, 0);
        retv_if(ret < 0, -1);