diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2363b1f Binary files /dev/null and b/.DS_Store differ diff --git a/pipboyIO/config.sh b/config.sh similarity index 100% rename from pipboyIO/config.sh rename to config.sh diff --git a/pipboyIO/pipboy-setup.sh b/pipboy-setup.sh similarity index 100% rename from pipboyIO/pipboy-setup.sh rename to pipboy-setup.sh diff --git a/pipboyIO/.DS_Store b/pipboyIO/.DS_Store new file mode 100644 index 0000000..1b47bf0 Binary files /dev/null and b/pipboyIO/.DS_Store differ diff --git a/pipboyIO/legacy/run.sh b/pipboyIO/legacy/run.sh new file mode 100755 index 0000000..258f839 --- /dev/null +++ b/pipboyIO/legacy/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cd /home/pipboy/Pipboy3Kmark4/pipboyIO/legacy +sudo -u pipboy python3 serialX.py \ No newline at end of file diff --git a/pipboyIO/serialTest.py b/pipboyIO/legacy/serialTest.py similarity index 83% rename from pipboyIO/serialTest.py rename to pipboyIO/legacy/serialTest.py index dd227d8..44e1697 100644 --- a/pipboyIO/serialTest.py +++ b/pipboyIO/legacy/serialTest.py @@ -27,14 +27,20 @@ class Pin: self.id = id self.value = initValue self.name = Const._[id] if id in Const._ else None + self.movement = MOVEMENT.NONE def updateValue(self, value): #(Pin.class, value, movement) if self.value == value: - return (self, 0, MOVEMENT.NONE) + self.value = 0 + self.movement = MOVEMENT.NONE + return (self, self.value, MOVEMENT.NONE) else: - movement = MOVEMENT.LEFT if value < self.value else MOVEMENT.RIGHT + self.movement = MOVEMENT.LEFT if value < self.value else MOVEMENT.RIGHT self.value = value - return (self, value, movement) + return (self, value, self.movement) + + def __call__(self): + return (self, self.value, self.movement) class SerialListener: ser: serial.Serial = None diff --git a/pipboyIO/serialX.py b/pipboyIO/legacy/serialX.py similarity index 100% rename from pipboyIO/serialX.py rename to pipboyIO/legacy/serialX.py diff --git a/pipboyIO/next/requirements.txt b/pipboyIO/next/requirements.txt new file mode 100644 index 0000000..c9a47a2 --- /dev/null +++ b/pipboyIO/next/requirements.txt @@ -0,0 +1 @@ +pyserial-asyncio \ No newline at end of file diff --git a/pipboyIO/next/serialAX.py b/pipboyIO/next/serialAX.py new file mode 100644 index 0000000..94569a7 --- /dev/null +++ b/pipboyIO/next/serialAX.py @@ -0,0 +1,150 @@ +import asyncio +import serial_asyncio +import os, subprocess +import argparse + +#5 - 101 - a d +#4 - xxx +#3 - 104 - 1 2 3 4 5 +#2 - 103 - hz +#1 - 102 - w s +#upButton = 1001 + +class MOVEMENT: + RIGHT = 1 + NONE = 0 + LEFT = -1 + +class Pin: + def __init__(self, id, initValue): + self.id = id + self.value = initValue + self.movement = MOVEMENT.NONE + + def updateValue(self, value): #(Pin.class, value, movement) + if self.value == value: + self.value = 0 + self.movement = MOVEMENT.NONE + return (self, self.value, MOVEMENT.NONE) + else: + self.movement = MOVEMENT.LEFT if value < self.value else MOVEMENT.RIGHT + self.value = value + return (self, value, self.movement) + + def __call__(self): + return (self, self.value, self.movement) + + +class ENTER: + def __init__(self): + self.pin = 1001 + self.enter = "xdotool key Return".split() + def __call__(self, data): + print(self.enter) + subprocess.Popen(self.enter) + +class AD: + def __init__(self): + self.pin = 101 + self.a = "xdotool key a".split() + self.d = "xdotool key d".split() + + def __call__(self, data): + if data[2] == MOVEMENT.LEFT: + print(self.a) + subprocess.Popen(self.a) + if data[2] == MOVEMENT.RIGHT: + print(self.d) + subprocess.Popen(self.d) + +class WS: + def __init__(self): + self.pin = 101 + self.w = "xdotool key w".split() + self.s = "xdotool key s".split() + + def __call__(self, data): + if data[2] == MOVEMENT.LEFT: + print(self.s) + subprocess.Popen(self.s) + if data[2] == MOVEMENT.RIGHT: + print(self.w) + subprocess.Popen(self.w) + +class MODE: + def __init__(self): + self.pin = 104 + self.min_key = 1 + self.max_key = 5 + self.current_key = 1 + + def __call__(self, data): + if data[2] == MOVEMENT.LEFT: + self.current_key -= 1 + if data[2] == MOVEMENT.RIGHT: + self.current_key += 1 + + if (self.current_key < self.min_key): + self.current_key = self.min_key + + if (self.current_key > self.max_key): + self.current_key = self.max_key + + cmd = f"xdotool key {self.current_key}".split() + print(cmd) + subprocess.Popen(cmd) + +class Listener(asyncio.Protocol): + binds = { + 101: AD(), + 104: MODE(), + 102: WS(), + 1001: ENTER() + } + + def __init__(self): + super().__init__() + self.buffer = b"" + self.store = {} + + def connection_made(self, transport): + self.transport = transport + print("Connected") + + def data_received(self, data): + self.buffer += data + lines = self.buffer.split(b'\n') + self.buffer = lines[-1] + + for line in lines[:-1]: + try: + pin, value = line.split("*") + pin = int(pin) + value = int(value) + + if pin not in self.store: + self.store[pin] = Pin(pin, value) + else: + self.store[pin].updateValue(value) + + self.useX(self.store[pin]()) + except: + print("Cannot parse") + + def useX(self, pin): + if pin[0].id in self.binds: + self.binds[pin[0]](pin) + +if __name__ == "__main__": + os.environ['DISPLAY'] = ':0' + + parser = argparse.ArgumentParser() + parser.add_argument("--serial", type=str, default="/dev/serial0") + parser.add_argument("--baudrate", type=int, default=9600) + args = parser.parse_args() + + loop = asyncio.get_event_loop() + coro = serial_asyncio.create_serial_connection(loop, Listener, args.serial, baudrate = args.baudrate) + transport, protocol = loop.run_until_complete(coro) + loop.run_forever() + loop.close() \ No newline at end of file diff --git a/pipboyIO/run.sh b/pipboyIO/run.sh deleted file mode 100755 index 24c789b..0000000 --- a/pipboyIO/run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -cd /home/pipboy/Pipboy3Kmark4/pipboyIO -sudo -u pipboy python3 serialX.py \ No newline at end of file diff --git a/pipboyIO/pipboyUI.sh b/pipboyUI/pipboyUI.sh similarity index 100% rename from pipboyIO/pipboyUI.sh rename to pipboyUI/pipboyUI.sh