Skip to content

Commit

Permalink
Support for deadzone customization
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuzu-Typ committed Jul 2, 2019
1 parent cb31517 commit c76b5b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It can be inmported like this:
XInput\-Python provides a few functions:
`get_connected() -> (bool, bool, bool, bool)` Query which controllers are connected \(note: don't query each frame\)

`get_state(user_index) -> State` Get the State of the controller `user_index`
`get_state(user_index) -> State` Gets the State of the controller `user_index`

`get_button_values(state) -> dict` Returns a dictionary, showing which buttons are currently being pressed\.

Expand All @@ -29,6 +29,15 @@ XInput\-Python provides a few functions:

`get_battery_information(user_index) -> (<type>, <level>)` Returns the battery information for `user_index`

`set_deadzone(deadzone, value) -> None` Sets the deadzone values for left/right thumb stick and triggers\.

The following deadzones exist:
`XInput.DEADZONE_LEFT_THUMB` \- \(range 0 to 32767\) Left thumb stick deadzone \(default is 7849\)

`XInput.DEADZONE_RIGHT_THUMB` \- \(range 0 to 32767\) Right thumb stick deadzone \(default is 8689\)

`XInput.DEADZONE_TRIGGER` \- \(range 0 to 255\) Trigger deadzone \(default is 30\)

#### Using Events
You can also use the Event\-system:

Expand Down
11 changes: 10 additions & 1 deletion README.rml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It can be inmported like this:
XInput-Python provides a few functions:
[code]get_connected() -> (bool, bool, bool, bool)[/code] Query which controllers are connected (note: don't query each frame)

[code]get_state(user_index) -> State[/code] Get the State of the controller [code]user_index[/code]
[code]get_state(user_index) -> State[/code] Gets the State of the controller [code]user_index[/code]

[code]get_button_values(state) -> dict[/code] Returns a dictionary, showing which buttons are currently being pressed.

Expand All @@ -25,6 +25,15 @@ XInput-Python provides a few functions:

[code]get_battery_information(user_index) -> (<type>, <level>)[/] Returns the battery information for [code]user_index[/]

[code]set_deadzone(deadzone, value) -> None[/] Sets the deadzone values for left/right thumb stick and triggers.

The following deadzones exist:
[code]XInput.DEADZONE_LEFT_THUMB[/] - (range 0 to 32767) Left thumb stick deadzone (default is 7849)

[code]XInput.DEADZONE_RIGHT_THUMB[/] - (range 0 to 32767) Right thumb stick deadzone (default is 8689)

[code]XInput.DEADZONE_TRIGGER[/] - (range 0 to 255) Trigger deadzone (default is 30)

[s3]Using Events[/]
You can also use the Event-system:
[code]events = get_events()[/code]
Expand Down
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Using XInput\-Python
| XInput\-Python provides a few functions\:
| :code:`get_connected() -> (bool, bool, bool, bool)` Query which controllers are connected \(note\: don\'t query each frame\)
|
| :code:`get_state(user_index) -> State` Get the State of the controller :code:`user_index`
| :code:`get_state(user_index) -> State` Gets the State of the controller :code:`user_index`
|
| :code:`get_button_values(state) -> dict` Returns a dictionary\, showing which buttons are currently being pressed\.
|
Expand All @@ -49,6 +49,15 @@ Using XInput\-Python
|
| :code:`get_battery_information(user_index) -> (<type>, <level>)` Returns the battery information for :code:`user_index`
|
| :code:`set_deadzone(deadzone, value) -> None` Sets the deadzone values for left\/right thumb stick and triggers\.
|
| The following deadzones exist\:
| :code:`XInput.DEADZONE_LEFT_THUMB` \- \(range 0 to 32767\) Left thumb stick deadzone \(default is 7849\)
|
| :code:`XInput.DEADZONE_RIGHT_THUMB` \- \(range 0 to 32767\) Right thumb stick deadzone \(default is 8689\)
|
| :code:`XInput.DEADZONE_TRIGGER` \- \(range 0 to 255\) Trigger deadzone \(default is 30\)
|
Using Events
^^^^^^^^^^^^
Expand Down
53 changes: 51 additions & 2 deletions XInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,56 @@ def XInputGetBatteryInformation(dwUserIndex, devType, batteryInformation):

_last_checked = 0

DEADZONE_LEFT_THUMB = 0
DEADZONE_RIGHT_THUMB = 1
DEADZONE_TRIGGER = 2

DEADZONE_DEFAULT = -1

_deadzones = [{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD},
{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD},
{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD},
{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD}]

class XInputNotConnectedError(Exception):
pass

class XInputBadArgumentError(ValueError):
pass

def set_deadzone(dzone, value):
global XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE, XINPUT_GAMEPAD_TRIGGER_THRESHOLD

assert dzone >= 0 and dzone <= 2, "invalid deadzone"

if value == DEADZONE_DEFAULT:
value = 7849 if dzone == DEADZONE_LEFT_THUMB else \
8689 if dzone == DEADZONE_RIGHT_THUMB else \
30

if dzone == DEADZONE_LEFT_THUMB:
assert value >= 0 and value <= 32767
if value == DEADZONE_DEFAULT: XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = 7849
else: XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = value

elif dzone == DEADZONE_RIGHT_THUMB:
assert value >= 0 and value <= 32767
if value == DEADZONE_DEFAULT: XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = 8689
else: XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = value

else:
assert value >= 0 and value <= 255
if value == DEADZONE_DEFAULT: XINPUT_GAMEPAD_TRIGGER_THRESHOLD = 30
else: XINPUT_GAMEPAD_TRIGGER_THRESHOLD = value

def get_connected():
state = XINPUT_STATE()
out = [False] * 4
Expand Down Expand Up @@ -424,6 +468,8 @@ def get_events():
root.title("XInput")
canvas = tk.Canvas(root, width= 600, height = 400, bg="white")
canvas.pack()

set_deadzone(DEADZONE_TRIGGER,10)

class Controller:
def __init__(self, center):
Expand Down Expand Up @@ -613,6 +659,9 @@ def __init__(self, center):
canvas.itemconfig(controller.Y_button, fill="")
elif event.button == "X":
canvas.itemconfig(controller.X_button, fill="")

root.update()

try:
root.update()
except tk.TclError:
break

0 comments on commit c76b5b6

Please sign in to comment.