Fix the python script

This commit is contained in:
s3lph 2020-10-24 19:55:28 +01:00
parent 324cabe40b
commit a0e02681a3

60
main.py
View file

@ -1,54 +1,50 @@
import serial import serial
import uinput 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_p3 = uinput.Device(ev, name="Zockbox3")
device_p4 = uinput.Device(ev, name="Zockbox4") device_p4 = uinput.Device(ev, name="Zockbox4")
p3int = 0 p3int = 0
p4int = 64 p4int = 0
s = serial.Serial('/dev/ttyACM0', 9600) s = serial.Serial('/dev/ttyACM0', 9600)
def handle_ctrl(controller, byte): def handle_ctrl(controller, byte):
if byte % 32 == 0: if byte & 0x20:
controller.emit(uinput.BTN_SOUTH, 1) controller.emit(uinput.BTN_SOUTH, 1)
byte -= 32
else: else:
controller.emit(uinput.BTN_SOUTH, 0) controller.emit(uinput.BTN_SOUTH, 0)
if byte % 16 == 0:
if byte & 0x10:
controller.emit(uinput.BTN_EAST, 1) controller.emit(uinput.BTN_EAST, 1)
byte -= 16
else: else:
controller.emit(uinput.BTN_EAST, 0) controller.emit(uinput.BTN_EAST, 0)
if byte % 8 == 0:
controller.emit(uinput.BTN_DPAD_RIGHT, 1) if byte & 0x08:
byte -= 8 controller.emit(uinput.ABS_X, 100)
elif byte & 0x04:
controller.emit(uinput.ABS_X, 0)
else: else:
controller.emit(uinput.BTN_DPAD_RIGHT, 0) controller.emit(uinput.ABS_X, 50)
if byte % 4 == 0:
controller.emit(uinput.BTN_DPAD_LEFT, 1) if byte & 0x02:
byte -= 4 controller.emit(uinput.ABS_Y, 0)
elif byte & 0x01:
controller.emit(uinput.ABS_Y, 100)
else: else:
controller.emit(uinput.BTN_DPAD_LEFT, 0) controller.emit(uinput.ABS_Y, 50)
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)
while s.isOpen(): while s.isOpen():
control = s.read() control = s.read(2)
ctrlint = int.from_bytes(control, byteorder='big', signed=False) for c in struct.unpack('>BB', control):
if ctrlint < 64 and p3int != ctrlint: i = 3 if c < 64 else 4
p3int = ctrlint c = c & 0x3f
handle_ctrl(device_p3, ctrlint) ctrl = device_p3 if i == 3 else device_p4
if ctrlint >= 64 and p4int != ctrlint: if i == 3:
p4int = ctrlint p3int = c
ctrlint -= 64 else:
handle_ctrl(device_p4, ctrlint) p4int = c
handle_ctrl(ctrl, c)