Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / fc / multifunction.c
blobc217110842dbbacc1afb59ee215bd579e9939024
1 /*
2 * This file is part of INAV Project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
25 #include "platform.h"
26 #include "build/debug.h"
27 #include "drivers/time.h"
29 #ifdef USE_MULTI_FUNCTIONS
31 #include "fc/fc_core.h"
32 #include "fc/multifunction.h"
33 #include "fc/rc_modes.h"
34 #include "fc/runtime_config.h"
36 #include "io/osd.h"
37 #include "navigation/navigation.h"
39 multi_function_e selectedItem = MULTI_FUNC_NONE;
40 uint8_t multiFunctionFlags;
41 bool nextItemIsAvailable = false;
43 static void multiFunctionApply(multi_function_e selectedItem)
45 switch (selectedItem) {
46 case MULTI_FUNC_NONE:
47 break;
48 case MULTI_FUNC_1: // redisplay current warnings
49 osdResetWarningFlags();
50 break;
51 case MULTI_FUNC_2: // control manual emergency landing
52 checkManualEmergencyLandingControl(ARMING_FLAG(ARMED));
53 break;
54 case MULTI_FUNC_3: // toggle Safehome suspend
55 #if defined(USE_SAFE_HOME)
56 if (navConfig()->general.flags.safehome_usage_mode != SAFEHOME_USAGE_OFF) {
57 MULTI_FUNC_FLAG(MF_SUSPEND_SAFEHOMES) ? MULTI_FUNC_FLAG_DISABLE(MF_SUSPEND_SAFEHOMES) : MULTI_FUNC_FLAG_ENABLE(MF_SUSPEND_SAFEHOMES);
59 #endif
60 break;
61 case MULTI_FUNC_4: // toggle RTH Trackback suspend
62 if (navConfig()->general.flags.rth_trackback_mode != RTH_TRACKBACK_OFF) {
63 MULTI_FUNC_FLAG(MF_SUSPEND_TRACKBACK) ? MULTI_FUNC_FLAG_DISABLE(MF_SUSPEND_TRACKBACK) : MULTI_FUNC_FLAG_ENABLE(MF_SUSPEND_TRACKBACK);
65 break;
66 case MULTI_FUNC_5:
67 #ifdef USE_DSHOT
68 if (STATE(MULTIROTOR)) { // toggle Turtle mode
69 MULTI_FUNC_FLAG(MF_TURTLE_MODE) ? MULTI_FUNC_FLAG_DISABLE(MF_TURTLE_MODE) : MULTI_FUNC_FLAG_ENABLE(MF_TURTLE_MODE);
71 #endif
72 break;
73 case MULTI_FUNC_6: // emergency ARM
74 if (!ARMING_FLAG(ARMED)) {
75 emergencyArmingUpdate(true, true);
77 case MULTI_FUNC_END:
78 break;
82 bool isNextMultifunctionItemAvailable(void)
84 return nextItemIsAvailable;
87 void setMultifunctionSelection(multi_function_e item)
89 selectedItem = item == MULTI_FUNC_END ? MULTI_FUNC_1 : item;
90 nextItemIsAvailable = false;
93 multi_function_e multiFunctionSelection(void)
95 static timeMs_t startTimer;
96 static timeMs_t selectTimer;
97 static bool toggle = true;
98 const timeMs_t currentTime = millis();
100 if (IS_RC_MODE_ACTIVE(BOXMULTIFUNCTION)) {
101 if (selectTimer) {
102 if (currentTime - selectTimer > 3000) { // 3s selection duration to activate selected function
103 multiFunctionApply(selectedItem);
104 selectTimer = 0;
105 selectedItem = MULTI_FUNC_NONE;
106 nextItemIsAvailable = false;
108 } else if (toggle) {
109 if (selectedItem == MULTI_FUNC_NONE) {
110 selectedItem++;
111 } else {
112 selectTimer = currentTime;
113 nextItemIsAvailable = true;
116 startTimer = currentTime;
117 toggle = false;
118 } else if (startTimer) {
119 if (!toggle && selectTimer) {
120 setMultifunctionSelection(++selectedItem);
122 if (currentTime - startTimer > 4000) { // 4s reset delay after mode deselected
123 startTimer = 0;
124 selectedItem = MULTI_FUNC_NONE;
126 selectTimer = 0;
127 toggle = true;
130 return selectedItem;
132 #endif