ArduinoAmstradCPCJoystick/main.py

51 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-10-24 01:05:03 +02:00
import serial
import uinput
2020-10-24 20:55:28 +02:00
import struct
2020-10-24 01:05:03 +02:00
2020-10-24 20:55:28 +02:00
ev = (uinput.BTN_SOUTH, uinput.BTN_EAST, uinput.ABS_X + (0, 100, 0, 0), uinput.ABS_Y + (0, 100, 0, 0))
2020-10-24 01:05:03 +02:00
device_p3 = uinput.Device(ev, name="Zockbox3")
device_p4 = uinput.Device(ev, name="Zockbox4")
p3int = 0
2020-10-24 20:55:28 +02:00
p4int = 0
2020-10-24 01:05:03 +02:00
s = serial.Serial('/dev/ttyACM0', 9600)
def handle_ctrl(controller, byte):
2020-10-24 20:55:28 +02:00
if byte & 0x20:
2020-10-24 01:05:03 +02:00
controller.emit(uinput.BTN_SOUTH, 1)
else:
controller.emit(uinput.BTN_SOUTH, 0)
2020-10-24 20:55:28 +02:00
if byte & 0x10:
2020-10-24 01:05:03 +02:00
controller.emit(uinput.BTN_EAST, 1)
else:
controller.emit(uinput.BTN_EAST, 0)
2020-10-24 20:55:28 +02:00
if byte & 0x08:
controller.emit(uinput.ABS_X, 100)
elif byte & 0x04:
controller.emit(uinput.ABS_X, 0)
2020-10-24 01:05:03 +02:00
else:
2020-10-24 20:55:28 +02:00
controller.emit(uinput.ABS_X, 50)
if byte & 0x02:
controller.emit(uinput.ABS_Y, 0)
elif byte & 0x01:
controller.emit(uinput.ABS_Y, 100)
2020-10-24 01:05:03 +02:00
else:
2020-10-24 20:55:28 +02:00
controller.emit(uinput.ABS_Y, 50)
2020-10-24 01:05:03 +02:00
while s.isOpen():
2020-10-24 20:55:28 +02:00
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)