Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
hdw-usb.h File Reference

Detailed Description

Design Philosophy

The USB component uses Espressif's USB Device Driver. It's based on the TinyUSB Human Interface Device Example.

The Swadge primarily functions as a USB gamepad.

The Swadge can also exchange USB data with a host by setting a Swadge mode's swadgeMode_t.fnAdvancedUSB function pointer. If that is set, then USB SET_REPORT and GET_REPORT messages with report_id == 173 will be sent to that function.

For development purposes, the Swadge has advanced USB capabilities to load and execute code from RAM. These capabilites can be read about in advanced_usb_control.h.

Usage

You don't need to call initUsb() or deinitUsb(). The system does this the appropriate time.

sendUsbGamepadReport() should be called whenever there is an updated gamepad state to be sent to the host.

usbSetSwadgeMode() should NOT be called, except during development.

Example

static hid_gamepad_report_t report;
report.buttons = lastBtnState;
void sendUsbGamepadReport(hid_gamepad_report_t *report)
Send a USB gamepad report to the system.
Definition hdw-usb.c:214

Go to the source code of this file.

Data Structures

struct  TU_ATTR_PACKED
 HID Switch Gamepad Protocol Report. More...
 

Typedefs

typedef struct TU_ATTR_PACKED hid_gamepad_ns_report_t
 HID Switch Gamepad Protocol Report.
 
typedef int16_t(* fnAdvancedUsbHandler) (uint8_t *buffer, uint16_t length, uint8_t isGet)
 Function typedef for a callback which will send USB SET_REPORT and GET_REPORT messages to a Swadge mode.
 
typedef void(* fnSetSwadgeMode) (void *mode)
 Function typedef for a function which will switch the Swadge mode.
 

Enumerations

enum  hid_gamepad_ns_button_bm_t {
  GAMEPAD_NS_BUTTON_Y = 0x01 , GAMEPAD_NS_BUTTON_B = 0x02 , GAMEPAD_NS_BUTTON_A = 0x04 , GAMEPAD_NS_BUTTON_X = 0x08 ,
  GAMEPAD_NS_BUTTON_TL = 0x10 , GAMEPAD_NS_BUTTON_TR = 0x20 , GAMEPAD_NS_BUTTON_TL2 = 0x40 , GAMEPAD_NS_BUTTON_TR2 = 0x80 ,
  GAMEPAD_NS_BUTTON_MINUS = 0x100 , GAMEPAD_NS_BUTTON_PLUS = 0x200 , GAMEPAD_NS_BUTTON_THUMBL = 0x400 , GAMEPAD_NS_BUTTON_THUMBR = 0x800 ,
  GAMEPAD_NS_BUTTON_HOME = 0x1000 , GAMEPAD_NS_BUTTON_CAPTURE = 0x2000 , GAMEPAD_NS_BUTTON_Z = 0x4000
}
 Switch Gamepad Buttons Bitmap. More...
 
enum  hid_gamepad_ns_hat_t {
  GAMEPAD_NS_HAT_CENTERED = 8 , GAMEPAD_NS_HAT_UP = 0 , GAMEPAD_NS_HAT_UP_RIGHT = 1 , GAMEPAD_NS_HAT_RIGHT = 2 ,
  GAMEPAD_NS_HAT_DOWN_RIGHT = 3 , GAMEPAD_NS_HAT_DOWN = 4 , GAMEPAD_NS_HAT_DOWN_LEFT = 5 , GAMEPAD_NS_HAT_LEFT = 6 ,
  GAMEPAD_NS_HAT_UP_LEFT = 7
}
 Switch Gamepad HAT/DPAD Buttons (from Linux input event codes) More...
 

Functions

void initUsb (fnSetSwadgeMode _setSwadgeMode, fnAdvancedUsbHandler _advancedUsbHandler, bool redirectPrintf)
 Initialize USB HID device.
 
void deinitUsb (void)
 Deinitialize USB HID device Note, this does nothing as tinyusb_driver_uninstall() doesn't exist.
 
void sendUsbGamepadReport (hid_gamepad_report_t *report)
 Send a USB gamepad report to the system.
 
void usbSetSwadgeMode (void *newMode)
 Set the Swadge mode to the given pointer.
 
void initTusb (const tinyusb_config_t *tusb_cfg, const uint8_t *descriptor)
 Initialize TinyUSB.
 
bool tud_hid_gamepad_report_ns (uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons)
 Send a USB gamepad report to a Switch.
 

Data Structure Documentation

◆ TU_ATTR_PACKED

struct TU_ATTR_PACKED
Data Fields
uint16_t buttons Buttons mask for currently pressed buttons.
uint8_t hat Buttons mask for currently pressed buttons in the DPad/hat.
int8_t x Delta x movement of left analog-stick.
int8_t y Delta y movement of left analog-stick.
int8_t rx Delta Rx movement of analog left trigger.
int8_t ry Delta Ry movement of analog right trigger.
int8_t z Delta z movement of right analog-joystick.
int8_t rz Delta Rz movement of right analog-joystick.

