优化adb获取屏幕方位时的程序效率

pull/12/head
DanielToyama 2024-08-31 03:20:23 +08:00
parent 87296f447a
commit cac84d5c2f
1 changed files with 29 additions and 26 deletions

55
main.py
View File

@ -13,7 +13,7 @@ import sys
# 编辑好的图片路径 # 编辑好的图片路径
IMAGE_PATH = "./image/image_monitor.png" IMAGE_PATH = "./image/image_monitor.png"
# 串口号 # 串口号
COM_PORT = "COM33" COM_PORT = "COM44"
# 比特率 # 比特率
COM_BAUDRATE = 9600 COM_BAUDRATE = 9600
# Android 多点触控数量 # Android 多点触控数量
@ -52,6 +52,8 @@ exp_image_dict = {'41-65-93': 'A1', '87-152-13': 'A2', '213-109-81': 'A3', '23-2
class SerialManager: class SerialManager:
def __init__(self): def __init__(self):
self.p1Serial = serial.Serial(COM_PORT, COM_BAUDRATE) self.p1Serial = serial.Serial(COM_PORT, COM_BAUDRATE)
self.settingPacket = bytearray([40, 0, 0, 0, 0, 41]) self.settingPacket = bytearray([40, 0, 0, 0, 0, 41])
@ -66,7 +68,6 @@ class SerialManager:
self.now_touch_keys = [] self.now_touch_keys = []
self.ping_touch_thread() self.ping_touch_thread()
def start(self): def start(self):
print(f"开始监听 {COM_PORT} 串口...") print(f"开始监听 {COM_PORT} 串口...")
self.touchThread.start() self.touchThread.start()
@ -256,48 +257,49 @@ def getevent():
# 读取实时输出 # 读取实时输出
for line in iter(process.stdout.readline, b''): for line in iter(process.stdout.readline, b''):
try: try:
event = line.decode('utf-8').strip() event = line.decode('utf-8').strip()
_, _, event_type, event_value = event.split() parts = event.split()
# print(event_type, int(event_value, 16))
# 屏蔽没用的东西
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': if event_type == 'ABS_MT_POSITION_X':
key_is_changed = True key_is_changed = True
if not ANDROID_REVERSE_MONITOR: 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
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)
elif event_type == 'ABS_MT_POSITION_Y': elif event_type == 'ABS_MT_POSITION_Y':
key_is_changed = True key_is_changed = True
if not ANDROID_REVERSE_MONITOR: 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
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)
elif event_type == 'SYN_REPORT': elif event_type == 'SYN_REPORT':
if not key_is_changed: if key_is_changed:
continue convert(touch_data)
# print("Touch Data:", touch_data) key_is_changed = False
# 向 convert 函数发送数据
key_is_changed = False
# start_time = time.perf_counter()
convert(touch_data)
# print("单次执行时间:", (time.perf_counter() - start_time) * 1e3, "毫秒")
elif event_type == 'ABS_MT_SLOT': elif event_type == 'ABS_MT_SLOT':
key_is_changed = True key_is_changed = True
touch_index = int(event_value, 16) touch_index = event_value
if touch_index >= touch_sum: if touch_index >= touch_sum:
touch_sum = touch_index + 1 touch_sum = touch_index + 1
elif event_type == 'ABS_MT_TRACKING_ID': elif event_type == 'ABS_MT_TRACKING_ID':
key_is_changed = True key_is_changed = True
if event_value == "ffffffff": if event_value_hex == "ffffffff":
touch_data[touch_index]['p'] = False touch_data[touch_index]['p'] = False
touch_sum = max(0, touch_sum - 1) touch_sum = max(0, touch_sum - 1)
else: else:
touch_data[touch_index]['p'] = True touch_data[touch_index]['p'] = True
touch_sum += 1 touch_sum += 1
else:
continue except Exception as e:
except Exception:
event_error_output = line.decode('utf-8') event_error_output = line.decode('utf-8')
if "name" in event_error_output: if "name" not in event_error_output:
continue continue
print(event_error_output) print(event_error_output)
@ -331,6 +333,7 @@ if __name__ == "__main__":
abs_multi_y = ANDROID_ABS_MONITOR_SIZE[1] / ANDROID_ABS_INPUT_SIZE[1] abs_multi_y = ANDROID_ABS_MONITOR_SIZE[1] / ANDROID_ABS_INPUT_SIZE[1]
print("当前触控区域X轴放大倍数:", abs_multi_x) print("当前触控区域X轴放大倍数:", abs_multi_x)
print("当前触控区域Y轴放大倍数:", abs_multi_y) print("当前触控区域Y轴放大倍数:", abs_multi_y)
print("当前链接到端口:", COM_PORT)
print(('' if ANDROID_REVERSE_MONITOR else '') + "开启屏幕反转") print(('' if ANDROID_REVERSE_MONITOR else '') + "开启屏幕反转")
serial_manager = SerialManager() serial_manager = SerialManager()
serial_manager.start() serial_manager.start()