From 447c167d3e7729a5fdc54dd462fc0d55d2276f44 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sun, 20 Aug 2017 16:08:53 +0200 Subject: [PATCH] Navigator: Add on_inactivation() interface. This allows to run a command / function once when being deactivated. This avoids having flight modes which are not active run unnecessary code all the time. --- src/modules/navigator/navigator_mode.cpp | 24 +++++++++++++++++------- src/modules/navigator/navigator_mode.h | 9 +++++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/modules/navigator/navigator_mode.cpp b/src/modules/navigator/navigator_mode.cpp index 9d1fad72ae..51debc68fe 100644 --- a/src/modules/navigator/navigator_mode.cpp +++ b/src/modules/navigator/navigator_mode.cpp @@ -45,11 +45,12 @@ NavigatorMode::NavigatorMode(Navigator *navigator, const char *name) : SuperBlock(navigator, name), _navigator(navigator), - _first_run(true) + _active(false) { /* load initial params */ updateParams(); /* set initial mission items */ + on_inactivation(); on_inactive(); } @@ -57,10 +58,8 @@ void NavigatorMode::run(bool active) { if (active) { - if (_first_run) { - /* first run */ - _first_run = false; - /* Reset stay in failsafe flag */ + if (!_active) { + /* first run, reset stay in failsafe flag */ _navigator->get_mission_result()->stay_in_failsafe = false; _navigator->set_mission_result_updated(); on_activation(); @@ -72,9 +71,15 @@ NavigatorMode::run(bool active) } else { /* periodic updates when inactive */ - _first_run = true; - on_inactive(); + if (_active) { + on_inactivation(); + + } else { + on_inactive(); + } } + + _active = active; } void @@ -82,6 +87,11 @@ NavigatorMode::on_inactive() { } +void +NavigatorMode::on_inactivation() +{ +} + void NavigatorMode::on_activation() { diff --git a/src/modules/navigator/navigator_mode.h b/src/modules/navigator/navigator_mode.h index fff39ac2f0..22a76d093f 100644 --- a/src/modules/navigator/navigator_mode.h +++ b/src/modules/navigator/navigator_mode.h @@ -69,10 +69,15 @@ public: virtual void on_inactive(); /** - * This function is called one time when mode become active, pos_sp_triplet must be initialized here + * This function is called one time when mode becomes active, pos_sp_triplet must be initialized here */ virtual void on_activation(); + /** + * This function is called one time when mode becomes inactive + */ + virtual void on_inactivation(); + /** * This function is called while the mode is active */ @@ -82,7 +87,7 @@ protected: Navigator *_navigator{nullptr}; private: - bool _first_run{true}; + bool _active{false}; }; #endif