Skip to content

Commit

Permalink
feat: flex ADC input
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Aug 5, 2023
1 parent 659a25a commit dbe99ac
Show file tree
Hide file tree
Showing 72 changed files with 856 additions and 575 deletions.
72 changes: 13 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,34 @@

#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;
}
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 @@ -480,11 +483,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 @@ -145,14 +145,14 @@ static inline void check_struct()
CHKSIZE(ModelData, 6706);
#elif defined(PCBHORUS)
#if defined(PCBX10)
CHKSIZE(RadioData, 916);
CHKSIZE(RadioData, 904);
CHKSIZE(ModelData, 15607);
#else
CHKSIZE(RadioData, 916);
CHKSIZE(RadioData, 904);
CHKSIZE(ModelData, 15607);
#endif
#elif defined(PCBNV14)
CHKSIZE(RadioData, 916);
CHKSIZE(RadioData, 904);
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 @@ -930,6 +930,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 @@ -342,7 +342,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 @@ -589,7 +589,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 @@ -896,7 +896,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 @@ -1071,7 +1071,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 @@ -1085,7 +1085,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 @@ -1099,7 +1099,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
4 changes: 2 additions & 2 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
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 @@ -534,7 +534,7 @@ void menuModelSetup(event_t event)
SW_WARN_ROWS, // Switch warning
POT_WARN_ROWS, // Pot warning

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 @@ -968,7 +968,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 @@ -1002,8 +1002,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 @@ -1015,7 +1015,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
4 changes: 2 additions & 2 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
36 changes: 26 additions & 10 deletions radio/src/gui/colorlcd/hw_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,14 @@ HWPots::HWPots(Window* parent) : FormWindow(parent, rect_t{})
FlexGridLayout grid(col_three_dsc, row_dsc, 2);
setFlexLayout();

auto max_pots = adcGetMaxInputs(ADC_INPUT_POT);
auto max_pots = adcGetMaxInputs(ADC_INPUT_FLEX);
for (int i = 0; i < max_pots; i++) {
// TODO: check initialised ADC inputs instead!

// Display EX3 & EX4 (= last two pots) only when FlySky gimbals are present
// TODO: use input disabled mask instead
// #if !defined(SIMU) && defined(RADIO_FAMILY_T16)
// if (!globalData.flyskygimbals && (i >= (NUM_POTS - 2))) continue;
// #endif
auto line = newLine(&grid);
new StaticText(line, rect_t{}, adcGetInputLabel(ADC_INPUT_POT, i), 0,
new StaticText(line, rect_t{}, adcGetInputLabel(ADC_INPUT_FLEX, i), 0,
COLOR_THEME_PRIMARY1);

auto box = new FormWindow(line, rect_t{});
Expand All @@ -93,9 +90,9 @@ HWPots::HWPots(Window* parent) : FormWindow(parent, rect_t{})
auto box_obj = box->getLvObj();
lv_obj_set_style_flex_cross_place(box_obj, LV_FLEX_ALIGN_CENTER, 0);

new HWInputEdit(box, (char*)analogGetCustomLabel(ADC_INPUT_POT, i), LEN_ANA_NAME);
new HWInputEdit(box, (char*)analogGetCustomLabel(ADC_INPUT_FLEX, i), LEN_ANA_NAME);
new Choice(
box, rect_t{}, STR_POTTYPES, POT_NONE, POT_SLIDER_WITH_DETENT,
box, rect_t{}, STR_POTTYPES, FLEX_NONE, FLEX_SWITCH,
[=]() -> int {
return bfGet<potconfig_t>(g_eeGeneral.potsConfig, POT_CFG_BITS * i,
POT_CFG_BITS);
Expand All @@ -120,7 +117,7 @@ class SwitchDynamicLabel : public StaticText

std::string label()
{
std::string str(switchGetName(index));
std::string str(switchGetCanonicalName(index));
return str + getSwitchPositionSymbol(lastpos);
}

Expand Down Expand Up @@ -154,11 +151,30 @@ HWSwitches::HWSwitches(Window* parent) : FormWindow(parent, rect_t{})
FlexGridLayout grid(col_three_dsc, row_dsc, 2);
setFlexLayout();

for (int i = 0; i < switchGetMaxSwitches(); i++) {
auto max_switches = switchGetMaxSwitches();
for (int i = 0; i < max_switches; i++) {
auto line = newLine(&grid);
new SwitchDynamicLabel(line, i);

new HWInputEdit(line, (char*)switchGetCustomName(i), LEN_SWITCH_NAME);
auto box = new FormWindow(line, rect_t{});
box->setFlexLayout(LV_FLEX_FLOW_ROW, lv_dpx(4));

auto box_obj = box->getLvObj();
lv_obj_set_style_flex_cross_place(box_obj, LV_FLEX_ALIGN_CENTER, 0);

new HWInputEdit(box, (char*)switchGetCustomName(i), LEN_SWITCH_NAME);
if (switchIsFlex(i)) {
auto channel = new Choice(
box, rect_t{}, 0, adcGetMaxInputs(ADC_INPUT_FLEX) - 1,
[=]() -> int { return switchGetFlexConfig(i); },
[=](int newValue) { switchConfigFlex(i, newValue); });
channel->setAvailableHandler(
[=](int val) { return POT_CONFIG(val) == FLEX_SWITCH; });
channel->setTextHandler([=](int val) -> std::string {
return adcGetInputLabel(ADC_INPUT_FLEX, val);
});
}

new Choice(
line, rect_t{}, STR_SWTYPES, SWITCH_NONE, switchGetMaxType(i),
[=]() -> int { return SWITCH_CONFIG(i); },
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/layouts/sliders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ MainViewSlider::MainViewSlider(Window* parent, const rect_t& rect,
void MainViewSlider::checkEvents()
{
Window::checkEvents();
auto pot_idx = adcGetInputOffset(ADC_INPUT_POT) + idx;
auto pot_idx = adcGetInputOffset(ADC_INPUT_FLEX) + idx;
int16_t newValue = calibratedAnalogs[pot_idx];
if (value != newValue) {
value = newValue;
Expand Down
Loading

0 comments on commit dbe99ac

Please sign in to comment.