From 7b63c3d89ded13f3f7dc15dc8db0384d1e1a6b0e Mon Sep 17 00:00:00 2001 From: gsd Date: Sat, 15 Nov 2025 22:30:45 +0300 Subject: [PATCH] python io --- pipboyIO/serialTest.py | 20 +++++++--- pipboyIO/serialX.py | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 pipboyIO/serialX.py diff --git a/pipboyIO/serialTest.py b/pipboyIO/serialTest.py index 33feaed..dd227d8 100644 --- a/pipboyIO/serialTest.py +++ b/pipboyIO/serialTest.py @@ -2,6 +2,10 @@ import serial import sys import argparse +#100x - button +#10x - encoder +#1x - variable resitor + class Const: _ = { 35:"Крутилка 5", @@ -13,20 +17,24 @@ class Const: } FULL = 100 +class MOVEMENT: + RIGHT = 1 + NONE = 0 + LEFT = -1 + class Pin: def __init__(self, id, initValue): self.id = id self.value = initValue self.name = Const._[id] if id in Const._ else None - def updateValue(self, value): + def updateValue(self, value): #(Pin.class, value, movement) if self.value == value: - return (self, 0) - #elif abs(self.value - value) in [1,2,3,4]: - # return (self, 0) + return (self, 0, MOVEMENT.NONE) else: + movement = MOVEMENT.LEFT if value < self.value else MOVEMENT.RIGHT self.value = value - return (self, value) + return (self, value, movement) class SerialListener: ser: serial.Serial = None @@ -46,7 +54,7 @@ class SerialListener: while True: try: if self.ser.in_waiting > 0: - data = self.ser.readline().decode().split("\n")[0].split("-") + data = self.ser.readline().decode().split("\n")[0].split("*") pin = int(data[0]) value = int(data[1]) if pin not in self.pins: diff --git a/pipboyIO/serialX.py b/pipboyIO/serialX.py new file mode 100644 index 0000000..a7117bb --- /dev/null +++ b/pipboyIO/serialX.py @@ -0,0 +1,84 @@ +from serialTest import SerialListener +from serialTest import MOVEMENT +import os +import subprocess + +#5 - 101 - a d +#4 - xxx +#3 - 104 - 1 2 3 4 5 +#2 - 103 - hz +#1 - 102 - w s +#upButton = 1001 + +class ENTER: + def __init__(self): + self.pin = 1001 + self.enter = "xdotool key Return".split() + def __call__(self): + 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) + +if __name__ == "__main__": + os.environ['DISPLAY'] = ':0' + binds = { + 101: AD(), + 104: MODE(), + 102: WS(), + 1001: ENTER() + } + + listener = SerialListener() + for pin in listener.listen(): + if pin[0].id in binds: + binds[pin[0].id](pin)