Merge pull request #12 from DanielToyama/main

优化了adb提取方位的效率
main^2
illegal prompt 2024-08-31 06:01:09 +08:00 committed by GitHub
commit 4ae8d0c1e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 25 deletions

53
main.py
View File

@ -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()