Skip to content

Commit

Permalink
Add helper for input mapping switch combo check
Browse files Browse the repository at this point in the history
  • Loading branch information
xerpi committed Oct 27, 2021
1 parent 35205ec commit c76617b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions include/button_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ void bm_calculate_ir(
/* Outputs */
struct ir_dot_t ir_dots[static IR_MAX_DOTS]);

bool bm_check_switch_mapping(u32 *buttons, bool *switch_mapping, u32 switch_mapping_combo);

static inline void bm_nunchuk_format(struct wiimote_extension_data_format_nunchuk_t *out,
u8 buttons, u8 analog_axis[static BM_NUNCHUK_ANALOG_AXIS__NUM],
u16 ax, u16 ay, u16 az)
Expand Down
15 changes: 15 additions & 0 deletions source/button_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@ void bm_calculate_ir(
ir_dots[1].x = (IR_HIGH_X - dot.x) + IR_HORIZONTAL_OFFSET;
ir_dots[1].y = dot.y + vert_offset;
}

bool bm_check_switch_mapping(u32 *buttons, bool *switch_mapping, u32 switch_mapping_combo)
{
bool switch_pressed = (*buttons & switch_mapping_combo) == switch_mapping_combo;
bool ret = false;

if (switch_pressed && !*switch_mapping) {
/* Remove the mapping switch combo from the pressed buttons */
*buttons &= ~switch_mapping_combo;
ret = true;
}

*switch_mapping = switch_pressed;
return ret;
}
14 changes: 5 additions & 9 deletions source/usb_driver_ds3.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ struct ds3_private_data_t {
u8 mapping;
u8 leds;
bool rumble_on;
bool switch_input_combo_pressed;
bool switch_mapping;
};
static_assert(sizeof(struct ds3_private_data_t) <= USB_INPUT_DEVICE_PRIVATE_DATA_SIZE);

#define SWITCH_INPUT_MAPPING_COMBO (BIT(DS3_BUTTON_R3))
#define SWITCH_MAPPING_COMBO (BIT(DS3_BUTTON_R3))

static const struct {
enum wiimote_ext_e extension;
Expand Down Expand Up @@ -301,7 +301,7 @@ int ds3_driver_ops_init(usb_input_device_t *device, u16 vid, u16 pid)
priv->leds = 0;
priv->rumble_on = false;
priv->mapping = 0;
priv->switch_input_combo_pressed = false;
priv->switch_mapping = false;

/* Set initial extension */
fake_wiimote_set_extension(device->wiimote, input_mappings[priv->mapping].extension);
Expand Down Expand Up @@ -351,18 +351,14 @@ int ds3_driver_ops_usb_async_resp(usb_input_device_t *device)
u16 acc_x, acc_y, acc_z;
u16 wiimote_buttons = 0;
union wiimote_extension_data_t extension_data;
bool switch_input;

ds3_get_buttons(report, &ds3_buttons);
ds3_get_analog_axis(report, ds3_analog_axis);

switch_input = (ds3_buttons & SWITCH_INPUT_MAPPING_COMBO) == SWITCH_INPUT_MAPPING_COMBO;
if (switch_input && !priv->switch_input_combo_pressed) {
if (bm_check_switch_mapping(&ds3_buttons, &priv->switch_mapping, SWITCH_MAPPING_COMBO)) {
priv->mapping = (priv->mapping + 1) % ARRAY_SIZE(input_mappings);
fake_wiimote_set_extension(device->wiimote,
input_mappings[priv->mapping].extension);
fake_wiimote_set_extension(device->wiimote, input_mappings[priv->mapping].extension);
}
priv->switch_input_combo_pressed = switch_input;

bm_map_wiimote(DS3_BUTTON__NUM, ds3_buttons,
input_mappings[priv->mapping].wiimote_button_map,
Expand Down
14 changes: 5 additions & 9 deletions source/usb_driver_ds4.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ struct ds4_private_data_t {
u8 mapping;
u8 leds;
bool rumble_on;
bool switch_input_combo_pressed;
bool switch_mapping;
};
static_assert(sizeof(struct ds4_private_data_t) <= USB_INPUT_DEVICE_PRIVATE_DATA_SIZE);

#define SWITCH_INPUT_MAPPING_COMBO (BIT(DS4_BUTTON_R3))
#define SWITCH_MAPPING_COMBO (BIT(DS4_BUTTON_R3))

static const struct {
enum wiimote_ext_e extension;
Expand Down Expand Up @@ -294,7 +294,7 @@ int ds4_driver_ops_init(usb_input_device_t *device, u16 vid, u16 pid)
priv->leds = 0;
priv->rumble_on = false;
priv->mapping = 0;
priv->switch_input_combo_pressed = false;
priv->switch_mapping = false;

/* Set initial extension */
fake_wiimote_set_extension(device->wiimote, input_mappings[priv->mapping].extension);
Expand Down Expand Up @@ -343,19 +343,15 @@ int ds4_driver_ops_usb_async_resp(usb_input_device_t *device)
int num_fingers = 0;
u16 finger_x[2], finger_y[2];
struct ir_dot_t ir_dots[IR_MAX_DOTS];
bool switch_input;

if (report->report_id == 0x01) {
ds4_get_buttons(report, &ds4_buttons);
ds4_get_analog_axis(report, ds4_analog_axis);

switch_input = (ds4_buttons & SWITCH_INPUT_MAPPING_COMBO) == SWITCH_INPUT_MAPPING_COMBO;
if (switch_input && !priv->switch_input_combo_pressed) {
if (bm_check_switch_mapping(&ds4_buttons, &priv->switch_mapping, SWITCH_MAPPING_COMBO)) {
priv->mapping = (priv->mapping + 1) % ARRAY_SIZE(input_mappings);
fake_wiimote_set_extension(device->wiimote,
input_mappings[priv->mapping].extension);
fake_wiimote_set_extension(device->wiimote, input_mappings[priv->mapping].extension);
}
priv->switch_input_combo_pressed = switch_input;

bm_map_wiimote(DS4_BUTTON__NUM, ds4_buttons,
input_mappings[priv->mapping].wiimote_button_map,
Expand Down

0 comments on commit c76617b

Please sign in to comment.