优化ucd调用结构

This commit is contained in:
xinzhu.yin
2026-06-11 16:29:36 +08:00
parent cc7218411c
commit 46a97d6ae7
13 changed files with 1700 additions and 1702 deletions

View File

@@ -1,622 +0,0 @@
from enum import IntEnum
import UniTAP
class UCDEnum:
class ColorInfo:
"""
Class contains information of frame `ColorFormat`, `DynamicRange`, `Colorimetry`.
"""
class ColorFormat(IntEnum):
"""
Contains values of possible color format.
"""
CF_NONE = 0
CF_UNKNOWN = 1
CF_RGB = 2
CF_YCbCr_422 = 3
CF_YCbCr_444 = 4
CF_YCbCr_420 = 5
CF_IDO_DEFINED = 6
CF_Y_ONLY = 7
CF_RAW = 8
CF_DSC = 9
class DynamicRange(IntEnum):
"""
Contains values of possible dynamic range.
"""
DR_UNKNOWN = -1
DR_VESA = 0
DR_CTA = 1
class Colorimetry(IntEnum):
"""
Contains values of possible colorimetry.
"""
CM_NONE = 0
CM_RESERVED = 1
CM_sRGB = 2
CM_SMPTE_170M = 3
CM_ITUR_BT601 = 4
CM_ITUR_BT709 = 5
CM_xvYCC601 = 6
CM_xvYCC709 = 7
CM_sYCC601 = 8
CM_AdobeYCC601 = 9
CM_AdobeRGB = 10
CM_ITUR_BT2020_YcCbcCrc = 11
CM_ITUR_BT2020_YCbCr = 12
CM_ITUR_BT2020_RGB = 13
CM_RGB_WIDE_GAMUT_FIX = 14
CM_RGB_WIDE_GAMUT_FLT = 15
CM_DCI_P3 = 16
CM_DICOM_1_4_GRAY_SCALE = 17
CM_CUSTOM_COLOR_PROFILE = 18
CM_opYCC601 = CM_AdobeYCC601
CM_opRGB = CM_AdobeRGB
# 颜色格式映射 - 支持不区分大小写的字符串匹配
@staticmethod
def get_color_format(format_str):
format_map = {
"none": UniTAP.ColorInfo.ColorFormat.CF_NONE,
"unknown": UniTAP.ColorInfo.ColorFormat.CF_UNKNOWN,
"rgb": UniTAP.ColorInfo.ColorFormat.CF_RGB,
"ycbcr422": UniTAP.ColorInfo.ColorFormat.CF_YCbCr_422,
"ycbcr444": UniTAP.ColorInfo.ColorFormat.CF_YCbCr_444,
"ycbcr420": UniTAP.ColorInfo.ColorFormat.CF_YCbCr_420,
"ido_defined": UniTAP.ColorInfo.ColorFormat.CF_IDO_DEFINED,
"yonly": UniTAP.ColorInfo.ColorFormat.CF_Y_ONLY,
"raw": UniTAP.ColorInfo.ColorFormat.CF_RAW,
"dsc": UniTAP.ColorInfo.ColorFormat.CF_DSC,
}
if not format_str:
return None
return format_map.get(format_str.lower(), None)
# 色度映射 - 支持不区分大小写的字符串匹配
@staticmethod
def get_colorimetry(colorimetry_str):
colorimetry_map = {
"none": UniTAP.ColorInfo.Colorimetry.CM_NONE,
"reserved": UniTAP.ColorInfo.Colorimetry.CM_RESERVED,
"srgb": UniTAP.ColorInfo.Colorimetry.CM_sRGB,
"smpte170m": UniTAP.ColorInfo.Colorimetry.CM_SMPTE_170M,
"bt601": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT601,
"bt709": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT709,
"xvycc601": UniTAP.ColorInfo.Colorimetry.CM_xvYCC601,
"xvycc709": UniTAP.ColorInfo.Colorimetry.CM_xvYCC709,
"sycc601": UniTAP.ColorInfo.Colorimetry.CM_sYCC601,
"adobeycc601": UniTAP.ColorInfo.Colorimetry.CM_AdobeYCC601,
"adobergb": UniTAP.ColorInfo.Colorimetry.CM_AdobeRGB,
"bt2020yccbccrc": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_YcCbcCrc,
"bt2020ycbcr": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_YCbCr,
"bt2020rgb": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_RGB,
"rgbwidegamutfix": UniTAP.ColorInfo.Colorimetry.CM_RGB_WIDE_GAMUT_FIX,
"rgbwidegamutflt": UniTAP.ColorInfo.Colorimetry.CM_RGB_WIDE_GAMUT_FLT,
"dcip3": UniTAP.ColorInfo.Colorimetry.CM_DCI_P3,
"dicom14grayscale": UniTAP.ColorInfo.Colorimetry.CM_DICOM_1_4_GRAY_SCALE,
"customcolorprofile": UniTAP.ColorInfo.Colorimetry.CM_CUSTOM_COLOR_PROFILE,
"opycc601": UniTAP.ColorInfo.Colorimetry.CM_opYCC601,
"oprgb": UniTAP.ColorInfo.Colorimetry.CM_opRGB,
}
if not colorimetry_str:
return None
# Normalize: strip hyphens, spaces, dots, underscores so that
# "DCI-P3" → "dcip3", "BT.709" → "bt709", "BT.2020 YCbCr" → "bt2020ycbcr"
normalized = (
colorimetry_str.lower()
.replace("-", "")
.replace(" ", "")
.replace(".", "")
.replace("_", "")
)
return colorimetry_map.get(normalized, colorimetry_map.get(colorimetry_str.lower(), None))
class VideoPatternInfo:
class VideoPattern(IntEnum):
"""
Class `VideoPattern` contains all possible variants of patterns which can be set in the function `set_pattern`.
"""
Disabled = 0
ColorBars = 1
Chessboard = 2
SolidColor = 3
SolidWhite = 4
SolidRed = 5
SolidGreen = 6
SolidBlue = 7
WhiteVStrips = 8
GradientRGBStripes = 9
ColorRamp = 10
ColorSquares = 11
MotionPattern = 12
SquareWindow = 15
class VideoPatternParams(IntEnum):
"""
Class `VideoPatternParams` contains all possible variants of parameters which can be set in the function `set_pattern_params`.
"""
SolidColor = 3
WhiteVStrips = 8
GradientRGBStripes = 9
MotionPattern = 12
SquareWindow = 15
@staticmethod
def get_video_pattern(pattern_str):
pattern_map = {
"disabled": UniTAP.VideoPattern.Disabled,
"colorbars": UniTAP.VideoPattern.ColorBars,
"chessboard": UniTAP.VideoPattern.Chessboard,
"solidcolor": UniTAP.VideoPattern.SolidColor,
"solidwhite": UniTAP.VideoPattern.SolidWhite,
"solidred": UniTAP.VideoPattern.SolidRed,
"solidgreen": UniTAP.VideoPattern.SolidGreen,
"solidblue": UniTAP.VideoPattern.SolidBlue,
"whitevstrips": UniTAP.VideoPattern.WhiteVStrips,
"gradientrgbstripes": UniTAP.VideoPattern.GradientRGBStripes,
"colorramp": UniTAP.VideoPattern.ColorRamp,
"coloursquares": UniTAP.VideoPattern.ColorSquares,
"motionpattern": UniTAP.VideoPattern.MotionPattern,
"squarewindow": UniTAP.VideoPattern.SquareWindow,
}
if not pattern_str:
return None
return pattern_map.get(pattern_str.lower(), None)
class TimingInfo:
class ResolutionType(IntEnum):
"""
分辨率类型枚举包含DMT、CTA、CVT和OVT四种类型
"""
DMT = 0 # VESA Display Monitor Timing
CTA = 1 # Consumer Technology Association
CVT = 2 # Coordinated Video Timing
OVT = 3 # Other Video Timing
# 分辨率类型映射
resolution_type_map = {
"dmt": ResolutionType.DMT,
"cta": ResolutionType.CTA,
"cvt": ResolutionType.CVT,
"ovt": ResolutionType.OVT,
}
# DMT分辨率ID映射
dmt_resolution_map = {
9: {"width": 800, "height": 600, "refresh_rate": 60.317, "id_hex": "9h"},
14: {"width": 848, "height": 480, "refresh_rate": 60.0, "id_hex": "Eh"},
16: {"width": 1024, "height": 768, "refresh_rate": 60.0, "id_hex": "10h"},
23: {"width": 1280, "height": 768, "refresh_rate": 60.0, "id_hex": "17h"},
27: {
"width": 1280,
"height": 800,
"refresh_rate": 60.0,
"id_hex": "1Bh",
"note": "RB1",
},
28: {"width": 1280, "height": 800, "refresh_rate": 60.0, "id_hex": "1Ch"},
32: {"width": 1280, "height": 960, "refresh_rate": 60.0, "id_hex": "20h"},
35: {"width": 1280, "height": 1024, "refresh_rate": 60.0, "id_hex": "23h"},
39: {"width": 1360, "height": 768, "refresh_rate": 60.0, "id_hex": "27h"},
41: {
"width": 1400,
"height": 1050,
"refresh_rate": 60.0,
"id_hex": "29h",
"note": "RB1",
},
42: {"width": 1400, "height": 1050, "refresh_rate": 60.0, "id_hex": "2Ah"},
47: {"width": 1440, "height": 900, "refresh_rate": 59.887, "id_hex": "2Fh"},
51: {"width": 1600, "height": 1200, "refresh_rate": 60.0, "id_hex": "33h"},
57: {
"width": 1680,
"height": 1050,
"refresh_rate": 60.0,
"id_hex": "39h",
"note": "RB1",
},
58: {"width": 1680, "height": 1050, "refresh_rate": 60.0, "id_hex": "3Ah"},
62: {"width": 1792, "height": 1344, "refresh_rate": 60.0, "id_hex": "3Eh"},
65: {"width": 1856, "height": 1392, "refresh_rate": 60.0, "id_hex": "41h"},
69: {"width": 1920, "height": 1200, "refresh_rate": 60.0, "id_hex": "45h"},
73: {"width": 1920, "height": 1440, "refresh_rate": 60.0, "id_hex": "49h"},
76: {
"width": 2560,
"height": 1600,
"refresh_rate": 60.0,
"id_hex": "4Ch",
"note": "RB1",
},
77: {"width": 2560, "height": 1600, "refresh_rate": 60.0, "id_hex": "4Dh"},
82: {"width": 1920, "height": 1080, "refresh_rate": 60.0, "id_hex": "52h"},
}
# CTA分辨率ID映射
cta_resolution_map = {
1: {"width": 640, "height": 480, "refresh_rate": 59.94, "vic": 1},
2: {"width": 720, "height": 480, "refresh_rate": 59.94, "vic": 2},
3: {"width": 720, "height": 480, "refresh_rate": 59.94, "vic": 3},
4: {"width": 1280, "height": 720, "refresh_rate": 60.0, "vic": 4},
8: {"width": 1440, "height": 240, "refresh_rate": 59.826, "vic": 8},
9: {"width": 1440, "height": 240, "refresh_rate": 60.054, "vic": 9},
12: {"width": 2880, "height": 240, "refresh_rate": 59.826, "vic": 12},
13: {"width": 2880, "height": 240, "refresh_rate": 59.826, "vic": 13},
14: {"width": 1440, "height": 480, "refresh_rate": 59.94, "vic": 14},
15: {"width": 1440, "height": 480, "refresh_rate": 59.94, "vic": 15},
16: {"width": 1920, "height": 1080, "refresh_rate": 60.0, "vic": 16},
17: {"width": 720, "height": 576, "refresh_rate": 50.0, "vic": 17},
18: {"width": 720, "height": 576, "refresh_rate": 50.0, "vic": 18},
19: {"width": 1280, "height": 720, "refresh_rate": 50.0, "vic": 19},
23: {"width": 1440, "height": 288, "refresh_rate": 49.761, "vic": 23},
24: {"width": 1440, "height": 288, "refresh_rate": 49.761, "vic": 24},
27: {"width": 2880, "height": 288, "refresh_rate": 49.761, "vic": 27},
28: {"width": 2880, "height": 288, "refresh_rate": 49.761, "vic": 28},
29: {"width": 1440, "height": 576, "refresh_rate": 50.0, "vic": 29},
30: {"width": 1440, "height": 576, "refresh_rate": 50.0, "vic": 30},
31: {"width": 1920, "height": 1080, "refresh_rate": 50.0, "vic": 31},
32: {"width": 1920, "height": 1080, "refresh_rate": 24.0, "vic": 32},
33: {"width": 1920, "height": 1080, "refresh_rate": 25.0, "vic": 33},
34: {"width": 1920, "height": 1080, "refresh_rate": 30.0, "vic": 34},
35: {"width": 2880, "height": 480, "refresh_rate": 59.94, "vic": 35},
36: {"width": 2880, "height": 480, "refresh_rate": 59.94, "vic": 36},
37: {"width": 2880, "height": 576, "refresh_rate": 50.0, "vic": 37},
38: {"width": 2880, "height": 576, "refresh_rate": 50.0, "vic": 38},
41: {"width": 1280, "height": 720, "refresh_rate": 100.0, "vic": 41},
42: {"width": 720, "height": 576, "refresh_rate": 100.0, "vic": 42},
43: {"width": 720, "height": 576, "refresh_rate": 100.0, "vic": 43},
47: {"width": 1280, "height": 720, "refresh_rate": 120.0, "vic": 47},
48: {"width": 720, "height": 480, "refresh_rate": 120.0, "vic": 48},
49: {"width": 720, "height": 480, "refresh_rate": 119.88, "vic": 49},
52: {"width": 720, "height": 576, "refresh_rate": 200.0, "vic": 52},
53: {"width": 720, "height": 576, "refresh_rate": 200.0, "vic": 53},
56: {"width": 720, "height": 480, "refresh_rate": 239.76, "vic": 56},
57: {"width": 720, "height": 480, "refresh_rate": 239.76, "vic": 57},
60: {"width": 1280, "height": 720, "refresh_rate": 24.0, "vic": 60},
61: {"width": 1280, "height": 720, "refresh_rate": 25.0, "vic": 61},
62: {"width": 1280, "height": 720, "refresh_rate": 30.0, "vic": 62},
63: {"width": 1920, "height": 1080, "refresh_rate": 120.0, "vic": 63},
64: {"width": 1920, "height": 1080, "refresh_rate": 100.0, "vic": 64},
65: {"width": 1280, "height": 720, "refresh_rate": 24.0, "vic": 65},
66: {"width": 1280, "height": 720, "refresh_rate": 25.0, "vic": 66},
67: {"width": 1280, "height": 720, "refresh_rate": 30.0, "vic": 67},
68: {"width": 1280, "height": 720, "refresh_rate": 50.0, "vic": 68},
69: {"width": 1280, "height": 720, "refresh_rate": 60.0, "vic": 69},
70: {"width": 1280, "height": 720, "refresh_rate": 100.0, "vic": 70},
71: {"width": 1280, "height": 720, "refresh_rate": 120.0, "vic": 71},
72: {"width": 1920, "height": 1080, "refresh_rate": 24.0, "vic": 72},
73: {"width": 1920, "height": 1080, "refresh_rate": 25.0, "vic": 73},
74: {"width": 1920, "height": 1080, "refresh_rate": 30.0, "vic": 74},
75: {"width": 1920, "height": 1080, "refresh_rate": 50.0, "vic": 75},
76: {"width": 1920, "height": 1080, "refresh_rate": 60.0, "vic": 76},
77: {"width": 1920, "height": 1080, "refresh_rate": 100.0, "vic": 77},
78: {"width": 1920, "height": 1080, "refresh_rate": 120.0, "vic": 78},
79: {"width": 1680, "height": 720, "refresh_rate": 24.0, "vic": 79},
80: {"width": 1680, "height": 720, "refresh_rate": 25.0, "vic": 80},
81: {"width": 1680, "height": 720, "refresh_rate": 30.0, "vic": 81},
82: {"width": 1680, "height": 720, "refresh_rate": 50.0, "vic": 82},
83: {"width": 1680, "height": 720, "refresh_rate": 60.0, "vic": 83},
84: {"width": 1680, "height": 720, "refresh_rate": 100.0, "vic": 84},
85: {"width": 1680, "height": 720, "refresh_rate": 120.0, "vic": 85},
86: {"width": 2560, "height": 1080, "refresh_rate": 24.0, "vic": 86},
87: {"width": 2560, "height": 1080, "refresh_rate": 25.0, "vic": 87},
88: {"width": 2560, "height": 1080, "refresh_rate": 30.0, "vic": 88},
89: {"width": 2560, "height": 1080, "refresh_rate": 50.0, "vic": 89},
90: {"width": 2560, "height": 1080, "refresh_rate": 60.0, "vic": 90},
91: {"width": 2560, "height": 1080, "refresh_rate": 100.0, "vic": 91},
92: {"width": 2560, "height": 1080, "refresh_rate": 120.0, "vic": 92},
93: {"width": 3840, "height": 2160, "refresh_rate": 24.0, "vic": 93},
94: {"width": 3840, "height": 2160, "refresh_rate": 25.0, "vic": 94},
95: {"width": 3840, "height": 2160, "refresh_rate": 30.0, "vic": 95},
96: {"width": 3840, "height": 2160, "refresh_rate": 50.0, "vic": 96},
97: {"width": 3840, "height": 2160, "refresh_rate": 60.0, "vic": 97},
98: {"width": 4096, "height": 2160, "refresh_rate": 24.0, "vic": 98},
99: {"width": 4096, "height": 2160, "refresh_rate": 25.0, "vic": 99},
100: {"width": 4096, "height": 2160, "refresh_rate": 30.0, "vic": 100},
101: {"width": 4096, "height": 2160, "refresh_rate": 50.0, "vic": 101},
102: {"width": 4096, "height": 2160, "refresh_rate": 60.0, "vic": 102},
103: {"width": 3840, "height": 2160, "refresh_rate": 24.0, "vic": 103},
104: {"width": 3840, "height": 2160, "refresh_rate": 25.0, "vic": 104},
105: {"width": 3840, "height": 2160, "refresh_rate": 30.0, "vic": 105},
106: {"width": 3840, "height": 2160, "refresh_rate": 50.0, "vic": 106},
107: {"width": 3840, "height": 2160, "refresh_rate": 60.0, "vic": 107},
108: {"width": 1280, "height": 720, "refresh_rate": 48.0, "vic": 108},
109: {"width": 1280, "height": 720, "refresh_rate": 48.0, "vic": 109},
110: {"width": 1680, "height": 720, "refresh_rate": 48.0, "vic": 110},
111: {"width": 1920, "height": 1080, "refresh_rate": 48.0, "vic": 111},
112: {"width": 1920, "height": 1080, "refresh_rate": 48.0, "vic": 112},
113: {"width": 2560, "height": 1080, "refresh_rate": 48.0, "vic": 113},
114: {"width": 3840, "height": 2160, "refresh_rate": 48.0, "vic": 114},
115: {"width": 4096, "height": 2160, "refresh_rate": 48.0, "vic": 115},
116: {"width": 3840, "height": 2160, "refresh_rate": 48.0, "vic": 116},
117: {"width": 3840, "height": 2160, "refresh_rate": 100.0, "vic": 117},
118: {"width": 3840, "height": 2160, "refresh_rate": 120.0, "vic": 118},
119: {"width": 3840, "height": 2160, "refresh_rate": 100.0, "vic": 119},
120: {"width": 3840, "height": 2160, "refresh_rate": 120.0, "vic": 120},
218: {"width": 4096, "height": 2160, "refresh_rate": 100.0, "vic": 218},
219: {"width": 4096, "height": 2160, "refresh_rate": 120.0, "vic": 219},
}
# CVT分辨率ID映射
cvt_resolution_map = {
0: [
{"width": 640, "height": 480, "refresh_rate": 60.0},
{"width": 768, "height": 480, "refresh_rate": 84.502},
{"width": 1024, "height": 640, "refresh_rate": 59.887},
{"width": 1152, "height": 720, "refresh_rate": 74.721},
{"width": 1280, "height": 768, "refresh_rate": 60.0, "note": "RB1"},
{"width": 1280, "height": 960, "refresh_rate": 59.939},
{"width": 1536, "height": 960, "refresh_rate": 84.884},
{"width": 1600, "height": 1200, "refresh_rate": 60.0, "note": "RB1"},
{"width": 1920, "height": 1080, "refresh_rate": 30.0, "note": "RB1"},
{"width": 1920, "height": 1080, "refresh_rate": 30.0, "note": "RB2"},
{"width": 1920, "height": 1080, "refresh_rate": 60.0, "note": "RB1"},
{"width": 1920, "height": 1080, "refresh_rate": 60.0, "note": "RB2"},
{"width": 1920, "height": 1080, "refresh_rate": 84.884},
{"width": 1920, "height": 1080, "refresh_rate": 120.0, "note": "RB1"},
{"width": 1920, "height": 1080, "refresh_rate": 120.0, "note": "RB2"},
{"width": 1920, "height": 1080, "refresh_rate": 144.05, "note": "RB1"},
{"width": 1920, "height": 1080, "refresh_rate": 144.05, "note": "RB2"},
{"width": 1920, "height": 1080, "refresh_rate": 144.05, "note": "RB3"},
{"width": 1920, "height": 1080, "refresh_rate": 200.07, "note": "RB3"},
{"width": 1920, "height": 1080, "refresh_rate": 239.898, "note": "RB1"},
{"width": 1920, "height": 1080, "refresh_rate": 239.898, "note": "RB2"},
{"width": 1920, "height": 1080, "refresh_rate": 239.898, "note": "RB3"},
{"width": 1920, "height": 1440, "refresh_rate": 59.974, "note": "RB1"},
{"width": 2048, "height": 1280, "refresh_rate": 59.922, "note": "RB1"},
{"width": 2048, "height": 1536, "refresh_rate": 60.0, "note": "RB1"},
{"width": 2128, "height": 1200, "refresh_rate": 59.946},
{"width": 2456, "height": 1536, "refresh_rate": 50.0},
{"width": 2456, "height": 1536, "refresh_rate": 74.935},
{"width": 2560, "height": 1080, "refresh_rate": 60.0},
{"width": 2560, "height": 1080, "refresh_rate": 60.0, "note": "RB1"},
{"width": 2560, "height": 1080, "refresh_rate": 144.051, "note": "RB3"},
{"width": 2560, "height": 1080, "refresh_rate": 200.07, "note": "RB3"},
{"width": 2560, "height": 1440, "refresh_rate": 60.0, "note": "RB1"},
{"width": 2560, "height": 1440, "refresh_rate": 60.0, "note": "RB2"},
{"width": 2560, "height": 1440, "refresh_rate": 144.05, "note": "RB3"},
{"width": 2560, "height": 1440, "refresh_rate": 200.07, "note": "RB3"},
{"width": 2560, "height": 1920, "refresh_rate": 74.979},
{"width": 2728, "height": 1536, "refresh_rate": 59.944},
{"width": 3440, "height": 1440, "refresh_rate": 60.0},
{"width": 3440, "height": 1440, "refresh_rate": 60.0, "note": "RB1"},
{"width": 3440, "height": 1440, "refresh_rate": 60.0, "note": "RB2"},
{"width": 3440, "height": 1440, "refresh_rate": 120.0},
{"width": 3440, "height": 1440, "refresh_rate": 120.0, "note": "RB1"},
{"width": 3440, "height": 1440, "refresh_rate": 120.0, "note": "RB2"},
{"width": 3440, "height": 1440, "refresh_rate": 165.0, "note": "RB1"},
{"width": 3440, "height": 1440, "refresh_rate": 165.0, "note": "RB2"},
{"width": 3440, "height": 1440, "refresh_rate": 200.0, "note": "RB1"},
{"width": 3440, "height": 1440, "refresh_rate": 200.0, "note": "RB2"},
{"width": 3840, "height": 2160, "refresh_rate": 30.0, "note": "RB1"},
{"width": 3840, "height": 2160, "refresh_rate": 30.0, "note": "RB2"},
{"width": 3840, "height": 2160, "refresh_rate": 60.0, "note": "RB1"},
{"width": 3840, "height": 2160, "refresh_rate": 60.0, "note": "RB2"},
{"width": 3840, "height": 2160, "refresh_rate": 60.021, "note": "RB3"},
{"width": 3840, "height": 2160, "refresh_rate": 120.0, "note": "RB1"},
{"width": 3840, "height": 2160, "refresh_rate": 120.0, "note": "RB2"},
{"width": 4096, "height": 2160, "refresh_rate": 60.0, "note": "RB1"},
{"width": 4096, "height": 2160, "refresh_rate": 60.0, "note": "RB2"},
{"width": 4096, "height": 2160, "refresh_rate": 60.021, "note": "RB3"},
]
}
# OVT分辨率ID映射
ovt_resolution_map = {
0: [
{"width": 768, "height": 480, "refresh_rate": 85.0},
{"width": 1024, "height": 640, "refresh_rate": 60.0},
{"width": 1152, "height": 720, "refresh_rate": 75.0},
{"width": 1280, "height": 768, "refresh_rate": 60.0},
{"width": 1280, "height": 960, "refresh_rate": 60.0},
{"width": 1440, "height": 240, "refresh_rate": 60.0},
{"width": 1440, "height": 480, "refresh_rate": 60.0},
{"width": 1440, "height": 900, "refresh_rate": 60.0},
{"width": 1536, "height": 960, "refresh_rate": 85.0},
{"width": 1920, "height": 1080, "refresh_rate": 30.0},
{"width": 1920, "height": 1080, "refresh_rate": 60.0},
{"width": 1920, "height": 1080, "refresh_rate": 85.0},
{"width": 1920, "height": 1080, "refresh_rate": 100.0},
{"width": 1920, "height": 1080, "refresh_rate": 120.0},
{"width": 1920, "height": 1440, "refresh_rate": 60.0},
{"width": 2048, "height": 1280, "refresh_rate": 60.0},
{"width": 2048, "height": 1536, "refresh_rate": 60.0},
{"width": 2128, "height": 1200, "refresh_rate": 60.0},
{"width": 2456, "height": 1536, "refresh_rate": 50.0},
{"width": 2456, "height": 1536, "refresh_rate": 75.0},
{"width": 2560, "height": 1080, "refresh_rate": 30.0},
{"width": 2560, "height": 1080, "refresh_rate": 60.0},
{"width": 2560, "height": 1080, "refresh_rate": 120.0},
{"width": 2560, "height": 1600, "refresh_rate": 60.0},
{"width": 2560, "height": 1920, "refresh_rate": 75.0},
{"width": 2728, "height": 1536, "refresh_rate": 60.0},
{"width": 3840, "height": 2160, "refresh_rate": 30.0},
{"width": 3840, "height": 2160, "refresh_rate": 60.0},
{"width": 3840, "height": 2160, "refresh_rate": 120.0},
{"width": 4096, "height": 2160, "refresh_rate": 30.0},
],
1: [
{"width": 1280, "height": 720, "refresh_rate": 24.0},
{"width": 1280, "height": 720, "refresh_rate": 120.0},
],
4: [
{"width": 1920, "height": 1080, "refresh_rate": 144.0},
{"width": 1920, "height": 1080, "refresh_rate": 240.0},
],
}
# 根据分辨率类型和ID获取分辨率信息的函数
@staticmethod
def get_resolution_info(resolution_type, resolution_id):
"""
根据分辨率类型和ID获取分辨率信息
Args:
resolution_type: 分辨率类型可以是DMT、CTA、CVT或OVT
resolution_id: 分辨率ID
Returns:
包含分辨率信息的字典如果未找到则返回None
"""
if resolution_type == UCDEnum.ResolutionType.DMT:
return UCDEnum.dmt_resolution_map.get(resolution_id)
elif resolution_type == UCDEnum.ResolutionType.CTA:
return UCDEnum.cta_resolution_map.get(resolution_id)
elif resolution_type == UCDEnum.ResolutionType.CVT:
resolutions = UCDEnum.cvt_resolution_map.get(resolution_id, [])
return resolutions[0] if resolutions else None
elif resolution_type == UCDEnum.ResolutionType.OVT:
resolutions = UCDEnum.ovt_resolution_map.get(resolution_id, [])
return resolutions[0] if resolutions else None
return None
@staticmethod
def get_formatted_resolution_list():
"""
从分辨率映射中生成格式化的分辨率字符串列表用于UI显示
格式为: "类型 宽度x 高度 @ 刷新率Hz"
Returns:
包含格式化分辨率字符串的列表
"""
formatted_list = []
# 添加DMT分辨率
for res_id, res_info in UCDEnum.TimingInfo.dmt_resolution_map.items():
formatted_str = f"DMT {res_info['width']}x {res_info['height']} @ {int(res_info['refresh_rate'])}Hz"
formatted_list.append(formatted_str)
# 添加CTA分辨率
for res_id, res_info in UCDEnum.TimingInfo.cta_resolution_map.items():
formatted_str = f"CTA {res_info['width']}x {res_info['height']} @ {int(res_info['refresh_rate'])}Hz"
formatted_list.append(formatted_str)
# 添加CVT分辨率 (只取每个ID的第一个)
for res_id, res_list in UCDEnum.TimingInfo.cvt_resolution_map.items():
for res_info in res_list:
note = f" {res_info.get('note', '')}" if "note" in res_info else ""
formatted_str = f"CVT {res_info['width']}x {res_info['height']} @ {int(res_info['refresh_rate'])}Hz{note}"
formatted_list.append(formatted_str)
# 添加OVT分辨率 (只取每个ID的第一个)
for res_id, res_list in UCDEnum.TimingInfo.ovt_resolution_map.items():
for res_info in res_list:
formatted_str = f"OVT {res_info['width']}x {res_info['height']} @ {int(res_info['refresh_rate'])}Hz"
formatted_list.append(formatted_str)
# 排序并去重
return sorted(list(set(formatted_list)))
class SignalFormat:
"""信号格式相关枚举"""
class GammaType:
"""Gamma 类型枚举"""
GAMMA_22 = "2.2"
GAMMA_24 = "2.4"
GAMMA_26 = "2.6"
@staticmethod
def get_list():
return ["2.2", "2.4", "2.6"]
@staticmethod
def get_gamma_value(gamma_str):
"""将 Gamma 字符串转换为数值"""
gamma_map = {"2.2": 2.2, "2.4": 2.4, "2.6": 2.6}
return gamma_map.get(gamma_str, 2.2)
class DataRange:
"""数据范围枚举"""
FULL = "Full"
LIMITED = "Limited"
@staticmethod
def get_list():
return ["Full", "Limited"]
class BitDepth:
"""编码位深枚举"""
BIT_8 = "8bit"
BIT_10 = "10bit"
BIT_12 = "12bit"
@staticmethod
def get_list():
return ["8bit", "10bit", "12bit"]
@staticmethod
def get_bit_value(bit_str):
"""将位深字符串转换为数值"""
bit_map = {"8bit": 8, "10bit": 10, "12bit": 12}
return bit_map.get(bit_str, 8)
class HDRMetadata:
"""HDR Metadata 参数"""
# MaxCLL (Maximum Content Light Level) - 最大内容亮度级别
MAX_CLL_DEFAULT = 1000
MAX_CLL_OPTIONS = [400, 600, 800, 1000, 1200, 1500, 2000, 4000]
# MaxFALL (Maximum Frame Average Light Level) - 最大帧平均亮度级别
MAX_FALL_DEFAULT = 400
MAX_FALL_OPTIONS = [200, 300, 400, 500, 600, 800, 1000]
@staticmethod
def get_maxcll_list():
return [400, 600, 800, 1000, 1200, 1500, 2000, 4000]
@staticmethod
def get_maxfall_list():
return [200, 300, 400, 500, 600, 800, 1000]
class OutputFormat:
"""输出色彩格式枚举(决定信号是 RGB 还是 YCbCr 格式)"""
RGB = "RGB"
YCBCR_422 = "YCbCr 4:2:2"
YCBCR_444 = "YCbCr 4:4:4"
YCBCR_420 = "YCbCr 4:2:0"
Y_ONLY = "Y Only"
IDO_DEFINED = "IDO Defined"
RAW = "RAW"
DSC = "DSC"
@staticmethod
def get_list():
return ["RGB", "YCbCr 4:4:4", "YCbCr 4:2:2", "YCbCr 4:2:0",
"Y Only", "IDO Defined", "RAW", "DSC"]
@staticmethod
def is_ycbcr(format_str):
return "YCbCr" in (format_str or "")
@staticmethod
def get_format_key(format_str):
"""将显示字符串转换为 UCDEnum.ColorInfo.get_color_format() 的 key"""
fmt_map = {
"RGB": "rgb",
"YCbCr 4:4:4": "ycbcr444",
"YCbCr 4:2:2": "ycbcr422",
"YCbCr 4:2:0": "ycbcr420",
"Y Only": "yonly",
"IDO Defined": "ido_defined",
"RAW": "raw",
"DSC": "dsc",
}
return fmt_map.get(format_str, "rgb")

