Skip to content

Commit

Permalink
Add support for Direct/PS4 Input mode
Browse files Browse the repository at this point in the history
* Change the reconnect keybinding to `Start`
* Fix include guards
  • Loading branch information
copyrat90 committed May 27, 2023
1 parent 287399a commit be230df
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
14 changes: 14 additions & 0 deletions GP2040-CE/headers/gba/GBAKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

enum GBAKey : uint32_t {
A = 1 << 0,
B = 1 << 1,
SELECT = 1 << 2,
START = 1 << 3,
RIGHT = 1 << 4,
LEFT = 1 << 5,
UP = 1 << 6,
DOWN = 1 << 7,
R = 1 << 8,
L = 1 << 9
};
7 changes: 2 additions & 5 deletions GP2040-CE/headers/gba/multiboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
* https://github.com/akkera102/gba_03_multiboot
*/

#ifndef _GBA_SPI32_H_
#define _GBA_SPI32_H_
#pragma once

#include <cstdint>

namespace gba
{

/// @return `true` if ROM is sent
/// @return `false` if GBA program is already running, and only `L` is pressed on it
/// @return `false` if GBA program is already running, and only `Start` is pressed on it
bool sendGBARom(const uint8_t* romAddr, uint32_t romSize);

}

#endif
5 changes: 1 addition & 4 deletions GP2040-CE/headers/gba/spi32.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* SPDX-License-Identifier: CC0-1.0
*/

#ifndef _GBA_SPI32_H_
#define _GBA_SPI32_H_
#pragma once

// Pico SPI
#include "hardware/spi.h"
Expand All @@ -18,5 +17,3 @@ void deinitSpi32();
uint32_t spi32(uint32_t val);

}

#endif
14 changes: 1 addition & 13 deletions GP2040-CE/src/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "CRC32.h"

#include "gba/spi32.h"
#include "gba/GBAKey.h"

// MUST BE DEFINED for mpgs
uint32_t getMillis() {
Expand Down Expand Up @@ -180,19 +181,6 @@ void Gamepad::read()
{
constexpr uint32_t GBA_SPI_ERROR = 0xFFFFFFFFu;

enum GBAKey : uint32_t {
A = 1 << 0,
B = 1 << 1,
SELECT = 1 << 2,
START = 1 << 3,
RIGHT = 1 << 4,
LEFT = 1 << 5,
UP = 1 << 6,
DOWN = 1 << 7,
R = 1 << 8,
L = 1 << 9
};

uint32_t received = gba::spi32(state.buttons);

if (received == GBA_SPI_ERROR) {
Expand Down
11 changes: 7 additions & 4 deletions GP2040-CE/src/gba/multiboot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
* https://github.com/akkera102/gba_03_multiboot
*/

#include "gba/spi32.h"
#include "gba/multiboot.h"

#include <cstdlib>
#include "pico/stdlib.h"

#include "gba/spi32.h"
#include "gba/GBAKey.h"

namespace gba
{

Expand All @@ -34,10 +37,10 @@ bool sendGBARom(const uint8_t* romAddr, const uint32_t romSize) {
do {
recv = gba::spi32(0x6202);
sleep_ms(10);
} while ((recv >> 16) != 0x7202 && recv != (1 << 9));
} while ((recv >> 16) != 0x7202 && recv != (GBAKey::START));

// if GBA program is already running, and only `L` is pressed on it
if (recv == (1 << 9))
// if GBA program is already running, and only `Start` is pressed on it
if (recv == (GBAKey::START))
return false;

// -----------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions GP2040-CE/src/gp2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ GP2040::BootAction GP2040::getBootAction() {

if (gamepad->pressedF1() && gamepad->pressedUp()) {
return BootAction::ENTER_USB_MODE;
} else if (gamepad->pressedS2()) {
return BootAction::ENTER_WEBCONFIG_MODE;
} else if (gamepad->pressedB3()) { // P1
}
// else if (gamepad->pressedS2()) {
// return BootAction::ENTER_WEBCONFIG_MODE;
// }
else if (gamepad->pressedL1()) { // P1
return BootAction::SET_INPUT_MODE_HID;
} else if (gamepad->pressedB4()) { // P2
} else if (gamepad->pressedR1()) { // P2
return BootAction::SET_INPUT_MODE_PS4;
} else if (gamepad->pressedB1()) { // K1
return BootAction::SET_INPUT_MODE_SWITCH;
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Proof-of-concept Game Boy Advance (GBA) to USB controller, which uses Raspberry
[![](https://img.youtube.com/vi/JmBufgcb4Gw/hqdefault.jpg)](https://www.youtube.com/watch?v=JmBufgcb4Gw "Youtube video for gba-pico-gamepad")


# How it works
# How it's done

This project is essentially a modified version of [GP2040-CE](https://github.com/OpenStickCommunity/GP2040-CE) that enables communication with the Game Boy Advance using the GBA link cable.

Expand Down Expand Up @@ -43,16 +43,17 @@ And that's about it.

4. Plug the USB Cable to your PC.\
It will start sending the program once the GBA is ready.
* You can [hold down certain key on boot to change Input Mode.](https://gp2040-ce.info/#/usage?id=input-modes)
* You can hold down certain key on boot to change Input Mode.
+ Note that the key binding is differ from the [original](https://gp2040-ce.info/#/usage?id=input-modes).
+ Hold `B` on boot -> Nintendo Switch
+ Hold `A` on boot -> XInput
+ The other options cannot be pressed with keys on the GBA,\
but no modifications are done yet to deal with that.
+ Hold `L` on boot -> DirectInput/PS3
+ Hold `R` on boot -> PS4
* You can [change the D-Pad Mode anytime with certain key combination.](https://gp2040-ce.info/#/usage?id=d-pad-modes)
* GP2040-CE's Web Config is disabled.

5. Enjoy your GBA as an USB gamepad.
* If you accidentally pulled out your cable, you can re-plug it and press `L` to reconnect.
* If you accidentally pulled out your cable, you can re-plug it and press `Start` to reconnect.


# Build
Expand Down

0 comments on commit be230df

Please sign in to comment.