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.

Supported Platforms

The following platforms are currently supported:

  • Raspberry Pi RP2040 and RP2350 series MCUs
  • Silicon Labs EFR32 Series 1 and Series 2 MCUs

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>
#include <joybus/host/gamecube.h>
struct joybus_rp2xxx rp2xxx_bus;
struct joybus *bus = JOYBUS(&rp2xxx_bus);
uint8_t joybus_buffer[JOYBUS_BLOCK_SIZE];
void poll_cb(struct joybus *bus, int result, void *user_data) {
// Check for errors
// TODO
// Unpack the input data from the response
// Do something with the input data
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);
while (1) {
// Read a GameCube controller in analog mode 3 with the rumble motor off
poll_cb, NULL);
sleep_ms(10);
}
}
@ JOYBUS_GCN_MOTOR_STOP
Stop the rumble motor.
Definition gamecube.h:86
@ JOYBUS_GCN_ANALOG_MODE_3
Substick X/Y and triggers full precision, analog A/B omitted.
Definition gamecube.h:75
#define JOYBUS_GCN_BUTTON_A
GameCube controller button bitmask flags.
Definition gamecube.h:11
int joybus_rp2xxx_init(struct joybus_rp2xxx *rp2xxx_bus, uint8_t gpio, PIO pio)
Initialize a RP2xxx Joybus instance.
Definition joybus.c:427
int joybus_gcn_read(struct joybus *bus, enum joybus_gcn_analog_mode analog_mode, enum joybus_gcn_motor_state motor_state, uint8_t *response, joybus_transfer_cb_t callback, void *user_data)
Read the current input state of a GameCube controller.
Definition gamecube.c:6
int joybus_gcn_unpack_input(struct joybus_gc_controller_input *dest, const uint8_t *src, enum joybus_gcn_analog_mode analog_mode)
Unpack raw input data from a GameCube controller.
Definition gamecube.c:68
static int joybus_enable(struct joybus *bus)
Enable the Joybus instance.
Definition bus.h:71
#define JOYBUS_BLOCK_SIZE
Maximum size of a Joybus transfer, in bytes.
Definition bus.h:24
#define JOYBUS(bus)
Macro to cast a backend-specific Joybus instance to a generic Joybus instance.
Definition bus.h:35
GameCube controller input state.
Definition gamecube.h:32
A RP2xxx Joybus instance.
Definition rp2xxx.h:60
A Joybus instance.
Definition bus.h:59

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.

#include <joybus/joybus.h>
#include <joybus/target/gc_controller.h>
struct joybus_rp2xxx rp2xxx_bus;
struct joybus *bus = JOYBUS(&rp2xxx_bus);
struct joybus_gc_controller controller;
void main() {
// Initialize the Joybus
joybus_rp2xxx_init(&rp2xxx_bus, MY_GPIO, pio0);
// Initialize a GameCube controller target
// Register the target 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);
}
}
#define JOYBUS_GAMECUBE_CONTROLLER
Device type for a standard GameCube controller.
Definition commands.h:116
void joybus_gc_controller_init(struct joybus_gc_controller *controller, uint16_t type)
Initialize a GameCube controller.
Definition gc_controller.c:341
#define JOYBUS_TARGET(target)
Macro to cast a concrete Joybus target instance to a generic Joybus target instance.
Definition target.h:15
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:115
GameCube controller Joybus target.
Definition gc_controller.h:38