feat(radio): refactor of switch handling to better support customisable switches (#6095)

Co-authored-by: philmoz <phil.a.mitchell@gmail.com>
Co-authored-by: 3djc <3djc@gh.com>
Co-authored-by: elecpower <elecpower@users.noreply.github.com>
This commit is contained in:
philmoz
2025-07-28 12:59:22 +10:00
committed by GitHub
co-authored by philmoz 3djc elecpower
parent 28833f42e6
commit 0c8c39b134
155 changed files with 4538 additions and 2306 deletions
+24 -74
View File
@@ -19,19 +19,24 @@
* GNU General Public License for more details.
*/
#include "bsp_io.h"
#include "hal/switch_driver.h"
#include "drivers/pca95xx.h"
#include "stm32_i2c_driver.h"
#include "timers_driver.h"
#include "delays_driver.h"
#include "stm32_ws2812.h"
#include "stm32_switch_driver.h"
#include "os/async.h"
#include "os/timer.h"
#include "myeeprom.h"
#define IO_INT_GPIO GPIO_PIN(GPIOE, 14)
#define IO_RESET_GPIO GPIO_PIN(GPIOE, 15)
extern const stm32_switch_t* boardGetSwitchDef(uint8_t idx);
struct bsp_io_expander {
pca95xx_t exp;
uint32_t mask;
@@ -79,8 +84,8 @@ static void _poll_switches(void *param1, uint32_t trigger_source)
timer_reset(&_poll_timer);
}
bsp_io_read_switches();
bsp_io_read_fs_switches();
_read_io_expander(&_io_switches);
_read_io_expander(&_io_fs_switches);
}
static void _io_int_handler()
@@ -126,90 +131,41 @@ int bsp_io_init()
gpio_init(IO_RESET_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
gpio_set(IO_RESET_GPIO);
bsp_io_read_switches();
bsp_io_read_fs_switches();
_read_io_expander(&_io_switches);
_read_io_expander(&_io_fs_switches);
start_poll_timer();
return 0;
}
uint32_t bsp_io_read_switches()
{
return _read_io_expander(&_io_switches);
}
uint32_t bsp_io_read_fs_switches()
{
return _read_io_expander(&_io_fs_switches);
}
void boardInitSwitches()
{
bsp_io_init();
}
struct bsp_io_sw_def {
uint32_t pin_high;
uint32_t pin_low;
};
static constexpr uint32_t RGB_OFFSET = (1 << 16); // first after bspio pins
static uint16_t soft2POSLogicalState = 0xFFFF;
static const bsp_io_sw_def _switch_defs[] = {
{ SWITCH_A, RGB_OFFSET + 7 },
{ SWITCH_B_H, SWITCH_B_L },
{ SWITCH_C_H, SWITCH_C_L },
{ SWITCH_D, RGB_OFFSET + 6 },
{ SWITCH_E_H, SWITCH_E_L },
{ SWITCH_F_H, SWITCH_F_L },
{ SWITCH_G, 0 },
{ SWITCH_H, 0 },
};
static SwitchHwPos _get_switch_pos(uint8_t idx)
{
static uint32_t oldState = 0;
SwitchHwPos pos = SWITCH_HW_UP;
const bsp_io_sw_def* def = &_switch_defs[idx];
const stm32_switch_t* def = boardGetSwitchDef(idx);
uint32_t state = _io_switches.state;
if (def->pin_low > RGB_OFFSET) {
// Potential soft 2pos
if ((SWITCH_CONFIG(idx) == SWITCH_TOGGLE)) {
if ((state & def->pin_high) == 0) {
pos = SWITCH_HW_DOWN;
}
}
else {
if (((state & def->pin_high) == 0) && ((state & def->pin_high) != (oldState & def->pin_high))) {
soft2POSLogicalState ^= def->pin_high;
}
if ((soft2POSLogicalState & def->pin_high) == 0) {
pos = SWITCH_HW_DOWN;
}
else {
pos = SWITCH_HW_UP;
}
}
if (pos == SWITCH_HW_UP) {
ws2812_set_color(def->pin_low - RGB_OFFSET, 0x0, 0x0, 0x0);
if (def->isCustomSwitch) {
if ((state & def->Pin_high) == 0) {
return SWITCH_HW_DOWN;
} else {
ws2812_set_color(def->pin_low - RGB_OFFSET, 0xFF, 0xFF, 0xFF);
return SWITCH_HW_UP;
}
}
else if (!def->pin_low) {
else if (!def->Pin_low) {
// 2POS switch
if ((state & def->pin_high) == 0) {
if ((state & def->Pin_high) == 0) {
pos = SWITCH_HW_DOWN;
}
} else {
bool hi = state & def->pin_high;
bool lo = state & def->pin_low;
bool hi = state & def->Pin_high;
bool lo = state & def->Pin_low;
if (hi && lo) {
pos = SWITCH_HW_MID;
@@ -218,29 +174,23 @@ static SwitchHwPos _get_switch_pos(uint8_t idx)
}
}
if (idx == switchGetMaxSwitches() - 1)
oldState = state;
return pos;
}
static SwitchHwPos _get_fs_switch_pos(uint8_t idx)
{
const stm32_switch_t* def = boardGetSwitchDef(idx);
uint32_t state = _io_fs_switches.state;
if ((state & (1 << idx)) == 0) {
if ((state & def->Pin_high) == 0) {
return SWITCH_HW_DOWN;
} else {
return SWITCH_HW_UP;
}
}
SwitchHwPos boardSwitchGetPosition(SwitchCategory cat, uint8_t idx)
SwitchHwPos boardSwitchGetPosition(uint8_t idx)
{
if (cat == SWITCH_PHYSICAL) {
if (idx < 8)
return _get_switch_pos(idx);
} else if (cat == SWITCH_FUNCTION){
return _get_fs_switch_pos(idx);
}
return SWITCH_HW_UP;
return _get_fs_switch_pos(idx);
}
-59
View File
@@ -1,59 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "drivers/pca95xx.h"
// Port expander 1 (0x74)
#define SWITCH_H PCA95XX_PIN_0
#define SWITCH_D PCA95XX_PIN_1
#define SWITCH_F_H PCA95XX_PIN_3
#define SWITCH_F_L PCA95XX_PIN_2
#define SWITCH_C_L PCA95XX_PIN_4
#define SWITCH_C_H PCA95XX_PIN_5
#define SWITCH_B_H PCA95XX_PIN_10
#define SWITCH_B_L PCA95XX_PIN_11
#define SWITCH_E_H PCA95XX_PIN_12
#define SWITCH_E_L PCA95XX_PIN_13
#define SWITCH_A PCA95XX_PIN_14
#define SWITCH_G PCA95XX_PIN_15
#define IO_INT_GPIO GPIO_PIN(GPIOE, 14)
#define IO_RESET_GPIO GPIO_PIN(GPIOE, 15)
// Port expander 1 (0x75)
#define FSSWITCH_1 PCA95XX_PIN_0
#define FSSWITCH_2 PCA95XX_PIN_1
#define FSSWITCH_3 PCA95XX_PIN_2
#define FSSWITCH_4 PCA95XX_PIN_3
#define FSSWITCH_5 PCA95XX_PIN_4
#define FSSWITCH_6 PCA95XX_PIN_5
int bsp_io_init();
uint32_t bsp_io_read_switches();
uint32_t bsp_io_read_fs_switches();