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/>.
27 #include "common/bitarray.h"
28 #include "common/maths.h"
30 #include "drivers/time.h"
32 #include "config/config.h"
33 #include "config/feature.h"
35 #include "fc/rc_controls.h"
37 #include "io/piniobox.h"
40 #include "pg/pg_ids.h"
47 #define STICKY_MODE_BOOT_DELAY_US 5e6
49 boxBitmask_t rcModeActivationMask
; // one bit per mode defined in boxId_e
50 static boxBitmask_t stickyModesEverDisabled
;
52 static bool airmodeEnabled
;
54 static int activeMacCount
= 0;
55 static uint8_t activeMacArray
[MAX_MODE_ACTIVATION_CONDITION_COUNT
];
56 static int activeLinkedMacCount
= 0;
57 static uint8_t activeLinkedMacArray
[MAX_MODE_ACTIVATION_CONDITION_COUNT
];
59 PG_REGISTER_ARRAY(modeActivationCondition_t
, MAX_MODE_ACTIVATION_CONDITION_COUNT
, modeActivationConditions
, PG_MODE_ACTIVATION_PROFILE
, 2);
61 #if defined(USE_CUSTOM_BOX_NAMES)
62 PG_REGISTER_WITH_RESET_TEMPLATE(modeActivationConfig_t
, modeActivationConfig
, PG_MODE_ACTIVATION_CONFIG
, 0);
64 PG_RESET_TEMPLATE(modeActivationConfig_t
, modeActivationConfig
,
65 .box_user_1_name
= { 0 },
66 .box_user_2_name
= { 0 },
67 .box_user_3_name
= { 0 },
68 .box_user_4_name
= { 0 },
72 bool IS_RC_MODE_ACTIVE(boxId_e boxId
)
74 return bitArrayGet(&rcModeActivationMask
, boxId
);
77 void rcModeUpdate(boxBitmask_t
*newState
)
79 rcModeActivationMask
= *newState
;
82 bool airmodeIsEnabled(void) {
83 return airmodeEnabled
;
86 bool isRangeActive(uint8_t auxChannelIndex
, const channelRange_t
*range
) {
87 if (!IS_RANGE_USABLE(range
)) {
91 const uint16_t channelValue
= constrain(rcData
[auxChannelIndex
+ NON_AUX_CHANNEL_COUNT
], CHANNEL_RANGE_MIN
, CHANNEL_RANGE_MAX
- 1);
92 return (channelValue
>= 900 + (range
->startStep
* 25) &&
93 channelValue
< 900 + (range
->endStep
* 25));
99 * The following are the possible logic states at each MAC update:
102 * F F - no previous AND macs evaluated, no previous active OR macs
103 * F T - at least 1 previous active OR mac (***this state is latched True***)
104 * T F - all previous AND macs active, no previous active OR macs
105 * T T - at least 1 previous inactive AND mac, no previous active OR macs
107 void updateMasksForMac(const modeActivationCondition_t
*mac
, boxBitmask_t
*andMask
, boxBitmask_t
*newMask
, bool bActive
)
109 if (bitArrayGet(andMask
, mac
->modeId
) || !bitArrayGet(newMask
, mac
->modeId
)) {
110 bool bAnd
= mac
->modeLogic
== MODELOGIC_AND
;
112 if (!bAnd
) { // OR mac
114 bitArrayClr(andMask
, mac
->modeId
);
115 bitArraySet(newMask
, mac
->modeId
);
118 bitArraySet(andMask
, mac
->modeId
);
120 bitArraySet(newMask
, mac
->modeId
);
126 void updateMasksForStickyModes(const modeActivationCondition_t
*mac
, boxBitmask_t
*andMask
, boxBitmask_t
*newMask
)
128 if (IS_RC_MODE_ACTIVE(mac
->modeId
)) {
129 bitArrayClr(andMask
, mac
->modeId
);
130 bitArraySet(newMask
, mac
->modeId
);
132 bool bActive
= isRangeActive(mac
->auxChannelIndex
, &mac
->range
);
134 if (bitArrayGet(&stickyModesEverDisabled
, mac
->modeId
)) {
135 updateMasksForMac(mac
, andMask
, newMask
, bActive
);
137 if (micros() >= STICKY_MODE_BOOT_DELAY_US
&& !bActive
) {
138 bitArraySet(&stickyModesEverDisabled
, mac
->modeId
);
144 void updateActivatedModes(void)
146 boxBitmask_t newMask
, andMask
, stickyModes
;
147 memset(&andMask
, 0, sizeof(andMask
));
148 memset(&newMask
, 0, sizeof(newMask
));
149 memset(&stickyModes
, 0, sizeof(stickyModes
));
150 bitArraySet(&stickyModes
, BOXPARALYZE
);
152 // determine which conditions set/clear the mode
153 for (int i
= 0; i
< activeMacCount
; i
++) {
154 const modeActivationCondition_t
*mac
= modeActivationConditions(activeMacArray
[i
]);
156 if (bitArrayGet(&stickyModes
, mac
->modeId
)) {
157 updateMasksForStickyModes(mac
, &andMask
, &newMask
);
158 } else if (mac
->modeId
< CHECKBOX_ITEM_COUNT
) {
159 bool bActive
= isRangeActive(mac
->auxChannelIndex
, &mac
->range
);
160 updateMasksForMac(mac
, &andMask
, &newMask
, bActive
);
164 // Update linked modes
165 for (int i
= 0; i
< activeLinkedMacCount
; i
++) {
166 const modeActivationCondition_t
*mac
= modeActivationConditions(activeLinkedMacArray
[i
]);
167 bool bActive
= bitArrayGet(&andMask
, mac
->linkedTo
) != bitArrayGet(&newMask
, mac
->linkedTo
);
169 updateMasksForMac(mac
, &andMask
, &newMask
, bActive
);
172 bitArrayXor(&newMask
, sizeof(newMask
), &newMask
, &andMask
);
174 rcModeUpdate(&newMask
);
176 airmodeEnabled
= featureIsEnabled(FEATURE_AIRMODE
) || IS_RC_MODE_ACTIVE(BOXAIRMODE
);
179 bool isModeActivationConditionPresent(boxId_e modeId
)
181 for (int i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
182 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
184 if (mac
->modeId
== modeId
&& (IS_RANGE_USABLE(&mac
->range
) || mac
->linkedTo
)) {
192 bool isModeActivationConditionLinked(boxId_e modeId
)
194 for (int i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
195 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
197 if (mac
->modeId
== modeId
&& mac
->linkedTo
!= 0) {
205 void removeModeActivationCondition(const boxId_e modeId
)
208 for (unsigned i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
209 modeActivationCondition_t
*mac
= modeActivationConditionsMutable(i
);
211 if (mac
->modeId
== modeId
&& !offset
) {
216 while (i
+ offset
< MAX_MODE_ACTIVATION_CONDITION_COUNT
&& modeActivationConditions(i
+ offset
)->modeId
== modeId
) {
220 if (i
+ offset
< MAX_MODE_ACTIVATION_CONDITION_COUNT
) {
221 memcpy(mac
, modeActivationConditions(i
+ offset
), sizeof(modeActivationCondition_t
));
223 memset(mac
, 0, sizeof(modeActivationCondition_t
));
229 bool isModeActivationConditionConfigured(const modeActivationCondition_t
*mac
, const modeActivationCondition_t
*emptyMac
)
231 if (memcmp(mac
, emptyMac
, sizeof(*emptyMac
))) {
238 // Build the list of used modeActivationConditions indices
239 // We can then use this to speed up processing by only evaluating used conditions
240 void analyzeModeActivationConditions(void)
242 modeActivationCondition_t emptyMac
;
243 memset(&emptyMac
, 0, sizeof(emptyMac
));
246 activeLinkedMacCount
= 0;
248 for (uint8_t i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
249 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
251 activeLinkedMacArray
[activeLinkedMacCount
++] = i
;
252 } else if (isModeActivationConditionConfigured(mac
, &emptyMac
)) {
253 activeMacArray
[activeMacCount
++] = i
;
257 pinioBoxTaskControl();