feat: More flexible ADC inputs (#3870)

Co-authored-by: 3djc <lesitewebdejc@hotmail.com>
Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
Co-authored-by: philmoz <phil.a.mitchell@gmail.com>
This commit is contained in:
Raphael Coeffic
2023-11-02 11:07:53 +10:00
committed by GitHub
co-authored by 3djc Peter Feerick philmoz
parent f5593c44e8
commit b48e07026d
78 changed files with 1291 additions and 773 deletions
+35 -51
View File
@@ -296,6 +296,18 @@ int16_t applyLimits(uint8_t channel, int32_t value)
return ofs;
}
static const getvalue_t _switch_2pos_lookup[] = {
-1024, // SWITCH_HW_UP
+1024, // SWITCH_HW_MID
+1024, // SWITCH_HW_DOWN
};
static const getvalue_t _switch_3pos_lookup[] = {
-1024, // SWITCH_HW_UP
0, // SWITCH_HW_MID
+1024, // SWITCH_HW_DOWN
};
// TODO same naming convention than the drawSource
// *valid added to return status to Lua for invalid sources
getvalue_t getValue(mixsrc_t i, bool* valid)
@@ -329,24 +341,13 @@ getvalue_t getValue(mixsrc_t i, bool* valid)
}
else if (i <= MIXSRC_LAST_POT) {
i -= MIXSRC_FIRST_POT;
if (i >= adcGetMaxInputs(ADC_INPUT_POT)) {
if (i >= adcGetMaxInputs(ADC_INPUT_FLEX)) {
if (valid != nullptr) *valid = false;
return 0;
}
return calibratedAnalogs[i + adcGetInputOffset(ADC_INPUT_POT)];
return calibratedAnalogs[i + adcGetInputOffset(ADC_INPUT_FLEX)];
}
#if MAX_AXIS > 0
else if (i <= MIXSRC_LAST_AXIS) {
i -= MIXSRC_FIRST_AXIS;
if (i >= adcGetMaxInputs(ADC_INPUT_AXIS)) {
if (valid != nullptr) *valid = false;
return 0;
}
return calibratedAnalogs[i + adcGetInputOffset(ADC_INPUT_AXIS)];
}
#endif
#if defined(IMU)
else if (i == MIXSRC_TILT_X) {
return gyro.scaledX();
@@ -383,31 +384,30 @@ getvalue_t getValue(mixsrc_t i, bool* valid)
auto trim_value = getTrimValue(mixerCurrentFlightMode, i);
return calc1000toRESX((int16_t)8 * trim_value);
}
#if defined(FUNCTION_SWITCHES)
else if (i >= MIXSRC_FIRST_SWITCH && i <= MIXSRC_LAST_REGULAR_SWITCH) {
mixsrc_t sw = i - MIXSRC_FIRST_SWITCH;
if (SWITCH_EXISTS(sw)) {
return (switchState(3*sw) ? -1024 : (IS_CONFIG_3POS(sw) && switchState(3*sw+1) ? 0 : 1024));
}
else {
if (valid != nullptr) *valid = false;
return 0;
}
} else if (i >= MIXSRC_FIRST_FS_SWITCH && i <= MIXSRC_LAST_SWITCH) {
return getFSLogicalState(i - MIXSRC_FIRST_SWITCH - switchGetMaxSwitches()) ? +1024 : -1024;
}
#else
else if (i >= MIXSRC_FIRST_SWITCH && i <= MIXSRC_LAST_SWITCH) {
mixsrc_t sw = i - MIXSRC_FIRST_SWITCH;
if (SWITCH_EXISTS(sw)) {
return (switchState(3*sw) ? -1024 : (IS_CONFIG_3POS(sw) && switchState(3*sw+1) ? 0 : 1024));
auto sw_idx = (uint8_t)(i - MIXSRC_FIRST_SWITCH);
#if defined(FUNCTION_SWITCHES)
auto max_reg_switches = switchGetMaxSwitches();
if (sw_idx >= max_reg_switches) {
auto fct_idx = sw_idx - max_reg_switches;
auto max_fct_switches = switchGetMaxFctSwitches();
if (fct_idx < max_fct_switches) {
return _switch_2pos_lookup[getFSLogicalState(fct_idx)];
}
}
else {
#endif
auto sw_cfg = (SwitchConfig)SWITCH_CONFIG(sw_idx);
switch(sw_cfg) {
case SWITCH_NONE:
if (valid != nullptr) *valid = false;
return 0;
case SWITCH_TOGGLE:
case SWITCH_2POS:
return _switch_2pos_lookup[switchGetPosition(sw_idx)];
case SWITCH_3POS:
return _switch_3pos_lookup[switchGetPosition(sw_idx)];
}
}
#endif
else if (i <= MIXSRC_LAST_LOGICAL_SWITCH) {
return getSwitch(SWSRC_FIRST_LOGICAL_SWITCH + i - MIXSRC_FIRST_LOGICAL_SWITCH) ? 1024 : -1024;
@@ -503,30 +503,14 @@ void evalInputs(uint8_t mode)
#endif
auto max_calib_analogs = adcGetInputOffset(ADC_INPUT_VBAT);
auto pots_offset = adcGetInputOffset(ADC_INPUT_POT);
auto pots_offset = adcGetInputOffset(ADC_INPUT_FLEX);
for (uint8_t i = 0; i < max_calib_analogs; i++) {
// normalization [0..2048] -> [-1024..1024]
int16_t v = anaIn(i);
uint8_t ch = (i < pots_offset ? inputMappingConvertMode(i) : i);
if (i >= pots_offset && IS_POT_MULTIPOS(i - pots_offset)) {
v -= RESX;
}
else {
#if defined(SIMU)
// Simu uses normed inputs
v -= RESX;
#else
CalibData * calib = &g_eeGeneral.calib[i];
v -= calib->mid;
v = v * (int32_t) RESX / (max((int16_t) 100, (v > 0 ? calib->spanPos : calib->spanNeg)));
#endif
}
// Limit values to supported range
if (v < -RESX) v = -RESX;
if (v > RESX) v = RESX;
// [0..2048] -> [-1024..1024]
v -= RESX;
#if defined(STICK_DEAD_ZONE)
// dead zone invented by FlySky in my opinion it should goes into ADC