From a0e02681a363d6a277f54a04b03770c293343197 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 24 Oct 2020 19:55:28 +0100 Subject: [PATCH] Fix the python script --- main.py | 60 +++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/main.py b/main.py index 82e35f9..dfef15e 100644 --- a/main.py +++ b/main.py @@ -1,54 +1,50 @@ import serial import uinput +import struct -ev = (uinput.BTN_SOUTH, uinput.BTN_EAST, uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT) +ev = (uinput.BTN_SOUTH, uinput.BTN_EAST, uinput.ABS_X + (0, 100, 0, 0), uinput.ABS_Y + (0, 100, 0, 0)) device_p3 = uinput.Device(ev, name="Zockbox3") device_p4 = uinput.Device(ev, name="Zockbox4") p3int = 0 -p4int = 64 +p4int = 0 s = serial.Serial('/dev/ttyACM0', 9600) def handle_ctrl(controller, byte): - if byte % 32 == 0: + if byte & 0x20: controller.emit(uinput.BTN_SOUTH, 1) - byte -= 32 else: controller.emit(uinput.BTN_SOUTH, 0) - if byte % 16 == 0: + + if byte & 0x10: controller.emit(uinput.BTN_EAST, 1) - byte -= 16 else: controller.emit(uinput.BTN_EAST, 0) - if byte % 8 == 0: - controller.emit(uinput.BTN_DPAD_RIGHT, 1) - byte -= 8 + + if byte & 0x08: + controller.emit(uinput.ABS_X, 100) + elif byte & 0x04: + controller.emit(uinput.ABS_X, 0) else: - controller.emit(uinput.BTN_DPAD_RIGHT, 0) - if byte % 4 == 0: - controller.emit(uinput.BTN_DPAD_LEFT, 1) - byte -= 4 + controller.emit(uinput.ABS_X, 50) + + if byte & 0x02: + controller.emit(uinput.ABS_Y, 0) + elif byte & 0x01: + controller.emit(uinput.ABS_Y, 100) else: - controller.emit(uinput.BTN_DPAD_LEFT, 0) - if byte % 2 == 0: - controller.emit(uinput.BTN_DPAD_DOWN, 1) - byte -= 2 - else: - controller.emit(uinput.BTN_DPAD_DOWN, 0) - if byte % 1 == 0: - controller.emit(uinput.BTN_DPAD_UP, 1) - else: - controller.emit(uinput.BTN_DPAD_UP, 0) + controller.emit(uinput.ABS_Y, 50) while s.isOpen(): - control = s.read() - ctrlint = int.from_bytes(control, byteorder='big', signed=False) - if ctrlint < 64 and p3int != ctrlint: - p3int = ctrlint - handle_ctrl(device_p3, ctrlint) - if ctrlint >= 64 and p4int != ctrlint: - p4int = ctrlint - ctrlint -= 64 - handle_ctrl(device_p4, ctrlint) + control = s.read(2) + for c in struct.unpack('>BB', control): + i = 3 if c < 64 else 4 + c = c & 0x3f + ctrl = device_p3 if i == 3 else device_p4 + if i == 3: + p3int = c + else: + p4int = c + handle_ctrl(ctrl, c)