2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
29 #include "common/bitarray.h"
30 #include "common/maths.h"
31 #include "drivers/time.h"
33 #include "config/feature.h"
35 #include "pg/pg_ids.h"
38 #include "config/config.h"
39 #include "fc/rc_controls.h"
43 #define STICKY_MODE_BOOT_DELAY_US 5e6
45 boxBitmask_t rcModeActivationMask
; // one bit per mode defined in boxId_e
46 static boxBitmask_t stickyModesEverDisabled
;
48 static bool airmodeEnabled
;
50 static int activeMacCount
= 0;
51 static uint8_t activeMacArray
[MAX_MODE_ACTIVATION_CONDITION_COUNT
];
52 static int activeLinkedMacCount
= 0;
53 static uint8_t activeLinkedMacArray
[MAX_MODE_ACTIVATION_CONDITION_COUNT
];
55 PG_REGISTER_ARRAY(modeActivationCondition_t
, MAX_MODE_ACTIVATION_CONDITION_COUNT
, modeActivationConditions
, PG_MODE_ACTIVATION_PROFILE
, 2);
57 #if defined(USE_CUSTOM_BOX_NAMES)
58 PG_REGISTER_WITH_RESET_TEMPLATE(modeActivationConfig_t
, modeActivationConfig
, PG_MODE_ACTIVATION_CONFIG
, 0);
60 PG_RESET_TEMPLATE(modeActivationConfig_t
, modeActivationConfig
,
61 .box_user_1_name
= { 0 },
62 .box_user_2_name
= { 0 },
63 .box_user_3_name
= { 0 },
64 .box_user_4_name
= { 0 },
68 bool IS_RC_MODE_ACTIVE(boxId_e boxId
)
70 return bitArrayGet(&rcModeActivationMask
, boxId
);
73 void rcModeUpdate(boxBitmask_t
*newState
)
75 rcModeActivationMask
= *newState
;
78 bool airmodeIsEnabled(void) {
79 return airmodeEnabled
;
82 bool isRangeActive(uint8_t auxChannelIndex
, const channelRange_t
*range
) {
83 if (!IS_RANGE_USABLE(range
)) {
87 const uint16_t channelValue
= constrain(rcData
[auxChannelIndex
+ NON_AUX_CHANNEL_COUNT
], CHANNEL_RANGE_MIN
, CHANNEL_RANGE_MAX
- 1);
88 return (channelValue
>= 900 + (range
->startStep
* 25) &&
89 channelValue
< 900 + (range
->endStep
* 25));
95 * The following are the possible logic states at each MAC update:
98 * F F - no previous AND macs evaluated, no previous active OR macs
99 * F T - at least 1 previous active OR mac (***this state is latched True***)
100 * T F - all previous AND macs active, no previous active OR macs
101 * T T - at least 1 previous inactive AND mac, no previous active OR macs
103 void updateMasksForMac(const modeActivationCondition_t
*mac
, boxBitmask_t
*andMask
, boxBitmask_t
*newMask
, bool bActive
)
105 if (bitArrayGet(andMask
, mac
->modeId
) || !bitArrayGet(newMask
, mac
->modeId
)) {
106 bool bAnd
= mac
->modeLogic
== MODELOGIC_AND
;
108 if (!bAnd
) { // OR mac
110 bitArrayClr(andMask
, mac
->modeId
);
111 bitArraySet(newMask
, mac
->modeId
);
114 bitArraySet(andMask
, mac
->modeId
);
116 bitArraySet(newMask
, mac
->modeId
);
122 void updateMasksForStickyModes(const modeActivationCondition_t
*mac
, boxBitmask_t
*andMask
, boxBitmask_t
*newMask
)
124 if (IS_RC_MODE_ACTIVE(mac
->modeId
)) {
125 bitArrayClr(andMask
, mac
->modeId
);
126 bitArraySet(newMask
, mac
->modeId
);
128 bool bActive
= isRangeActive(mac
->auxChannelIndex
, &mac
->range
);
130 if (bitArrayGet(&stickyModesEverDisabled
, mac
->modeId
)) {
131 updateMasksForMac(mac
, andMask
, newMask
, bActive
);
133 if (micros() >= STICKY_MODE_BOOT_DELAY_US
&& !bActive
) {
134 bitArraySet(&stickyModesEverDisabled
, mac
->modeId
);
140 void updateActivatedModes(void)
142 boxBitmask_t newMask
, andMask
, stickyModes
;
143 memset(&andMask
, 0, sizeof(andMask
));
144 memset(&newMask
, 0, sizeof(newMask
));
145 memset(&stickyModes
, 0, sizeof(stickyModes
));
146 bitArraySet(&stickyModes
, BOXPARALYZE
);
148 // determine which conditions set/clear the mode
149 for (int i
= 0; i
< activeMacCount
; i
++) {
150 const modeActivationCondition_t
*mac
= modeActivationConditions(activeMacArray
[i
]);
152 if (bitArrayGet(&stickyModes
, mac
->modeId
)) {
153 updateMasksForStickyModes(mac
, &andMask
, &newMask
);
154 } else if (mac
->modeId
< CHECKBOX_ITEM_COUNT
) {
155 bool bActive
= isRangeActive(mac
->auxChannelIndex
, &mac
->range
);
156 updateMasksForMac(mac
, &andMask
, &newMask
, bActive
);
160 // Update linked modes
161 for (int i
= 0; i
< activeLinkedMacCount
; i
++) {
162 const modeActivationCondition_t
*mac
= modeActivationConditions(activeLinkedMacArray
[i
]);
163 bool bActive
= bitArrayGet(&andMask
, mac
->linkedTo
) != bitArrayGet(&newMask
, mac
->linkedTo
);
165 updateMasksForMac(mac
, &andMask
, &newMask
, bActive
);
168 bitArrayXor(&newMask
, sizeof(newMask
), &newMask
, &andMask
);
170 rcModeUpdate(&newMask
);
172 airmodeEnabled
= featureIsEnabled(FEATURE_AIRMODE
) || IS_RC_MODE_ACTIVE(BOXAIRMODE
);
175 bool isModeActivationConditionPresent(boxId_e modeId
)
177 for (int i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
178 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
180 if (mac
->modeId
== modeId
&& (IS_RANGE_USABLE(&mac
->range
) || mac
->linkedTo
)) {
188 bool isModeActivationConditionLinked(boxId_e modeId
)
190 for (int i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
191 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
193 if (mac
->modeId
== modeId
&& mac
->linkedTo
!= 0) {
201 void removeModeActivationCondition(const boxId_e modeId
)
204 for (unsigned i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
205 modeActivationCondition_t
*mac
= modeActivationConditionsMutable(i
);
207 if (mac
->modeId
== modeId
&& !offset
) {
212 while (i
+ offset
< MAX_MODE_ACTIVATION_CONDITION_COUNT
&& modeActivationConditions(i
+ offset
)->modeId
== modeId
) {
216 if (i
+ offset
< MAX_MODE_ACTIVATION_CONDITION_COUNT
) {
217 memcpy(mac
, modeActivationConditions(i
+ offset
), sizeof(modeActivationCondition_t
));
219 memset(mac
, 0, sizeof(modeActivationCondition_t
));
225 bool isModeActivationConditionConfigured(const modeActivationCondition_t
*mac
, const modeActivationCondition_t
*emptyMac
)
227 if (memcmp(mac
, emptyMac
, sizeof(*emptyMac
))) {
234 // Build the list of used modeActivationConditions indices
235 // We can then use this to speed up processing by only evaluating used conditions
236 void analyzeModeActivationConditions(void)
238 modeActivationCondition_t emptyMac
;
239 memset(&emptyMac
, 0, sizeof(emptyMac
));
242 activeLinkedMacCount
= 0;
244 for (uint8_t i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
245 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
247 activeLinkedMacArray
[activeLinkedMacCount
++] = i
;
248 } else if (isModeActivationConditionConfigured(mac
, &emptyMac
)) {
249 activeMacArray
[activeMacCount
++] = i
;