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:
co-authored by
3djc
Peter Feerick
philmoz
parent
f5593c44e8
commit
b48e07026d
@@ -22,72 +22,6 @@
|
||||
#include "stm32_switch_driver.h"
|
||||
#include "hal/adc_driver.h"
|
||||
|
||||
bool stm32_switch_get_state(const stm32_switch_t* sw, SwitchHwPos pos)
|
||||
{
|
||||
switch (sw->type) {
|
||||
case SWITCH_HW_2POS:
|
||||
switch (pos) {
|
||||
case SWITCH_HW_UP:
|
||||
if (sw->flags & SWITCH_HW_INVERTED)
|
||||
return !LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
|
||||
else
|
||||
return LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
|
||||
|
||||
case SWITCH_HW_DOWN:
|
||||
if (sw->flags & SWITCH_HW_INVERTED)
|
||||
return LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
|
||||
else
|
||||
return !LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case SWITCH_HW_3POS: {
|
||||
auto hi = LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
|
||||
auto lo = LL_GPIO_IsInputPinSet(sw->GPIOx_low, sw->Pin_low);
|
||||
|
||||
switch (pos) {
|
||||
case SWITCH_HW_UP:
|
||||
if (sw->flags & SWITCH_HW_INVERTED)
|
||||
return !hi && lo;
|
||||
else
|
||||
return hi && !lo;
|
||||
|
||||
case SWITCH_HW_DOWN:
|
||||
if (sw->flags & SWITCH_HW_INVERTED)
|
||||
return hi && !lo;
|
||||
else
|
||||
return !hi && lo;
|
||||
|
||||
case SWITCH_HW_MID:
|
||||
return hi && lo;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} break;
|
||||
|
||||
case SWITCH_HW_ADC: {
|
||||
uint16_t value = getAnalogValue(sw->Pin_high);
|
||||
switch (pos) {
|
||||
case SWITCH_HW_UP:
|
||||
return value < 1024;
|
||||
case SWITCH_HW_DOWN:
|
||||
return value > 3 * 1024;
|
||||
case SWITCH_HW_MID:
|
||||
return (value >= 1024) && (value <= 3 * 1024);
|
||||
}
|
||||
} break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
SwitchHwPos stm32_switch_get_position(const stm32_switch_t* sw)
|
||||
{
|
||||
bool inv = sw->flags & SWITCH_HW_INVERTED;
|
||||
|
||||
@@ -37,18 +37,5 @@ struct stm32_switch_t
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
#define SWITCH_HW_DEF_3POS(x, flags) \
|
||||
{ \
|
||||
SWITCHES_GPIO_REG_##x##_H, SWITCHES_GPIO_PIN_##x##_H, \
|
||||
SWITCHES_GPIO_REG_##x##_L, SWITCHES_GPIO_PIN_##x##_L, \
|
||||
SWITCH_HW_3POS, (flags) \
|
||||
}
|
||||
|
||||
#define SWITCH_HW_DEF_2POS(x, flags) \
|
||||
{ \
|
||||
SWITCHES_GPIO_REG_##x, SWITCHES_GPIO_PIN_##x, \
|
||||
nullptr, 0, SWITCH_HW_2POS, (flags) \
|
||||
}
|
||||
|
||||
SwitchHwPos stm32_switch_get_position(const stm32_switch_t* sw);
|
||||
bool stm32_switch_get_state(const stm32_switch_t* sw, SwitchHwPos pos);
|
||||
|
||||
@@ -110,6 +110,7 @@ if (PCB STREQUAL X10)
|
||||
endif()
|
||||
set(INTERNAL_GPS_BAUDRATE "9600" CACHE STRING "Baud rate for internal GPS")
|
||||
set(FLYSKY_GIMBAL ON)
|
||||
set(FLEXSW "2" CACHE STRING "Max flex inputs usable as switches")
|
||||
elseif (PCBREV STREQUAL T18)
|
||||
set(FLAVOUR t18)
|
||||
add_definitions(-DRADIO_T18)
|
||||
|
||||
@@ -51,7 +51,7 @@ QVector<QIODevice *> OpenTxSimulator::tracebackDevices;
|
||||
uint16_t simu_get_analog(uint8_t idx)
|
||||
{
|
||||
// 6POS simu mechanism use a different scale, so needs specific offset
|
||||
if (IS_POT_MULTIPOS(idx - adcGetInputOffset(ADC_INPUT_POT))) {
|
||||
if (IS_POT_MULTIPOS(idx - adcGetInputOffset(ADC_INPUT_FLEX))) {
|
||||
// Use radio calibration data to determine conversion factor
|
||||
StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[idx];
|
||||
int range6POS = 2048; // Default if calibration is not valid
|
||||
@@ -256,11 +256,11 @@ void OpenTxSimulator::setInputValue(int type, uint8_t index, int16_t value)
|
||||
setAnalogValue(index, value);
|
||||
break;
|
||||
case INPUT_SRC_KNOB :
|
||||
setAnalogValue(index + adcGetInputOffset(ADC_INPUT_POT), value);
|
||||
setAnalogValue(index + adcGetInputOffset(ADC_INPUT_FLEX), value);
|
||||
break;
|
||||
case INPUT_SRC_SLIDER :
|
||||
// TODO redo this when Companion refactored to use radio json adc files
|
||||
//setAnalogValue(index + adcGetInputOffset(ADC_INPUT_POT), value);
|
||||
//setAnalogValue(index + adcGetInputOffset(ADC_INPUT_FLEX), value);
|
||||
static const int slideroffset = adcGetInputIdx("SL1", 3);
|
||||
//qDebug() << "SL1:" << slideroffset;
|
||||
if (slideroffset >= 0)
|
||||
|
||||
@@ -33,92 +33,36 @@ struct hw_switch_def {
|
||||
|
||||
#include "simu_switches.inc"
|
||||
|
||||
int8_t switchesStates[n_total_switches] = { -1 };
|
||||
int8_t switchesStates[MAX_SWITCHES] = { -1 };
|
||||
|
||||
void simuSetSwitch(uint8_t swtch, int8_t state)
|
||||
{
|
||||
assert(swtch < n_total_switches);
|
||||
assert(swtch < switchGetMaxSwitches() + switchGetMaxFctSwitches());
|
||||
switchesStates[swtch] = state;
|
||||
}
|
||||
|
||||
swconfig_t switchGetDefaultConfig()
|
||||
void boardInitSwitches() {}
|
||||
|
||||
static uint8_t get_switch_index(uint8_t cat, uint8_t idx)
|
||||
{
|
||||
return _switch_default_config;
|
||||
switch(cat) {
|
||||
case SWITCH_PHYSICAL:
|
||||
assert(idx < n_switches);
|
||||
return idx;
|
||||
|
||||
case SWITCH_FUNCTION:
|
||||
assert(idx < n_fct_switches);
|
||||
return idx + n_switches;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
switch_display_pos_t switchGetDisplayPosition(uint8_t idx)
|
||||
SwitchHwPos boardSwitchGetPosition(uint8_t cat, uint8_t idx)
|
||||
{
|
||||
if (idx >= DIM(_switch_display))
|
||||
return {0, 0};
|
||||
|
||||
return _switch_display[idx];
|
||||
}
|
||||
|
||||
uint8_t switchGetMaxSwitches()
|
||||
{
|
||||
return n_switches;
|
||||
}
|
||||
|
||||
uint8_t getSwitchCount()
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < switchGetMaxSwitches(); ++i) {
|
||||
if (SWITCH_EXISTS(i)) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
uint8_t switchGetMaxRow(uint8_t col)
|
||||
{
|
||||
uint8_t lastrow = 0;
|
||||
for (int i = 0; i < switchGetMaxSwitches(); ++i) {
|
||||
if (SWITCH_EXISTS(i)) {
|
||||
auto switch_display = switchGetDisplayPosition(i);
|
||||
if (switch_display.col == col)
|
||||
lastrow = switch_display.row > lastrow ? switch_display.row : lastrow;
|
||||
}
|
||||
}
|
||||
return lastrow;
|
||||
}
|
||||
|
||||
uint8_t switchGetMaxFctSwitches()
|
||||
{
|
||||
return n_fct_switches;
|
||||
}
|
||||
|
||||
const char* switchGetName(uint8_t idx)
|
||||
{
|
||||
assert(idx < n_total_switches);
|
||||
return _hw_switch_defs[idx].name;
|
||||
}
|
||||
|
||||
SwitchHwType switchGetHwType(uint8_t idx)
|
||||
{
|
||||
assert(idx < n_total_switches);
|
||||
return _hw_switch_defs[idx].type;
|
||||
}
|
||||
|
||||
uint32_t switchState(uint8_t pos)
|
||||
{
|
||||
assert(pos < n_total_switches * 3);
|
||||
|
||||
div_t qr = div(pos, 3);
|
||||
int state = switchesStates[qr.quot];
|
||||
switch (qr.rem) {
|
||||
case SWITCH_HW_UP:
|
||||
return state < 0;
|
||||
case SWITCH_HW_DOWN:
|
||||
return state > 0;
|
||||
default:
|
||||
return state == 0;
|
||||
}
|
||||
}
|
||||
|
||||
SwitchHwPos switchGetPosition(uint8_t idx)
|
||||
{
|
||||
assert(idx < n_total_switches);
|
||||
idx = get_switch_index(cat, idx);
|
||||
|
||||
if (switchesStates[idx] < 0)
|
||||
return SWITCH_HW_UP;
|
||||
@@ -127,3 +71,27 @@ SwitchHwPos switchGetPosition(uint8_t idx)
|
||||
else
|
||||
return SWITCH_HW_DOWN;
|
||||
}
|
||||
|
||||
const char* boardSwitchGetName(uint8_t cat, uint8_t idx)
|
||||
{
|
||||
idx = get_switch_index(cat, idx);
|
||||
return _switch_defs[idx].name;
|
||||
}
|
||||
|
||||
SwitchHwType boardSwitchGetType(uint8_t cat, uint8_t idx)
|
||||
{
|
||||
idx = get_switch_index(cat, idx);
|
||||
return _switch_defs[idx].type;
|
||||
}
|
||||
|
||||
uint8_t boardGetMaxSwitches() { return n_switches; }
|
||||
uint8_t boardGetMaxFctSwitches() { return n_fct_switches; }
|
||||
|
||||
swconfig_t boardSwitchGetDefaultConfig() { return _switch_default_config; }
|
||||
|
||||
switch_display_pos_t switchGetDisplayPosition(uint8_t idx)
|
||||
{
|
||||
if (idx >= DIM(_switch_display)) return {0, 0};
|
||||
|
||||
return _switch_display[idx];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user