Skip to content

Instantly share code, notes, and snippets.

@danricho
Last active December 20, 2020 04:55
Show Gist options
  • Save danricho/d64140a29ba0407ba0736cf171982a25 to your computer and use it in GitHub Desktop.
Save danricho/d64140a29ba0407ba0736cf171982a25 to your computer and use it in GitHub Desktop.
Test of XInput-Python library with Xbox Series X controller.
# library: https://github.com/Zuzu-Typ/XInput-Python
# install using: pip install XInput-Python
#
# Tested with Xbox Series X controller on Windows 10, Python 3.8.6
# Controller firmware up to date on 20/12/2020 (Xbox Accessory app on PC)
# New "Share" Button inoperative.
# Got status of battery pack (Xbox Rechargable Battery with USB-C Cable)
# Tested with cable not bluetooth.
from XInput import *
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
set_deadzone(DEADZONE_TRIGGER,10)
controllers = {}
decimals = 1
while 1:
events = get_events()
for event in events:
print(event)
update = False
index = event.user_index
id = "cont_" + str(event.user_index)
if id not in controllers:
controllers[id] = dotdict({
"stick_left": {"pos": (0,0), "press": False},
"stick_right": {"pos": (0,0), "press": False},
"trigger_left": {"pos": 0},
"trigger_right": {"pos": 0},
"shoulder_left": {"press": False},
"shoulder_right": {"press": False},
"btn_back": {"press": False},
"btn_start": {"press": False},
"dpad": {"up": False, "down": False, "left": False, "right": False},
"btn_a": {"press": False},
"btn_b": {"press": False},
"btn_y": {"press": False},
"btn_x": {"press": False},
})
update = True
if event.type == EVENT_CONNECTED:
controllers[id]["connected"] = True
update = True
elif event.type == EVENT_DISCONNECTED:
controllers[id]["connected"] = False
update = True
elif event.type == EVENT_STICK_MOVED:
if event.stick == LEFT:
new_pos = (round(event.x,decimals), round(event.y,decimals))
if new_pos != controllers[id]["stick_left"]["pos"]:
controllers[id]["stick_left"]["pos"] = new_pos
update = True
elif event.stick == RIGHT:
new_pos = (round(event.x,decimals), round(event.y,decimals))
if new_pos != controllers[id]["stick_right"]["pos"]:
controllers[id]["stick_right"]["pos"] = new_pos
update = True
elif event.type == EVENT_TRIGGER_MOVED:
if event.trigger == LEFT:
new_pos = round(event.value,decimals)
if new_pos != controllers[id]["trigger_left"]["pos"]:
controllers[id]["trigger_left"]["pos"] = new_pos
update = True
elif event.trigger == RIGHT:
new_pos = round(event.value,decimals)
if new_pos != controllers[id]["trigger_right"]["pos"]:
controllers[id]["trigger_right"]["pos"] = new_pos
update = True
elif event.type in [EVENT_BUTTON_PRESSED,EVENT_BUTTON_RELEASED]:
value = (event.type == EVENT_BUTTON_PRESSED)
update = True
if event.button == "LEFT_THUMB":
controllers[id]["stick_left"]["press"] = value
elif event.button == "RIGHT_THUMB":
controllers[id]["stick_right"]["press"] = value
elif event.button == "LEFT_SHOULDER":
controllers[id]["shoulder_left"]["press"] = value
elif event.button == "RIGHT_SHOULDER":
controllers[id]["shoulder_right"]["press"] = value
elif event.button == "BACK":
controllers[id]["btn_back"]["press"] = value
elif event.button == "START":
controllers[id]["btn_start"]["press"] = value
elif event.button == "BACK":
controllers[id]["btn_back"]["press"] = value
elif event.button == "START":
controllers[id]["btn_start"]["press"] = value
elif event.button == "DPAD_LEFT":
controllers[id]["dpad"]["left"] = value
elif event.button == "DPAD_RIGHT":
controllers[id]["dpad"]["right"] = value
elif event.button == "DPAD_UP":
controllers[id]["dpad"]["up"] = value
elif event.button == "DPAD_DOWN":
controllers[id]["dpad"]["down"] = value
elif event.button == "A":
controllers[id]["btn_a"]["press"] = value
elif event.button == "B":
controllers[id]["btn_b"]["press"] = value
elif event.button == "Y":
controllers[id]["btn_y"]["press"] = value
elif event.button == "X":
controllers[id]["btn_x"]["press"] = value
if update:
controllers[id]["battery"] = get_battery_information(index)
set_vibration(index, int(controllers[id]["trigger_left"]["pos"]*65535), int(controllers[id]["trigger_right"]["pos"]*65535))
print(controllers[id])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment