From 582990c0afbf195b5e476f7a0390164cf3570e85 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sat, 4 Nov 2017 21:44:39 +0100 Subject: [PATCH] FlightTasks: revised task switching algorithm to catch errors during activation of a new task and make it more readable --- src/lib/FlightTasks/FlightTasks.hpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/lib/FlightTasks/FlightTasks.hpp b/src/lib/FlightTasks/FlightTasks.hpp index a3d908a616..a58e17615c 100644 --- a/src/lib/FlightTasks/FlightTasks.hpp +++ b/src/lib/FlightTasks/FlightTasks.hpp @@ -107,21 +107,23 @@ public: */ int switch_task(int task_number) { + /* switch to the running task, nothing to do */ if (task_number == _current_task) { return 0; } + /* disable the old task if there is any */ if (is_any_task_active()) { _tasks[_current_task]->disable(); } - _current_task = task_number; - - if (is_any_task_active()) { - _tasks[_current_task]->activate(); + /* if the new task exists and it activates succesfully we switched */ + if (is_task_number_valid(task_number) && !_tasks[task_number]->activate()) { + _current_task = task_number; return 0; } + /* something went wrong, no task running anymore */ _current_task = -1; return 1; }; @@ -134,16 +136,21 @@ public: /** * Check if any task is active - * @return true if a task is active, flase if not + * @return true if a task is active, false if not */ - bool is_any_task_active() const { return _current_task > -1 && _current_task < _task_count; }; + bool is_any_task_active() const { return is_task_number_valid(_current_task); }; -private: - int _current_task = -1; + /** + * Check if the task number exists + * @return true if yes, false if not + */ + bool is_task_number_valid(int task_number) const { return task_number > -1 && task_number < _task_count; }; +private: + static constexpr int _task_count = 2; + FlightTask *_tasks[_task_count] = {&Manual, &Orbit}; FlightTaskManual Manual; FlightTaskOrbit Orbit; - static const int _task_count = 2; - FlightTask *_tasks[_task_count] = {&Manual, &Orbit}; + int _current_task = -1; };