fix(motions): fix max size and add disable method

This commit is contained in:
John Zhao 2019-02-21 22:17:53 +08:00
parent 055c6a23d0
commit fd8616f475
2 changed files with 15 additions and 3 deletions

View File

@ -66,7 +66,10 @@ void Motions::SetMotionCallback(motion_callback_t callback) {
std::lock_guard<std::mutex> _(mtx_datas_); std::lock_guard<std::mutex> _(mtx_datas_);
motion_data_t data = {imu}; motion_data_t data = {imu};
if (motion_datas_enabled_) { if (motion_datas_enabled_ && motion_datas_max_size_ > 0) {
if (motion_datas_.size() >= motion_datas_max_size_) {
motion_datas_.erase(motion_datas_.begin());
}
motion_datas_.push_back(data); motion_datas_.push_back(data);
} }
@ -98,13 +101,21 @@ void Motions::StopMotionTracking() {
} }
} }
void Motions::DisableMotionDatas() {
std::lock_guard<std::mutex> _(mtx_datas_);
motion_datas_enabled_ = false;
motion_datas_max_size_ = 0;
motion_datas_.clear();
}
void Motions::EnableMotionDatas(std::size_t max_size) { void Motions::EnableMotionDatas(std::size_t max_size) {
if (max_size <= 0) { if (max_size <= 0) {
LOG(WARNING) << "Could not enable motion datas with max_size <= 0"; LOG(WARNING) << "Could not enable motion datas with max_size <= 0";
return; return;
} }
std::lock_guard<std::mutex> _(mtx_datas_);
motion_datas_enabled_ = true; motion_datas_enabled_ = true;
motion_datas_max_size = max_size; motion_datas_max_size_ = max_size;
} }
Motions::motion_datas_t Motions::GetMotionDatas() { Motions::motion_datas_t Motions::GetMotionDatas() {

View File

@ -42,6 +42,7 @@ class Motions {
void StartMotionTracking(); void StartMotionTracking();
void StopMotionTracking(); void StopMotionTracking();
void DisableMotionDatas();
void EnableMotionDatas(std::size_t max_size); void EnableMotionDatas(std::size_t max_size);
motion_datas_t GetMotionDatas(); motion_datas_t GetMotionDatas();
@ -52,7 +53,7 @@ class Motions {
motion_datas_t motion_datas_; motion_datas_t motion_datas_;
bool motion_datas_enabled_; bool motion_datas_enabled_;
std::size_t motion_datas_max_size; std::size_t motion_datas_max_size_;
bool is_imu_tracking; bool is_imu_tracking;