View File

@@ -1,757 +0,0 @@
# -*- coding: UTF-8 -*-
import logging
import UniTAP
import time
import gc
from drivers.UCD323_Enum import UCDEnum
log = logging.getLogger(__name__)
class UCDController:
"""UCD323信号发生器控制类"""
def __init__(self):
self.lUniTAP = UniTAP.TsiLib()
self.dev = None
self.role = None
self.timing_manager = None
self.config = None
self.color_info = None
self.status = False
self.current_interface = "HDMI"
self.current_timing = None
self.current_pattern = None
self.current_pattern_param = None
self.current_pattern_params = None
self.current_pattern_index = 0
self.last_error = None
def search_device(self):
"""搜索可用设备"""
available_devices = self.lUniTAP.get_list_of_available_devices()
return available_devices if available_devices else []
def open(self, device_name):
"""打开设备"""
temp_dev = None
try:
if self.dev is not None or self.status:
self._force_cleanup()
device_id = int(device_name.split(":")[0])
temp_dev = self.lUniTAP.open(device_id)
try:
self.role = temp_dev.select_role(UniTAP.dev.UCD323.HDMISource)
self.dev = temp_dev
self.current_interface = "HDMI"
except Exception as role_error:
self._close_device_object(temp_dev)
raise role_error
pg, ag = self.get_tx_modules()
self.timing_manager = pg.timing_manager
self.color_info = UniTAP.ColorInfo()
self._stop_audio_output(ag)
self.status = True
return True
except Exception as e:
self._force_cleanup()
return False
def _reset_state(self):
"""重置所有运行时状态(不关闭设备句柄)"""
self.dev = None
self.role = None
self.status = False
self.timing_manager = None
self.current_timing = None
self.current_pattern = None
self.current_pattern_param = None
self.current_pattern_params = None
self.current_pattern_index = 0
self.current_interface = "HDMI"
def close(self):
"""关闭设备"""
try:
if self.dev:
try:
self._stop_audio_output()
except Exception:
pass
self._close_device_object(self.dev)
self._reset_state()
self.lUniTAP = None
for i in range(3):
gc.collect()
time.sleep(2.0)
self.lUniTAP = UniTAP.TsiLib()
return True
except Exception as e:
self._reset_state()
try:
self.lUniTAP = None
gc.collect()
time.sleep(2.0)
self.lUniTAP = UniTAP.TsiLib()
except Exception as init_error:
pass
return False
def _close_device_object(self, dev_obj):
"""显式关闭设备对象"""
try:
if dev_obj is None:
return
if self.lUniTAP and hasattr(self.lUniTAP, "close"):
try:
self.lUniTAP.close(dev_obj)
except Exception as e:
pass
dev_obj = None
gc.collect()
time.sleep(1.0)
except Exception as e:
pass
def _force_cleanup(self):
"""强制清理所有状态"""
try:
if self.dev:
self._close_device_object(self.dev)
self._reset_state()
except Exception as e:
pass
def get_tx_modules(self):
"""根据当前接口返回 (pg, ag) 模块。"""
if not self.role:
raise RuntimeError("UCD 未打开,无法获取 TX 模块")
interface = getattr(self, "current_interface", None)
log.info("UCDController.get_tx_modules interface=%s", interface)
if interface in (None, "HDMI"):
return self.role.hdtx.pg, self.role.hdtx.ag
if interface in ("DP", "Type-C"):
return self.role.dptx.pg, self.role.dptx.ag
raise ValueError(f"不支持的接口类型: {interface}")
def _stop_audio_output(self, ag=None) -> None:
"""关闭 HDMI/DP 音频发生器。PQ 测试仅需视频图案,避免电视持续输出测试音。"""
if not self.status or not self.role:
return
try:
if ag is None:
_, ag = self.get_tx_modules()
ag.stop_generate()
log.info("UCDController._stop_audio_output done")
except Exception:
log.exception("UCDController._stop_audio_output failed")
def _apply_pg_output(self, pg) -> bool:
"""提交 PG 输出,并确保音频发生器处于关闭状态。"""
try:
ok = bool(pg.apply())
except Exception:
log.exception("UCDController._apply_pg_output pg.apply failed")
return False
self._stop_audio_output()
return ok
def _resolve_timing(self, pg=None):
"""优先从 current_timing 读取 timing必要时回退到 TX 模块。"""
if self.current_timing is not None:
return self.current_timing
if pg is not None:
get_vm = getattr(pg, "get_vm", None)
if callable(get_vm):
try:
vm = get_vm()
return getattr(vm, "timing", None)
except Exception:
pass
return None
def get_current_resolution(self, default=(3840, 2160)):
"""从当前 timing 获取 (width, height),失败时返回 default。"""
try:
pg, _ = self.get_tx_modules()
timing = self._resolve_timing(pg)
if timing and hasattr(timing, "h_active") and hasattr(timing, "v_active"):
return timing.h_active, timing.v_active
except Exception:
pass
return default
def _cleanup(self):
"""清理设备资源"""
try:
if self.dev:
self._close_device_object(self.dev)
self.dev = None
if hasattr(self.lUniTAP, "cleanup"):
self.lUniTAP.cleanup()
except Exception as e:
pass
def set_ucd_params(self, config):
"""设置UCD323参数"""
self.last_error = None
self.config = config
test_type = self.config.current_test_type
pg, _ = self.get_tx_modules()
self.timing_manager = pg.timing_manager
color_format = self.config.current_test_types[test_type]["color_format"]
bpc = self.config.current_test_types[test_type]["bpc"]
colorimetry = self.config.current_test_types[test_type]["colorimetry"]
if not self.set_color_mode(color_format, bpc, colorimetry):
self.last_error = (
f"set_color_mode failed: color_format={color_format}, bpc={bpc}, colorimetry={colorimetry}"
)
log.error(
"UCDController.set_ucd_params set_color_mode failed test_type=%s color_format=%s bpc=%s colorimetry=%s",
test_type,
color_format,
bpc,
colorimetry,
)
return False
timing_str = self.config.current_test_types[test_type]["timing"]
if not self.set_timing_from_string(timing_str):
self.last_error = f"set_timing_from_string failed: timing={timing_str}"
log.error(
"UCDController.set_ucd_params set_timing_from_string failed test_type=%s timing=%s",
test_type,
timing_str,
)
return False
self.current_pattern_index = 0
pattern_mode = self.config.current_pattern["pattern_mode"]
pattern = UCDEnum.VideoPatternInfo.get_video_pattern(pattern_mode)
if pattern is None:
self.last_error = f"get_video_pattern failed: pattern_mode={pattern_mode}"
return False
self.current_pattern = pattern
self.current_pattern_params = self.config.current_pattern["pattern_params"]
return True
def run(self):
"""运行设备"""
log.info(
"UCDController.run start current_pattern=%s has_pattern_param=%s",
getattr(self.current_pattern, "name", self.current_pattern),
self.current_pattern_param is not None,
)
self.apply_video_mode()
self.apply_pattern()
pg, _ = self.get_tx_modules()
log.info("UCDController.run calling pg.apply()")
ok = self._apply_pg_output(pg)
log.info("UCDController.run done ok=%s", ok)
return ok
def send_image_pattern(self, image_path):
"""发送图片 Pattern依赖当前 timing/color_info 状态)。"""
if not self.status or not self.role:
return False
try:
pg, _ = self.get_tx_modules()
# 仅切换图案,不重复 set_vm重复 apply video mode 会触发电视 HDMI 重锁发声。
if getattr(self, "_last_sent_config", None) is None:
self.apply_video_mode()
pg.set_pattern(pattern=image_path)
return self._apply_pg_output(pg)
except Exception:
return False
def send_solid_rgb_pattern(self, rgb):
"""发送纯色 RGB Pattern依赖当前 timing/color_info 状态)。"""
if not self.status or not self.role:
return False
try:
log.info("UCDController.send_solid_rgb_pattern rgb=%s", rgb)
self.current_pattern = UCDEnum.VideoPatternInfo.get_video_pattern("solidcolor")
if self.current_pattern is None:
log.error("UCDController.send_solid_rgb_pattern failed: solidcolor pattern not found")
return False
return self.send_current_pattern_params(list(rgb))
except Exception:
log.exception("UCDController.send_solid_rgb_pattern exception")
return False
def send_current_pattern_params(self, pattern_params):
"""发送当前已配置的 pattern并可附带当前 pattern 参数。"""
if not self.status or not self.role:
return False
try:
if self.current_pattern is None:
log.error("UCDController.send_current_pattern_params failed: current_pattern is None")
return False
log.info(
"UCDController.send_current_pattern_params pattern=%s params=%s",
getattr(self.current_pattern, "name", self.current_pattern),
pattern_params,
)
if pattern_params is not None and not self.set_pattern(
self.current_pattern,
pattern_params,
):
log.error("UCDController.send_current_pattern_params failed: set_pattern returned False")
return False
log.info("UCDController.send_current_pattern_params calling run()")
self.run()
log.info("UCDController.send_current_pattern_params done")
return True
except Exception:
log.exception("UCDController.send_current_pattern_params exception")
return False
def set_color_mode(self, cf, bpc, cm):
"""设置颜色模式"""
current_dynamic_range = self.color_info.dynamic_range
color_format = UCDEnum.ColorInfo.get_color_format(cf)
if color_format is None:
fmt_key = UCDEnum.SignalFormat.OutputFormat.get_format_key(cf)
color_format = UCDEnum.ColorInfo.get_color_format(fmt_key)
if color_format is None:
return False
if not isinstance(bpc, int) or bpc <= 0:
return False
colorimetry = UCDEnum.ColorInfo.get_colorimetry(cm)
if colorimetry is None:
return False
self.color_info.color_format = color_format
self.color_info.bpc = bpc
self.color_info.colorimetry = colorimetry
self.color_info.dynamic_range = current_dynamic_range
return True
def apply_video_mode(self):
"""应用当前 color_info 和 timing"""
if self.current_timing:
log.info("UCDController.apply_video_mode start timing=%s", self.current_timing)
self.set_video_mode()
log.info("UCDController.apply_video_mode done")
return True
log.warning("UCDController.apply_video_mode skipped: current_timing is None")
return False
def set_video_mode(self):
"""设置视频模式"""
# 对比上次发出的配置,判断是否会触发电视重新锁定信号
current_config = (
self.current_timing,
self.color_info.color_format,
self.color_info.colorimetry,
self.color_info.dynamic_range,
self.color_info.bpc,
)
self.format_changed = (current_config != getattr(self, "_last_sent_config", None))
log.info(
"UCDController.set_video_mode format_changed=%s color_format=%s colorimetry=%s dynamic_range=%s bpc=%s",
self.format_changed,
self.color_info.color_format,
self.color_info.colorimetry,
self.color_info.dynamic_range,
self.color_info.bpc,
)
if not self.format_changed:
log.info("UCDController.set_video_mode skipped pg.set_vm(): config unchanged")
return True
video_mode = UniTAP.VideoMode(
timing=self.current_timing, color_info=self.color_info
)
pg, _ = self.get_tx_modules()
log.info("UCDController.set_video_mode calling pg.set_vm()")
pg.set_vm(vm=video_mode)
self._stop_audio_output()
log.info("UCDController.set_video_mode done")
self._last_sent_config = current_config
return True
def set_pattern(self, pattern, pattern_params=None):
"""设置pattern"""
if self.current_timing is None:
# Pattern-only updates (e.g. Calman patch click) can still be applied on
# an already active output mode. Missing timing should not block pattern staging.
log.warning("UCDController.set_pattern current_timing is None; continue with pattern-only apply")
needs_params = {
UCDEnum.VideoPatternInfo.VideoPatternParams.SolidColor,
UCDEnum.VideoPatternInfo.VideoPattern.SolidColor,
UCDEnum.VideoPatternInfo.VideoPatternParams.WhiteVStrips,
UCDEnum.VideoPatternInfo.VideoPatternParams.GradientRGBStripes,
UCDEnum.VideoPatternInfo.VideoPatternParams.MotionPattern,
UCDEnum.VideoPatternInfo.VideoPatternParams.SquareWindow,
}
log.info(
"UCDController.set_pattern pattern=%s pattern_params=%s needs_params=%s",
getattr(pattern, "name", pattern),
pattern_params,
pattern in needs_params,
)
if pattern in needs_params and pattern_params is not None:
self.set_pattern_params(pattern, pattern_params)
return True
def set_next_pattern(self):
"""设置下一个pattern"""
if self.current_pattern_index < len(self.current_pattern_params):
p = self.current_pattern_params[self.current_pattern_index]
self.set_pattern(self.current_pattern, p)
self.current_pattern_index += 1
else:
error_msg = (
f"No more patterns to set. (已设置 {self.current_pattern_index} 个图案)"
)
raise IndexError(error_msg)
def set_pattern_params(self, pattern, pattern_params):
"""设置pattern参数"""
if pattern is not None:
solid_color_patterns = {
UCDEnum.VideoPatternInfo.VideoPatternParams.SolidColor,
UCDEnum.VideoPatternInfo.VideoPattern.SolidColor,
}
if pattern in solid_color_patterns:
log.info("UCDController.set_pattern_params solid_color rgb=%s", pattern_params)
self.current_pattern_param = UniTAP.SolidColorParams(
first=pattern_params[0],
second=pattern_params[1],
third=pattern_params[2],
)
return True
log.warning("UCDController.set_pattern_params unsupported pattern=%s", getattr(pattern, "name", pattern))
return False
def apply_pattern(self):
"""应用当前pattern"""
if self.current_pattern is not None:
log.info(
"UCDController.apply_pattern start pattern=%s has_params=%s",
getattr(self.current_pattern, "name", self.current_pattern),
self.current_pattern_param is not None,
)
pg, _ = self.get_tx_modules()
log.info("UCDController.apply_pattern calling pg.set_pattern()")
pg.set_pattern(self.current_pattern)
if self.current_pattern_param is not None:
log.info("UCDController.apply_pattern calling pg.set_pattern_params()")
pg.set_pattern_params(self.current_pattern_param)
log.info("UCDController.apply_pattern done")
return True
log.warning("UCDController.apply_pattern skipped: current_pattern is None")
return False
def search_timing(self, width, height, refresh_rate, resolution_type=None):
"""根据分辨率参数搜索合适的timing"""
if resolution_type:
resolution_type = resolution_type.lower()
standard = None
if resolution_type == "dmt":
standard = UniTAP.common.timing.Timing.Standard.SD_DMT
elif resolution_type == "cta":
standard = UniTAP.common.timing.Timing.Standard.SD_CTA
elif resolution_type == "cvt":
standard = UniTAP.common.timing.Timing.Standard.SD_CVT
rr = float(refresh_rate)
# Try both exact and NTSC-compatible rates (e.g. 120000 / 119880).
f_rate_candidates = [
int(round(rr * 1000)),
int(rr * 1000),
int(round((rr * 1000.0) * 1000.0 / 1001.0)),
]
# 去重并保持顺序
f_rate_candidates = list(dict.fromkeys(f_rate_candidates))
standards = [standard]
if standard is not None:
standards.append(None)
for std in standards:
for f_rate in f_rate_candidates:
timing = self.timing_manager.search(
h_active=width,
v_active=height,
f_rate=f_rate,
standard=std,
)
if timing:
return timing
else:
for res_type in ["dmt", "cta", "cvt", "ovt"]:
result = self.search_timing(width, height, refresh_rate, res_type)
if result:
return result
return None
def parse_formatted_timing(self, timing_str):
"""解析格式化的timing字符串"""
if not isinstance(timing_str, str):
raise ValueError("timing_str 必须是字符串")
s = " ".join(timing_str.strip().split())
s = s.replace(" x", "x").replace("x ", "x")
parts = s.split(" ", 1)
if len(parts) < 2:
raise ValueError(f"无法解析timing: {timing_str}")
type_str = parts[0].strip().upper()
rest = parts[1].strip()
if "@" not in rest:
raise ValueError(f"无法解析timing(缺少 @): {timing_str}")
left, right = [p.strip() for p in rest.split("@", 1)]
if "x" not in left:
raise ValueError(f"无法解析分辨率(缺少 x): {timing_str}")
wh = left.split("x")
if len(wh) != 2:
raise ValueError(f"无法解析分辨率: {timing_str}")
try:
width = int(wh[0])
height = int(wh[1])
except Exception:
raise ValueError(f"分辨率数字解析失败: {timing_str}")
hz_str = right.replace("Hz", "").replace("HZ", "").strip()
try:
refresh_rate = float(hz_str)
except Exception:
raise ValueError(f"刷新率解析失败: {timing_str}")
rtype_map = {
"DMT": "dmt",
"CTA": "cta",
"CVT": "cvt",
"OVT": "ovt",
}
if type_str not in rtype_map:
raise ValueError(f"未知的分辨率类型: {type_str}")
resolution_type = rtype_map[type_str]
def find_best_id_in_dict(res_map):
best_id, best_diff = None, float("inf")
for rid, info in res_map.items():
if info["width"] == width and info["height"] == height:
diff = abs(float(info["refresh_rate"]) - refresh_rate)
if diff < best_diff:
best_diff = diff
best_id = rid
return best_id if best_diff <= 1.0 else None
def find_best_id_in_list_map(res_map):
best_id, best_diff = None, float("inf")
for rid, infos in res_map.items():
for info in infos:
if info["width"] == width and info["height"] == height:
diff = abs(float(info["refresh_rate"]) - refresh_rate)
if diff < best_diff:
best_diff = diff
best_id = rid
return best_id if best_diff <= 1.0 else None
resolution_id = None
if resolution_type == "dmt":
resolution_id = find_best_id_in_dict(UCDEnum.TimingInfo.dmt_resolution_map)
elif resolution_type == "cta":
resolution_id = find_best_id_in_dict(UCDEnum.TimingInfo.cta_resolution_map)
elif resolution_type == "cvt":
resolution_id = find_best_id_in_list_map(
UCDEnum.TimingInfo.cvt_resolution_map
)
elif resolution_type == "ovt":
resolution_id = find_best_id_in_list_map(
UCDEnum.TimingInfo.ovt_resolution_map
)
result = {
"resolution_type": resolution_type,
"width": width,
"height": height,
"refresh_rate": refresh_rate,
"resolution_id": resolution_id,
}
return result
def set_timing_from_string(self, timing_str):
"""根据格式化timing字符串设置设备timing"""
try:
spec = self.parse_formatted_timing(timing_str)
except Exception:
log.exception("UCDController.set_timing_from_string parse failed timing=%s", timing_str)
return False
rtype = spec["resolution_type"]
rid = spec.get("resolution_id")
width = spec["width"]
height = spec["height"]
fr = spec["refresh_rate"]
if rid is not None and self.set_timing_from_id(rtype, rid):
log.info(
"UCDController.set_timing_from_string success by id timing=%s parsed=(%s id=%s)",
timing_str,
rtype,
rid,
)
return True
# Respect selected timing family first (DMT/CTA/CVT/OVT).
timing = self.search_timing(width, height, fr, rtype)
if timing is None:
# Fallback only for robustness: some SDKs may not classify a timing
# exactly as requested family even though width/height/fps matches.
timing = self.search_timing(width, height, fr, None)
if timing:
self.current_timing = timing
log.info(
"UCDController.set_timing_from_string success timing=%s parsed=(%s %sx%s@%s)",
timing_str,
rtype,
width,
height,
fr,
)
return True
log.error(
"UCDController.set_timing_from_string no timing matched timing=%s parsed=(%s %sx%s@%s)",
timing_str,
rtype,
width,
height,
fr,
)
return False
def set_timing_from_id(self, rtype, rid):
"""根据(type, id)设置设备timing"""
timing = None
if rtype.lower() == "dmt":
timing = self.timing_manager.get_dmt(rid)
elif rtype.lower() == "cta":
timing = self.timing_manager.get_cta(rid)
elif rtype.lower() == "cvt":
timing = self.timing_manager.get_cvt(rid)
elif rtype.lower() == "ovt":
get_ovt = getattr(self.timing_manager, "get_ovt", None)
if callable(get_ovt):
timing = get_ovt(rid)
else:
return False
else:
raise ValueError(f"不支持的分辨率类型: {rtype}")
if timing:
self.current_timing = timing
return True
else:
return False
def apply_signal_format(
self, color_space=None, data_range=None, bit_depth=None, color_format=None, **_
):
"""统一设置信号格式color_format / colorimetry / dynamic_range / bpc
Gamma/EOTF 传输特性在 ColorInfo API 中不存在;
max_cll / max_fall 暂无对应 SDK 接口,通过 **_ 接收后忽略。
"""
try:
if color_format:
fmt_key = UCDEnum.SignalFormat.OutputFormat.get_format_key(color_format)
cf = UCDEnum.ColorInfo.get_color_format(fmt_key)
if cf is not None:
self.color_info.color_format = cf
if color_space:
colorimetry = self._get_colorimetry_from_color_space(color_space, color_format)
if colorimetry:
self.color_info.colorimetry = colorimetry
if data_range:
if data_range == "Full":
self.color_info.dynamic_range = UniTAP.ColorInfo.DynamicRange.DR_VESA
elif data_range == "Limited":
self.color_info.dynamic_range = UniTAP.ColorInfo.DynamicRange.DR_CTA
if bit_depth:
bpc = UCDEnum.SignalFormat.BitDepth.get_bit_value(bit_depth)
self.color_info.bpc = bpc
if self.current_timing:
self.set_video_mode()
return True
except Exception:
return False
def _get_colorimetry_from_color_space(self, color_space, color_format=None):
"""将色彩空间字符串转换为UniTAP.ColorInfo.Colorimetry。
BT.2020 在 YCbCr 输出时使用 CM_ITUR_BT2020_YCbCrRGB 输出时使用 CM_ITUR_BT2020_RGB。
"""
is_ycbcr = UCDEnum.SignalFormat.OutputFormat.is_ycbcr(color_format)
bt2020_cm = (
UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_YCbCr
if is_ycbcr
else UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_RGB
)
colorimetry_map = {
"sRGB": UniTAP.ColorInfo.Colorimetry.CM_sRGB,
"BT.709": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT709,
"BT.601": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT601,
"BT.2020": bt2020_cm,
"DCI-P3": UniTAP.ColorInfo.Colorimetry.CM_DCI_P3,
}
return colorimetry_map.get(color_space)

