From 87296f447a0253c2a02237a6ea38680360f1697f Mon Sep 17 00:00:00 2001 From: DanielToyama Date: Fri, 30 Aug 2024 03:08:48 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=AA=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E8=AF=BB=E5=8F=96=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E7=AB=AF=E5=8F=A3=E6=95=B0=E6=8D=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 662be54..6b509f4 100644 --- a/main.py +++ b/main.py @@ -52,12 +52,12 @@ exp_image_dict = {'41-65-93': 'A1', '87-152-13': 'A2', '213-109-81': 'A3', '23-2 class SerialManager: - p1Serial = serial.Serial(COM_PORT, COM_BAUDRATE) - settingPacket = bytearray([40, 0, 0, 0, 0, 41]) - startUp = False - recvData = "" - def __init__(self): + self.p1Serial = serial.Serial(COM_PORT, COM_BAUDRATE) + self.settingPacket = bytearray([40, 0, 0, 0, 0, 41]) + self.startUp = False + self.recvData = "" + self.touchQueue = queue.Queue() self.data_lock = threading.Lock() self.touchThread = threading.Thread(target=self.touch_thread) @@ -66,6 +66,7 @@ class SerialManager: self.now_touch_keys = [] self.ping_touch_thread() + def start(self): print(f"开始监听 {COM_PORT} 串口...") self.touchThread.start() From cac84d5c2f08c720708e7e4b5e18ac0a437a1ab0 Mon Sep 17 00:00:00 2001 From: DanielToyama Date: Sat, 31 Aug 2024 03:20:23 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BC=98=E5=8C=96adb=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=E6=96=B9=E4=BD=8D=E6=97=B6=E7=9A=84=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/main.py b/main.py index 6b509f4..4b035b1 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ import sys # 编辑好的图片路径 IMAGE_PATH = "./image/image_monitor.png" # 串口号 -COM_PORT = "COM33" +COM_PORT = "COM44" # 比特率 COM_BAUDRATE = 9600 # Android 多点触控数量 @@ -52,6 +52,8 @@ exp_image_dict = {'41-65-93': 'A1', '87-152-13': 'A2', '213-109-81': 'A3', '23-2 class SerialManager: + + def __init__(self): self.p1Serial = serial.Serial(COM_PORT, COM_BAUDRATE) self.settingPacket = bytearray([40, 0, 0, 0, 0, 41]) @@ -66,7 +68,6 @@ class SerialManager: self.now_touch_keys = [] self.ping_touch_thread() - def start(self): print(f"开始监听 {COM_PORT} 串口...") self.touchThread.start() @@ -256,48 +257,49 @@ def getevent(): # 读取实时输出 for line in iter(process.stdout.readline, b''): try: + event = line.decode('utf-8').strip() - _, _, event_type, event_value = event.split() - # print(event_type, int(event_value, 16)) + parts = event.split() + + # 屏蔽没用的东西 + if len(parts) < 4: + continue + + event_type = parts[2] + event_value_hex = parts[3] + event_value = int(event_value_hex, 16) + if event_type == 'ABS_MT_POSITION_X': key_is_changed = True - if not ANDROID_REVERSE_MONITOR: - touch_data[touch_index]["x"] = int(int(event_value, 16) * abs_multi_x) - else: - touch_data[touch_index]["x"] = ANDROID_ABS_MONITOR_SIZE[0] - int(int(event_value, 16) * abs_multi_x) + touch_data[touch_index]["x"] = (ANDROID_ABS_MONITOR_SIZE[0] - event_value * abs_multi_x) if ANDROID_REVERSE_MONITOR else event_value * abs_multi_x + elif event_type == 'ABS_MT_POSITION_Y': key_is_changed = True - if not ANDROID_REVERSE_MONITOR: - touch_data[touch_index]["y"] = int(int(event_value, 16) * abs_multi_y) - else: - touch_data[touch_index]["y"] = ANDROID_ABS_MONITOR_SIZE[1] - int(int(event_value, 16) * abs_multi_y) + touch_data[touch_index]["y"] = (ANDROID_ABS_MONITOR_SIZE[1] - event_value * abs_multi_y) if ANDROID_REVERSE_MONITOR else event_value * abs_multi_y + elif event_type == 'SYN_REPORT': - if not key_is_changed: - continue - # print("Touch Data:", touch_data) - # 向 convert 函数发送数据 - key_is_changed = False - # start_time = time.perf_counter() - convert(touch_data) - # print("单次执行时间:", (time.perf_counter() - start_time) * 1e3, "毫秒") + if key_is_changed: + convert(touch_data) + key_is_changed = False + elif event_type == 'ABS_MT_SLOT': key_is_changed = True - touch_index = int(event_value, 16) + touch_index = event_value if touch_index >= touch_sum: touch_sum = touch_index + 1 + elif event_type == 'ABS_MT_TRACKING_ID': key_is_changed = True - if event_value == "ffffffff": + if event_value_hex == "ffffffff": touch_data[touch_index]['p'] = False touch_sum = max(0, touch_sum - 1) else: touch_data[touch_index]['p'] = True touch_sum += 1 - else: - continue - except Exception: + + except Exception as e: event_error_output = line.decode('utf-8') - if "name" in event_error_output: + if "name" not in event_error_output: continue print(event_error_output) @@ -331,6 +333,7 @@ if __name__ == "__main__": abs_multi_y = ANDROID_ABS_MONITOR_SIZE[1] / ANDROID_ABS_INPUT_SIZE[1] print("当前触控区域X轴放大倍数:", abs_multi_x) print("当前触控区域Y轴放大倍数:", abs_multi_y) + print("当前链接到端口:", COM_PORT) print(('已' if ANDROID_REVERSE_MONITOR else '未') + "开启屏幕反转") serial_manager = SerialManager() serial_manager.start() From 738cff3588f742d755b2d440dee6f026e9a26233 Mon Sep 17 00:00:00 2001 From: DanielToyama Date: Sat, 31 Aug 2024 03:29:08 +0800 Subject: [PATCH 3/5] =?UTF-8?q?COM=E7=BC=BA=E7=9C=81=E5=80=BC=E6=94=B9?= =?UTF-8?q?=E5=9B=9E33?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 4b035b1..6e3efad 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,7 @@ import sys # 编辑好的图片路径 IMAGE_PATH = "./image/image_monitor.png" # 串口号 -COM_PORT = "COM44" +COM_PORT = "COM33" # 比特率 COM_BAUDRATE = 9600 # Android 多点触控数量 From 9caa2855b25ddf2b7e8e7c0e4b6d573c8ae83e3b Mon Sep 17 00:00:00 2001 From: DanielToyama Date: Fri, 18 Oct 2024 00:53:03 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E5=AE=89?= =?UTF-8?q?=E5=8D=93=E8=AE=BE=E5=A4=87=E5=90=8C=E6=97=B6=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++++- config.yaml | 2 ++ main.py | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7967025..49c003c 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ 16. 如果单点延迟低但滑动时延迟极大, 请将脚本中 `TOUCH_THREAD_SLEEP_MODE` 修改为 false, 或者可以调小 `TOUCH_THREAD_SLEEP_DELAY` 的值(如果还是卡请提 issue 反馈) + ## 命令列表 游戏时如果不小心断开连接, 请在控制台输入 `start` 并回车来重新连接游戏 @@ -71,9 +72,13 @@ Q: 在安卓高版本(13,14)上测试触摸区域完全对不上,只有点屏 A: 按步骤修改脚本内 `ANDROID_ABS_MONITOR_SIZE` 和 `ANDROID_ABS_INPUT_SIZE` 配置 +Q: 关闭再打开报错 + +A: 如果直接关闭控制台窗口有可能导致后台进程残留,请使用任务管理器彻底关闭进程或者使用ctrl+z终止程序 + ## 注意 -想要加 2P 的重新复制一下脚本并添加串口 COM4 到 COM44 的转发就好 +想要加 2P 的重新复制一下脚本并添加串口 COM4 到 COM44 的转发,并且在配置文件“SPECIFIED_DEVICES”中指定使用“adb devices”获取到的设备序列号 该脚本仅用于测试, 目前来说打12+及以下应该是问题不大, 12+以上水平不够没试过. diff --git a/config.yaml b/config.yaml index fc34906..d3c9806 100644 --- a/config.yaml +++ b/config.yaml @@ -20,6 +20,8 @@ ANDROID_REVERSE_MONITOR: false TOUCH_THREAD_SLEEP_MODE: false # 每次 sleep 的延迟, 单位: 微秒, 默认 100 微秒 TOUCH_THREAD_SLEEP_DELAY: 100 +#当需要指定触控设备时填上使用“adb devices”获取到的设备序列号,留空则只支持单设备连接 +SPECIFIED_DEVICES: "" # RGB 颜色值对应区块配置 exp_image_dict: diff --git a/main.py b/main.py index 6e3efad..1b560e3 100644 --- a/main.py +++ b/main.py @@ -32,6 +32,8 @@ ANDROID_REVERSE_MONITOR = False TOUCH_THREAD_SLEEP_MODE = False # 每次 sleep 的延迟, 单位: 微秒, 默认 100 微秒 TOUCH_THREAD_SLEEP_DELAY = 100 +#当需要指定触控设备时填上使用“adb devices”获取到的设备序列号,留空则只支持单设备连接 +SPECIFIED_DEVICES = "" exp_list = [ ["A1", "A2", "A3", "A4", "A5", ], @@ -251,7 +253,10 @@ def getevent(): touch_index = 0 # 执行 adb shell getevent 命令并捕获输出 - process = subprocess.Popen(['adb', 'shell', 'getevent', '-l'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) + adb_cmd = 'adb shell getevent -l' + if SPECIFIED_DEVICES: + adb_cmd = 'adb -s ' + SPECIFIED_DEVICES + ' shell getevent -l' + process = subprocess.Popen(adb_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) key_is_changed = False # 读取实时输出 @@ -324,6 +329,7 @@ if __name__ == "__main__": TOUCH_THREAD_SLEEP_MODE = c["TOUCH_THREAD_SLEEP_MODE"] TOUCH_THREAD_SLEEP_DELAY = c["TOUCH_THREAD_SLEEP_DELAY"] exp_image_dict = c["exp_image_dict"] + SPECIFIED_DEVICES = c["SPECIFIED_DEVICES"] else: print("未找到配置文件, 使用默认配置") From be821409cc4626fa53ff153623fa6560b48c7b1e Mon Sep 17 00:00:00 2001 From: DanielToyama Date: Fri, 18 Oct 2024 10:30:13 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3QA=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 49c003c..21ccd88 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ A: 按步骤修改脚本内 `ANDROID_ABS_MONITOR_SIZE` 和 `ANDROID_ABS_INPUT_SI Q: 关闭再打开报错 -A: 如果直接关闭控制台窗口有可能导致后台进程残留,请使用任务管理器彻底关闭进程或者使用ctrl+z终止程序 +A: 如果直接关闭控制台窗口有可能导致后台进程残留,请使用任务管理器彻底关闭进程或者使用Ctrl+C终止程序 ## 注意