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.
*/
/*
_E("Cannot notify message");
*/
+
return ECORE_CALLBACK_RENEW;
}
* 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;
#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;
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)
{
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);
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;
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);