libjoybus
Joybus implementation for 32-bit MCUs
Loading...
Searching...
No Matches
libjoybus

An implementation of the Joybus protocol used by N64 and GameCube controllers, for 32-bit microcontrollers.

Logic Analyzer Capture of a GameCube Controller Read

Features

  • C implementation, no external dependencies (besides backend-specific SDKs)
  • Provides both host mode and target mode functionality
  • Host mode allows communication with N64/GameCube controllers from a microcontroller
  • Target mode allows you to build custom N64/GameCube controllers using a microcontroller
  • Near-ASIC timing accuracy for reliable communication
  • Pre-built targets for N64 controllers and GameCube controllers

Supported Platforms

  • Raspberry Pi Pico and Pico 2 (and other RP2xxx-based boards)
  • Silicon Labs EFR32 Series 1 and Series 2 MCUs

Examples

The project is used in WavePhoenix, an open source implementation of a GameCube WaveBird receiver.

You can find a number of additonal examples in the examples/ directory, including a full implementation of the official Nintendo GameCube Controller Adapter for Pi Pico.

Please let me know if you build something with libjoybus! I love seeing my projects used in the wild, and I'll consider adding it to the examples list!

Usage

You can find the full API documentation here, but here are some basic examples to get you started.

Initializing the Joybus

Before using libjoybus, you need to initialize the Joybus interface for your platform. Here's an example for the RP2040:

#include <joybus/joybus.h>
#include <joybus/backend/rp2xxx.h>
struct joybus_rp2xxx rp2xxx_bus;
struct joybus *bus = JOYBUS(&rp2xxx_bus);
int main() {
// Initialize the Joybus on a specific GPIO pin and PIO instance
joybus_rp2xxx_init(&rp2xxx_bus, JOYBUS_GPIO, pio0);
// ...your code here
return 0;
}
int joybus_rp2xxx_init(struct joybus_rp2xxx *rp2xxx_bus, uint8_t gpio, PIO pio)
Initialize a RP2xxx Joybus instance.
Definition joybus.c:430
#define JOYBUS(bus)
Macro to cast a backend-specific Joybus instance to a generic Joybus instance.
Definition bus.h:61
A RP2xxx Joybus instance.
Definition rp2xxx.h:61
A Joybus instance.
Definition bus.h:96

Communicating with Controllers

In host mode, libjoybus allows a microcontroller to communicate with N64 and GameCube controllers. This allows you to use input data from N64 and GameCube controllers in your projects.

#include <joybus/joybus.h>
struct joybus_rp2xxx rp2xxx_bus;
struct joybus *bus = JOYBUS(&rp2xxx_bus);
void read_controller() {
// Read a GameCube controller in analog mode 3 with the rumble motor off
if (rc < 0) {
// ...handle read error
return;
}
// Do something with the input state
if (input.buttons & JOYBUS_GCN_BUTTON_A) {
// The A button is pressed
}
}
void main() {
// Initialize the Joybus
joybus_rp2xxx_init(&rp2xxx_bus, MY_GPIO, pio0);
// Read the controller state in a loop
while (1) {
read_controller();
sleep_ms(10);
}
}
@ JOYBUS_GCN_MOTOR_STOP
Stop the rumble motor.
Definition gcn_controller.h:108
@ JOYBUS_GCN_ANALOG_MODE_3
Substick X/Y and triggers full precision, analog A/B omitted.
Definition gcn_controller.h:97
#define JOYBUS_GCN_BUTTON_A
GameCube controller button bitmask flags.
Definition gcn_controller.h:18
int joybus_gcn_read(struct joybus *bus, enum joybus_gcn_analog_mode analog_mode, enum joybus_gcn_motor_state motor_state, struct joybus_gcn_controller_state *response)
Read the current input state of a GameCube controller.
Definition gcn.c:73
static int joybus_enable(struct joybus *bus)
Enable the Joybus instance.
Definition bus.h:110
GameCube controller input/origin state.
Definition gcn_controller.h:43

Emulating a Controller

In target mode, libjoybus allows a microcontroller to act as an N64 or GameCube controller. This allows you to create custom controllers that can interface with N64, GameCube, and Wii consoles.

I've provided built-in targets for N64 controllers and GameCube controllers so you can just populate the input state and let libjoybus handle the rest.

#include <joybus/joybus.h>
struct joybus_rp2xxx rp2xxx_bus;
struct joybus *bus = JOYBUS(&rp2xxx_bus);
struct joybus_target_gcn_controller controller;
void main() {
// Initialize and enable the Joybus
joybus_rp2xxx_init(&rp2xxx_bus, MY_GPIO, pio0);
// Initialize a GameCube controller target and register it on the bus
// At this point the target will respond to commands from a connected console!
// Modify the input state as needed, for example based on GPIO or ADC readings
while (1) {
// Clear previous button state
controller.input.buttons &= ~JOYBUS_GCN_BUTTON_MASK;
// Simulate pressing the A button
controller.input.buttons |= JOYBUS_GCN_BUTTON_A;
// Simulate setting the analog stick position
controller.input.stick_x = 200;
controller.input.stick_y = 200;
sleep_ms(10);
}
}
static void joybus_target_gcn_controller_init(struct joybus_target_gcn_controller *controller)
Initialize a GameCube controller target as a regular wired controller.
Definition gcn_controller.h:80
#define JOYBUS_TARGET(target)
Macro to cast a concrete Joybus target instance to a generic Joybus target instance.
Definition target.h:37
static int joybus_target_register(struct joybus *bus, struct joybus_target *target)
Enable Joybus "target" mode, and register a target to handle commands.
Definition bus.h:172
GameCube controller Joybus target.
Definition gcn_controller.h:41

License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.