diff --git a/main.py b/main.py index 6b509f4..6e3efad 100644 --- a/main.py +++ b/main.py @@ -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()