View File

@@ -1,639 +0,0 @@
"""UCD 驱动层。
唯一暴露给上层的入口为 :class:`IUcdDevice` 抽象接口,以及实现:
:class:`UCD323Device`(生产)和 :class:`FakeUcdDevice`(单测)。
实现策略
--------
:class:`UCD323Device` 对外暴露完整的 :class:`IUcdDevice` 接口SDK 调用
当前仍委托给 :class:`drivers.UCD323_Function.UCDController`。
上层Service / GUI**不得**直接访问 ``UCDController``。
后续可将 SDK 调用逐步迁入本模块并删除旧文件。
文件分区:
§1 DeviceInfo / list_devices
§2 IUcdDevice 抽象接口
§3 UCD323Device 真实实现
§4 FakeUcdDevice 单测实现
"""
from __future__ import annotations
from contextlib import contextmanager
import logging
import threading
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING
from app.ucd_domain import (
ConnectionChanged,
EventBus,
Interface,
PatternApplied,
PatternKind,
PatternSpec,
SignalApplied,
SignalFormat,
TimingSpec,
UcdApplyFailed,
UcdConfigError,
UcdNotConnected,
UcdSdkError,
UcdState,
UcdStateError,
assert_transition,
)
if TYPE_CHECKING:
from drivers.UCD323_Function import UCDController
log = logging.getLogger(__name__)
_DEVICE_LOCK_TIMEOUT_SECONDS = 8.0
# ─── §1 DeviceInfo / list_devices ────────────────────────────────
@dataclass(frozen=True)
class DeviceInfo:
"""UCD 设备发现条目。
``display`` 是 SDK 给出的完整字符串(``"0: UCD-323 #12345678"``
``index`` / ``serial`` / ``model`` 通过解析得到,解析失败时为 None。
"""
display: str
index: int | None = None
serial: str | None = None
model: str | None = None
@classmethod
def parse(cls, display: str) -> "DeviceInfo":
idx: int | None = None
model: str | None = None
serial: str | None = None
try:
head, rest = display.split(":", 1)
idx = int(head.strip())
rest = rest.strip()
# 形如 "UCD-323 #12345678" 或 "UCD-323 #12345678 (in use)"
tokens = rest.split()
if tokens:
model = tokens[0]
for tok in tokens[1:]:
if tok.startswith("#") and len(tok) >= 2:
serial = tok.lstrip("#")
break
except Exception: # noqa: BLE001 - 解析失败保留原 display 即可
pass
return cls(display=display, index=idx, serial=serial, model=model)
def list_devices(controller: "UCDController") -> list[DeviceInfo]:
"""通过给定的底层 controller 枚举可用 UCD 设备。"""
try:
raw_list = controller.search_device()
except Exception as exc: # noqa: BLE001
raise UcdSdkError("枚举 UCD 设备失败") from exc
return [DeviceInfo.parse(s) for s in (raw_list or [])]
# ─── §2 IUcdDevice 抽象接口 ──────────────────────────────────────
class IUcdDevice(ABC):
"""UCD 信号发生器抽象设备。
上层Service / GUI**只**通过本接口操作硬件,不得穿透到
UniTAP SDK 或具体实现细节。
"""
@property
@abstractmethod
def state(self) -> UcdState: ...
@property
@abstractmethod
def info(self) -> DeviceInfo | None: ...
@abstractmethod
def open(self, info: DeviceInfo, *, interface: Interface = Interface.HDMI) -> None:
"""打开设备并选择接口角色。失败抛 :class:`UcdSdkError` 等。"""
@abstractmethod
def close(self) -> None:
"""关闭设备(幂等)。"""
@abstractmethod
def configure(self, signal: SignalFormat, timing: TimingSpec) -> bool:
"""写入信号格式与 timing未 apply。返回 ``format_changed``。"""
@abstractmethod
def set_pattern(self, pattern: PatternSpec) -> None:
"""设置当前图案(未 apply"""
@abstractmethod
def apply(self) -> None:
"""将已配置的信号格式 + 图案一次性提交给硬件。"""
@abstractmethod
def current_resolution(self) -> tuple[int, int]:
"""读取当前 timing 的 (width, height);未连接时返回默认 (3840, 2160)。"""
@abstractmethod
def search_devices(self) -> list[str]:
"""枚举可用设备的 SDK 显示字符串列表。"""
@property
@abstractmethod
def format_changed(self) -> bool:
"""最近一次视频模式提交是否相对上次发生变化。"""
@property
@abstractmethod
def last_error(self) -> str | None:
"""最近一次配置/应用失败时的错误描述。"""
@abstractmethod
def apply_signal_format(
self,
*,
color_space: str | None = None,
data_range: str | None = None,
bit_depth: str | None = None,
color_format: str | None = None,
max_cll: int | None = None,
max_fall: int | None = None,
) -> bool:
"""仅更新信号格式(沿用当前 timing不切换图案。"""
@abstractmethod
def set_ucd_params(self, config) -> bool:
"""按 PQConfig stage 色彩 / Timing / Pattern 类型(不 apply 输出)。"""
@abstractmethod
def send_current_pattern_params(self, pattern_params) -> bool:
"""更新当前 pattern 参数并 apply 到硬件。"""
@abstractmethod
def apply_config_and_run(self, config, pattern_params) -> bool:
"""set_ucd_params + set_pattern + run 复合操作。"""
# ─── §3 UCD323Device 真实实现 ────────────────────────────────────
class UCD323Device(IUcdDevice):
"""生产环境实现。内部委托给传统 :class:`UCDController`Phase 1"""
def __init__(self, bus: EventBus, controller: "UCDController | None" = None):
from drivers.UCD323_Function import UCDController as _UCDController
self._bus = bus
self._controller: "UCDController" = controller or _UCDController()
self._state: UcdState = UcdState.CLOSED
self._info: DeviceInfo | None = None
self._interface: Interface = Interface.HDMI
self._lock = threading.RLock()
self._lock_owner_tid: int | None = None
self._lock_owner_name: str | None = None
self._curr_signal: SignalFormat | None = None
self._curr_timing: TimingSpec | None = None
self._curr_pattern: PatternSpec | None = None
self._last_applied: tuple[SignalFormat, TimingSpec] | None = None
@contextmanager
def _acquire_device_lock(self, op_name: str):
current = threading.current_thread()
log.info(
"UCD323Device.%s acquiring lock timeout=%.1fs tid=%s thread=%s owner_tid=%s owner_thread=%s",
op_name,
_DEVICE_LOCK_TIMEOUT_SECONDS,
threading.get_ident(),
current.name,
self._lock_owner_tid,
self._lock_owner_name,
)
acquired = self._lock.acquire(timeout=_DEVICE_LOCK_TIMEOUT_SECONDS)
if not acquired:
raise UcdStateError(
"UCD device busy: lock timeout in "
f"UCD323Device.{op_name}, "
f"owner_tid={self._lock_owner_tid}, owner_thread={self._lock_owner_name}"
)
prev_owner_tid = self._lock_owner_tid
prev_owner_name = self._lock_owner_name
self._lock_owner_tid = threading.get_ident()
self._lock_owner_name = current.name
log.info(
"UCD323Device.%s lock acquired tid=%s thread=%s",
op_name,
self._lock_owner_tid,
self._lock_owner_name,
)
try:
yield
finally:
self._lock_owner_tid = prev_owner_tid
self._lock_owner_name = prev_owner_name
self._lock.release()
# -- 读访问 --------------------------------------------------
@property
def state(self) -> UcdState:
return self._state
@property
def info(self) -> DeviceInfo | None:
return self._info
# -- 生命周期 ------------------------------------------------
def open(self, info: DeviceInfo, *, interface: Interface = Interface.HDMI) -> None:
with self._acquire_device_lock("open"):
assert_transition(self._state, UcdState.OPENED)
if interface is not Interface.HDMI:
# Phase 1底层 UCDController.open() 写死了 HDMISource。
raise UcdConfigError(
f"暂不支持接口 {interface.value};当前仅实现 HDMI"
)
try:
ok = self._controller.open(info.display)
except Exception as exc: # noqa: BLE001
raise UcdSdkError(f"打开设备失败: {info.display}") from exc
if not ok:
raise UcdSdkError(f"打开设备失败: {info.display}")
self._info = info
self._interface = interface
self._state = UcdState.OPENED
self._bus.publish(ConnectionChanged(True, info.serial))
def close(self) -> None:
with self._acquire_device_lock("close"):
if self._state == UcdState.CLOSED:
return
try:
self._controller.close()
except Exception: # noqa: BLE001
log.exception("关闭 UCD 时发生异常")
self._state = UcdState.CLOSED
self._curr_signal = None
self._curr_timing = None
self._curr_pattern = None
self._last_applied = None
prev_serial = self._info.serial if self._info else None
self._info = None
self._bus.publish(ConnectionChanged(False, prev_serial))
# -- 配置 ----------------------------------------------------
def configure(self, signal: SignalFormat, timing: TimingSpec) -> bool:
with self._acquire_device_lock("configure"):
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 configure")
try:
# 颜色模式color_format / bpc / colorimetry
if not self._controller.set_color_mode(
signal.color_format.value,
int(signal.bpc),
_colorimetry_to_legacy_key(signal),
):
raise UcdConfigError(
f"set_color_mode 失败: {signal!r}"
)
# dynamic_range 在新接口中是一等公民
self._apply_dynamic_range(signal)
# Timing
if not self._controller.set_timing_from_string(str(timing)):
raise UcdConfigError(f"set_timing_from_string 失败: {timing}")
except UcdConfigError:
raise
except Exception as exc: # noqa: BLE001
raise UcdSdkError("configure 异常") from exc
self._curr_signal = signal
self._curr_timing = timing
self._state = UcdState.CONFIGURED
return (signal, timing) != self._last_applied
def set_pattern(self, pattern: PatternSpec) -> None:
with self._acquire_device_lock("set_pattern"):
# Phase 2 过渡:允许从 OPENED 直接 set_pattern——遗留路径
# test_runner 等)通过旧 controller.apply_signal_format 写入
# 信号格式,未经过本设备的 configure。此时 self._state 仍为
# OPENED但硬件实际已处于可接收 pattern 状态。
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 set_pattern")
self._curr_pattern = pattern
# 仅本地暂存,真正写硬件在 apply()
def apply(self) -> None:
with self._acquire_device_lock("apply"):
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 apply")
if self._curr_pattern is None:
raise UcdStateError("apply 前必须先 set_pattern")
try:
ok = self._apply_pattern_via_controller(self._curr_pattern)
except Exception as exc: # noqa: BLE001
raise UcdSdkError(
f"apply 异常: {type(exc).__name__}: {exc}"
) from exc
if not ok:
raise UcdApplyFailed(
f"apply 失败: pattern={self._curr_pattern.kind.value}"
)
# SignalApplied 事件仅在通过新 API configure 过时发出;
# 遗留路径下 self._curr_signal/_curr_timing 可能为 None。
if self._curr_signal is not None and self._curr_timing is not None:
changed = (self._curr_signal, self._curr_timing) != self._last_applied
self._last_applied = (self._curr_signal, self._curr_timing)
self._bus.publish(
SignalApplied(self._curr_signal, self._curr_timing, changed)
)
self._state = UcdState.APPLIED
self._bus.publish(PatternApplied(self._curr_pattern))
# -- 查询 ----------------------------------------------------
def current_resolution(self) -> tuple[int, int]:
try:
return self._controller.get_current_resolution((3840, 2160))
except Exception: # noqa: BLE001
return (3840, 2160)
def search_devices(self) -> list[str]:
try:
return self._controller.search_device() or []
except Exception as exc: # noqa: BLE001
raise UcdSdkError("枚举 UCD 设备失败") from exc
@property
def format_changed(self) -> bool:
return bool(getattr(self._controller, "format_changed", True))
@property
def last_error(self) -> str | None:
err = getattr(self._controller, "last_error", None)
return str(err) if err else None
def apply_signal_format(
self,
*,
color_space: str | None = None,
data_range: str | None = None,
bit_depth: str | None = None,
color_format: str | None = None,
max_cll: int | None = None,
max_fall: int | None = None,
) -> bool:
with self._acquire_device_lock("apply_signal_format"):
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 apply_signal_format")
return bool(
self._controller.apply_signal_format(
color_space=color_space,
data_range=data_range,
bit_depth=bit_depth,
color_format=color_format,
max_cll=max_cll,
max_fall=max_fall,
)
)
def set_ucd_params(self, config) -> bool:
with self._acquire_device_lock("set_ucd_params"):
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 set_ucd_params")
return bool(self._controller.set_ucd_params(config))
def send_current_pattern_params(self, pattern_params) -> bool:
with self._acquire_device_lock("send_current_pattern_params"):
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 send_current_pattern_params")
ok = bool(self._controller.send_current_pattern_params(pattern_params))
if ok:
self._state = UcdState.APPLIED
return ok
def apply_config_and_run(self, config, pattern_params) -> bool:
with self._acquire_device_lock("apply_config_and_run"):
if self._state == UcdState.CLOSED:
raise UcdNotConnected("UCD 未连接,无法 apply_config_and_run")
ctrl = self._controller
if not ctrl.set_ucd_params(config):
return False
if not ctrl.set_pattern(ctrl.current_pattern, pattern_params):
return False
ok = bool(ctrl.run())
if ok:
self._state = UcdState.APPLIED
return ok
# -- 内部辅助 ------------------------------------------------
def _apply_dynamic_range(self, signal: SignalFormat) -> None:
import UniTAP # 局部导入,避免本模块在无 SDK 环境下导入即失败
from app.ucd_domain import DynamicRange
ci = self._controller.color_info
if ci is None:
return
if signal.dynamic_range is DynamicRange.FULL:
ci.dynamic_range = UniTAP.ColorInfo.DynamicRange.DR_VESA
else:
ci.dynamic_range = UniTAP.ColorInfo.DynamicRange.DR_CTA
def _apply_pattern_via_controller(self, pattern: PatternSpec) -> bool:
"""根据 PatternKind 走最合适的旧 controller 路径。"""
if pattern.kind is PatternKind.IMAGE:
if not pattern.image_path:
raise UcdConfigError("IMAGE pattern 必须提供 image_path")
return bool(self._controller.send_image_pattern(pattern.image_path))
# 预定义图案路径:复用 controller.set_pattern + run()
from drivers.UCD323_Enum import UCDEnum # 局部导入避免循环
video_pattern = UCDEnum.VideoPatternInfo.get_video_pattern(pattern.kind.value)
if video_pattern is None:
raise UcdConfigError(f"不支持的 PatternKind: {pattern.kind!r}")
self._controller.current_pattern = video_pattern
params: list[int] | None = None
if pattern.kind is PatternKind.SOLID:
if pattern.solid_rgb is None:
raise UcdConfigError("SOLID pattern 必须提供 solid_rgb")
params = list(pattern.solid_rgb)
elif pattern.extras:
params = list(pattern.extras)
if not self._controller.set_pattern(video_pattern, params):
raise UcdApplyFailed("controller.set_pattern 返回 False")
# Skip apply_video_mode() (i.e. pg.set_vm) the video format is already
# configured by the main signal panel and re-applying it blocks until the
# device re-locks, causing an apparent UI freeze for pattern-only sends.
if not self._controller.apply_pattern():
raise UcdApplyFailed("controller.apply_pattern 返回 False")
if getattr(self._controller, "current_timing", None) is None:
raise UcdConfigError(
"current_timing is None; please apply selected test profile/timing before sending pattern"
)
try:
pg, _ = self._controller.get_tx_modules()
if not self._controller._apply_pg_output(pg):
raise UcdApplyFailed("controller.apply_pg_output 返回 False")
except UcdApplyFailed:
raise
except Exception as exc:
raise UcdSdkError("pg.apply() 失败") from exc
return True
def _colorimetry_to_legacy_key(signal: SignalFormat) -> str:
"""新 :class:`Colorimetry` → 旧 ``UCDEnum.ColorInfo.get_colorimetry`` 的 key。
BT.2020 在 YCbCr / RGB 输出下走不同 SDK 枚举(参考旧
``_get_colorimetry_from_color_space`` 的逻辑),这里也做同样的分支。
"""
from app.ucd_domain import Colorimetry, is_ycbcr
cm = signal.colorimetry
ycbcr = is_ycbcr(signal.color_format)
if cm is Colorimetry.BT2020:
return "bt2020ycbcr" if ycbcr else "bt2020rgb"
return {
Colorimetry.SRGB: "srgb",
Colorimetry.BT709: "bt709",
Colorimetry.BT601: "bt601",
Colorimetry.DCI_P3: "dcip3",
Colorimetry.ADOBE_RGB: "adobergb",
}.get(cm, "srgb")
# ─── §4 FakeUcdDevice 单测实现 ───────────────────────────────────
class FakeUcdDevice(IUcdDevice):
"""无硬件依赖的 Fake 实现;记录调用序列供单测断言。"""
def __init__(self, bus: EventBus | None = None) -> None:
self._bus = bus or EventBus()
self._state = UcdState.CLOSED
self._info: DeviceInfo | None = None
self._signal: SignalFormat | None = None
self._timing: TimingSpec | None = None
self._pattern: PatternSpec | None = None
self._last_applied: tuple[SignalFormat, TimingSpec] | None = None
self.calls: list[tuple] = [] # ("open", info) / ("configure", ...) ...
@property
def state(self) -> UcdState:
return self._state
@property
def info(self) -> DeviceInfo | None:
return self._info
def open(self, info: DeviceInfo, *, interface: Interface = Interface.HDMI) -> None:
assert_transition(self._state, UcdState.OPENED)
self.calls.append(("open", info, interface))
self._info = info
self._state = UcdState.OPENED
self._bus.publish(ConnectionChanged(True, info.serial))
def close(self) -> None:
if self._state == UcdState.CLOSED:
return
self.calls.append(("close",))
self._state = UcdState.CLOSED
prev = self._info.serial if self._info else None
self._info = None
self._signal = self._timing = self._pattern = None
self._last_applied = None
self._bus.publish(ConnectionChanged(False, prev))
def configure(self, signal: SignalFormat, timing: TimingSpec) -> bool:
if self._state == UcdState.CLOSED:
raise UcdNotConnected()
self.calls.append(("configure", signal, timing))
self._signal, self._timing = signal, timing
self._state = UcdState.CONFIGURED
return (signal, timing) != self._last_applied
def set_pattern(self, pattern: PatternSpec) -> None:
if self._state == UcdState.CLOSED:
raise UcdNotConnected()
self.calls.append(("set_pattern", pattern))
self._pattern = pattern
def apply(self) -> None:
if self._signal is None or self._timing is None:
raise UcdStateError("apply 前必须 configure")
if self._pattern is None:
raise UcdStateError("apply 前必须 set_pattern")
self.calls.append(("apply",))
changed = (self._signal, self._timing) != self._last_applied
self._last_applied = (self._signal, self._timing)
self._state = UcdState.APPLIED
self._bus.publish(SignalApplied(self._signal, self._timing, changed))
self._bus.publish(PatternApplied(self._pattern))
def current_resolution(self) -> tuple[int, int]:
if self._timing is None:
return (3840, 2160)
return (self._timing.width, self._timing.height)
def search_devices(self) -> list[str]:
return []
@property
def format_changed(self) -> bool:
return self._last_applied is None
@property
def last_error(self) -> str | None:
return None
def apply_signal_format(self, **kwargs) -> bool:
if self._state == UcdState.CLOSED:
raise UcdNotConnected()
self.calls.append(("apply_signal_format", kwargs))
return True
def set_ucd_params(self, config) -> bool:
if self._state == UcdState.CLOSED:
raise UcdNotConnected()
self.calls.append(("set_ucd_params", config))
self._state = UcdState.OPENED
return True
def send_current_pattern_params(self, pattern_params) -> bool:
if self._state == UcdState.CLOSED:
raise UcdNotConnected()
self.calls.append(("send_current_pattern_params", pattern_params))
self._state = UcdState.APPLIED
return True
def apply_config_and_run(self, config, pattern_params) -> bool:
if self._state == UcdState.CLOSED:
raise UcdNotConnected()
self.calls.append(("apply_config_and_run", config, pattern_params))
self._state = UcdState.APPLIED
return True
__all__ = [
"DeviceInfo",
"list_devices",
"IUcdDevice",
"UCD323Device",
"FakeUcdDevice",
]