Last active
March 24, 2018 06:09
-
-
Save BraynStorm/5b9827477dc32a731ccb6291550332f9 to your computer and use it in GitHub Desktop.
Joystick usb analyze. Dump data...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import recordclass | |
from typing import IO | |
from traitlets import Float, Int | |
stop = False | |
events = { | |
'simple': { | |
0x01: 'green', | |
0x02: 'orange', | |
0x03: 'blue', | |
0x04: 'pink', | |
0x05: 'left_1', | |
0x06: 'right_1', | |
0x07: 'left_2', | |
0x08: 'right_2', | |
0x09: 'select', | |
0x0A: 'start', | |
0x0B: 'left_stick_click', | |
0x0C: 'right_stick_click', | |
} | |
} | |
ir = lambda x: int(round(x, 0)) | |
irs = lambda x: str(ir(x)) | |
class Joystick: | |
BUTTON = [ | |
'INVALID', | |
'green', | |
'orange', | |
'blue', | |
'pink', | |
'left_1', | |
'right_1', | |
'left_2', | |
'right_2', | |
'select', | |
'start', | |
'left_stick_click', | |
'right_stick_click', | |
] | |
def __init__(self): | |
# Left Axel | |
self.x1: Float = 0 | |
self.y1: Float = 0 | |
# Right Axel | |
self.x2: Float = 0 | |
self.y2: Float = 0 | |
self.button = [ | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False, | |
False | |
] | |
def __str__(self) -> str: | |
string = "Joystick{x1=" + irs(self.x1 * 100) + \ | |
", y1=" + irs(self.y1 * 100) + \ | |
", x2=" + irs(self.x2 * 100) + \ | |
", y2=" + irs(self.y2 * 100) + \ | |
", buttons=[" | |
for idx, state in enumerate(self.button): | |
if state: | |
string += Joystick.BUTTON[idx] + ', ' | |
if string.endswith(', '): | |
string = string[:-2] | |
string += ']}' | |
return string | |
joystick = Joystick() | |
def handle_simple(message: bytes, stream: IO): | |
# Part A | |
event = message.hex()[-16:] | |
event_top = int(event[:8], 16) | |
event_bottom = int(event[8:], 16) | |
if (event_top & 0x04000400) == 0x04000400: | |
button_id = event_bottom >> 24 | |
event_name = events['simple'][button_id] | |
# Part B | |
message = stream.read(24) | |
event = message.hex()[-16:] | |
# event_top = int(event[:8], 16) | |
event_bottom = int(event[8:], 16) | |
action = event_bottom >> 24 | |
if action == 0: | |
action_name = 'released' | |
joystick.button[button_id] = False | |
else: | |
action_name = 'pressed' | |
joystick.button[button_id] = True | |
# print(event_name, action_name) | |
print(joystick) | |
return True | |
return False | |
dict_axis = { | |
0x00: 'X1', | |
0x01: 'Y1', | |
0x03: 'X2', | |
0x05: 'Y2', | |
0x10: 'PadX', | |
0x11: 'PadY' | |
} | |
def handle_movement(message: bytes, stream: IO): | |
event = message.hex()[-16:] | |
event_top = int(event[:8], 16) # 03_00_02_00 | |
event_bottom = int(event[8:], 16) | |
if event_top != 0 and event_top != 0x03000200: | |
axis = (event_top & 0xFF00) >> 8 | |
amount = event_bottom >> 24 | |
if axis in [0x00, 0x03]: | |
amount -= 127.5 | |
elif axis in [0x10, 0x11]: | |
if axis == 0x10: | |
if amount == 0xFF: | |
amount = -128 | |
elif amount != 0x00: | |
amount = 128 | |
if axis == 0x11: | |
if amount == 0xFF: | |
amount = 128 | |
elif amount != 0x00: | |
amount = -128 | |
else: | |
amount = 127.5 - amount | |
amount = int(round(amount, 0)) | |
if axis == 0x00: | |
joystick.x1 = amount / 128 | |
elif axis == 0x01: | |
joystick.y1 = amount / 128 | |
elif axis == 0x03: | |
joystick.x2 = amount / 128 | |
elif axis == 0x05: | |
joystick.y2 = amount / 128 | |
elif axis == 0x10: | |
joystick.x1 = amount / 128 | |
elif axis == 0x11: | |
joystick.y1 = amount / 128 | |
# print(dict_axis[axis], amount / 128) | |
print(joystick) | |
return True | |
return False | |
def print_event(event: bytes): | |
print('event=' + event.hex(), flush=True) | |
def debug_output(stream: IO): | |
for i in range(5000): | |
event_message = stream.read(24) | |
if not handle_simple(event_message, stream): | |
handle_movement(event_message, stream) | |
if __name__ == '__main__': | |
with open('/dev/input/by-id/usb-DragonRise_Inc._Generic_USB_Joystick-event-joystick', 'rb') as j_stream: | |
debug_output(j_stream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment