From 28979b0f721b00fee7c8268bb86dc303c16535d1 Mon Sep 17 00:00:00 2001 From: Rafal Galka Date: Thu, 14 May 2015 16:01:07 +0200 Subject: [PATCH] [HAM] WRIST_UP implementation [Info] Unable to verify. Gesture not supported on emulator. Change-Id: I2d7751c92f4cb1c8e3dbe566f03004b7f926c8ac Signed-off-by: Rafal Galka --- .../humanactivitymonitor_api.js | 16 +++- .../humanactivitymonitor_manager.cc | 82 ++++++++++++++++++- .../humanactivitymonitor_manager.h | 11 +++ 3 files changed, 105 insertions(+), 4 deletions(-) diff --git a/src/humanactivitymonitor/humanactivitymonitor_api.js b/src/humanactivitymonitor/humanactivitymonitor_api.js index 77677f50..daa9a125 100644 --- a/src/humanactivitymonitor/humanactivitymonitor_api.js +++ b/src/humanactivitymonitor/humanactivitymonitor_api.js @@ -87,7 +87,21 @@ HumanActivityMonitorManager.prototype.start = function(type, changedCallback) { } var listener = function(result) { - native_.callIfPossible(args.changedCallback, new HumanActivityHRMData(result)); + switch (args.type) { + case HumanActivityType.PEDOMETER: + // TODO(r.galka) Not Supported in current implementation + break; + case HumanActivityType.WRIST_UP: + native_.callIfPossible(args.changedCallback, null); + break; + case HumanActivityType.HRM: + native_.callIfPossible(args.changedCallback, new HumanActivityHRMData(result)); + break; + case HumanActivityType.GPS: + // TODO(r.galka) implement + break; + } + }; native_.addListener(listenerId, listener); }; diff --git a/src/humanactivitymonitor/humanactivitymonitor_manager.cc b/src/humanactivitymonitor/humanactivitymonitor_manager.cc index e60784fc..450a7cbe 100644 --- a/src/humanactivitymonitor/humanactivitymonitor_manager.cc +++ b/src/humanactivitymonitor/humanactivitymonitor_manager.cc @@ -17,6 +17,7 @@ using common::ErrorCode; HumanActivityMonitorManager::HumanActivityMonitorManager() {} HumanActivityMonitorManager::~HumanActivityMonitorManager() { + UnsetWristUpListener(); UnsetHrmListener(); } @@ -40,7 +41,12 @@ PlatformResult HumanActivityMonitorManager::IsSupported( // TODO(r.galka) no native api for pedometer // so just pass it for not supported. } else if (type == kActivityTypeWristUp) { - // TODO(r.galka) implement when available in platform + ret = gesture_is_supported(GESTURE_WRIST_UP, &supported); + if (ret != SENSOR_ERROR_NONE) { + LOGGER(ERROR) << "gesture_is_supported(GESTURE_WRIST_UP), error: " << ret; + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "WRIST_UP gesture check failed"); + } } else if (type == kActivityTypeHrm) { ret = sensor_is_supported(SENSOR_HRM, &supported); if (ret != SENSOR_ERROR_NONE) { @@ -78,7 +84,7 @@ PlatformResult HumanActivityMonitorManager::SetListener( // WRIST_UP if (type == kActivityTypeWristUp) { - // TODO(r.galka) implement + return SetWristUpListener(callback); } // HRM @@ -107,7 +113,7 @@ PlatformResult HumanActivityMonitorManager::UnsetListener( // WRIST_UP if (type == kActivityTypeWristUp) { - // TODO(r.galka) implement + return UnsetWristUpListener(); } // HRM @@ -121,6 +127,71 @@ PlatformResult HumanActivityMonitorManager::UnsetListener( } } +// WRIST_UP +PlatformResult HumanActivityMonitorManager::SetWristUpListener( + JsonCallback callback) { + int ret; + + ret = gesture_create(&gesture_handle_); + if (ret != GESTURE_ERROR_NONE) { + LOGGER(ERROR) << "Failed to create WRIST_UP handle, error: " << ret; + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Failed to create WRIST_UP listener"); + } + + ret = gesture_start_recognition(gesture_handle_, + GESTURE_WRIST_UP, + GESTURE_OPTION_DEFAULT, + OnWristUpEvent, + this); + if (ret != GESTURE_ERROR_NONE) { + LOGGER(ERROR) << "Failed to start WRIST_UP listener, error: " << ret; + return PlatformResult(ErrorCode::UNKNOWN_ERR, + "Failed to start WRIST_UP listener"); + } + + wrist_up_event_callback_ = callback; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +PlatformResult HumanActivityMonitorManager::UnsetWristUpListener() { + int ret; + + if (gesture_handle_) { + ret = gesture_stop_recognition(gesture_handle_); + if (ret != GESTURE_ERROR_NONE) { + LOGGER(ERROR) << "Failed to stop WRIST_UP detection, error: " << ret; + } + + ret = gesture_release(gesture_handle_); + if (ret != GESTURE_ERROR_NONE) { + LOGGER(ERROR) << "Failed to release WRIST_UP handle, error: " << ret; + } + } + + wrist_up_event_callback_ = nullptr; + + return PlatformResult(ErrorCode::NO_ERROR); +} + +void HumanActivityMonitorManager::OnWristUpEvent(gesture_type_e gesture, + const gesture_data_h data, + double timestamp, + gesture_error_e error, + void* user_data) { + HumanActivityMonitorManager* manager = + static_cast(user_data); + + if (!manager->wrist_up_event_callback_) { + LOGGER(ERROR) << "No WRIST_UP event callback registered, skipping."; + return; + } + + picojson::value v = picojson::value(); // null value + manager->wrist_up_event_callback_(&v); +} + // HRM PlatformResult HumanActivityMonitorManager::SetHrmListener( JsonCallback callback) { @@ -219,6 +290,11 @@ void HumanActivityMonitorManager::OnHrmSensorEvent( HumanActivityMonitorManager* manager = static_cast(user_data); + if (!manager->hrm_event_callback_) { + LOGGER(ERROR) << "No HRM event callback registered, skipping."; + return; + } + picojson::value hrm_data = picojson::value(picojson::object()); picojson::object& hrm_data_o = hrm_data.get(); diff --git a/src/humanactivitymonitor/humanactivitymonitor_manager.h b/src/humanactivitymonitor/humanactivitymonitor_manager.h index fbf311d1..8b32a615 100644 --- a/src/humanactivitymonitor/humanactivitymonitor_manager.h +++ b/src/humanactivitymonitor/humanactivitymonitor_manager.h @@ -40,6 +40,17 @@ class HumanActivityMonitorManager { private: std::map supported_; + // WRIST_UP + gesture_h gesture_handle_; + JsonCallback wrist_up_event_callback_; + common::PlatformResult SetWristUpListener(JsonCallback callback); + common::PlatformResult UnsetWristUpListener(); + static void OnWristUpEvent(gesture_type_e gesture, + const gesture_data_h data, + double timestamp, + gesture_error_e error, + void* user_data); + // HRM sensor_listener_h hrm_sensor_listener_; JsonCallback hrm_event_callback_; -- 2.34.1