Skip to content

Commit

Permalink
Merge pull request #32 from RobertDaleSmith/chore-gp2040-button-defs
Browse files Browse the repository at this point in the history
chore(devices): creates common button value button defs matching gp2040 usage format
  • Loading branch information
RobertDaleSmith authored Feb 4, 2024
2 parents 3e62fd7 + 0abd3ed commit 1648dd8
Show file tree
Hide file tree
Showing 31 changed files with 680 additions and 595 deletions.
2 changes: 1 addition & 1 deletion src/common/codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// Define constants
#define CODE_LENGTH 10
#ifndef KONAMI_CODE
#define KONAMI_CODE {0x01, 0x01, 0x04, 0x04, 0x08, 0x02, 0x08, 0x02, 0x20, 0x10}
#define KONAMI_CODE {USBR_BUTTON_DU, USBR_BUTTON_DU, USBR_BUTTON_DD, USBR_BUTTON_DD, USBR_BUTTON_DL, USBR_BUTTON_DR, USBR_BUTTON_DL, USBR_BUTTON_DR, USBR_BUTTON_B1, USBR_BUTTON_B2}
#endif

// Declaration of global variables
Expand Down
23 changes: 23 additions & 0 deletions src/common/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@

#define MAX_DEVICES 6

// USBRetro Buttons -> Xinput/Switch/PlayStation
#define USBR_BUTTON_B1 0x00020 // A/B/Cross
#define USBR_BUTTON_B2 0x00010 // B/A/Circle
#define USBR_BUTTON_B3 0x02000 // X/Y/Square
#define USBR_BUTTON_B4 0x01000 // Y/X/Triangle
#define USBR_BUTTON_L1 0x04000 // LB/L/L1
#define USBR_BUTTON_R1 0x08000 // RB/R/R1
#define USBR_BUTTON_L2 0x00100 // LT/ZL/L2
#define USBR_BUTTON_R2 0x00200 // RT/ZR/R2
#define USBR_BUTTON_S1 0x00040 // Back/Minus/Share/Select
#define USBR_BUTTON_S2 0x00080 // Start/Plus/Options
#define USBR_BUTTON_L3 0x10000 // LS/L3
#define USBR_BUTTON_R3 0x20000 // RS/R3
#define USBR_BUTTON_A1 0x00400 // Guide/Home/PS
#define USBR_BUTTON_A2 0x00000 // Capture/Touchpad

// USBRetro Dpad Directions
#define USBR_BUTTON_DU 0x00001 // Dpad Up
#define USBR_BUTTON_DD 0x00004 // Dpad Down
#define USBR_BUTTON_DL 0x00008 // Dpad Left
#define USBR_BUTTON_DR 0x00002 // Dpad Right
// TODO: add A2 and make button values uniform

// handles state of multi-player inputs
bool update_pending;

