Skip to main content

Library for basic xbox joystick controllers.

Project description

docs

Documentation Status

package

PyPI Package latest release PyPI Wheel Supported versions

Library for basic xbox joystick controllers.

  • Free software: MIT License

Installation

pip install robot-xbox-joystick

You can also install the in-development version with:

pip install https://github.com/fmorton/robot-xbox-joystick/archive/main.zip

Xbox Joystick Example

import pygame

from robot.xbox_joystick import XboxJoystick

joystick = XboxJoystick().connect()

print("Joystick connected")

running = True

while running:
  for event in pygame.event.get():
      if event.type == pygame.QUIT:
          running = False
      elif event.type == pygame.JOYAXISMOTION:
          print(f"Joystick axis {event.axis} value {event.value}")
      elif event.type == pygame.JOYBUTTONDOWN:
          print(f"Button {event.button} pressed on joystick {event.joy}")

          if event.button == XboxJoystick.BUTTON_XBOX:
              running = False

Xbox Joystick Example Using run() and the Joystick State

The run() loop maintains the state of the joysticks and all buttons for processing by your supplied method, “robot” in the example below.

from robot.xbox_joystick import XboxJoystick
from time import sleep


def robot(joystick):
  print("runner", joystick.state.state_string())

  if joystick.state.button_down_milliseconds(XboxJoystick.BUTTON_XBOX):
      return False

  sleep(XboxJoystick.EVENT_LOOP_DELAY)

  return True


joystick = XboxJoystick().connect()

joystick.run(robot)

The joystick.state has the following methods to retrieve the selections from the controller.

joystick.state.left_x()        # returns x value of left joystick (-1 means max forward/1 max backward)
joystick.state.left_y()        # returns y value of left joystick (-1 means max left/1 max right)
joystick.state.left_axis()     # returns left_x, left_y

joystick.state.right_x()       # returns x value of right joystick (-1 means max forward/1 max backward)
joystick.state.right_y()       # returns y value of right joystick (-1 means max left/1 max right)
joystick.state.right_axis()    # returns right_x, right_y

joystick.state.left_trigger()       # returns left trigger value (0 to 1 if fully pressed)
joystick.state.right_trigger()      # returns right trigger value (0 to 1 if fully pressed)

joystick.state.left_trigger_raw()   # returns left trigger unadjusted value (-1 to 1 if fully pressed)
joystick.state.right_trigger_raw()   # returns right trigger unadjusted value (-1 to 1 if fully pressed)

joystick.state.button_down_seconds(button)       # how many seconds "button" has been pressed
joystick.state.button_down_milliseconds(button)  # how many milliseconds "button" has been pressed

The following button constant values are available in XboxJoystick:

BUTTON_A = 0
BUTTON_B = 1
BUTTON_X = 2
BUTTON_Y = 3
BUTTON_VIEW = 4   #  left middle button
BUTTON_XBOX = 5   #  center middle button
BUTTON_SHARE = 6  # right middle button
BUTTON_LEFT_JOYSTICK = 7
BUTTON_RIGHT_JOYSTICK = 8
BUTTON_LEFT_BUMPER = 9
BUTTON_RIGHT_BUMPER = 10
BUTTON_FORWARD = 11
BUTTON_BACKWARD = 12
BUTTON_LEFT = 13
BUTTON_RIGHT = 14

Testing

To run all the tests run:

pytest -s

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

robot_xbox_joystick-0.0.10.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

robot_xbox_joystick-0.0.10-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file robot_xbox_joystick-0.0.10.tar.gz.

File metadata

  • Download URL: robot_xbox_joystick-0.0.10.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for robot_xbox_joystick-0.0.10.tar.gz
Algorithm Hash digest
SHA256 37a8b8be76682e4ae3179ba037e2a7a22841671234ae8eeb851451a36181fec1
MD5 9c08066e2d6ab04134862c1f2b44efcd
BLAKE2b-256 d3a6d6ef6d9e20197d20d661c83a2b021f1fa598f7009a1a4b3806d0811cc273

See more details on using hashes here.

File details

Details for the file robot_xbox_joystick-0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for robot_xbox_joystick-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 c7fa9c76c4ac082665be9ce4e5584aa08a13fcff970f8a04f0caee50ec07d795
MD5 8097c2e5b2894c9af21abb59b9ea0374
BLAKE2b-256 d07c938fc05e91013d00f64e0ce125ff79a33f85c451b91ae6a38cf8bc1249e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page