Skip to content

Commit

Permalink
feat: More flexible ADC inputs (#3870)
Browse files Browse the repository at this point in the history
Co-authored-by: 3djc <[email protected]>
Co-authored-by: Peter Feerick <[email protected]>
Co-authored-by: philmoz <[email protected]>
  • Loading branch information
4 people authored Nov 2, 2023
1 parent f5593c4 commit b48e070
Show file tree
Hide file tree
Showing 78 changed files with 1,283 additions and 765 deletions.
73 changes: 14 additions & 59 deletions radio/src/boards/generic_stm32/switches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,80 +32,35 @@

#include <stdlib.h>

void switchInit()
void boardInitSwitches()
{
_init_switches();
}

swconfig_t switchGetDefaultConfig()
SwitchHwPos boardSwitchGetPosition(uint8_t cat, uint8_t idx)
{
return _switch_default_config;
return stm32_switch_get_position(&_switch_offsets[cat][idx]);
}

switch_display_pos_t switchGetDisplayPosition(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)
const char* boardSwitchGetName(uint8_t cat, uint8_t idx)
{
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;
return _switch_offsets[cat][idx].name;
}

uint8_t switchGetMaxFctSwitches()
SwitchHwType boardSwitchGetType(uint8_t cat, uint8_t idx)
{
return n_fct_switches;
return _switch_offsets[cat][idx].type;
}

// returns state (0 / 1) of a specific switch position
uint32_t switchState(uint8_t pos_idx)
{
auto d = div(pos_idx, 3);
if (d.quot >= n_total_switches) return 0;
return stm32_switch_get_state(&_switch_defs[d.quot], (SwitchHwPos)d.rem);
}
uint8_t boardGetMaxSwitches() { return n_switches; }
uint8_t boardGetMaxFctSwitches() { return n_fct_switches; }

SwitchHwPos switchGetPosition(uint8_t idx)
{
if (idx >= n_total_switches) return SWITCH_HW_UP;
return stm32_switch_get_position(&_switch_defs[idx]);
}
swconfig_t boardSwitchGetDefaultConfig() { return _switch_default_config; }

const char* switchGetName(uint8_t idx)
switch_display_pos_t switchGetDisplayPosition(uint8_t idx)
{
if (idx >= n_total_switches) return "";
return _switch_defs[idx].name;
}
// TODO: find a solution for FLEX switches so they can be displayed on main view
if (idx >= DIM(_switch_display)) return {0, 0};

SwitchHwType switchGetHwType(uint8_t idx)
{
if (idx >= n_total_switches) return SWITCH_HW_2POS;
return _switch_defs[idx].type;
return _switch_display[idx];
}
16 changes: 7 additions & 9 deletions radio/src/dataconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,22 @@ enum PotsWarnMode {

#if defined(COLORLCD)
#define MAX_POTS 16
#define MAX_AXIS 2
#else
#define MAX_POTS 8
#define MAX_AXIS 0
#endif

#define MAX_VBAT 1
#define MAX_RTC_BAT 1

#define MAX_ANALOG_INPUTS (MAX_STICKS + MAX_POTS + MAX_AXIS + MAX_VBAT + MAX_RTC_BAT)
#define MAX_CALIB_ANALOG_INPUTS (MAX_STICKS + MAX_POTS + MAX_AXIS)
#define MAX_ANALOG_INPUTS (MAX_STICKS + MAX_POTS + MAX_VBAT + MAX_RTC_BAT)
#define MAX_CALIB_ANALOG_INPUTS (MAX_STICKS + MAX_POTS)

#define MAX_SWITCHES 20

#if !defined(MAX_FLEX_SWITCHES)
#define MAX_FLEX_SWITCHES 0
#endif

#if defined(RADIO_T20)
#define MAX_TRIMS 8
#else
Expand Down Expand Up @@ -482,11 +485,6 @@ enum MixSources {
MIXSRC_FIRST_POT SKIP,
MIXSRC_LAST_POT SKIP = MIXSRC_FIRST_POT + MAX_POTS - 1,

#if MAX_AXIS > 0
MIXSRC_FIRST_AXIS SKIP,
MIXSRC_LAST_AXIS SKIP = MIXSRC_FIRST_AXIS + MAX_AXIS - 1,
#endif

#if defined(IMU)
MIXSRC_TILT_X,
MIXSRC_TILT_Y,
Expand Down
6 changes: 3 additions & 3 deletions radio/src/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ static inline void check_struct()
CHKSIZE(ModelData, 6706);
#elif defined(PCBHORUS)
#if defined(PCBX10)
CHKSIZE(RadioData, 848);
CHKSIZE(RadioData, 836);
CHKSIZE(ModelData, 15607);
#else
CHKSIZE(RadioData, 848);
CHKSIZE(RadioData, 836);
CHKSIZE(ModelData, 15607);
#endif
#elif defined(PCBNV14)
CHKSIZE(RadioData, 848);
CHKSIZE(RadioData, 836);
CHKSIZE(ModelData, 15463);
#endif

Expand Down
1 change: 1 addition & 0 deletions radio/src/datastructs_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ PACK(struct RadioData {
CUST_ARRAY(slidersConfig, struct_sliderConfig, MAX_POTS, nullptr);
potconfig_t potsConfig ARRAY(4,struct_potConfig,nullptr);
swconfig_t switchConfig ARRAY(2,struct_switchConfig,nullptr);
CUST_ARRAY(flexSwitches, struct_flexSwitch, MAX_FLEX_SWITCHES, flex_sw_valid);

EXTRA_GENERAL_FIELDS

Expand Down
12 changes: 6 additions & 6 deletions radio/src/gui/128x64/model_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ inline uint8_t TIMER_ROW(uint8_t timer, uint8_t value)
return HIDDEN_ROW;
}

#define POT_WARN_ROWS PREFLIGHT_ROW(((g_model.potsWarnMode) ? adcGetMaxInputs(ADC_INPUT_POT) : (uint8_t)0))
#define POT_WARN_ROWS PREFLIGHT_ROW(((g_model.potsWarnMode) ? adcGetMaxInputs(ADC_INPUT_FLEX) : (uint8_t)0))

#define TIMER_ROWS(x) \
1, TIMER_ROW(x,0), \
Expand Down Expand Up @@ -592,7 +592,7 @@ void menuModelSetup(event_t event)
PREFLIGHT_ROW(0), // Custom position for throttle warning value
WARN_ROWS

uint8_t(NAVIGATION_LINE_BY_LINE | (adcGetInputOffset(ADC_INPUT_POT + 1) - 1)), // Center beeps
uint8_t(NAVIGATION_LINE_BY_LINE | (adcGetInputOffset(ADC_INPUT_FLEX + 1) - 1)), // Center beeps

0, // ADC Jitter filter

Expand Down Expand Up @@ -899,7 +899,7 @@ void menuModelSetup(event_t event)
if (attr)
CHECK_INCDEC_MODELVAR_ZERO_CHECK(
event, g_model.thrTraceSrc,
adcGetMaxInputs(ADC_INPUT_POT) + MAX_OUTPUT_CHANNELS,
adcGetMaxInputs(ADC_INPUT_FLEX) + MAX_OUTPUT_CHANNELS,
isThrottleSourceAvailable);

uint8_t idx = throttleSource2Source(g_model.thrTraceSrc);
Expand Down Expand Up @@ -1078,7 +1078,7 @@ void menuModelSetup(event_t event)
}
if (g_model.potsWarnMode) {
coord_t x = MODEL_SETUP_2ND_COLUMN+28;
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_POT);
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_FLEX);
for (int i = 0; i < max_pots; ++i) {

if (!IS_POT_SLIDER_AVAILABLE(i)) {
Expand All @@ -1092,7 +1092,7 @@ void menuModelSetup(event_t event)
flags |= INVERS;
}
if (max_pots > 3) {
lcdDrawText(x, y, getAnalogShortLabel(adcGetInputOffset(ADC_INPUT_POT) + i), flags);
lcdDrawText(x, y, getAnalogShortLabel(adcGetInputOffset(ADC_INPUT_FLEX) + i), flags);
x = lcdNextPos + 1;
}
else {
Expand All @@ -1106,7 +1106,7 @@ void menuModelSetup(event_t event)

case ITEM_MODEL_SETUP_BEEP_CENTER: {
lcdDrawTextAlignedLeft(y, STR_BEEPCTR);
uint8_t input_max = adcGetMaxInputs(ADC_INPUT_MAIN) + adcGetMaxInputs(ADC_INPUT_POT);
uint8_t input_max = adcGetMaxInputs(ADC_INPUT_MAIN) + adcGetMaxInputs(ADC_INPUT_FLEX);
for (uint8_t i = 0; i < input_max; i++) {
coord_t x = MODEL_SETUP_2ND_COLUMN + i*FW;
LcdFlags flags = 0;
Expand Down
10 changes: 5 additions & 5 deletions radio/src/gui/128x64/view_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ void drawExternalAntennaAndRSSI()

void drawPotsBars()
{
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_POT);
uint8_t offset = adcGetInputOffset(ADC_INPUT_POT);
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_FLEX);
uint8_t offset = adcGetInputOffset(ADC_INPUT_FLEX);
uint8_t configured_pots = 0;

for (uint8_t i = 0; i < max_pots; i++) {
Expand Down Expand Up @@ -567,14 +567,14 @@ void menuMainView(event_t event)
uint8_t configured_switches = 0;

for (uint8_t i = 0; i < switches; i++) {
if (SWITCH_EXISTS(i)) {
if (SWITCH_EXISTS(i) && !switchIsFlex(i)) {
configured_switches ++;
}
}

if (configured_switches < 9) {
for (int i = 0; i < switches; ++i) {
if (SWITCH_EXISTS(i)) {
if (SWITCH_EXISTS(i) && !switchIsFlex(i)) {
auto switch_display = switchGetDisplayPosition(i);
if (switch_display.row >= 3) {
drawSmallSwitch(switch_display.col == 0 ? 28 : 16 * FW + 1,
Expand All @@ -594,7 +594,7 @@ void menuMainView(event_t event)
}
else {
for (int i = 0; i < switches; ++i) {
if (SWITCH_EXISTS(i)) {
if (SWITCH_EXISTS(i) && !switchIsFlex(i)) {
auto switch_display = switchGetDisplayPosition(i);
coord_t x = (switch_display.col == 0 ? 8 : 96) + switch_display.row * 5;
drawSmallSwitch(x, 5 * FH + 1, 4, i);
Expand Down
10 changes: 5 additions & 5 deletions radio/src/gui/212x64/model_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ void menuModelSetup(event_t event)
SW_WARN_ROWS, // ITEM_MODEL_SETUP_SWITCHES_WARNING1
POT_WARN_ROWS, // ITEM_MODEL_SETUP_POTS_WARNING

uint8_t(NAVIGATION_LINE_BY_LINE | (adcGetInputOffset(ADC_INPUT_POT + 1) - 1)), // ITEM_MODEL_SETUP_BEEP_CENTER
uint8_t(NAVIGATION_LINE_BY_LINE | (adcGetInputOffset(ADC_INPUT_FLEX + 1) - 1)), // ITEM_MODEL_SETUP_BEEP_CENTER

0, // ITEM_MODEL_SETUP_USE_JITTER_FILTER

Expand Down Expand Up @@ -977,7 +977,7 @@ void menuModelSetup(event_t event)
}
if (g_model.potsWarnMode) {
coord_t x = MODEL_SETUP_2ND_COLUMN+28;
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_POT);
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_FLEX);
for (int i = 0; i < max_pots; ++i) {

if (!IS_POT_SLIDER_AVAILABLE(i)) {
Expand Down Expand Up @@ -1011,8 +1011,8 @@ void menuModelSetup(event_t event)

case ITEM_MODEL_SETUP_BEEP_CENTER: {
lcdDrawTextAlignedLeft(y, STR_BEEPCTR);
uint8_t pot_offset = adcGetInputOffset(ADC_INPUT_POT);
uint8_t max_inputs = adcGetMaxInputs(ADC_INPUT_MAIN) + adcGetMaxInputs(ADC_INPUT_POT);
uint8_t pot_offset = adcGetInputOffset(ADC_INPUT_FLEX);
uint8_t max_inputs = adcGetMaxInputs(ADC_INPUT_MAIN) + adcGetMaxInputs(ADC_INPUT_FLEX);
coord_t x = MODEL_SETUP_2ND_COLUMN;
for (uint8_t i = 0; i < max_inputs; i++) {
if ( i >= pot_offset && IS_POT_MULTIPOS(i - pot_offset) ) {
Expand All @@ -1024,7 +1024,7 @@ void menuModelSetup(event_t event)
flags = BLINK | INVERS;
else if (ANALOG_CENTER_BEEP(i) || (attr && CURSOR_ON_LINE()))
flags = INVERS;
if (adcGetMaxInputs(ADC_INPUT_POT) > 4 || i < pot_offset) {
if (adcGetMaxInputs(ADC_INPUT_FLEX) > 4 || i < pot_offset) {
lcdDrawText(x, y, getAnalogShortLabel(i), flags);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions radio/src/gui/212x64/radio_calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
void drawPotsBars()
{
// Optimization by Mike Blandford
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_POT);
uint8_t offset = adcGetInputOffset(ADC_INPUT_POT);
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_FLEX);
uint8_t offset = adcGetInputOffset(ADC_INPUT_FLEX);

for (uint8_t x = LCD_W / 2 - max_pots / 2 * BAR_SPACING + BAR_SPACING / 2,
i = 0;
Expand Down
8 changes: 4 additions & 4 deletions radio/src/gui/212x64/view_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ static const coord_t _pot_slots[] = {
void drawSliders()
{
uint8_t slot_idx = 0;
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_POT);
uint8_t offset = adcGetInputOffset(ADC_INPUT_POT);
uint8_t max_pots = adcGetMaxInputs(ADC_INPUT_FLEX);
uint8_t offset = adcGetInputOffset(ADC_INPUT_FLEX);

for (uint8_t i = 0; i < max_pots; i++) {

Expand Down Expand Up @@ -534,7 +534,7 @@ void menuMainView(event_t event)
uint8_t switches = switchGetMaxSwitches();
if (getSwitchCount() > 16) { // beware, there is a desired col/row swap in this special mode
for (int i = 0; i < switches; ++i) {
if (SWITCH_EXISTS(i)) {
if (SWITCH_EXISTS(i) && !switchIsFlex(i)) {
auto switch_display = switchGetDisplayPosition(i);
if (g_model.view == VIEW_INPUTS) {
coord_t x = 50 + (switch_display.row % 5) * 4 +
Expand All @@ -552,7 +552,7 @@ void menuMainView(event_t event)
else {
coord_t shiftright = switchGetMaxRow(1) < 4 ? 20 : 0;
for (int i = 0; i < switches; ++i) {
if (SWITCH_EXISTS(i)) {
if (SWITCH_EXISTS(i) && !switchIsFlex(i)) {
auto switch_display = switchGetDisplayPosition(i);
if (g_model.view == VIEW_INPUTS) {
coord_t x = (switch_display.col == 0 ? 50 : 125) +
Expand Down
Loading

0 comments on commit b48e070

Please sign in to comment.