change led_send.py
This commit is contained in:
@ -64,16 +64,13 @@ if lib is None:
|
||||
sys.exit(1)
|
||||
|
||||
# ====================== 生成 LED 表格 ======================
|
||||
def generate_led_table(data, output_path="led_send.png", font_path="simsun.ttc"):
|
||||
"""
|
||||
根据接口返回的 Data 生成 LED 显示表格,适配 640x448 LED 屏
|
||||
"""
|
||||
def generate_led_table(data, output_path="led_send.png", font_path="msyh.ttc"):
|
||||
try:
|
||||
font_title = ImageFont.truetype(font_path, 24)
|
||||
font_data = ImageFont.truetype(font_path, 20)
|
||||
font_data_big = ImageFont.truetype(font_path, 22)
|
||||
font_small = ImageFont.truetype(font_path, 16)
|
||||
header_font = ImageFont.truetype(font_path, 30)
|
||||
header_font = ImageFont.truetype(font_path, 26)
|
||||
except IOError:
|
||||
print("字体未找到,使用默认字体")
|
||||
font_title = font_data = font_data_big = font_small = ImageFont.load_default()
|
||||
@ -101,9 +98,7 @@ def generate_led_table(data, output_path="led_send.png", font_path="simsun.ttc")
|
||||
task_quantity = float(data.get("TotMete", 0))
|
||||
except Exception:
|
||||
task_quantity = 0.0
|
||||
fixed_value = 214.1
|
||||
task_quantity_str = f"{task_quantity}"
|
||||
fixed_value_str = f"/{fixed_value}"
|
||||
|
||||
table_data = [
|
||||
["本盘方量", "当前模具", "高斗称值", "低斗称值"],
|
||||
@ -142,14 +137,21 @@ def generate_led_table(data, output_path="led_send.png", font_path="simsun.ttc")
|
||||
bbox_task = draw.textbbox((0, 0), task_quantity_str, font=font_data)
|
||||
tw_task = bbox_task[2] - bbox_task[0]
|
||||
th_task = bbox_task[3] - bbox_task[1]
|
||||
draw.text((x1 + (col_width - 1.8 * tw_task) // 2, y1 + (h - th_task) // 2), task_quantity_str, fill="red", font=font_data)
|
||||
bbox_fixed = draw.textbbox((0, 0), fixed_value_str, font=font_data)
|
||||
# 红色显示任务数量
|
||||
draw.text((x1 + (col_width - 1.8 * tw_task) // 2, y1 + (h - th_task) // 2),
|
||||
task_quantity_str, fill="red", font=font_data)
|
||||
# 亮绿色显示固定值 "/214.1"
|
||||
fixed_text = "/214.1"
|
||||
bbox_fixed = draw.textbbox((0, 0), fixed_text, font=font_data)
|
||||
tw_fixed = bbox_fixed[2] - bbox_fixed[0]
|
||||
draw.text((x1 + (col_width - tw_fixed) // 2 + 0.78 * tw_task, y1 + (h - th_task) // 2), fixed_value_str, fill="green", font=font_data)
|
||||
draw.text((x1 + (col_width - tw_fixed) // 2 + 0.78 * tw_task, y1 + (h - th_task) // 2),
|
||||
fixed_text, fill=(0, 255, 0), font=font_data)
|
||||
continue
|
||||
|
||||
is_header = r in (0, 2, 4, 6, 7)
|
||||
color = "green" if is_header else "red"
|
||||
# 亮绿色显示表头
|
||||
color = (0, 255, 0) if is_header else "red"
|
||||
|
||||
if color == "red" and r < 3:
|
||||
font = font_data_big
|
||||
elif color == "red" and r >= 6:
|
||||
@ -194,56 +196,42 @@ class EQpageHeader_G6(Structure):
|
||||
("txtSpace", c_uint8), ("Valign", c_uint8), ("Halign", c_uint8)
|
||||
]
|
||||
|
||||
lib.bxDual_dynamicArea_DelArea_6G.argtypes = [c_char_p, c_uint32, c_uint8]
|
||||
lib.bxDual_dynamicArea_AddAreaPic_6G.argtypes = [
|
||||
c_char_p, c_uint32, c_uint8, c_uint8, c_uint16, c_uint16,
|
||||
c_uint16, c_uint16, POINTER(EQpageHeader_G6), c_char_p
|
||||
]
|
||||
lib.bxDual_dynamicArea_DelArea_6G.restype = c_int
|
||||
lib.bxDual_dynamicArea_AddAreaPic_6G.restype = c_int
|
||||
|
||||
# ====================== 发送动态区帧 ======================
|
||||
# ====================== 发送动态区帧(丝滑覆盖) ======================
|
||||
def send_dynamic_frame(ip="10.6.242.2", port=5005, frame=None, filename="led_send.png"):
|
||||
if frame is None:
|
||||
print("frame 为空!") #因为相机SDK接口需要的是待发送图片的地址,所以加上确认需要发送图片是否存在。
|
||||
print("frame 为空!")
|
||||
return
|
||||
|
||||
target_w, target_h = 640, 448
|
||||
resized = cv2.resize(frame, (target_w, target_h))
|
||||
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
save_path = os.path.join(current_dir, filename)
|
||||
|
||||
# 使用 cv2.imwrite 保存确保文件编码一致
|
||||
save_path = os.path.join(CURRENT_DIR, filename)
|
||||
cv2.imwrite(save_path, resized)
|
||||
# 这些参数都可以设置,我备注一下参数名称和调节的信息
|
||||
page = EQpageHeader_G6()
|
||||
page.PageStyle = 0 #数据页类型,默认为0
|
||||
page.DisplayMode = 2 #显示方式: 0x00 :随机显示 0x01 :静止显示 0x02 :快速打出 0x03 :向左移动 0x04 :向左连移 0x05 :向上移动 0x06 :向上连移 0x07 :闪烁 ......
|
||||
page.ClearMode = 1 #退出方式/清屏方式
|
||||
page.Speed = 10 #速度等级/背景速度等级
|
||||
page.StayTime = 1000 #停留时间, 单位为 10ms
|
||||
page.RepeatTime = 1 #重复次数/背景拼接步长(左右拼接下为宽度, 上下拼接为高度)
|
||||
page.ValidLen = 64 #用法比较复杂请参考协议,默认不动
|
||||
page.CartoonFrameRate = 0 #特技为动画方式时,该值代表其帧率
|
||||
page.BackNotValidFlag = 0 #背景无效标志
|
||||
#字体信息
|
||||
page.arrMode = 1 #排列方式--单行多行
|
||||
page.fontSize = 16 #字体大小
|
||||
page.color = 1 #字体颜色 E_Color_G56此通过此枚举值可以直接配置七彩色,如果大于枚举范围使用RGB888模式
|
||||
page.fontBold = 0 #是否为粗体
|
||||
page.fontItalic = 0 #是否为斜体
|
||||
page.tdirection = 0 #文字方向
|
||||
page.txtSpace = 0 #文字间隔
|
||||
page.Valign = 2 #纵向对齐方式(0系统自适应、1上对齐、2居中、3下对齐)
|
||||
page.Halign = 1 #横向对齐方式(0系统自适应、1左对齐、2居中、3右对齐)
|
||||
|
||||
print("删除旧动态区 ...")
|
||||
try:
|
||||
ret_del = lib.bxDual_dynamicArea_DelArea_6G(ip.encode(), port, 0xFF)
|
||||
print("删除返回码:", ret_del)
|
||||
except Exception as e:
|
||||
print("调用 DelArea 失败:", e)
|
||||
page = EQpageHeader_G6()
|
||||
page.PageStyle = 0
|
||||
page.DisplayMode = 2
|
||||
page.ClearMode = 1
|
||||
page.Speed = 10
|
||||
page.StayTime = 1000
|
||||
page.RepeatTime = 1
|
||||
page.ValidLen = 64
|
||||
page.CartoonFrameRate = 0
|
||||
page.BackNotValidFlag = 0
|
||||
page.arrMode = 1
|
||||
page.fontSize = 16
|
||||
page.color = 1
|
||||
page.fontBold = 0
|
||||
page.fontItalic = 0
|
||||
page.tdirection = 0
|
||||
page.txtSpace = 0
|
||||
page.Valign = 2
|
||||
page.Halign = 1
|
||||
|
||||
try:
|
||||
ret = lib.bxDual_dynamicArea_AddAreaPic_6G(
|
||||
@ -251,7 +239,7 @@ def send_dynamic_frame(ip="10.6.242.2", port=5005, frame=None, filename="led_sen
|
||||
byref(page), save_path.encode("gb2312")
|
||||
)
|
||||
if ret == 0:
|
||||
print("Frame 发送成功!")
|
||||
print("Frame 覆盖成功!")
|
||||
else:
|
||||
print("Frame 发送失败,返回码:", ret)
|
||||
except Exception as e:
|
||||
@ -260,7 +248,6 @@ def send_dynamic_frame(ip="10.6.242.2", port=5005, frame=None, filename="led_sen
|
||||
def send_led_data(data: dict):
|
||||
img_path = os.path.join(CURRENT_DIR, "led_send.png")
|
||||
generate_led_table(data, output_path=img_path)
|
||||
#这里读取图片是为了保证生成图片函数已经在改文件夹下生成了图片,因为相机SDK接口需要的是待发送图片的地址,所以加上确认。
|
||||
frame = cv2.imread(img_path)
|
||||
send_dynamic_frame(frame=frame, filename="led_send.png")
|
||||
|
||||
@ -289,4 +276,3 @@ if __name__ == "__main__":
|
||||
|
||||
send_led_data(data)
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user