Fix WS2812 led definition
[inav.git] / src / main / fc / rc_modes.c
blob741308755caef33115976d75876c31357ba1bc1c
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
22 #include "platform.h"
24 #include "rc_modes.h"
26 #include "common/bitarray.h"
27 #include "common/maths.h"
28 #include "common/utils.h"
30 #include "config/feature.h"
31 #include "config/parameter_group.h"
32 #include "config/parameter_group_ids.h"
34 #include "fc/config.h"
35 #include "fc/rc_controls.h"
36 #include "fc/runtime_config.h"
37 #include "fc/settings.h"
39 #include "rx/rx.h"
41 static uint8_t specifiedConditionCountPerMode[CHECKBOX_ITEM_COUNT];
42 static bool isUsingNAVModes = false;
44 boxBitmask_t rcModeActivationMask; // one bit per mode defined in boxId_e
46 // TODO(alberto): It looks like we can now safely remove this assert, since everything
47 // but BB is able to handle more than 32 boxes and all the definitions use
48 // CHECKBOX_ITEM_COUNT rather than hardcoded values. Note, however, that BB will only
49 // log the first 32 flight modes, so the ones affecting actual flight should be <= 32.
51 // Leaving the assert commented for now, just in case there are some unexpected issues
52 // and someone else has to debug it.
53 // STATIC_ASSERT(CHECKBOX_ITEM_COUNT <= 32, too_many_box_modes);
55 PG_REGISTER_ARRAY(modeActivationCondition_t, MAX_MODE_ACTIVATION_CONDITION_COUNT, modeActivationConditions, PG_MODE_ACTIVATION_PROFILE, 0);
56 PG_REGISTER(modeActivationOperatorConfig_t, modeActivationOperatorConfig, PG_MODE_ACTIVATION_OPERATOR_CONFIG, 0);
58 PG_RESET_TEMPLATE(modeActivationOperatorConfig_t, modeActivationOperatorConfig,
59 .modeActivationOperator = SETTING_MODE_RANGE_LOGIC_OPERATOR_DEFAULT
62 static void processAirmodeAirplane(void) {
63 if (feature(FEATURE_AIRMODE) || IS_RC_MODE_ACTIVE(BOXAIRMODE)) {
64 ENABLE_STATE(AIRMODE_ACTIVE);
65 } else {
66 DISABLE_STATE(AIRMODE_ACTIVE);
70 static void processAirmodeMultirotor(void) {
71 if ((rcControlsConfig()->airmodeHandlingType == STICK_CENTER) || (rcControlsConfig()->airmodeHandlingType == STICK_CENTER_ONCE)) {
72 if (feature(FEATURE_AIRMODE) || IS_RC_MODE_ACTIVE(BOXAIRMODE)) {
73 ENABLE_STATE(AIRMODE_ACTIVE);
74 } else {
75 DISABLE_STATE(AIRMODE_ACTIVE);
77 } else if (rcControlsConfig()->airmodeHandlingType == THROTTLE_THRESHOLD) {
79 if (!ARMING_FLAG(ARMED)) {
81 * Disarm disables airmode immediately
83 DISABLE_STATE(AIRMODE_ACTIVE);
84 } else if (
85 !STATE(AIRMODE_ACTIVE) &&
86 rcCommand[THROTTLE] > rcControlsConfig()->airmodeThrottleThreshold &&
87 (feature(FEATURE_AIRMODE) || IS_RC_MODE_ACTIVE(BOXAIRMODE))
88 ) {
90 * Airmode is allowed to be active only after ARMED and then THROTTLE goes above
91 * activation threshold
93 ENABLE_STATE(AIRMODE_ACTIVE);
94 } else if (
95 STATE(AIRMODE_ACTIVE) &&
96 !feature(FEATURE_AIRMODE) &&
97 !IS_RC_MODE_ACTIVE(BOXAIRMODE)
98 ) {
100 * When user disables BOXAIRMODE, turn airmode off as well
102 DISABLE_STATE(AIRMODE_ACTIVE);
105 } else {
106 DISABLE_STATE(AIRMODE_ACTIVE);
110 void processAirmode(void) {
112 if (STATE(AIRPLANE)) {
113 processAirmodeAirplane();
114 } else if (STATE(MULTIROTOR)) {
115 processAirmodeMultirotor();
120 bool isUsingNavigationModes(void)
122 return isUsingNAVModes;
125 bool IS_RC_MODE_ACTIVE(boxId_e boxId)
127 return bitArrayGet(rcModeActivationMask.bits, boxId);
130 void rcModeUpdate(boxBitmask_t *newState)
132 rcModeActivationMask = *newState;
135 bool isModeActivationConditionPresent(boxId_e modeId)
137 return specifiedConditionCountPerMode[modeId] > 0;
140 bool isRangeActive(uint8_t auxChannelIndex, const channelRange_t *range)
142 if (!IS_RANGE_USABLE(range)) {
143 return false;
146 // No need to constrain() here, since we're testing for a closed range defined
147 // by the channelRange_t. If channelValue has an invalid value, the test will
148 // be false anyway.
149 uint16_t channelValue = rxGetChannelValue(auxChannelIndex + NON_AUX_CHANNEL_COUNT);
150 return (channelValue >= CHANNEL_RANGE_MIN + (range->startStep * CHANNEL_RANGE_STEP_WIDTH) &&
151 channelValue < CHANNEL_RANGE_MIN + (range->endStep * CHANNEL_RANGE_STEP_WIDTH));
154 void updateActivatedModes(void)
156 // Disable all modes to begin with
157 boxBitmask_t newMask;
158 memset(&newMask, 0, sizeof(newMask));
160 // Unfortunately for AND logic it's not enough to simply check if any of the specified channel range conditions are valid for a mode.
161 // We need to count the total number of conditions specified for each mode, and check that all those conditions are currently valid.
162 uint8_t activeConditionCountPerMode[CHECKBOX_ITEM_COUNT];
163 memset(activeConditionCountPerMode, 0, CHECKBOX_ITEM_COUNT);
165 for (int index = 0; index < MAX_MODE_ACTIVATION_CONDITION_COUNT; index++) {
166 if (isRangeActive(modeActivationConditions(index)->auxChannelIndex, &modeActivationConditions(index)->range)) {
167 // Increment the number of valid conditions for this mode
168 activeConditionCountPerMode[modeActivationConditions(index)->modeId]++;
172 // Now see which modes should be enabled
173 for (int modeIndex = 0; modeIndex < CHECKBOX_ITEM_COUNT; modeIndex++) {
174 // only modes with conditions specified are considered
175 if (specifiedConditionCountPerMode[modeIndex] > 0) {
176 // For AND logic, the specified condition count and valid condition count must be the same.
177 // For OR logic, the valid condition count must be greater than zero.
179 if (modeActivationOperatorConfig()->modeActivationOperator == MODE_OPERATOR_AND) {
180 // AND the conditions
181 if (activeConditionCountPerMode[modeIndex] == specifiedConditionCountPerMode[modeIndex]) {
182 bitArraySet(newMask.bits, modeIndex);
185 else {
186 // OR the conditions
187 if (activeConditionCountPerMode[modeIndex] > 0) {
188 bitArraySet(newMask.bits, modeIndex);
194 rcModeUpdate(&newMask);
197 void updateUsedModeActivationConditionFlags(void)
199 memset(specifiedConditionCountPerMode, 0, CHECKBOX_ITEM_COUNT);
200 for (int index = 0; index < MAX_MODE_ACTIVATION_CONDITION_COUNT; index++) {
201 if (IS_RANGE_USABLE(&modeActivationConditions(index)->range)) {
202 specifiedConditionCountPerMode[modeActivationConditions(index)->modeId]++;
206 isUsingNAVModes = isModeActivationConditionPresent(BOXNAVPOSHOLD) ||
207 isModeActivationConditionPresent(BOXNAVRTH) ||
208 isModeActivationConditionPresent(BOXNAVCOURSEHOLD) ||
209 isModeActivationConditionPresent(BOXNAVCRUISE) ||
210 isModeActivationConditionPresent(BOXNAVWP);