Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n64: Analog Stick Swap Toggle #45

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void input_update_input_devices()
#endif
}

uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed, uint8_t swap_thumb_sticks)
{
uint32_t _buttons = 0;
int32_t _axis[JoystickController::STANDARD_AXIS_COUNT] = {0};
Expand Down Expand Up @@ -300,18 +300,31 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
N64_CL |
N64_CR;
//Analog stick (Normalise 0 to +/-100)
state->x_axis = _axis[0] * 100 / 32768;
state->y_axis = _axis[1] * 100 / 32768;
if (swap_thumb_sticks == 1) {
state->x_axis = _axis[2] * 100 / 32768;
state->y_axis = _axis[3] * 100 / 32768;
} else {
state->x_axis = _axis[0] * 100 / 32768;
state->y_axis = _axis[1] * 100 / 32768;
}


//Z button
if (_axis[4] > 10) state->dButtons |= N64_Z; //LT
if (_axis[5] > 10) state->dButtons |= N64_Z; //RT

//C usb_buttons
if (_axis[2] > 16000) state->dButtons |= N64_CR;
if (_axis[2] < -16000) state->dButtons |= N64_CL;
if (_axis[3] > 16000) state->dButtons |= N64_CU;
if (_axis[3] < -16000) state->dButtons |= N64_CD;
if (swap_thumb_sticks == 1) {
if (_axis[0] > 16000) state->dButtons |= N64_CR;
if (_axis[0] < -16000) state->dButtons |= N64_CL;
if (_axis[1] > 16000) state->dButtons |= N64_CU;
if (_axis[1] < -16000) state->dButtons |= N64_CD;
} else {
if (_axis[2] > 16000) state->dButtons |= N64_CR;
if (_axis[2] < -16000) state->dButtons |= N64_CL;
if (_axis[3] > 16000) state->dButtons |= N64_CU;
if (_axis[3] < -16000) state->dButtons |= N64_CD;
}

//Button to hold for 'combos'
if (combo_pressed)
Expand All @@ -337,7 +350,7 @@ uint16_t input_get_state(uint8_t id, void *response, bool *combo_pressed)
if (_buttons & (1 << 5)) state->dButtons |= N64_B; //B
if (_buttons & (1 << 6)) state->dButtons |= N64_B; //X
if (_buttons & (1 << 7)) state->dButtons |= 0; //Y
if (_buttons & (1 << 15)) state->dButtons |= N64_CU | //RS triggers
if (_buttons & (1 << 15)) state->dButtons |= N64_CU | //RS triggers
N64_CD | //all C usb_buttons
N64_CL |
N64_CR;
Expand Down
2 changes: 1 addition & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ uint16_t input_get_id_product(int id);
uint16_t input_get_id_vendor(int id);
const char *input_get_manufacturer_string(int id);
const char *input_get_product_string(int id);
uint16_t input_get_state(uint8_t id, void *n64_response, bool *combo_pressed);
uint16_t input_get_state(uint8_t id, void *n64_response, bool *combo_pressed, uint8_t swap_thumb_sticks);
void input_apply_rumble(int id, uint8_t strength);
void input_enable_dualstick_mode(int id);
void input_disable_dualstick_mode(int id);
Expand Down
7 changes: 3 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ void loop()
if (input_is_gamecontroller(c))
{
n64_buttonmap *new_state = (n64_buttonmap *)n64_response[c];
input_get_state(c, new_state, &n64_combo);
input_get_state(c, new_state, &n64_combo, settings->swap_thumb_sticks[c]);

if(n64_in_dev[c].is_mouse == true || n64_in_dev[c].is_kb == true)
{
n64_in_dev[c].is_mouse = false;
n64_in_dev[c].is_kb = false;
tft_flag_update();
}
n64_settings *settings = n64_settings_get();
float x, y, range;
astick_apply_deadzone(&x, &y, new_state->x_axis / 100.0f,
new_state->y_axis / 100.0f,
Expand Down Expand Up @@ -195,7 +194,7 @@ void loop()
else if (input_is_mouse(c))
{
n64_buttonmap *new_state = (n64_buttonmap *)n64_response[c];
input_get_state(c, new_state, &n64_combo);
input_get_state(c, new_state, &n64_combo, settings->swap_thumb_sticks[c]);

if(n64_in_dev[c].is_mouse == false)
{
Expand All @@ -215,7 +214,7 @@ void loop()
//Maintain the old led state
new_state->led_state = n64_in_dev[c].kb_state.led_state;

input_get_state(c, new_state, &n64_combo);
input_get_state(c, new_state, &n64_combo, settings->swap_thumb_sticks[c]);

if (n64_in_dev[c].is_kb == false)
{
Expand Down
1 change: 1 addition & 0 deletions src/n64/n64_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void n64_settings_init(n64_settings *settings)
settings->deadzone[i] = DEFAULT_DEADZONE;
settings->sensitivity[i] = DEFAULT_SENSITIVITY;
settings->snap_axis[i] = DEFAULT_SNAP;
settings->swap_thumb_sticks[i] = DEFAULT_SWAP_THUMB_STICKS;
settings->octa_correct[i] = DEFAULT_OCTA_CORRECT;
}
n64_settings_update_checksum(settings);
Expand Down
1 change: 1 addition & 0 deletions src/n64/n64_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct
uint8_t sensitivity[MAX_CONTROLLERS]; //0 to 4
uint8_t deadzone[MAX_CONTROLLERS]; //0 to 4
uint8_t snap_axis[MAX_CONTROLLERS]; //0 or 1
uint8_t swap_thumb_sticks[MAX_CONTROLLERS]; //0 or 1
uint8_t octa_correct[MAX_CONTROLLERS]; //0 or 1
uint8_t checksum;
} n64_settings;
Expand Down
7 changes: 7 additions & 0 deletions src/n64/n64_virtualpak.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ void n64_virtualpak_update(n64_mempack *vpak)
n64_virtualpak_write_string("DEADZONE+", SUBHEADING + 7, MENU_NAME_FIELD);
n64_virtualpak_write_string("DEADZONE-", SUBHEADING + 8, MENU_NAME_FIELD);

n64_virtualpak_write_string("SWAP THUMB STICKS TOGGLE", SUBHEADING + 9, MENU_NAME_FIELD);
n64_virtualpak_write_string("SNAP TOGGLE", SUBHEADING + 10, MENU_NAME_FIELD);
n64_virtualpak_write_string("OCTA TOGGLE", SUBHEADING + 11, MENU_NAME_FIELD);

Expand All @@ -383,6 +384,8 @@ void n64_virtualpak_update(n64_mempack *vpak)
settings->deadzone[controller_page]++; //Deadzone increase
else if (selected_row == SUBHEADING + 8 && settings->deadzone[controller_page] > 0)
settings->deadzone[controller_page]--; //Deadzone decrease
else if (selected_row == SUBHEADING + 9)
settings->swap_thumb_sticks[controller_page] ^= 1; //Toggle control thumb stick swap for FPS games
else if (selected_row == SUBHEADING + 10)
settings->snap_axis[controller_page] ^= 1; //Toggle axis 45 degree snapping
else if (selected_row == SUBHEADING + 11)
Expand All @@ -391,6 +394,7 @@ void n64_virtualpak_update(n64_mempack *vpak)
{
settings->deadzone[controller_page] = DEFAULT_DEADZONE;
settings->sensitivity[controller_page] = DEFAULT_SENSITIVITY;
settings->swap_thumb_sticks[controller_page] = DEFAULT_SWAP_THUMB_STICKS;
settings->snap_axis[controller_page] = DEFAULT_SNAP;
settings->octa_correct[controller_page] = DEFAULT_OCTA_CORRECT;
}
Expand All @@ -404,6 +408,9 @@ void n64_virtualpak_update(n64_mempack *vpak)
sprintf(buff, "%u\0", settings->deadzone[controller_page]);
n64_virtualpak_write_string(buff, SUBHEADING + 7, MENU_EXT_FIELD);

sprintf(buff, "%u\0", settings->swap_thumb_sticks[controller_page]);
n64_virtualpak_write_string(buff, SUBHEADING + 9, MENU_EXT_FIELD);

sprintf(buff, "%u\0", settings->snap_axis[controller_page]);
n64_virtualpak_write_string(buff, SUBHEADING + 10, MENU_EXT_FIELD);

Expand Down
9 changes: 5 additions & 4 deletions src/usb64_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@
#define MEMPAK_SAVE_EXT ".MPK" //MEMPAKXX.MPK

/* FIRMWARE DEFAULTS (CONFIGURABLE DURING USE) */
#define DEFAULT_SENSITIVITY 2 //0 to 4 (0 = low sensitivity, 4 = max)
#define DEFAULT_DEADZONE 2 //0 to 4 (0 = no deadzone correction, 4 = max (40%))
#define DEFAULT_SNAP 1 //0 or 1 (0 = will output raw analog stick angle, 1 will snap to 45deg angles)
#define DEFAULT_OCTA_CORRECT 1 //0 or 1 (Will correct the circular analog stuck shape to N64 octagonal)
#define DEFAULT_SENSITIVITY 2 //0 to 4 (0 = low sensitivity, 4 = max)
#define DEFAULT_DEADZONE 2 //0 to 4 (0 = no deadzone correction, 4 = max (40%))
#define DEFAULT_SNAP 1 //0 or 1 (0 = will output raw analog stick angle, 1 will snap to 45deg angles)
#define DEFAULT_OCTA_CORRECT 1 //0 or 1 (Will correct the circular analog stuck shape to N64 octagonal)
#define DEFAULT_SWAP_THUMB_STICKS 0 //0 or 1 (0 = will disable C-Button mapping to left-hand stick, 1 will enable C-Button mapping to left-hand stick)

/* FIRMWARE DEFAULTS (NOT CONFIGURABLE DURING USE) */
#define SNAP_RANGE 5 //+/- what angle range will snap. 5 will snap to 45 degree if between 40 and 50 degrees.
Expand Down