Change peripheral io APIs
[apps/native/position-finder-server.git] / src / resource / resource_ultrasonic_sensor.c
index 2dd2b62..550caf6 100644 (file)
@@ -1,19 +1,19 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * 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 Apache License, Version 2.0 (the "License");
+ * 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://www.apache.org/licenses/LICENSE-2.0
+ * 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,
+ * 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 <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(int echo_pin_num, int trig_pin_num)
+void resource_close_ultrasonic_sensor_trig(int trig_pin_num)
 {
-       if (!resource_get_info(echo_pin_num)->opened) return;
        if (!resource_get_info(trig_pin_num)->opened) return;
 
-       _I("Ultrasonic sensor is finishing...");
+       _I("Ultrasonic sensor's trig is finishing...");
 
-       peripheral_gpio_close(resource_get_info(echo_pin_num)->sensor_h);
        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;
-       resource_get_info(trig_pin_num)->opened = 0;
 }
 
-static int _get_echo_value(int echo_pin_num)
+static unsigned long long _get_timestamp(void)
 {
-       int ret = 0;
-       int value = 0;
+       struct timespec t;
+       clock_gettime(CLOCK_REALTIME, &t);
+       return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
+}
 
-       ret = peripheral_gpio_read(resource_get_info(echo_pin_num)->sensor_h, &value);
-       retv_if(ret < 0, -1);
+static void _resource_read_ultrasonic_sensor_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
+{
+       float dist = 0;
+       uint32_t value;
+       static unsigned long long prev = 0;
+       unsigned long long now = _get_timestamp();
+       resource_read_s *resource_read_info = user_data;
+
+       ret_if(!resource_read_info);
+       ret_if(!resource_read_info->cb);
+
+       peripheral_gpio_read(gpio, &value);
+
+       if (prev > 0 && value == 0) {
+               dist = now - prev;
+               dist = (dist * 34300) / 2000000;
+               _I("Measured Distance : %0.2fcm\n", dist);
 
-       return value;
+               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, 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...");
@@ -64,10 +98,11 @@ int resource_read_ultrasonic_sensor(int trig_pin_num, int echo_pin_num, double *
                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);
+               ret = peripheral_gpio_set_direction(resource_get_info(trig_pin_num)->sensor_h, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
                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) {
@@ -79,48 +114,26 @@ 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;
+               resource_get_info(echo_pin_num)->close = resource_close_ultrasonic_sensor_echo;
+       }
+
+       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);
        }
 
        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;
 }