API Reference
relic_usb_host_gamepad
CircuitPython USB host driver for game controller devices.
Author(s): Cooper Dalrymple
Implementation Notes
Hardware:
Software and Dependencies:
Adafruit CircuitPython firmware for the supported boards: https://circuitpython.org/downloads
Adafruit’s USB Host Descriptors library: https://github.com/adafruit/Adafruit_CircuitPython_USB_Host_Descriptors
- relic_usb_host_gamepad.BUTTON_A = 0
The ID of the “A” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_B = 1
The ID of the “B” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_DOWN = 5
The ID of the “D-Pad Down” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_HOME = 10
The ID of the “Home” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_JOYSTICK_DOWN = 18
The ID of the “Joystick Down” button which is triggered when the left joystick exceeds the analog threshold in the down direction. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_JOYSTICK_LEFT = 19
The ID of the “Joystick Left” button which is triggered when the left joystick exceeds the analog threshold in the left direction. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_JOYSTICK_RIGHT = 20
The ID of the “Joystick Right” button which is triggered when the left joystick exceeds the analog threshold in the right direction. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_JOYSTICK_UP = 17
The ID of the “Joystick Up” button which is triggered when the left joystick exceeds the analog threshold in the up direction. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_L1 = 11
The ID of the “L1” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_L2 = 13
The ID of the “L2” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_L3 = 15
The ID of the “L3” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_LEFT = 6
The ID of the “D-Pad Left” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_NAMES = ('A', 'B', 'X', 'Y', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'START', 'SELECT', 'HOME', 'L1', 'R1', 'L2', 'R2', 'L3', 'R3', 'JOYSTICK_UP', 'JOYSTICK_DOWN', 'JOYSTICK_LEFT', 'JOYSTICK_RIGHT', 'TOUCH_PAD')
A list of all button names following the appropriate key number order. Useful for print statements.
- relic_usb_host_gamepad.BUTTON_R1 = 12
The ID of the “R1” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_R2 = 14
The ID of the “R2” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_R3 = 16
The ID of the “R3” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_RIGHT = 7
The ID of the “D-Pad Right” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_SELECT = 9
The ID of the “Select” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_START = 8
The ID of the “Start” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_TOUCH_PAD = 21
The ID of the “Touch Pad” button. At the moement, this button is only supported by PlayStation DUALSHOCK 4 controllers. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_UP = 4
The ID of the “D-Pad Up” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_X = 2
The ID of the “X” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- relic_usb_host_gamepad.BUTTON_Y = 3
The ID of the “Y” button. Used as the
keypad.Event.key_numberattribute inGamepad.events.
- class relic_usb_host_gamepad.Buttons
The class which handles the state of each digital button of a
Gamepaddevice.Initializes the state of all digital button inputs.
- JOYSTICK_DOWN: bool
Whether or not the “Joystick Down” button is pressed which occurs when the left joystick exceeds the analog threshold in the down direction.
- JOYSTICK_LEFT: bool
Whether or not the “Joystick Left” button is pressed which occurs when the left joystick exceeds the analog threshold in the left direction.
- JOYSTICK_RIGHT: bool
Whether or not the “Joystick Right” button is pressed which occurs when the left joystick exceeds the analog threshold in the right direction.
- JOYSTICK_UP: bool
Whether or not the “Joystick Up” button is pressed which occurs when the left joystick exceeds the analog threshold in the up direction.
- TOUCH_PAD: bool
Whether or not the “Touch Pad” is pressed. Only supported by PlayStation DUALSHOCK 4 controllers.
- property changed: bool
Whether or not the state of any buttons has changed since the last
Gamepaddevice update.
- property events: tuple
A list of all changed button states since the last
Gamepaddevice update represented askeypad.Eventobjects. Thekeypad.Event.key_numbervalue represents the button ID.
- relic_usb_host_gamepad.DEVICE_NAMES = ('Unknown', 'Switch Pro Controller', 'Adafruit SNES Controller', '8BitDo Zero 2', 'Generic XInput', 'PowerA Wired Controller', 'PlayStation DUALSHOCK 4 Controller', 'HID Joystick')
A list of all device names following the appropriate device type id. Useful for print statements.
- relic_usb_host_gamepad.DEVICE_TYPE_8BITDO_ZERO2 = 3
The type of a usb gamepad device which has been identified as an 8BitDo Zero 2 controller.
- relic_usb_host_gamepad.DEVICE_TYPE_ADAFRUIT_SNES = 2
The type of a usb gamepad device which has been identified as an Adafruit SNES controller.
- relic_usb_host_gamepad.DEVICE_TYPE_HID_JOYSTICK = 7
The type of a usb joystick device which is HID-compatible.
- relic_usb_host_gamepad.DEVICE_TYPE_PLAYSTATION_DS4 = 6
The type of a usb gamepad device which has been identified as a wired Sony PlayStation DUALSHOCK 4 controller.
- relic_usb_host_gamepad.DEVICE_TYPE_POWERA_WIRED = 5
The type of a usb gamepad device which has been identified as a PowerA Wired Controller.
- relic_usb_host_gamepad.DEVICE_TYPE_SWITCH_PRO = 1
The type of a usb gamepad device which has been identified as a Switch Pro Controller.
- relic_usb_host_gamepad.DEVICE_TYPE_UNKNOWN = 0
An unknown usb device.
- relic_usb_host_gamepad.DEVICE_TYPE_XINPUT = 4
The type of a usb gamepad device which has been identified as an X-Input compatible controller.
- class relic_usb_host_gamepad.Gamepad(port: int = None, debug: bool = False)
Helper class which coordinates device identification, initialization and reading for supported USB gamepad devices.
Initializes the
Gamepaddevice helper.- Parameters:
port – If using a USB hub such as the CH334F, you can specify the desired physical port to communicate with. This is useful for reading from multiple gamepad devices with a specific device location for each. Use
Noneto allow this class to communicate with devices on any USB port.debug – Set this value to
Trueto generate verbose debug messages over REPL.
- property device_type: int
The id of the device type if a usb gamepad device is connected. Otherwise, it will be
DEVICE_TYPE_UNKNOWN.
- property events: tuple
A tuple of all changed button states since the last successful update as
keypad.Eventobjects. Thekeypad.Event.key_numbervalue represents the button ID.
- property joystick_deadzone: float
A value from 0.0 to 1.0 which controls the area at which an analog joystick is treated as 0. All values above the deadzone will be scaled to the full range. Defaults to 0.1.
- property joystick_threshold: float
A value from 0.0 to 1.0 which controls the level at which the left analog joystick will activate the
JOYSTICK_UP,JOYSTICK_DOWN,JOYSTICK_LEFT, orJOYSTICK_RIGHTbuttons. Defaults to 0.25.
- property left_joystick: tuple
The position of the left analog joystick on each axis from -1.0 to 1.0 represented as a tuple with the format (x, y).
- property left_joystick_invert_x: bool
Whether or not the invert the direction of the X-axis of
Gamepad.left_joystick. Also affectsJOYSTICK_LEFT, andJOYSTICK_RIGHTbuttons. Changes take effect after the next successful device update. Defaults toFalse.
- property left_joystick_invert_y: bool
Whether or not the invert the direction of the Y-axis of
Gamepad.left_joystick. Also affectsJOYSTICK_UP, andJOYSTICK_DOWNbuttons. Changes take effect after the next successful device update. Defaults toFalse.
- property right_joystick: tuple
The position of the right analog joystick on each axis from -1.0 to 1.0 represented as a tuple with the format (x, y).
- property right_joystick_invert_x: bool
Whether or not the invert the direction of the X-axis of
Gamepad.right_joystick. Changes take effect after the next successful device update. Defaults toFalse.
- property right_joystick_invert_y: bool
Whether or not the invert the direction of the Y-axis of
Gamepad.right_joystick. Changes take effect after the next successful device update. Defaults toFalse.
- property trigger_threshold: float
A value from 0.0 to 1.0 which controls the level at which an analog trigger will activate the
BUTTON_L2orBUTTON_R2buttons. Defaults to 0.5.
- update() bool
Update the gamepad device. If no device is current active, it will attempt to identify and connect with a USB device at most once every second. If a device is active, it will poll it and update the gamepad state. If the device is deemed that it is no longer responsive, it will be automatically disconnected.
- Returns:
Whether or not the state of the gamepad was updated.