From 324cabe40b85afc94b1aa08f2b1053e98454f1b8 Mon Sep 17 00:00:00 2001 From: SPiNNiX Date: Sat, 24 Oct 2020 01:05:03 +0200 Subject: [PATCH] Initial commit --- amstradcpc2serial.ino | 72 +++++++++++++++++++++++++++++++++++++++++++ main.py | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 amstradcpc2serial.ino create mode 100644 main.py diff --git a/amstradcpc2serial.ino b/amstradcpc2serial.ino new file mode 100644 index 0000000..a1d27bf --- /dev/null +++ b/amstradcpc2serial.ino @@ -0,0 +1,72 @@ +const int pl1up = 44; +const int p11dn = 46; +const int p11lf = 42; +const int p11rg = 48; +const int p11f2 = 50; +const int p11f1 = 40; +const int pl2up = 30; +const int pl2dn = 32; +const int pl2lf = 28; +const int pl2rg = 34; +const int pl2f2 = 36; +const int pl2f1 = 26; + +void setup() { + Serial.begin(9600); + pinMode(pl1up, INPUT_PULLUP); + pinMode(p11dn, INPUT_PULLUP); + pinMode(p11lf, INPUT_PULLUP); + pinMode(p11rg, INPUT_PULLUP); + pinMode(p11f2, INPUT_PULLUP); + pinMode(p11f1, INPUT_PULLUP); + pinMode(pl2up, INPUT_PULLUP); + pinMode(pl2dn, INPUT_PULLUP); + pinMode(pl2lf, INPUT_PULLUP); + pinMode(pl2rg, INPUT_PULLUP); + pinMode(pl2f2, INPUT_PULLUP); + pinMode(pl2f1, INPUT_PULLUP); +} + +void loop() { + uint8_t pl1_state = 0; + if (digitalRead(pl1up) == LOW) { + pl1_state += 1; + } + if (digitalRead(p11dn) == LOW) { + pl1_state += 2; + } + if (digitalRead(p11lf) == LOW) { + pl1_state += 4; + } + if (digitalRead(p11rg) == LOW) { + pl1_state += 8; + } + if (digitalRead(p11f2) == LOW) { + pl1_state += 16; + } + if (digitalRead(p11f1) == LOW) { + pl1_state += 32; + } + Serial.write(pl1_state); + uint8_t pl2_state = 64; + if (digitalRead(pl2up) == LOW) { + pl2_state += 1; + } + if (digitalRead(pl2dn) == LOW) { + pl2_state += 2; + } + if (digitalRead(pl2lf) == LOW) { + pl2_state += 4; + } + if (digitalRead(pl2rg) == LOW) { + pl2_state += 8; + } + if (digitalRead(pl2f2) == LOW) { + pl2_state += 16; + } + if (digitalRead(pl2f1) == LOW) { + pl2_state += 32; + } + Serial.write(pl2_state); + delay(50); +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..82e35f9 --- /dev/null +++ b/main.py @@ -0,0 +1,54 @@ +import serial +import uinput + +ev = (uinput.BTN_SOUTH, uinput.BTN_EAST, uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT) +device_p3 = uinput.Device(ev, name="Zockbox3") +device_p4 = uinput.Device(ev, name="Zockbox4") + +p3int = 0 +p4int = 64 + +s = serial.Serial('/dev/ttyACM0', 9600) + +def handle_ctrl(controller, byte): + if byte % 32 == 0: + controller.emit(uinput.BTN_SOUTH, 1) + byte -= 32 + else: + controller.emit(uinput.BTN_SOUTH, 0) + if byte % 16 == 0: + 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 + else: + controller.emit(uinput.BTN_DPAD_RIGHT, 0) + if byte % 4 == 0: + controller.emit(uinput.BTN_DPAD_LEFT, 1) + byte -= 4 + 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) + + +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)