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:
co-authored by
philmoz
3djc
elecpower
parent
28833f42e6
commit
0c8c39b134
@@ -1,6 +1,5 @@
|
||||
from switch_config import switch_cfg_by_target
|
||||
|
||||
|
||||
class Switch:
|
||||
TYPE_2POS = "2POS"
|
||||
TYPE_3POS = "3POS"
|
||||
@@ -12,7 +11,7 @@ class Switch:
|
||||
self.flags = flags
|
||||
self.inverted = False
|
||||
self.default = None
|
||||
self.display = []
|
||||
self.display = None
|
||||
|
||||
|
||||
class Switch2POS(Switch):
|
||||
@@ -86,6 +85,8 @@ def parse_switches(target, hw_defs, adc_parser):
|
||||
adc_input_name = f"SW{s}"
|
||||
custom = f"SWITCHES_{s}_"
|
||||
|
||||
cfs_idx = f"SWITCHES_{s}_CFS_IDX"
|
||||
|
||||
switch = None
|
||||
if reg in hw_defs:
|
||||
# 2POS switch
|
||||
@@ -111,11 +112,18 @@ def parse_switches(target, hw_defs, adc_parser):
|
||||
if inverted in hw_defs:
|
||||
switch.inverted = True
|
||||
|
||||
if cfs_idx in hw_defs:
|
||||
switch.is_cfs = True
|
||||
switch.cfs_idx = hw_defs[cfs_idx]
|
||||
else:
|
||||
switch.is_cfs = False
|
||||
|
||||
cfg = switch_cfg_by_target(target, name)
|
||||
if cfg:
|
||||
switch.default = cfg.get("default")
|
||||
switch.display = cfg.get("display")
|
||||
# eprint(switch.default)
|
||||
else:
|
||||
switch.default = "NONE"
|
||||
|
||||
switches.append(switch)
|
||||
|
||||
@@ -124,11 +132,9 @@ def parse_switches(target, hw_defs, adc_parser):
|
||||
if f_sw_marker in hw_defs:
|
||||
switch = find_switch(hw_defs[f_sw_marker])
|
||||
if switch:
|
||||
switch.type = "FSWITCH"
|
||||
switch.name = f"SW{i}"
|
||||
else:
|
||||
switch = SwitchCustom(f"SW{i}", Switch.TYPE_2POS)
|
||||
switch.type = "FSWITCH"
|
||||
switches.append(switch)
|
||||
|
||||
return switches
|
||||
return switches
|
||||
|
||||
@@ -1,29 +1,26 @@
|
||||
{% set regular_switches = switches | selectattr('type','!=','FSWITCH') | list %}
|
||||
{% set function_switches = switches | selectattr('type','==','FSWITCH') | list %}
|
||||
//
|
||||
// WARNING: DO NOT EDIT THIS FILE
|
||||
// This file has been generated from the target's JSON hardware description
|
||||
//
|
||||
|
||||
{% set regular_switches = switches | selectattr('is_cfs','==',False) | list %}
|
||||
{% set display_switches = regular_switches | selectattr('display','!=',None) | list %}
|
||||
|
||||
const hw_switch_def _switch_defs[] = {
|
||||
{% for switch in regular_switches %}
|
||||
{ "{{ switch.name }}", SWITCH_HW_{{ switch.type }} },
|
||||
{% endfor %}
|
||||
{% for switch in function_switches %}
|
||||
{ "{{ switch.name }}", SWITCH_HW_2POS },
|
||||
{% for switch in switches %}
|
||||
{% if switch.is_cfs %}
|
||||
{ "{{ switch.name }}", SWITCH_HW_{{ switch.type }}, SWITCH_{{ switch.default }}, true, {{ switch.cfs_idx }} },
|
||||
{% else %}
|
||||
{ "{{ switch.name }}", SWITCH_HW_{{ switch.type }}, SWITCH_{{ switch.default }} },
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
};
|
||||
|
||||
constexpr uint8_t n_switches = {{ regular_switches | count }};
|
||||
constexpr uint8_t n_fct_switches = {{ function_switches | count }};
|
||||
constexpr uint8_t n_total_switches = {{ switches | count }};
|
||||
|
||||
constexpr swconfig_t _switch_default_config = (swconfig_t)0
|
||||
{% for sw in regular_switches %}
|
||||
{% if sw.default %}
|
||||
| ((swconfig_t)SWITCH_{{ sw.default }} << ({{ loop.index0 }} * SW_CFG_BITS))
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
;
|
||||
constexpr uint8_t n_switches = {{ switches | count }};
|
||||
|
||||
{% if display_switches | count > 0 %}
|
||||
const switch_display_pos_t _switch_display[] = {
|
||||
{% for sw in regular_switches %}
|
||||
{% for sw in switches %}
|
||||
{% if sw.display %}
|
||||
{ {{ sw.display[0] }}, {{ sw.display[1] }} },
|
||||
{% else %}
|
||||
@@ -31,3 +28,4 @@ const switch_display_pos_t _switch_display[] = {
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
};
|
||||
{% endif %}
|
||||
@@ -3,57 +3,42 @@
|
||||
// This file has been generated from the target's JSON hardware description
|
||||
//
|
||||
|
||||
{% set regular_switches = switches | selectattr('type','!=','FSWITCH') | list %}
|
||||
{% set function_switches = switches | selectattr('type','==','FSWITCH') | list %}
|
||||
{% set regular_switches = switches | selectattr('is_cfs','==',False) | list %}
|
||||
{% set display_switches = regular_switches | selectattr('display','!=',None) | list %}
|
||||
|
||||
static const stm32_switch_t _switch_defs[] = {
|
||||
{% for switch in regular_switches %}
|
||||
{% for switch in switches %}
|
||||
{% if switch.type == '2POS' %}
|
||||
{
|
||||
"{{ switch.name }}",
|
||||
{{ switch.gpio or "nullptr" }},
|
||||
{{ switch.pin or 0 }},
|
||||
{{ switch.gpio or "nullptr" }}, {{ switch.pin or 0 }},
|
||||
nullptr, 0,
|
||||
SWITCH_HW_2POS, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}
|
||||
SWITCH_HW_2POS, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}, SWITCH_{{ switch.default }},
|
||||
{% if switch.is_cfs %}
|
||||
true, {{ switch.cfs_idx }}
|
||||
{% endif %}
|
||||
},
|
||||
{% elif switch.type == '3POS' %}
|
||||
{
|
||||
"{{ switch.name }}",
|
||||
{{ switch.gpio_high or "nullptr" }},
|
||||
{{ switch.pin_high or 0 }},
|
||||
{{ switch.gpio_low or "nullptr" }},
|
||||
{{ switch.pin_low or 0 }},
|
||||
SWITCH_HW_3POS, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}
|
||||
{{ switch.gpio_high or "nullptr" }}, {{ switch.pin_high or 0 }},
|
||||
{{ switch.gpio_low or "nullptr" }}, {{ switch.pin_low or 0 }},
|
||||
SWITCH_HW_3POS, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}, SWITCH_{{ switch.default }},
|
||||
},
|
||||
{% elif switch.type == 'ADC' %}
|
||||
{
|
||||
"{{ switch.name }}",
|
||||
nullptr, {{ adc_index[switch.adc_input] }},
|
||||
nullptr, 0,
|
||||
SWITCH_HW_ADC, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}
|
||||
SWITCH_HW_ADC, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}, SWITCH_{{ switch.default }},
|
||||
},
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% for switch in function_switches %}
|
||||
{
|
||||
"{{ switch.name }}",
|
||||
{{ switch.gpio or "nullptr" }},
|
||||
{{ switch.pin or 0 }},
|
||||
nullptr, 0,
|
||||
SWITCH_HW_2POS, {{ 'SWITCH_HW_INVERTED' if switch.inverted else 0 }}
|
||||
},
|
||||
{% endfor %}
|
||||
};
|
||||
|
||||
static const stm32_switch_t* const _switch_offsets[] = {
|
||||
_switch_defs,
|
||||
{% if function_switches %}
|
||||
_switch_defs + {{ regular_switches | count }},
|
||||
{% endif %}
|
||||
};
|
||||
|
||||
static inline void _init_switches()
|
||||
{
|
||||
{% if switch_gpios|count > 0 %}
|
||||
LL_GPIO_InitTypeDef pinInit;
|
||||
LL_GPIO_StructInit(&pinInit);
|
||||
pinInit.Mode = LL_GPIO_MODE_INPUT;
|
||||
@@ -65,25 +50,19 @@ static inline void _init_switches()
|
||||
LL_GPIO_Init({{ sw_gpio }}, &pinInit);
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
}
|
||||
|
||||
constexpr uint8_t n_switches = {{ regular_switches | count }};
|
||||
constexpr uint8_t n_fct_switches = {{ function_switches | count }};
|
||||
|
||||
constexpr swconfig_t _switch_default_config = (swconfig_t)0
|
||||
{% for sw in regular_switches %}
|
||||
{% if sw.default %}
|
||||
| ((swconfig_t)SWITCH_{{ sw.default }} << ({{ loop.index0 }} * SW_CFG_BITS))
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
;
|
||||
constexpr uint8_t n_switches = {{ switches | count }};
|
||||
|
||||
{% if display_switches | count > 0 %}
|
||||
const switch_display_pos_t _switch_display[] = {
|
||||
{% for sw in regular_switches %}
|
||||
{% for sw in switches %}
|
||||
{% if sw.display %}
|
||||
{ {{ sw.display[0] }}, {{ sw.display[1] }} },
|
||||
{% else %}
|
||||
{ 0, 0 },
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
};
|
||||
};
|
||||
{% endif %}
|
||||
@@ -15,15 +15,22 @@ SWITCH_CONFIG = {
|
||||
},
|
||||
"gx12": {
|
||||
# left side
|
||||
"SA": {"default": "TOGGLE", "display": [0, 0]},
|
||||
"SB": {"default": "3POS", "display": [0, 1]},
|
||||
"SE": {"default": "3POS", "display": [0, 2]},
|
||||
"SG": {"default": "TOGGLE", "display": [0, 3]},
|
||||
"SA": {"default": "TOGGLE"},
|
||||
"SB": {"default": "3POS", "display": [0, 0]},
|
||||
"SE": {"default": "3POS", "display": [0, 1]},
|
||||
"SG": {"default": "TOGGLE", "display": [0, 2]},
|
||||
# right side
|
||||
"SC": {"default": "3POS", "display": [1, 1]},
|
||||
"SD": {"default": "TOGGLE", "display": [1, 0]},
|
||||
"SF": {"default": "3POS", "display": [1, 2]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 3]},
|
||||
"SC": {"default": "3POS", "display": [1, 0]},
|
||||
"SD": {"default": "TOGGLE"},
|
||||
"SF": {"default": "3POS", "display": [1, 1]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 2]},
|
||||
# custom switches
|
||||
"SI": {"default": "2POS"},
|
||||
"SJ": {"default": "2POS"},
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
"SM": {"default": "2POS"},
|
||||
"SN": {"default": "2POS"},
|
||||
},
|
||||
"commando8": {
|
||||
# left side
|
||||
@@ -90,6 +97,13 @@ SWITCH_CONFIG = {
|
||||
"SH": { "default": "TOGGLE" },
|
||||
"SI": { "default": "TOGGLE" },
|
||||
"SJ": { "default": "TOGGLE" },
|
||||
# custom switches
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
"SM": {"default": "2POS"},
|
||||
"SN": {"default": "2POS"},
|
||||
"SO": {"default": "2POS"},
|
||||
"SP": {"default": "2POS"},
|
||||
},
|
||||
"lr3pro": {
|
||||
# left side
|
||||
@@ -192,6 +206,13 @@ SWITCH_CONFIG = {
|
||||
"SD": {"default": "3POS"},
|
||||
"SE": {"default": "2POS"},
|
||||
"SF": {"default": "2POS"},
|
||||
# custom switches
|
||||
"SG": {"default": "2POS"},
|
||||
"SH": {"default": "2POS"},
|
||||
"SI": {"default": "2POS"},
|
||||
"SJ": {"default": "2POS"},
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
},
|
||||
"t16": {
|
||||
"SA": {"default": "3POS"},
|
||||
@@ -238,6 +259,13 @@ SWITCH_CONFIG = {
|
||||
"SB": {"default": "3POS", "display": [1, 0]},
|
||||
"SD": {"default": "TOGGLE", "display": [1, 1]},
|
||||
"SF": {"default": "NONE", "display": [1, 2]},
|
||||
# custom switches
|
||||
"SG": {"default": "2POS"},
|
||||
"SH": {"default": "2POS"},
|
||||
"SI": {"default": "2POS"},
|
||||
"SJ": {"default": "2POS"},
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
},
|
||||
"tprov2": {
|
||||
# left side
|
||||
@@ -250,6 +278,13 @@ SWITCH_CONFIG = {
|
||||
"SD": {"default": "TOGGLE", "display": [1, 1]},
|
||||
"SF": {"default": "2POS", "display": [1, 2]},
|
||||
"SH": {"default": "NONE", "display": [1, 3]},
|
||||
# custom switches
|
||||
"SI": {"default": "2POS"},
|
||||
"SJ": {"default": "2POS"},
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
"SM": {"default": "2POS"},
|
||||
"SN": {"default": "2POS"},
|
||||
},
|
||||
"tpros": {
|
||||
# left side
|
||||
@@ -272,6 +307,13 @@ SWITCH_CONFIG = {
|
||||
"SD": {"default": "TOGGLE", "display": [1, 1]},
|
||||
"SF": {"default": "2POS", "display": [1, 2]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 3]},
|
||||
# custom switches
|
||||
"SJ": {"default": "2POS"},
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
"SM": {"default": "2POS"},
|
||||
"SO": {"default": "2POS"},
|
||||
"SP": {"default": "2POS"},
|
||||
},
|
||||
"t20": {
|
||||
# left side
|
||||
@@ -285,7 +327,14 @@ SWITCH_CONFIG = {
|
||||
"SD": {"default": "3POS", "display": [1, 1]},
|
||||
"SF": {"default": "2POS", "display": [1, 2]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 3]},
|
||||
"SJ": {"default": "TOGGLE", "display": [1, 4]}
|
||||
"SJ": {"default": "TOGGLE", "display": [1, 4]},
|
||||
# custom switches
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
"SM": {"default": "2POS"},
|
||||
"SN": {"default": "2POS"},
|
||||
"SO": {"default": "2POS"},
|
||||
"SP": {"default": "2POS"},
|
||||
},
|
||||
"t20v2": {
|
||||
# left side
|
||||
@@ -299,7 +348,14 @@ SWITCH_CONFIG = {
|
||||
"SD": {"default": "3POS", "display": [1, 1]},
|
||||
"SF": {"default": "2POS", "display": [1, 2]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 3]},
|
||||
"SJ": {"default": "TOGGLE", "display": [1, 4]}
|
||||
"SJ": {"default": "TOGGLE", "display": [1, 4]},
|
||||
# custom switches
|
||||
"SK": {"default": "2POS"},
|
||||
"SL": {"default": "2POS"},
|
||||
"SM": {"default": "2POS"},
|
||||
"SN": {"default": "2POS"},
|
||||
"SO": {"default": "2POS"},
|
||||
"SP": {"default": "2POS"},
|
||||
},
|
||||
"tx12": {
|
||||
# left side
|
||||
@@ -351,7 +407,10 @@ SWITCH_CONFIG = {
|
||||
# right side
|
||||
"SC": {"default": "3POS", "display": [1, 0]},
|
||||
"SD": {"default": "3POS", "display": [1, 1]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 2]}
|
||||
"SH": {"default": "TOGGLE", "display": [1, 2]},
|
||||
# optional
|
||||
"SI": {"default": "NONE", "display": [0, 3]},
|
||||
"SJ": {"default": "NONE", "display": [0, 3]},
|
||||
},
|
||||
"x7access": {
|
||||
# left side
|
||||
@@ -361,7 +420,10 @@ SWITCH_CONFIG = {
|
||||
# right side
|
||||
"SC": {"default": "3POS", "display": [1, 0]},
|
||||
"SD": {"default": "3POS", "display": [1, 1]},
|
||||
"SH": {"default": "TOGGLE", "display": [1, 2]}
|
||||
"SH": {"default": "TOGGLE", "display": [1, 2]},
|
||||
# optional
|
||||
"SI": {"default": "NONE", "display": [0, 3]},
|
||||
"SJ": {"default": "NONE", "display": [0, 3]},
|
||||
},
|
||||
"x9d": {
|
||||
# left side
|
||||
|
||||
@@ -38,7 +38,7 @@ static const struct YamlNode {{ struct.name }}[] = {
|
||||
YAML_{{elmt.type|upper}}( "{{ max_len(elmt.name) }}", {{ elmt.bits }} ),
|
||||
{% endif %}
|
||||
{% elif elmt.type == 'enum' %}
|
||||
YAML_ENUM("{{ max_len(elmt.name) }}", {{elmt.bits}}, {{elmt.var_type}}),
|
||||
YAML_ENUM("{{ max_len(elmt.name) }}", {{elmt.bits}}, {{elmt.var_type}}, {{ elmt.func }}),
|
||||
{% elif elmt.type == 'struct' %}
|
||||
YAML_STRUCT("{{ max_len(elmt.name) }}", {{elmt.bits}}, {{elmt.var_type}}, {{ elmt.func }}),
|
||||
{% elif elmt.type == 'union' %}
|
||||
|
||||
Reference in New Issue
Block a user