Skip to content

Commit

Permalink
feat: add button combo mapping support
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyHaystack committed Jun 11, 2024
1 parent c5da944 commit 6f522b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions include/modes/CustomControllerMode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class CustomControllerMode : public ControllerMode {
private:
const CustomModeConfig *_custom_mode_config;
uint64_t _modifier_button_masks[10];
uint64_t _button_combo_mappings_masks[5];
uint64_t _buttons_to_ignore = 0;
uint64_t _filtered_buttons = 0;

Button GetDirectionButton(const Button *direction_buttons, StickDirectionButton direction);
};
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ custom_nanopb_options =
--error-on-unmatched
lib_deps =
nanopb/Nanopb@^0.4.8
https://github.com/JonnyHaystack/HayBox-proto#f37657b
https://github.com/JonnyHaystack/HayBox-proto#5b2bb5d

[avr_base]
platform = atmelavr
Expand Down
47 changes: 36 additions & 11 deletions src/modes/CustomControllerMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,41 @@ void CustomControllerMode::SetConfig(
custom_mode_config.modifiers[i].buttons_count
);
}
for (size_t i = 0; i < custom_mode_config.button_combo_mappings_count; i++) {
_button_combo_mappings_masks[i] = make_button_mask(
custom_mode_config.button_combo_mappings[i].buttons,
custom_mode_config.button_combo_mappings[i].buttons_count
);
}
}

void CustomControllerMode::UpdateDigitalOutputs(const InputState &inputs, OutputState &outputs) {
if (_custom_mode_config == nullptr) {
return;
}

// First check for button combo -> single output mappings, and lock out the normal function of
// the buttons in any buttons combos that are activated.
_buttons_to_ignore = 0;
const ButtonComboMapping *button_combo_mappings = _custom_mode_config->button_combo_mappings;
for (size_t i = 0; i < _custom_mode_config->button_combo_mappings_count; i++) {
const ButtonComboMapping &button_combo_mapping = button_combo_mappings[i];
if (!all_buttons_held(inputs.buttons, _button_combo_mappings_masks[i])) {
continue;
}

set_output(outputs.buttons, button_combo_mapping.digital_output, true);
_buttons_to_ignore |= _button_combo_mappings_masks[i];
}
_filtered_buttons = inputs.buttons & ~_buttons_to_ignore;

for (size_t output = 0; output < _custom_mode_config->digital_button_mappings_count; output++) {
Button input = _custom_mode_config->digital_button_mappings[output];
set_output(outputs.buttons, (DigitalOutput)(output + 1), get_button(inputs.buttons, input));
set_output(
outputs.buttons,
(DigitalOutput)(output + 1),
get_button(_filtered_buttons, input)
);
}

if (inputs.nunchuk_connected) {
Expand All @@ -44,14 +69,14 @@ void CustomControllerMode::UpdateAnalogOutputs(const InputState &inputs, OutputS
const Button *direction_buttons = _custom_mode_config->stick_direction_mappings;
uint8_t stick_range = _custom_mode_config->stick_range;
UpdateDirections(
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_LSTICK_LEFT)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_LSTICK_RIGHT)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_LSTICK_DOWN)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_LSTICK_UP)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_RSTICK_LEFT)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_RSTICK_RIGHT)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_RSTICK_DOWN)),
get_button(inputs.buttons, GetDirectionButton(direction_buttons, SD_RSTICK_UP)),
get_button(_filtered_buttons, SD_LSTICK_LEFT)),
get_button(_filtered_buttons, SD_LSTICK_RIGHT)),
get_button(_filtered_buttons, GetDirectionButton(direction_buttons, SD_LSTICK_DOWN)),
get_button(_filtered_buttons, GetDirectionButton(direction_buttons, SD_LSTICK_UP)),
get_button(_filtered_buttons, GetDirectionButton(direction_buttons, SD_RSTICK_LEFT)),
get_button(_filtered_buttons, GetDirectionButton(direction_buttons, SD_RSTICK_RIGHT)),
get_button(_filtered_buttons, GetDirectionButton(direction_buttons, SD_RSTICK_DOWN)),
get_button(_filtered_buttons, GetDirectionButton(direction_buttons, SD_RSTICK_UP)),
ANALOG_STICK_NEUTRAL - stick_range,
ANALOG_STICK_NEUTRAL,
ANALOG_STICK_NEUTRAL + stick_range,
Expand All @@ -64,7 +89,7 @@ void CustomControllerMode::UpdateAnalogOutputs(const InputState &inputs, OutputS
if (modifier.axis == AXIS_UNSPECIFIED || modifier.axis > _AnalogAxis_MAX) {
continue;
}
if (!all_buttons_held(inputs.buttons, _modifier_button_masks[i])) {
if (!all_buttons_held(_filtered_buttons, _modifier_button_masks[i])) {
continue;
}

Expand All @@ -91,7 +116,7 @@ void CustomControllerMode::UpdateAnalogOutputs(const InputState &inputs, OutputS
_custom_mode_config->analog_trigger_mappings;
for (size_t i = 0; i < _custom_mode_config->analog_trigger_mappings_count; i++) {
const AnalogTriggerMapping &mapping = analog_trigger_mappings[i];
if (get_button(inputs.buttons, mapping.button)) {
if (get_button(_filtered_buttons, mapping.button)) {
switch (mapping.trigger) {
case TRIGGER_LT:
outputs.triggerLAnalog = mapping.value;
Expand Down

0 comments on commit 6f522b0

Please sign in to comment.