Typedef Documentation

◆ hid_gamepad_ns_report_t

HID Switch Gamepad Protocol Report.

◆ fnAdvancedUsbHandler

typedef int16_t(* fnAdvancedUsbHandler) (uint8_t *buffer, uint16_t length, uint8_t isGet)

Function typedef for a callback which will send USB SET_REPORT and GET_REPORT messages to a Swadge mode.

Parameters
bufferPointer to full command
lengthTotal length of the buffer (command ID included)
isGet0 if this is a SET_REPORT, 1 if this is a GET_REPORT
Returns
The number of bytes returned to the host

◆ fnSetSwadgeMode

typedef void(* fnSetSwadgeMode) (void *mode)

Function typedef for a function which will switch the Swadge mode.

Parameters
modeA pointer to the new Swadge mode. Be very careful with this, it will execute whatever's at this address.

Enumeration Type Documentation

◆ hid_gamepad_ns_button_bm_t

Switch Gamepad Buttons Bitmap.

Enumerator
GAMEPAD_NS_BUTTON_Y 
GAMEPAD_NS_BUTTON_B 
GAMEPAD_NS_BUTTON_A 
GAMEPAD_NS_BUTTON_X 
GAMEPAD_NS_BUTTON_TL 
GAMEPAD_NS_BUTTON_TR 
GAMEPAD_NS_BUTTON_TL2 
GAMEPAD_NS_BUTTON_TR2 
GAMEPAD_NS_BUTTON_MINUS 
GAMEPAD_NS_BUTTON_PLUS 
GAMEPAD_NS_BUTTON_THUMBL 
GAMEPAD_NS_BUTTON_THUMBR 
GAMEPAD_NS_BUTTON_HOME 
GAMEPAD_NS_BUTTON_CAPTURE 
GAMEPAD_NS_BUTTON_Z 

◆ hid_gamepad_ns_hat_t

Switch Gamepad HAT/DPAD Buttons (from Linux input event codes)

Enumerator
GAMEPAD_NS_HAT_CENTERED 

DPAD_CENTERED.

GAMEPAD_NS_HAT_UP 

DPAD_UP.

GAMEPAD_NS_HAT_UP_RIGHT 

DPAD_UP_RIGHT.

GAMEPAD_NS_HAT_RIGHT 

DPAD_RIGHT.

GAMEPAD_NS_HAT_DOWN_RIGHT 

DPAD_DOWN_RIGHT.

GAMEPAD_NS_HAT_DOWN 

DPAD_DOWN.

GAMEPAD_NS_HAT_DOWN_LEFT 

DPAD_DOWN_LEFT.

GAMEPAD_NS_HAT_LEFT 

DPAD_LEFT.

GAMEPAD_NS_HAT_UP_LEFT 

DPAD_UP_LEFT.

Function Documentation

◆ initUsb()

void initUsb ( fnSetSwadgeMode _setSwadgeMode,
fnAdvancedUsbHandler _advancedUsbHandler,
bool redirectPrintf )

Initialize USB HID device.

Parameters
_setSwadgeModeA function that can be called from this component to set the Swadge mode
_advancedUsbHandlerA function that can be called from this component to handle USB commands
redirectPrintftrue to redirect printf() to USB, false to leave it over UART

◆ deinitUsb()

void deinitUsb ( void )

Deinitialize USB HID device Note, this does nothing as tinyusb_driver_uninstall() doesn't exist.

◆ sendUsbGamepadReport()

void sendUsbGamepadReport ( hid_gamepad_report_t * report)

Send a USB gamepad report to the system.

Parameters
reportThe report to send, the current state of all gamepad inputs

◆ usbSetSwadgeMode()

void usbSetSwadgeMode ( void * newMode)

Set the Swadge mode to the given pointer.

Parameters
newModeA pointer to the new mode to set

◆ initTusb()

void initTusb ( const tinyusb_config_t * tusb_cfg,
const uint8_t * descriptor )

Initialize TinyUSB.

Parameters
tusb_cfgThe TinyUSB configuration
descriptorThe descriptor to use for this configuration

◆ tud_hid_gamepad_report_ns()

bool tud_hid_gamepad_report_ns ( uint8_t report_id,
int8_t x,
int8_t y,
int8_t z,
int8_t rz,
int8_t rx,
int8_t ry,
uint8_t hat,
uint16_t buttons )

Send a USB gamepad report to a Switch.

Parameters
report_idShould be HID_ITF_PROTOCOL_NONE
xUnsure
yUnsure
zUnsure
rzUnsure
rxUnsure
ryUnsure
hatA bitmask of hid_gamepad_ns_hat_t
buttonsA bitmask of hid_gamepad_ns_button_bm_t
Returns
Unsure