laser non-blocking flash

This commit is contained in:
gcw_4spBpAfv
2026-03-23 11:49:56 +08:00
parent 75def0ff38
commit d1ae364dbd
2 changed files with 27 additions and 14 deletions

View File

@@ -4,6 +4,7 @@
激光管理器模块
提供激光控制、校准等功能
"""
import _thread
import json
import os
import binascii
@@ -209,20 +210,31 @@ class LaserManager:
self.logger.warning("🔇 无回包")
self._laser_turned_on = False
return resp
def flash_laser(self, duration_ms=1000):
"""闪一下激光(用于射箭反馈),如果射箭前激光已亮则保持亮"""
try:
was_on = self._laser_turned_on # 记住射箭前的激光状态
self.turn_on_laser()
time.sleep_ms(duration_ms)
if not was_on:
self.turn_off_laser() # 射箭前是灭的,才关闭
else:
self.logger.info("[LASER] 射箭前激光已亮(调瞄中),保持激光开启")
except Exception as e:
self.logger.error(f"闪激光失败: {e}")
def flash_laser(self, duration_ms=1000):
"""闪一下激光(非阻塞版本)"""
try:
# 1. 先打开激光(主线程立刻执行,不等待)
was_on = self._laser_turned_on
self.turn_on_laser()
# 2. 只有之前是激光off的状态下才会启动一个子线程去处理“延时关闭”的逻辑否则就保持着
if not was_on:
_thread.start_new_thread(self._laser_delay_off, (duration_ms, was_on))
except Exception as e:
self.logger.error(f"启动闪激光线程失败: {e}")
def _laser_delay_off(self, duration_ms, was_on):
"""子线程执行的函数:等待一段时间后关闭激光"""
try:
# 这里 sleep 不会阻塞主线程了,只会阻塞这个不起眼的子线程
time.sleep_ms(duration_ms)
self.turn_off_laser()
# print("[LASER] 射箭前激光已亮(调瞄中),保持激光开启")
except Exception as e:
# print(f"子线程关闭激光异常: {e}")
pass
# def find_red_laser(self, frame, threshold=150, search_radius=50):
# """
# 在图像中心附近查找最亮的红色激光点(基于 RGB 阈值)