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 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)