Expand Down
26 changes: 13 additions & 13 deletions src/console/gamecube/gamecube.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,18 @@ void __not_in_flash_func(update_output)(void)
if (players[0].button_mode != BUTTON_MODE_KB)
{
// global buttons
gc_report.dpad_up |= ((byte & 0x0001) == 0) ? 1 : 0; // up
gc_report.dpad_right |= ((byte & 0x0002) == 0) ? 1 : 0; // right
gc_report.dpad_down |= ((byte & 0x0004) == 0) ? 1 : 0; // down
gc_report.dpad_left |= ((byte & 0x0008) == 0) ? 1 : 0; // left
gc_report.a |= ((byte & 0x0010) == 0) ? 1 : 0; // b
gc_report.b |= ((byte & 0x0020) == 0) ? 1 : 0; // a
gc_report.z |= ((byte & 0x0040) == 0) ? 1 : 0; // select
gc_report.start |= ((byte & 0x0080) == 0) ? 1 : 0; // start
gc_report.x |= ((byte & 0x01000) == 0) ? 1 : 0; // y
gc_report.y |= ((byte & 0x02000) == 0) ? 1 : 0; // x
gc_report.l |= ((byte & 0x04000) == 0) ? 1 : 0; // l
gc_report.r |= ((byte & 0x08000) == 0) ? 1 : 0; // r
gc_report.dpad_up |= ((byte & USBR_BUTTON_DU) == 0) ? 1 : 0; // up
gc_report.dpad_right |= ((byte & USBR_BUTTON_DR) == 0) ? 1 : 0; // right
gc_report.dpad_down |= ((byte & USBR_BUTTON_DD) == 0) ? 1 : 0; // down
gc_report.dpad_left |= ((byte & USBR_BUTTON_DL) == 0) ? 1 : 0; // left
gc_report.a |= ((byte & USBR_BUTTON_B2) == 0) ? 1 : 0; // b
gc_report.b |= ((byte & USBR_BUTTON_B1) == 0) ? 1 : 0; // a
gc_report.z |= ((byte & USBR_BUTTON_S1) == 0) ? 1 : 0; // select
gc_report.start |= ((byte & USBR_BUTTON_S2) == 0) ? 1 : 0; // start
gc_report.x |= ((byte & USBR_BUTTON_B4) == 0) ? 1 : 0; // y
gc_report.y |= ((byte & USBR_BUTTON_B3) == 0) ? 1 : 0; // x
gc_report.l |= ((byte & USBR_BUTTON_L1) == 0) ? 1 : 0; // l
gc_report.r |= ((byte & USBR_BUTTON_R1) == 0) ? 1 : 0; // r

// global dominate axis
gc_report.stick_x = furthest_from_center(gc_report.stick_x, players[i].output_analog_1x, 128);
Expand Down Expand Up @@ -326,7 +326,7 @@ void __not_in_flash_func(post_globals)(
if (is_extra) instance = 0;

int player_index = find_player_index(dev_addr, instance);
uint16_t buttons_pressed = (~(buttons | 0x0800)) || keys;
uint16_t buttons_pressed = (~(buttons | 0x800)) || keys;
if (player_index < 0 && buttons_pressed)
{
printf("[add player] [%d, %d]\n", dev_addr, instance);
Expand Down
163 changes: 84 additions & 79 deletions src/console/loopy/loopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ void __not_in_flash_func(core1_entry)(void)
int16_t player_2 = (players[1].output_buttons & 0xffff);
int16_t player_3 = (players[2].output_buttons & 0xffff);
int16_t player_4 = (players[3].output_buttons & 0xffff);
bool isMouse = (!(player_1 & 0x000f)) || (!(player_2 & 0x000f)) ||
(!(player_3 & 0x000f)) || (!(player_4 & 0x000f));
bool isMouse = (!(player_1 & 0x0f))
|| (!(player_2 & 0x0f))
|| (!(player_3 & 0x0f))
|| (!(player_4 & 0x0f));

if (!isMouse) {
// Gamepad output
Expand All @@ -87,80 +89,80 @@ void __not_in_flash_func(core1_entry)(void)
if (gpio_get(ROW0_PIN)) {
// Player 1 - ROW0
gpio_put(BIT0_PIN, 1); // Presence
gpio_put(BIT1_PIN, ((player_1 & 0x0080) == 0) ? 1 : 0); // Start
gpio_put(BIT2_PIN, ((player_1 & 0x4000) == 0) ? 1 : 0); // L
gpio_put(BIT3_PIN, ((player_1 & 0x8000) == 0) ? 1 : 0); // R
gpio_put(BIT1_PIN, ((player_1 & USBR_BUTTON_S2) == 0) ? 1 : 0); // Start
gpio_put(BIT2_PIN, ((player_1 & USBR_BUTTON_L1) == 0) ? 1 : 0); // L
gpio_put(BIT3_PIN, ((player_1 & USBR_BUTTON_R1) == 0) ? 1 : 0); // R

// Player 2 - ROW0
gpio_put(BIT4_PIN, 1); // Presence
gpio_put(BIT5_PIN, ((player_2 & 0x0080) == 0) ? 1 : 0); // Start
gpio_put(BIT6_PIN, ((player_2 & 0x4000) == 0) ? 1 : 0); // L
gpio_put(BIT7_PIN, ((player_2 & 0x8000) == 0) ? 1 : 0); // R
gpio_put(BIT5_PIN, ((player_2 & USBR_BUTTON_S2) == 0) ? 1 : 0); // Start
gpio_put(BIT6_PIN, ((player_2 & USBR_BUTTON_L1) == 0) ? 1 : 0); // L
gpio_put(BIT7_PIN, ((player_2 & USBR_BUTTON_R1) == 0) ? 1 : 0); // R

} else if (gpio_get(ROW1_PIN)) {
// Player 1 - ROW1
gpio_put(BIT0_PIN, ((player_1 & 0x0020) == 0) ? 1 : 0); // A
gpio_put(BIT1_PIN, ((player_1 & 0x2000) == 0) ? 1 : 0); // D
gpio_put(BIT2_PIN, ((player_1 & 0x1000) == 0) ? 1 : 0); // C
gpio_put(BIT3_PIN, ((player_1 & 0x0010) == 0) ? 1 : 0); // B
gpio_put(BIT0_PIN, ((player_1 & USBR_BUTTON_B1) == 0) ? 1 : 0); // A
gpio_put(BIT1_PIN, ((player_1 & USBR_BUTTON_B4) == 0) ? 1 : 0); // D
gpio_put(BIT2_PIN, ((player_1 & USBR_BUTTON_B3) == 0) ? 1 : 0); // C
gpio_put(BIT3_PIN, ((player_1 & USBR_BUTTON_B2) == 0) ? 1 : 0); // B

// Player 2 - ROW1
gpio_put(BIT4_PIN, ((player_2 & 0x0020) == 0) ? 1 : 0); // A
gpio_put(BIT5_PIN, ((player_2 & 0x2000) == 0) ? 1 : 0); // D
gpio_put(BIT6_PIN, ((player_2 & 0x1000) == 0) ? 1 : 0); // C
gpio_put(BIT7_PIN, ((player_2 & 0x0010) == 0) ? 1 : 0); // B
gpio_put(BIT4_PIN, ((player_2 & USBR_BUTTON_B1) == 0) ? 1 : 0); // A
gpio_put(BIT5_PIN, ((player_2 & USBR_BUTTON_B4) == 0) ? 1 : 0); // D
gpio_put(BIT6_PIN, ((player_2 & USBR_BUTTON_B3) == 0) ? 1 : 0); // C
gpio_put(BIT7_PIN, ((player_2 & USBR_BUTTON_B2) == 0) ? 1 : 0); // B

} else if (gpio_get(ROW2_PIN)) {
// Player 1 - ROW2
gpio_put(BIT0_PIN, ((player_1 & 0x0001) == 0) ? 1 : 0); // Up
gpio_put(BIT1_PIN, ((player_1 & 0x0004) == 0) ? 1 : 0); // Down
gpio_put(BIT2_PIN, ((player_1 & 0x0008) == 0) ? 1 : 0); // Left
gpio_put(BIT3_PIN, ((player_1 & 0x0002) == 0) ? 1 : 0); // Right
gpio_put(BIT0_PIN, ((player_1 & USBR_BUTTON_DU) == 0) ? 1 : 0); // Up
gpio_put(BIT1_PIN, ((player_1 & USBR_BUTTON_DD) == 0) ? 1 : 0); // Down
gpio_put(BIT2_PIN, ((player_1 & USBR_BUTTON_DL) == 0) ? 1 : 0); // Left
gpio_put(BIT3_PIN, ((player_1 & USBR_BUTTON_DR) == 0) ? 1 : 0); // Right

// Player 2 - ROW2
gpio_put(BIT4_PIN, ((player_2 & 0x0001) == 0) ? 1 : 0); // Up
gpio_put(BIT5_PIN, ((player_2 & 0x0004) == 0) ? 1 : 0); // Down
gpio_put(BIT6_PIN, ((player_2 & 0x0008) == 0) ? 1 : 0); // Left
gpio_put(BIT7_PIN, ((player_2 & 0x0002) == 0) ? 1 : 0); // Right
gpio_put(BIT4_PIN, ((player_2 & USBR_BUTTON_DU) == 0) ? 1 : 0); // Up
gpio_put(BIT5_PIN, ((player_2 & USBR_BUTTON_DD) == 0) ? 1 : 0); // Down
gpio_put(BIT6_PIN, ((player_2 & USBR_BUTTON_DL) == 0) ? 1 : 0); // Left
gpio_put(BIT7_PIN, ((player_2 & USBR_BUTTON_DR) == 0) ? 1 : 0); // Right

} else if (gpio_get(ROW3_PIN)) {
// Player 3 - ROW0
gpio_put(BIT0_PIN, 1); // Presence
gpio_put(BIT1_PIN, ((player_3 & 0x0080) == 0) ? 1 : 0); // Start
gpio_put(BIT2_PIN, ((player_3 & 0x4000) == 0) ? 1 : 0); // L
gpio_put(BIT3_PIN, ((player_3 & 0x8000) == 0) ? 1 : 0); // R
gpio_put(BIT1_PIN, ((player_3 & USBR_BUTTON_S2) == 0) ? 1 : 0); // Start
gpio_put(BIT2_PIN, ((player_3 & USBR_BUTTON_L1) == 0) ? 1 : 0); // L
gpio_put(BIT3_PIN, ((player_3 & USBR_BUTTON_R1) == 0) ? 1 : 0); // R

// Player 4 - ROW0
gpio_put(BIT4_PIN, 1); // Presence
gpio_put(BIT5_PIN, ((player_4 & 0x0080) == 0) ? 1 : 0); // Start
gpio_put(BIT6_PIN, ((player_4 & 0x4000) == 0) ? 1 : 0); // L
gpio_put(BIT7_PIN, ((player_4 & 0x8000) == 0) ? 1 : 0); // R
gpio_put(BIT5_PIN, ((player_4 & USBR_BUTTON_S2) == 0) ? 1 : 0); // Start
gpio_put(BIT6_PIN, ((player_4 & USBR_BUTTON_L1) == 0) ? 1 : 0); // L
gpio_put(BIT7_PIN, ((player_4 & USBR_BUTTON_R1) == 0) ? 1 : 0); // R

} else if (gpio_get(ROW4_PIN)) {
// Player 3 - ROW1
gpio_put(BIT0_PIN, ((player_3 & 0x0020) == 0) ? 1 : 0); // A
gpio_put(BIT1_PIN, ((player_3 & 0x2000) == 0) ? 1 : 0); // D
gpio_put(BIT2_PIN, ((player_3 & 0x1000) == 0) ? 1 : 0); // C
gpio_put(BIT3_PIN, ((player_3 & 0x0010) == 0) ? 1 : 0); // B
gpio_put(BIT0_PIN, ((player_3 & USBR_BUTTON_B1) == 0) ? 1 : 0); // A
gpio_put(BIT1_PIN, ((player_3 & USBR_BUTTON_B4) == 0) ? 1 : 0); // D
gpio_put(BIT2_PIN, ((player_3 & USBR_BUTTON_B3) == 0) ? 1 : 0); // C
gpio_put(BIT3_PIN, ((player_3 & USBR_BUTTON_B2) == 0) ? 1 : 0); // B

// Player 4 - ROW1
gpio_put(BIT4_PIN, ((player_4 & 0x0020) == 0) ? 1 : 0); // A
gpio_put(BIT5_PIN, ((player_4 & 0x2000) == 0) ? 1 : 0); // D
gpio_put(BIT6_PIN, ((player_4 & 0x1000) == 0) ? 1 : 0); // C
gpio_put(BIT7_PIN, ((player_4 & 0x0010) == 0) ? 1 : 0); // B
gpio_put(BIT4_PIN, ((player_4 & USBR_BUTTON_B1) == 0) ? 1 : 0); // A
gpio_put(BIT5_PIN, ((player_4 & USBR_BUTTON_B4) == 0) ? 1 : 0); // D
gpio_put(BIT6_PIN, ((player_4 & USBR_BUTTON_B3) == 0) ? 1 : 0); // C
gpio_put(BIT7_PIN, ((player_4 & USBR_BUTTON_B2) == 0) ? 1 : 0); // B

} else if (gpio_get(ROW5_PIN)) {
// Player 3 - ROW2
gpio_put(BIT0_PIN, ((player_3 & 0x0001) == 0) ? 1 : 0); // Up
gpio_put(BIT1_PIN, ((player_3 & 0x0004) == 0) ? 1 : 0); // Down
gpio_put(BIT2_PIN, ((player_3 & 0x0008) == 0) ? 1 : 0); // Left
gpio_put(BIT3_PIN, ((player_3 & 0x0002) == 0) ? 1 : 0); // Right
gpio_put(BIT0_PIN, ((player_3 & USBR_BUTTON_DU) == 0) ? 1 : 0); // Up
gpio_put(BIT1_PIN, ((player_3 & USBR_BUTTON_DD) == 0) ? 1 : 0); // Down
gpio_put(BIT2_PIN, ((player_3 & USBR_BUTTON_DL) == 0) ? 1 : 0); // Left
gpio_put(BIT3_PIN, ((player_3 & USBR_BUTTON_DR) == 0) ? 1 : 0); // Right

// Player 4 - ROW2
gpio_put(BIT4_PIN, ((player_4 & 0x0001) == 0) ? 1 : 0); // Up
gpio_put(BIT5_PIN, ((player_4 & 0x0004) == 0) ? 1 : 0); // Down
gpio_put(BIT6_PIN, ((player_4 & 0x0008) == 0) ? 1 : 0); // Left
gpio_put(BIT7_PIN, ((player_4 & 0x0002) == 0) ? 1 : 0); // Right
gpio_put(BIT4_PIN, ((player_4 & USBR_BUTTON_DU) == 0) ? 1 : 0); // Up
gpio_put(BIT5_PIN, ((player_4 & USBR_BUTTON_DD) == 0) ? 1 : 0); // Down
gpio_put(BIT6_PIN, ((player_4 & USBR_BUTTON_DL) == 0) ? 1 : 0); // Left
gpio_put(BIT7_PIN, ((player_4 & USBR_BUTTON_DR) == 0) ? 1 : 0); // Right
} else {
// Time between ROWs
gpio_put(BIT0_PIN, 0);
Expand All @@ -182,14 +184,17 @@ void __not_in_flash_func(core1_entry)(void)
// [X encoder raw] [Y encoder raw] Left N/C Right Presence
//

gpio_put(BIT0_PIN, ((player_1 & 0x0008) == 0) ? 1 : 0); // X
gpio_put(BIT1_PIN, ((player_1 & 0x0004) == 0) ? 1 : 0); // X
gpio_put(BIT2_PIN, ((player_1 & 0x0001) == 0) ? 1 : 0); // Y
gpio_put(BIT3_PIN, ((player_1 & 0x0004) == 0) ? 1 : 0); // Y

gpio_put(BIT4_PIN, ((player_1 & 0x0020) == 0) ? 0 : 1); // Left
gpio_put(BIT5_PIN, 0);
gpio_put(BIT6_PIN, ((player_1 & 0x0010) == 0) ? 0 : 1); // Right
// TODO: actually parse mouse global into RAW gray code.
// May require comparing with last x/y to get direction.
// [0, 1, 3, 2 ... for one direction and 2, 3, 1, 0 ... for other]
gpio_put(BIT0_PIN, ((player_1 & USBR_BUTTON_DL) == 0) ? 1 : 0); // X
gpio_put(BIT1_PIN, ((player_1 & USBR_BUTTON_DR) == 0) ? 1 : 0); // X
gpio_put(BIT2_PIN, ((player_1 & USBR_BUTTON_DU) == 0) ? 1 : 0); // Y
gpio_put(BIT3_PIN, ((player_1 & USBR_BUTTON_DD) == 0) ? 1 : 0); // Y

gpio_put(BIT4_PIN, ((player_1 & USBR_BUTTON_B1) == 0) ? 0 : 1); // Left
gpio_put(BIT5_PIN, 0); // N/C
gpio_put(BIT6_PIN, ((player_1 & USBR_BUTTON_B2) == 0) ? 0 : 1); // Right
gpio_put(BIT7_PIN, 1); // Presence

// TODO: handle multiple mice + controllers.
Expand Down Expand Up @@ -223,38 +228,38 @@ void __not_in_flash_func(update_output)(void)
// {
// }

if (playersCount < 1)
{
bytes[0] = 0x00;
bytes[1] = 0x00;
bytes[2] = 0x00;
} else {
bytes[0] = 0x01;
bytes[1] = players[0].output_buttons & 0x0f;
bytes[2] = players[0].output_buttons & 0xf0;
}
// TODO: parse USBRetro button state to output_word here
// if (playersCount < 1)
// {
// bytes[0] = 0x00;
// bytes[1] = 0x00;
// bytes[2] = 0x00;
// } else {
// bytes[0] = 0x01;
// bytes[1] = players[0].output_buttons & 0x0f;
// bytes[2] = players[0].output_buttons & 0xf0;
// }

bool isMouse = !(players[0].output_buttons & 0x000f);
// bool isMouse = !(players[0].output_buttons & 0x0f);

// mouse x/y states
if (isMouse)
{
// TODO:
int8_t mouse_byte = 0b10000000;
// // mouse x/y states
// if (isMouse)
// {
// // TODO:
// int8_t mouse_byte = 0b10000000;

bytes[0] = mouse_byte;
bytes[1] = mouse_byte;
bytes[2] = mouse_byte;
}
// bytes[0] = mouse_byte;
// bytes[1] = mouse_byte;
// bytes[2] = mouse_byte;
// }

output_word = ((bytes[0] & 0xff)) | // ROW0
((bytes[1] & 0xff) << 8) | // ROW1
((bytes[2] & 0xff) << 16); // ROW2
// output_word = ((bytes[0] & 0xff)) | // ROW0
// ((bytes[1] & 0xff) << 8) | // ROW1
// ((bytes[2] & 0xff) << 16); // ROW2

codes_task();
}


//
// post_globals - accumulate the many intermediate mouse scans (~1ms)
// into an accumulator which will be reported back to loopy
Expand All @@ -270,7 +275,7 @@ void __not_in_flash_func(post_globals)(
if (is_extra) instance = 0;

int player_index = find_player_index(dev_addr, instance);
uint16_t buttons_pressed = (~(buttons | 0x0800)) || keys;
uint16_t buttons_pressed = (~(buttons | 0x800)) || keys;
if (player_index < 0 && buttons_pressed)
{
printf("[add player] [%d, %d]\n", dev_addr, instance);
Expand Down
32 changes: 15 additions & 17 deletions src/console/nuon/nuon.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ uint32_t map_nuon_buttons(uint32_t buttons)
uint32_t nuon_buttons = 0x0080;

// Mapping the buttons from the old format to the new format, inverting the logic
nuon_buttons |= (!(buttons & 0x00010)) ? 0x8000 : 0; // Circle -> C-DOWN
nuon_buttons |= (!(buttons & 0x00020)) ? 0x4000 : 0; // Cross -> A
nuon_buttons |= (!(buttons & 0x00080)) ? 0x2000 : 0; // Option -> START
nuon_buttons |= (!(buttons & 0x00040)) ? 0x1000 : 0; // Share -> SELECT
nuon_buttons |= (!(buttons & 0x00004)) ? 0x0800 : 0; // Dpad Down -> D-DOWN
nuon_buttons |= (!(buttons & 0x00008)) ? 0x0400 : 0; // Dpad Left -> D-LEFT
nuon_buttons |= (!(buttons & 0x00001)) ? 0x0200 : 0; // Dpad Up -> D-UP
nuon_buttons |= (!(buttons & 0x00002)) ? 0x0100 : 0; // Dpad Right -> D-RIGHT
nuon_buttons |= (!(buttons & USBR_BUTTON_B2)) ? NUON_BUTTON_C_DOWN : 0; // Circle -> C-DOWN
nuon_buttons |= (!(buttons & USBR_BUTTON_B1)) ? NUON_BUTTON_A : 0; // Cross -> A
nuon_buttons |= (!(buttons & USBR_BUTTON_S2)) ? NUON_BUTTON_START : 0; // Option -> START
nuon_buttons |= (!(buttons & USBR_BUTTON_S1)) ? NUON_BUTTON_NUON : 0; // Share -> NUON/Z
nuon_buttons |= (!(buttons & USBR_BUTTON_DD)) ? NUON_BUTTON_DOWN : 0; // Dpad Down -> D-DOWN
nuon_buttons |= (!(buttons & USBR_BUTTON_DL)) ? NUON_BUTTON_LEFT : 0; // Dpad Left -> D-LEFT
nuon_buttons |= (!(buttons & USBR_BUTTON_DU)) ? NUON_BUTTON_UP : 0; // Dpad Up -> D-UP
nuon_buttons |= (!(buttons & USBR_BUTTON_DR)) ? NUON_BUTTON_RIGHT : 0; // Dpad Right -> D-RIGHT
// Skipping the two buttons represented by 0x0080 and 0x0040 in the new format
nuon_buttons |= (!(buttons & 0x04000)) ? 0x0020 : 0; // L1 -> L
nuon_buttons |= (!(buttons & 0x08000)) ? 0x0010 : 0; // R1 -> R
nuon_buttons |= (!(buttons & 0x02000)) ? 0x0008 : 0; // Square -> B
nuon_buttons |= (!(buttons & 0x01000)) ? 0x0004 : 0; // Triangle -> C-LEFT
nuon_buttons |= (!(buttons & 0x00100)) ? 0x0002 : 0; // L2 -> C-UP
nuon_buttons |= (!(buttons & 0x00200)) ? 0x0001 : 0; // R2 -> C-RIGHT
nuon_buttons |= (!(buttons & USBR_BUTTON_L1)) ? NUON_BUTTON_L : 0; // L1 -> L
nuon_buttons |= (!(buttons & USBR_BUTTON_R1)) ? NUON_BUTTON_R : 0; // R1 -> R
nuon_buttons |= (!(buttons & USBR_BUTTON_B3)) ? NUON_BUTTON_B : 0; // Square -> B
nuon_buttons |= (!(buttons & USBR_BUTTON_B4)) ? NUON_BUTTON_C_LEFT : 0; // Triangle -> C-LEFT
nuon_buttons |= (!(buttons & USBR_BUTTON_L2)) ? NUON_BUTTON_C_UP : 0; // L2 -> C-UP
nuon_buttons |= (!(buttons & USBR_BUTTON_R2)) ? NUON_BUTTON_C_RIGHT : 0; // R2 -> C-RIGHT

return nuon_buttons;
}
Expand Down Expand Up @@ -459,14 +459,12 @@ void __not_in_flash_func(post_globals)(
uint32_t keys,
uint8_t quad_x)
{
bool has6Btn = !(buttons & 0x0800);

// for merging extra device instances into the root instance (ex: joycon charging grip)
bool is_extra = (instance == -1);
if (is_extra) instance = 0;

int player_index = find_player_index(dev_addr, instance);
uint16_t buttons_pressed = (~(buttons | 0x0800)) || keys;
uint16_t buttons_pressed = (~(buttons | 0x800)) || keys;
if (player_index < 0 && buttons_pressed)
{
printf("[add player] [%d, %d]\n", dev_addr, instance);
Expand Down
18 changes: 17 additions & 1 deletion src/console/nuon/nuon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,25 @@
#define CRC16 0x8005
#define MAGIC 0x4A554445 // HEX to ASCII == "JUDE" (The Polyface inventor)

// buttons
#define NUON_BUTTON_UP 0x0200
#define NUON_BUTTON_DOWN 0x0800
#define NUON_BUTTON_LEFT 0x0400
#define NUON_BUTTON_RIGHT 0x0100
#define NUON_BUTTON_A 0x4000
#define NUON_BUTTON_B 0x0008
#define NUON_BUTTON_L 0x0020
#define NUON_BUTTON_R 0x0010
#define NUON_BUTTON_C_UP 0x0002
#define NUON_BUTTON_C_DOWN 0x8000
#define NUON_BUTTON_C_LEFT 0x0004
#define NUON_BUTTON_C_RIGHT 0x0001
#define NUON_BUTTON_START 0x2000
#define NUON_BUTTON_NUON 0x1000 // Z

// fun
#undef KONAMI_CODE
#define KONAMI_CODE {0x0200, 0x0200, 0x0800, 0x0800, 0x0400, 0x0100, 0x0400, 0x0100, 0x0008, 0x4000}
#define KONAMI_CODE {NUON_BUTTON_UP, NUON_BUTTON_UP, NUON_BUTTON_DOWN, NUON_BUTTON_DOWN, NUON_BUTTON_LEFT, NUON_BUTTON_RIGHT, NUON_BUTTON_LEFT, NUON_BUTTON_RIGHT, NUON_BUTTON_B, NUON_BUTTON_A}

// Declaration of global variables
PIO pio;
Expand Down
Loading

0 comments on commit 1648dd8

Please sign in to comment.