重构移动utils文件夹
This commit is contained in:
590
drivers/UCD323_Enum.py
Normal file
590
drivers/UCD323_Enum.py
Normal file
@@ -0,0 +1,590 @@
|
||||
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
|
||||
return 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 [
|
||||
SignalFormat.GammaType.GAMMA_22,
|
||||
SignalFormat.GammaType.GAMMA_24,
|
||||
SignalFormat.GammaType.GAMMA_26,
|
||||
]
|
||||
|
||||
@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 [SignalFormat.DataRange.FULL, SignalFormat.DataRange.LIMITED]
|
||||
|
||||
@staticmethod
|
||||
def is_full_range(range_str):
|
||||
"""判断是否为 Full Range"""
|
||||
return range_str == SignalFormat.DataRange.FULL
|
||||
|
||||
class BitDepth:
|
||||
"""编码位深枚举"""
|
||||
|
||||
BIT_8 = "8bit"
|
||||
BIT_10 = "10bit"
|
||||
BIT_12 = "12bit"
|
||||
|
||||
@staticmethod
|
||||
def get_list():
|
||||
return [
|
||||
SignalFormat.BitDepth.BIT_8,
|
||||
SignalFormat.BitDepth.BIT_10,
|
||||
SignalFormat.BitDepth.BIT_12,
|
||||
]
|
||||
|
||||
@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 SignalFormat.HDRMetadata.MAX_CLL_OPTIONS
|
||||
|
||||
@staticmethod
|
||||
def get_maxfall_list():
|
||||
return SignalFormat.HDRMetadata.MAX_FALL_OPTIONS
|
||||
589
drivers/UCD323_Function.py
Normal file
589
drivers/UCD323_Function.py
Normal file
@@ -0,0 +1,589 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import UniTAP
|
||||
import time
|
||||
import gc
|
||||
from drivers.UCD323_Enum import UCDEnum
|
||||
|
||||
|
||||
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_mode = None
|
||||
self.status = False
|
||||
|
||||
self.current_timing = None
|
||||
self.current_pattern = None
|
||||
self.current_pattern_param = None
|
||||
self.current_pattern_params = None
|
||||
self.current_pattern_index = 0
|
||||
|
||||
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
|
||||
|
||||
except Exception as role_error:
|
||||
self._close_device_object(temp_dev)
|
||||
raise role_error
|
||||
|
||||
self.timing_manager = self.role.hdtx.pg.timing_manager
|
||||
self.color_mode = UniTAP.ColorInfo()
|
||||
self.status = True
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self._force_cleanup()
|
||||
return False
|
||||
|
||||
def close(self):
|
||||
"""关闭设备"""
|
||||
try:
|
||||
if self.dev:
|
||||
self._close_device_object(self.dev)
|
||||
|
||||
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.lUniTAP = None
|
||||
|
||||
for i in range(3):
|
||||
gc.collect()
|
||||
|
||||
time.sleep(2.0)
|
||||
|
||||
self.lUniTAP = UniTAP.TsiLib()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
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
|
||||
|
||||
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.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
|
||||
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
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.config = config
|
||||
test_type = self.config.current_test_type
|
||||
|
||||
if test_type == "hdr_movie":
|
||||
color_format_str = self.config.current_test_types[test_type]["color_format"]
|
||||
color_format = UCDEnum.ColorInfo.get_color_format(color_format_str)
|
||||
|
||||
if color_format:
|
||||
self.color_mode.color_format = color_format
|
||||
else:
|
||||
return False
|
||||
|
||||
else:
|
||||
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):
|
||||
return False
|
||||
|
||||
timing_str = self.config.current_test_types[test_type]["timing"]
|
||||
self.set_timing_from_string(timing_str)
|
||||
|
||||
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:
|
||||
return False
|
||||
|
||||
self.current_pattern = pattern
|
||||
self.current_pattern_params = self.config.current_pattern["pattern_params"]
|
||||
|
||||
return True
|
||||
|
||||
def run(self):
|
||||
"""运行设备"""
|
||||
self.apply_video_mode()
|
||||
self.apply_pattern()
|
||||
self.role.hdtx.pg.apply()
|
||||
return True
|
||||
|
||||
def set_color_mode(self, cf, bpc, cm):
|
||||
"""设置颜色模式"""
|
||||
current_dynamic_range = None
|
||||
current_transfer_characteristic = None
|
||||
|
||||
if hasattr(self.color_mode, "dynamic_range"):
|
||||
current_dynamic_range = self.color_mode.dynamic_range
|
||||
|
||||
if hasattr(self.color_mode, "transfer_characteristic"):
|
||||
current_transfer_characteristic = self.color_mode.transfer_characteristic
|
||||
|
||||
color_format = UCDEnum.ColorInfo.get_color_format(cf)
|
||||
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_mode.color_format = color_format
|
||||
self.color_mode.bpc = bpc
|
||||
self.color_mode.colorimetry = colorimetry
|
||||
|
||||
if current_dynamic_range is not None:
|
||||
self.color_mode.dynamic_range = current_dynamic_range
|
||||
|
||||
if current_transfer_characteristic is not None:
|
||||
self.color_mode.transfer_characteristic = current_transfer_characteristic
|
||||
|
||||
return True
|
||||
|
||||
def apply_video_mode(self):
|
||||
"""应用当前colormode和timing"""
|
||||
if self.current_timing:
|
||||
self.set_video_mode()
|
||||
return True
|
||||
return False
|
||||
|
||||
def set_video_mode(self):
|
||||
"""设置视频模式"""
|
||||
video_mode = UniTAP.VideoMode(
|
||||
timing=self.current_timing, color_info=self.color_mode
|
||||
)
|
||||
self.role.hdtx.pg.set_vm(vm=video_mode)
|
||||
return True
|
||||
|
||||
def set_pattern(self, pattern, pattern_params=None):
|
||||
"""设置pattern"""
|
||||
if self.current_timing:
|
||||
if (
|
||||
pattern == UCDEnum.VideoPatternInfo.VideoPatternParams.SolidColor
|
||||
or pattern == UCDEnum.VideoPatternInfo.VideoPatternParams.WhiteVStrips
|
||||
or pattern
|
||||
== UCDEnum.VideoPatternInfo.VideoPatternParams.GradientRGBStripes
|
||||
or pattern == UCDEnum.VideoPatternInfo.VideoPatternParams.MotionPattern
|
||||
or pattern == UCDEnum.VideoPatternInfo.VideoPatternParams.SquareWindow
|
||||
and pattern_params
|
||||
):
|
||||
self.set_pattern_params(pattern, pattern_params)
|
||||
return True
|
||||
return False
|
||||
|
||||
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:
|
||||
if pattern == UCDEnum.VideoPatternInfo.VideoPatternParams.SolidColor:
|
||||
self.current_pattern_param = UniTAP.SolidColorParams(
|
||||
first=pattern_params[0],
|
||||
second=pattern_params[1],
|
||||
third=pattern_params[2],
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
def apply_pattern(self):
|
||||
"""应用当前pattern"""
|
||||
if self.current_pattern:
|
||||
self.role.hdtx.pg.set_pattern(self.current_pattern)
|
||||
|
||||
if self.current_pattern_param:
|
||||
self.role.hdtx.pg.set_pattern_params(self.current_pattern_param)
|
||||
return True
|
||||
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
|
||||
|
||||
timing = self.timing_manager.search(
|
||||
h_active=width,
|
||||
v_active=height,
|
||||
f_rate=int(refresh_rate) * 1000,
|
||||
standard=standard,
|
||||
)
|
||||
|
||||
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"""
|
||||
spec = self.parse_formatted_timing(timing_str)
|
||||
rtype = spec["resolution_type"]
|
||||
width = spec["width"]
|
||||
height = spec["height"]
|
||||
fr = spec["refresh_rate"]
|
||||
|
||||
if rtype != "ovt":
|
||||
timing = self.search_timing(width, height, fr, rtype)
|
||||
if timing:
|
||||
self.current_timing = timing
|
||||
return True
|
||||
|
||||
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)
|
||||
else:
|
||||
raise ValueError(f"不支持的分辨率类型: {rtype}")
|
||||
|
||||
if timing:
|
||||
self.current_timing = timing
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def set_sdr_format(
|
||||
self, color_space=None, gamma=None, data_range=None, bit_depth=None
|
||||
):
|
||||
"""设置SDR信号格式"""
|
||||
|
||||
def _get_colorimetry_from_color_space(color_space_name):
|
||||
"""将色彩空间名称转换为UniTAP colorimetry枚举值"""
|
||||
try:
|
||||
colorimetry_map = {
|
||||
"BT.709": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT709,
|
||||
"BT.601": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT601,
|
||||
"BT.2020": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_RGB,
|
||||
}
|
||||
result = colorimetry_map.get(color_space_name)
|
||||
return result if result else UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT709
|
||||
except Exception as e:
|
||||
return UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT709
|
||||
|
||||
def _set_gamma_transfer_characteristic(gamma_value_str):
|
||||
"""设置Gamma传输特性"""
|
||||
try:
|
||||
gamma_value = float(gamma_value_str)
|
||||
|
||||
if abs(gamma_value - 2.2) < 0.1:
|
||||
self.color_mode.transfer_characteristic = (
|
||||
UniTAP.ColorInfo.TransferCharacteristic.TRANSFER_BT709
|
||||
)
|
||||
return True
|
||||
|
||||
elif abs(gamma_value - 2.4) < 0.1:
|
||||
if hasattr(
|
||||
UniTAP.ColorInfo.TransferCharacteristic, "TRANSFER_GAMMA24"
|
||||
):
|
||||
self.color_mode.transfer_characteristic = (
|
||||
UniTAP.ColorInfo.TransferCharacteristic.TRANSFER_GAMMA24
|
||||
)
|
||||
return True
|
||||
else:
|
||||
self.color_mode.transfer_characteristic = (
|
||||
UniTAP.ColorInfo.TransferCharacteristic.TRANSFER_BT709
|
||||
)
|
||||
return False
|
||||
|
||||
elif abs(gamma_value - 2.6) < 0.1:
|
||||
if hasattr(
|
||||
UniTAP.ColorInfo.TransferCharacteristic, "TRANSFER_GAMMA26"
|
||||
):
|
||||
self.color_mode.transfer_characteristic = (
|
||||
UniTAP.ColorInfo.TransferCharacteristic.TRANSFER_GAMMA26
|
||||
)
|
||||
return True
|
||||
else:
|
||||
self.color_mode.transfer_characteristic = (
|
||||
UniTAP.ColorInfo.TransferCharacteristic.TRANSFER_BT709
|
||||
)
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
try:
|
||||
if color_space:
|
||||
colorimetry = _get_colorimetry_from_color_space(color_space)
|
||||
if colorimetry:
|
||||
self.color_mode.colorimetry = colorimetry
|
||||
|
||||
if gamma:
|
||||
_set_gamma_transfer_characteristic(gamma)
|
||||
|
||||
if data_range:
|
||||
if data_range == "Full":
|
||||
self.color_mode.dynamic_range = (
|
||||
UniTAP.ColorInfo.DynamicRange.DR_VESA
|
||||
)
|
||||
elif data_range == "Limited":
|
||||
self.color_mode.dynamic_range = UniTAP.ColorInfo.DynamicRange.DR_CTA
|
||||
|
||||
if bit_depth:
|
||||
bpc = UCDEnum.SignalFormat.BitDepth.get_bit_value(bit_depth)
|
||||
self.color_mode.bpc = bpc
|
||||
|
||||
if self.current_timing:
|
||||
self.set_video_mode()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
def set_hdr_format(
|
||||
self,
|
||||
color_space=None,
|
||||
data_range=None,
|
||||
bit_depth=None,
|
||||
max_cll=None,
|
||||
max_fall=None,
|
||||
):
|
||||
"""设置HDR信号格式"""
|
||||
try:
|
||||
if color_space:
|
||||
colorimetry = self._get_colorimetry_from_color_space(color_space)
|
||||
if colorimetry:
|
||||
self.color_mode.colorimetry = colorimetry
|
||||
|
||||
if data_range:
|
||||
if data_range == "Full":
|
||||
self.color_mode.dynamic_range = (
|
||||
UniTAP.ColorInfo.DynamicRange.DR_VESA
|
||||
)
|
||||
elif data_range == "Limited":
|
||||
self.color_mode.dynamic_range = UniTAP.ColorInfo.DynamicRange.DR_CTA
|
||||
|
||||
if bit_depth:
|
||||
bpc = UCDEnum.SignalFormat.BitDepth.get_bit_value(bit_depth)
|
||||
self.color_mode.bpc = bpc
|
||||
|
||||
if self.current_timing:
|
||||
self.set_video_mode()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
def _get_colorimetry_from_color_space(self, color_space):
|
||||
"""将色彩空间字符串转换为UniTAP.ColorInfo.Colorimetry"""
|
||||
colorimetry_map = {
|
||||
"BT.709": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT709,
|
||||
"BT.601": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT601,
|
||||
"BT.2020": UniTAP.ColorInfo.Colorimetry.CM_ITUR_BT2020_RGB,
|
||||
"DCI-P3": UniTAP.ColorInfo.Colorimetry.CM_DCI_P3,
|
||||
}
|
||||
return colorimetry_map.get(color_space)
|
||||
0
drivers/__init__.py
Normal file
0
drivers/__init__.py
Normal file
117
drivers/baseSerail.py
Normal file
117
drivers/baseSerail.py
Normal file
@@ -0,0 +1,117 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import serial
|
||||
import time
|
||||
import binascii
|
||||
|
||||
# CRC16矩阵表;
|
||||
CRC_TABLE = [
|
||||
0x0000, 0x1021, 0x2042, 0x3063,
|
||||
0x4084, 0x50A5, 0x60C6, 0x70E7,
|
||||
0x8108, 0x9129, 0xA14A, 0xB16B,
|
||||
0xC18C, 0xD1AD, 0xE1CE, 0xF1EF ]
|
||||
|
||||
|
||||
# 计算CRC16值;
|
||||
def crc16(bys, bysLen):
|
||||
uwCRC, ucTemp = 0xFFFF, 0x00
|
||||
index = 0
|
||||
while index < bysLen:
|
||||
ucTemp = (uwCRC >> 0x0C) & 0xFF
|
||||
uwCRC <<= 4
|
||||
uwCRC ^= CRC_TABLE[ucTemp ^ ((bys[index]) >> 0x04)]
|
||||
uwCRC &= 0xFFFF
|
||||
ucTemp = (uwCRC >> 0x0C) & 0xFF
|
||||
uwCRC <<= 4
|
||||
uwCRC ^= CRC_TABLE[ucTemp ^ (bys[index] & 0x0F)]
|
||||
uwCRC &= 0xFFFF
|
||||
index += 1
|
||||
return uwCRC
|
||||
|
||||
def convert_rgb16(rgb16_values):
|
||||
data = bytearray()
|
||||
for value in rgb16_values:
|
||||
high_byte = (value & 0xFF00) >> 8
|
||||
low_byte = value & 0x00FF
|
||||
|
||||
data.append(high_byte)
|
||||
data.append(low_byte)
|
||||
|
||||
return data
|
||||
|
||||
class BaseSerial():
|
||||
def __init__(self):
|
||||
self.exception = None
|
||||
self.ser = serial.Serial()
|
||||
|
||||
def open(self, port, baudrate=115200, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=3,
|
||||
writeTimeout=3):
|
||||
if type(port) == type(0):
|
||||
self.ser.port = "COM" + str(port)
|
||||
else:
|
||||
self.ser.port = port
|
||||
# 波特率;
|
||||
self.ser.baudrate = baudrate
|
||||
# 数据位;
|
||||
self.ser.bytesize = bytesize
|
||||
# 校验码;
|
||||
self.ser.parity = parity # PARITY_NONE=0
|
||||
# 停止位;
|
||||
self.ser.stopbits = stopbits
|
||||
# 串口读超时值
|
||||
self.ser.timeout = timeout
|
||||
# 串口写超时值
|
||||
self.ser.write_timeout = writeTimeout
|
||||
try:
|
||||
self.ser.open()
|
||||
return self.ser.is_open
|
||||
except Exception as e:
|
||||
self.exception = e
|
||||
print('打开串口异常', str(e))
|
||||
return False
|
||||
|
||||
def reOpen(self):
|
||||
self.exception = None
|
||||
self.ser.close()
|
||||
try:
|
||||
self.ser.open()
|
||||
return self.ser.is_open
|
||||
except Exception as e:
|
||||
self.exception = e
|
||||
return False
|
||||
|
||||
def close(self):
|
||||
self.ser.close()
|
||||
|
||||
def write(self, cmd: bytearray):
|
||||
try:
|
||||
self.exception = None
|
||||
writelen = self.ser.write(cmd)
|
||||
self.ser.flush()
|
||||
# print("writehex:" + str(binascii.b2a_hex(cmd)) + " writelen=" + str(writelen))
|
||||
return True if writelen == len(cmd) else False
|
||||
except Exception as e:
|
||||
self.exception = e
|
||||
print('写串口异常', str(e))
|
||||
return False
|
||||
|
||||
def read(self, size=0):
|
||||
try:
|
||||
self.exception = None
|
||||
bys = self.ser.read(1)
|
||||
for i in range(0, 3):
|
||||
while self.ser.in_waiting > 0:
|
||||
d = self.ser.read(1)
|
||||
bys = bys + d
|
||||
time.sleep(0.01)
|
||||
|
||||
if bys.__len__() < size:
|
||||
time.sleep(0.1)
|
||||
if self.ser.in_waiting > 0:
|
||||
d = self.ser.read(self.ser.in_waiting)
|
||||
bys = bys + d
|
||||
# print("readhex:" + str(binascii.hexlify(bys)) + " readlen=" + str(bys.__len__()))
|
||||
return bys
|
||||
except Exception as e:
|
||||
self.exception = e
|
||||
print('读串口异常', str(e))
|
||||
return None
|
||||
181
drivers/caSerail.py
Normal file
181
drivers/caSerail.py
Normal file
@@ -0,0 +1,181 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import re
|
||||
import time
|
||||
from drivers.baseSerail import BaseSerial
|
||||
# from baseSerail import BaseSerial
|
||||
import colour
|
||||
|
||||
class CASerail(BaseSerial):
|
||||
def __init__(self):
|
||||
BaseSerial.__init__(self)
|
||||
|
||||
def __del__(self):
|
||||
self.close()
|
||||
|
||||
def checkport(self):
|
||||
if not self.ser.is_open:
|
||||
self.reOpen()
|
||||
|
||||
return self.ser.is_open
|
||||
|
||||
def sendcmd(self, cmd:str):
|
||||
self.write(cmd.encode('utf-8'))
|
||||
return self.read()
|
||||
|
||||
'''开启通讯'''
|
||||
def startCommunication(self):
|
||||
self.sendcmd('COM,1\r')
|
||||
|
||||
'''结束通讯'''
|
||||
def endCommunication(self):
|
||||
self.sendcmd('COM,0\r')
|
||||
|
||||
'''设置用于测量的校准通道'''
|
||||
def setChannel(self, channel:str):
|
||||
self.sendcmd('MCH,%s\r'%(channel))
|
||||
|
||||
'''
|
||||
设置显示模式
|
||||
'''
|
||||
def setDisplayMode(self, mode:int):
|
||||
return self.sendcmd('MDS,%d\r'%(mode))
|
||||
|
||||
'''设置显示模式为XYZ'''
|
||||
def set_XYZ_Display(self):
|
||||
return self.sendcmd('MDS,%d\r'%(7))
|
||||
|
||||
'''设置显示模式为xyLv'''
|
||||
def set_xyLv_Display(self):
|
||||
return self.sendcmd('MDS,%d\r'%(0))
|
||||
|
||||
'''设置显示模式为xyLv'''
|
||||
def set_all_Display(self):
|
||||
return self.sendcmd('MDS,%d\r'%(0))
|
||||
|
||||
def set_Display(self, mode:int):
|
||||
'''
|
||||
0: x,y,LV
|
||||
1: Tcp,duv,LV
|
||||
5: u’,v’,LV
|
||||
7: X,Y,Z
|
||||
8: λd,Pe,LV
|
||||
'''
|
||||
return self.sendcmd('MDS,%d\r'%(mode))
|
||||
|
||||
|
||||
'''
|
||||
设置同步模式和同步频率
|
||||
'''
|
||||
def setSynchMode(self, mode: int, freq: int = None):
|
||||
if freq == None:
|
||||
return self.sendcmd('SCS,%d\r'%(mode))
|
||||
else:
|
||||
return self.sendcmd('SCS,%d,%d\r'%(mode,freq))
|
||||
|
||||
'''
|
||||
设置测量速度
|
||||
'''
|
||||
def setMeasureSpeed(self, speed: int):
|
||||
return self.sendcmd('FSC,%d\r'%(speed))
|
||||
|
||||
'''执行零点校准'''
|
||||
def setZeroCalibration(self):
|
||||
return self.sendcmd('ZRC\r')
|
||||
|
||||
'''读取当前显示模式下的三值'''
|
||||
def readDisplay(self):
|
||||
data = self.sendcmd('MES\r')
|
||||
x,y,z = self.__get_display_data(data.decode('utf-8'))
|
||||
return float(x), float(y), float(z)
|
||||
|
||||
'''获取当前显示模式值'''
|
||||
def __get_display_data(self, data):
|
||||
p = re.compile(r"(\D*)(\d+),P1 (.*);(.*);(.*)", re.DOTALL)
|
||||
mo = p.search(data)
|
||||
if mo is None:
|
||||
print("无匹配正则")
|
||||
pw = re.compile(r"(\D*)(\d+)", re.DOTALL)
|
||||
mo = pw.search(data)
|
||||
if mo is None:
|
||||
# print("短匹配失败")
|
||||
return None, None, None
|
||||
else:
|
||||
return None, None, None
|
||||
else:
|
||||
# print(mo.group(1), mo.group(2), mo.group(3), mo.group(4), mo.group(5))
|
||||
return mo.group(3), mo.group(4), mo.group(5)
|
||||
|
||||
'''读取所有数据'''
|
||||
def readAllDisplay(self):
|
||||
data = self.sendcmd('MES,2\r')
|
||||
x,y,lv,X,Y,Z = self.__get_all_display_data(data.decode('utf-8'))
|
||||
return float(x), float(y), float(lv), float(X), float(Y), float(Z)
|
||||
|
||||
'''获取所有显示模式值'''
|
||||
def __get_all_display_data(self, data):
|
||||
p = re.compile(r"(\D*)(\d+),P1,(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)", re.DOTALL)
|
||||
mo = p.search(data)
|
||||
if mo is None:
|
||||
return None, None, None, None, None, None
|
||||
else:
|
||||
return mo.group(4), mo.group(5), mo.group(6), mo.group(9), mo.group(10), mo.group(11)
|
||||
|
||||
def xyY_to_XYZ( x, y, Y):
|
||||
X = x * Y / y
|
||||
Z = (1 - x - y) * Y / y
|
||||
return X, Y, Z
|
||||
|
||||
|
||||
def XYZ_to_lambda_pe(XYZ, illuminant='D65'):
|
||||
xy = colour.XYZ_to_xy(XYZ)
|
||||
white = colour.CCS_ILLUMINANTS['CIE 1931 2 Degree Standard Observer'][illuminant]
|
||||
λd, comp, xy_intersection = colour.dominant_wavelength(xy, white)
|
||||
Pe = colour.excitation_purity(xy, white)
|
||||
return xy, λd, comp, Pe
|
||||
|
||||
|
||||
def test_ca410():
|
||||
print('\r\rtest ca410\r\r')
|
||||
ca = CASerail()
|
||||
ca.open("COM3", 19200, 7, 'E', 2)
|
||||
ca.startCommunication()
|
||||
ca.setSynchMode(3)
|
||||
ca.setMeasureSpeed(2)
|
||||
ca.setZeroCalibration()
|
||||
ca.setChannel('01')
|
||||
|
||||
ca.set_Display(0)
|
||||
xyYXYZ = ca.readAllDisplay()
|
||||
print(f"xyYXYZ: {xyYXYZ}")
|
||||
XYZ = xyYXYZ[3], xyYXYZ[4], xyYXYZ[5]
|
||||
xy = colour.XYZ_to_xy(XYZ)
|
||||
print(f"xy: {xy}")
|
||||
|
||||
ca.set_Display(1)
|
||||
tcpduvlvXYZ = ca.readAllDisplay()
|
||||
print(f"tcpduvlvXYZ: {tcpduvlvXYZ}")
|
||||
XYZ = tcpduvlvXYZ[3], tcpduvlvXYZ[4], tcpduvlvXYZ[5]
|
||||
xy = colour.XYZ_to_xy(XYZ)
|
||||
TCP = colour.temperature.xy_to_CCT(xy, 'Hernandez 1999')
|
||||
print(f"TCP: {TCP}K")
|
||||
|
||||
ca.set_Display(5)
|
||||
uvlvXYZ = ca.readAllDisplay()
|
||||
print(f"uvlvXYZ: {uvlvXYZ}")
|
||||
XYZ = uvlvXYZ[3], uvlvXYZ[4], uvlvXYZ[5]
|
||||
xy = colour.XYZ_to_xy(XYZ)
|
||||
u_prime, v_prime, _ = colour.XYZ_to_CIE1976UCS(XYZ)
|
||||
print(f"u': {u_prime}, v': {v_prime}")
|
||||
|
||||
|
||||
ca.set_Display(8)
|
||||
lambdaPeXYZ = ca.readAllDisplay()
|
||||
print(f"lambdaPeXYZ: {lambdaPeXYZ}")
|
||||
XYZ = lambdaPeXYZ[3], lambdaPeXYZ[4], lambdaPeXYZ[5]
|
||||
xy, λd, isComplementary, Pe = XYZ_to_lambda_pe(XYZ)
|
||||
print(f"xy: {xy}, λd: {λd}, isComplementary: {isComplementary}, Pe: {Pe}")
|
||||
|
||||
|
||||
ca.endCommunication()
|
||||
|
||||
# test_ca410()
|
||||
423
drivers/tvSerail.py
Normal file
423
drivers/tvSerail.py
Normal file
@@ -0,0 +1,423 @@
|
||||
# -*- coding: UTF-8 -*-
|
||||
import zlib
|
||||
from xmlrpc.client import Boolean
|
||||
from drivers.baseSerail import BaseSerial
|
||||
import binascii
|
||||
import drivers.baseSerail as baseSerail
|
||||
|
||||
# 包头码(包引导码)
|
||||
PHeader = {
|
||||
"TV_Debug": 0xAA,
|
||||
"TV_Return": 0xAB,
|
||||
"TV_Panel_Debug": 0xAC,
|
||||
"TV_Panel_Return": 0xAD,
|
||||
"TV_Debug_Other": 0xAE,
|
||||
"TV_Other_Return": 0xAF,
|
||||
}
|
||||
|
||||
# 命令结果码;
|
||||
RCode = {
|
||||
"RC_OK": 0x0A, # 命令执行通过;
|
||||
"RC_ERR": 0x0E, # 命令错误或无法执行;
|
||||
"RC_LOSE": 0x0F, # 命令丢失(链路层出错);
|
||||
}
|
||||
|
||||
|
||||
# 命令的封装与解析;
|
||||
class TvParse():
|
||||
def __init__(self):
|
||||
# 头引导码(默认1字节,0x86命令二字节);
|
||||
self.Header = [PHeader['TV_Debug']]
|
||||
# 包长(默认1字节,0x86长度二字节);
|
||||
self.Length = [0x00]
|
||||
# 命令码,1字节(int类型);
|
||||
self.Command = [0x00]
|
||||
# 子命令参数;
|
||||
self.SubCommand = []
|
||||
# 数据,bytearray格式;
|
||||
self.Data = []
|
||||
# crch,1字节;
|
||||
self.CRCH = 0xFF
|
||||
# crcl,1字节;
|
||||
self.CRCL = 0xFF
|
||||
# 是否是特殊命令;
|
||||
self.FEFlag = False
|
||||
# 是否是多参数命令;(同一个Command,有多个含义的参数)
|
||||
self.isMultipleParams = False
|
||||
# 正确执行后的结果值,bytearray格式的二维数组;
|
||||
self.successData = []
|
||||
|
||||
def parseCommand(self, head: int, command: list[int], subCommand: list[int] = [], data: bytearray = bytearray(), isMultipleParams=False, FEFlag=False):
|
||||
self.Header[0] = head
|
||||
self.Command = command
|
||||
self.SubCommand = subCommand
|
||||
self.Data = data
|
||||
self.FEFlag = FEFlag
|
||||
self.isMultipleParams = isMultipleParams
|
||||
if FEFlag:
|
||||
self.Header.append(0xFE)
|
||||
# 注意:4 = crch + crcl + lenh + lenl
|
||||
length = 4 + self.Header.__len__() + self.Data.__len__() + self.Command.__len__() + self.SubCommand.__len__()
|
||||
# 高字节;
|
||||
self.Length[0] = length >> 8
|
||||
# 低字节;
|
||||
self.Length.append(length & 0xFF)
|
||||
else:
|
||||
# 注意:3 = crch + crcl + len
|
||||
self.Length[0] = 3 + self.Header.__len__() + self.Data.__len__() + self.Command.__len__() + self.SubCommand.__len__()
|
||||
|
||||
# 生成package;
|
||||
package = bytearray(self.Header + self.Length + self.Command + self.SubCommand) + self.Data
|
||||
|
||||
self.calculateCRC(package)
|
||||
|
||||
# 形成最终的命令;
|
||||
package = bytearray(self.Header + self.Length + self.Command + self.SubCommand) + self.Data + bytearray([self.CRCH, self.CRCL])
|
||||
|
||||
# 打印最终命令的十六进制形式,每两个字符之间加一个空格
|
||||
# hex_string = ' '.join(f'{byte:02X}' for byte in package)
|
||||
# print("最终命令的十六进制形式:", hex_string)
|
||||
return package
|
||||
|
||||
def calculateCRC(self, data: bytearray):
|
||||
crc = baseSerail.crc16(data, data.__len__())
|
||||
# 高字节;
|
||||
self.CRCH = crc >> 8
|
||||
# 低字节;
|
||||
self.CRCL = crc & 0xFF
|
||||
|
||||
def parseCommandFc(self, head: int, command: list[int], subCommand: list[int] = [], data: bytearray = bytearray(), isMultipleParams=False):
|
||||
self.Header[0] = head
|
||||
self.Command = command
|
||||
self.SubCommand = subCommand
|
||||
self.Data = data
|
||||
self.isMultipleParams = isMultipleParams
|
||||
self.Length[0] = 3 + self.Header.__len__() + self.Data.__len__() + self.Command.__len__() + self.SubCommand.__len__()
|
||||
|
||||
package = bytearray(self.Header + self.Length + self.Command + self.SubCommand) + self.Data
|
||||
self.calculateCRC(package)
|
||||
package = bytearray(self.Header + self.Length + self.Command + self.SubCommand) + self.Data + bytearray([self.CRCH]) + bytearray([self.CRCL])
|
||||
return package
|
||||
|
||||
# 成功返回:(AB 08 FC 08 EC 80 CR1 CR2)
|
||||
# 失败返回:(AB 08 FC 08 EC 00 CR1 CR2)
|
||||
def parseResultFc(self,data):
|
||||
data = bytearray(data)
|
||||
|
||||
return False
|
||||
|
||||
def parseResult(self, data):
|
||||
data = bytearray(data)
|
||||
if data.__len__() < 5:
|
||||
return False
|
||||
|
||||
retCode = 0
|
||||
if self.Header[0] == PHeader['TV_Debug']:
|
||||
retCode = PHeader['TV_Return']
|
||||
elif self.Header[0] == PHeader['TV_Panel_Debug']:
|
||||
retCode = PHeader['TV_Panel_Return']
|
||||
elif self.Header[0] == PHeader['TV_Debug_Other']:
|
||||
retCode = PHeader['TV_Other_Return']
|
||||
|
||||
if retCode != data[0]:
|
||||
return False
|
||||
|
||||
package = []
|
||||
tooken_len = 0
|
||||
return True
|
||||
|
||||
def parseString(self, data):
|
||||
data = bytearray(data)
|
||||
if data.__len__() < 5:
|
||||
return False
|
||||
|
||||
retCode = 0
|
||||
if self.Header[0] == PHeader['TV_Debug']:
|
||||
retCode = PHeader['TV_Return']
|
||||
elif self.Header[0] == PHeader['TV_Panel_Debug']:
|
||||
retCode = PHeader['TV_Panel_Return']
|
||||
elif self.Header[0] == PHeader['TV_Debug_Other']:
|
||||
retCode = PHeader['TV_Other_Return']
|
||||
|
||||
if retCode != data[0]:
|
||||
return False
|
||||
|
||||
package = []
|
||||
tooken_len = 0
|
||||
while True:
|
||||
if tooken_len >= data.__len__():
|
||||
break
|
||||
if self.FEFlag:
|
||||
package_len = data[tooken_len + 1] << 8 + data[tooken_len + 2]
|
||||
else:
|
||||
package_len = data[tooken_len + 1]
|
||||
package = data[tooken_len:tooken_len + package_len]
|
||||
|
||||
if package[0] != retCode:
|
||||
print('Incorrect package head!\n')
|
||||
return False
|
||||
|
||||
crc = crc16(package, package_len - 2)
|
||||
CRCH = crc >> 8
|
||||
CRCL = crc & 0xFF
|
||||
if CRCH != package[-2] and CRCL != package[-1]:
|
||||
return False
|
||||
|
||||
if tooken_len == 0:
|
||||
if package[2] != RCode['RC_OK']:
|
||||
return False
|
||||
else:
|
||||
if self.Command[0] == 0xFC:
|
||||
pass
|
||||
else:
|
||||
if package[2] - 1 != self.Command[0]:
|
||||
return False
|
||||
if package[2] == 0xFE:
|
||||
self.successData.append(package[5:-2])
|
||||
else:
|
||||
self.successData.append(package[3:-2])
|
||||
|
||||
tooken_len += package_len
|
||||
|
||||
# print('successData:', binascii.b2a_hex(bytearray(self.Command)), self.successData)
|
||||
return self.successData
|
||||
|
||||
|
||||
class tvSerial(BaseSerial):
|
||||
def __init__(self):
|
||||
BaseSerial.__init__(self)
|
||||
|
||||
def __del__(self):
|
||||
self.close()
|
||||
|
||||
def checkport(self):
|
||||
if not self.ser.is_open:
|
||||
self.reOpen()
|
||||
|
||||
return self.ser.is_open
|
||||
|
||||
def sendcmd(self, cmd: list[int]):
|
||||
self.write(bytearray(cmd))
|
||||
return self.read()
|
||||
|
||||
'''协议模式发送命令'''
|
||||
def sendcmdEx(self, head: int, command: list[int], subCommand: list[int] = [], data: bytearray = bytearray(), FEFlag: Boolean = False, returnParam: Boolean = False):
|
||||
cmd = TvParse()
|
||||
package = cmd.parseCommand(head, command, subCommand, data, returnParam, FEFlag)
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
return cmd.parseResult(package)
|
||||
|
||||
return False
|
||||
|
||||
def sendcmdEx_string_return(self, head: int, command: list[int], subCommand: list[int] = [], data: bytearray = bytearray(), FEFlag: Boolean = False, returnParam: Boolean = False):
|
||||
cmd = TvParse()
|
||||
package = cmd.parseCommand(head, command, subCommand, data, returnParam, FEFlag)
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
return cmd.parseString(package)
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def sendcmdFc(self, head: int, command: list[int], subCommand: list[int] = [], data: bytearray = bytearray(), FEFlag: Boolean = False, returnParam: Boolean = False):
|
||||
cmd = TvParse()
|
||||
package = cmd.parseCommand(head, command, subCommand, data, returnParam, FEFlag)
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
return cmd.parseResult(package)
|
||||
|
||||
return False
|
||||
|
||||
def gen_zzip(selfe,gm_file,save_file):
|
||||
with open(gm_file, 'rb') as f:
|
||||
data = f.read()
|
||||
|
||||
compressed_data = zlib.compress(data)
|
||||
|
||||
with open(save_file, 'wb') as f:
|
||||
f.write(compressed_data)
|
||||
|
||||
'''发送pattern'''
|
||||
def send_parttern(self, rgb: list[int]):
|
||||
cmd = TvParse()
|
||||
package = cmd.parseCommand(0xAA, [0x28], [], bytearray(rgb))
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
return cmd.parseResult(package)
|
||||
|
||||
return False
|
||||
|
||||
# '''发送10bit-pattern'''
|
||||
# def send_10_bit_parttern(self, rgb: list[int]):
|
||||
# cmd = MokaParse()
|
||||
# rgb16 = convert_rgb16(rgb)
|
||||
# package = cmd.parseCommandFc(0xAA, [0xFC,0x07,0x06], [0xEC,0x01,0x86,0x00], rgb16)
|
||||
# if self.write(package):
|
||||
# package = self.read()
|
||||
# return cmd.parseResult(package)
|
||||
|
||||
# return False
|
||||
|
||||
'''发送12bit-pattern'''
|
||||
def send_12_bit_parttern(self, rgb: list[int]):
|
||||
cmd = TvParse()
|
||||
rgb16 = baseSerail.convert_rgb16(rgb)
|
||||
package = cmd.parseCommand(0xAA, [0x28], [], rgb16)
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
return cmd.parseResult(package)
|
||||
|
||||
return False
|
||||
|
||||
'''发送gamma文件'''
|
||||
def send_gamma(self, file_path: str):
|
||||
fp = open(file_path, 'rb')
|
||||
cmd = TvParse()
|
||||
data = fp.read()
|
||||
# 生成命令包
|
||||
package = cmd.parseCommand(0xAA, [0xE9], [0x02], data, False, True)
|
||||
# 将命令包写入临时文件
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
print(cmd.parseResult(package))
|
||||
return cmd.parseResult(package)
|
||||
|
||||
'''激活gamma文件'''
|
||||
def send_gamma_active(self, ini_file: str):
|
||||
ini_fp = open(ini_file, 'rb')
|
||||
ind_data = ini_fp.read()
|
||||
ini_fp.close()
|
||||
|
||||
# 计算crc
|
||||
crc = crc16(ind_data, ind_data.__len__())
|
||||
CRC_GM = [crc >> 8, crc & 0xFF]
|
||||
|
||||
cmd = TvParse()
|
||||
package = cmd.parseCommand(0xAA, [0x99], [0x06], bytearray(CRC_GM))
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
print("---------------------")
|
||||
print(cmd.parseResult(package))
|
||||
return cmd.parseResult(package)
|
||||
|
||||
'''激活gamma文件-12bit版本'''
|
||||
def send_gamma_active_12bit(self, ini_file: str):
|
||||
ini_fp = open(ini_file, 'rb')
|
||||
ind_data = ini_fp.read()
|
||||
ini_fp.close()
|
||||
|
||||
# 计算crc
|
||||
crc = baseSerail.crc16(ind_data, ind_data.__len__())
|
||||
CRC_GM = [crc >> 8, crc & 0xFF]
|
||||
|
||||
cmd = TvParse()
|
||||
package = cmd.parseCommandFc(0xAA, [0xFC,0x07], [0x0E], bytearray(CRC_GM))
|
||||
if self.write(package):
|
||||
package = self.read()
|
||||
print("---------------------")
|
||||
print(cmd.parseResult(package))
|
||||
return cmd.parseResult(package)
|
||||
|
||||
|
||||
'''进工厂模式'''
|
||||
def enterFactory(self):
|
||||
return self.sendcmdEx(0xAA, [0x10], [0x01])
|
||||
|
||||
'''白平衡初始化'''
|
||||
def initWhiteBalance(self):
|
||||
return self.sendcmdEx(0xAA, [0x16], [0x01])
|
||||
|
||||
'''关闭localdimming'''
|
||||
def closeLocaldimming(self):
|
||||
return self.sendcmdEx(0xAA, [0x9F,0x07], [0x00])
|
||||
|
||||
'''打开内置pattern'''
|
||||
def openBuiltInPattern(self):
|
||||
return self.sendcmdEx(0xAA, [0x27], [0x01])
|
||||
|
||||
'''关闭内置pattern'''
|
||||
def closeBuiltInPattern(self):
|
||||
return self.sendcmdEx(0xAA, [0x27], [0x00])
|
||||
|
||||
'''切换标准色温'''
|
||||
def switchStdColorTemperature(self):
|
||||
return self.sendcmdEx(0xAA, [0x31], [0x01])
|
||||
|
||||
'''切换冷色温'''
|
||||
def switchColdColorTemperature(self):
|
||||
return self.sendcmdEx(0xAA, [0x31], [0x02])
|
||||
|
||||
'''切换暖色温'''
|
||||
def switchWarmColorTemperature(self):
|
||||
return self.sendcmdEx(0xAA, [0x31], [0x03])
|
||||
|
||||
'''切换暖2色温'''
|
||||
def switchWarm2ColorTemperature(self):
|
||||
return self.sendcmdEx(0xAA, [0x31], [0x04])
|
||||
|
||||
'''初始化gamma'''
|
||||
def initGamma(self):
|
||||
return self.sendcmdEx(0xAA, [0x9F,0x09], [0x01])
|
||||
|
||||
'''进老化模式'''
|
||||
def enterAgingMode(self):
|
||||
return self.sendcmdEx(0xAA, [0x13], [0x01])
|
||||
|
||||
'''退出老化模式'''
|
||||
def exitAgingMode(self):
|
||||
return self.sendcmdEx(0xAA, [0x13], [0x00])
|
||||
|
||||
'''软件版本查询'''
|
||||
def sendSoftwareVersionQuery(self):
|
||||
return self.sendcmdEx_string_return(0xAA, [0x57], [0x00])
|
||||
|
||||
'''PID查询'''
|
||||
def sendPIDQuery(self):
|
||||
return self.sendcmdEx_string_return(0xAA, [0x84], [0x00])
|
||||
|
||||
def switchHDMI1(self):
|
||||
"""切换到HDMI1信源"""
|
||||
return self.sendcmdEx(0xAA, [0x25], [0x01])
|
||||
|
||||
def switchHDMI2(self):
|
||||
"""切换到HDMI2信源"""
|
||||
return self.sendcmdEx(0xAA, [0x25], [0x02])
|
||||
|
||||
def switchHDMI3(self):
|
||||
"""切换到HDMI3信源"""
|
||||
return self.sendcmdEx(0xAA, [0x25], [0x03])
|
||||
|
||||
def switchVGA(self):
|
||||
"""切换到VGA信源"""
|
||||
return self.sendcmdEx(0xAA, [0x24], [0x01])
|
||||
|
||||
def switchAV1(self):
|
||||
"""切换到AV1信源"""
|
||||
return self.sendcmdEx(0xAA, [0x22], [0x01])
|
||||
|
||||
def switchAV2(self):
|
||||
"""切换到AV2信源"""
|
||||
return self.sendcmdEx(0xAA, [0x22], [0x02])
|
||||
|
||||
def switchAV3(self):
|
||||
"""切换到AV3信源"""
|
||||
return self.sendcmdEx(0xAA, [0x22], [0x03])
|
||||
|
||||
def switchDisplayMode(self, mode="natural"):
|
||||
"""切换图像预设模式 """
|
||||
if mode == "natural":
|
||||
return self.sendcmdEx(0xAA, [0x30], [0x01])
|
||||
elif mode == "soft":
|
||||
return self.sendcmdEx(0xAA, [0x30], [0x02])
|
||||
elif mode == "bright":
|
||||
return self.sendcmdEx(0xAA, [0x30], [0x03])
|
||||
elif mode == "personal":
|
||||
return self.sendcmdEx(0xAA, [0x30], [0x04])
|
||||
elif mode == "cinematic":
|
||||
return self.sendcmdEx(0xAA, [0x30], [0x05])
|
||||
else:
|
||||
raise ValueError("Invalid display mode. Use 'natural', 'soft', 'bright', 'personal', or 'cinematic'.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
pass
|
||||
80
drivers/ucd_helpers.py
Normal file
80
drivers/ucd_helpers.py
Normal file
@@ -0,0 +1,80 @@
|
||||
"""通用 UCD323/UCDController 辅助函数。
|
||||
|
||||
封装"按当前接口取 tx 模块"、"读取分辨率"、"发送图片 Pattern"等所有
|
||||
测试模块共用的低层 UCD 操作,避免在多个业务模块中重复 if/else。
|
||||
"""
|
||||
|
||||
import UniTAP
|
||||
|
||||
|
||||
def get_tx_modules(ucd):
|
||||
"""根据当前接口返回 (pg, ag) 模块。
|
||||
|
||||
兼容 UCD323Controller(多接口,含 current_interface 属性)
|
||||
与老的 UCDController(仅 HDMI)。
|
||||
"""
|
||||
interface = getattr(ucd, "current_interface", None)
|
||||
if interface in (None, "HDMI"):
|
||||
return ucd.role.hdtx.pg, ucd.role.hdtx.ag
|
||||
if interface in ("DP", "Type-C"):
|
||||
return ucd.role.dptx.pg, ucd.role.dptx.ag
|
||||
raise ValueError(f"不支持的接口类型: {interface}")
|
||||
|
||||
|
||||
def get_current_resolution(ucd, default=(3840, 2160)):
|
||||
"""从 UCD 当前 timing 获取 (width, height),失败时返回 default。"""
|
||||
try:
|
||||
pg, _ = get_tx_modules(ucd)
|
||||
vm = pg.get_vm()
|
||||
timing = getattr(vm, "timing", None)
|
||||
if timing and hasattr(timing, "h_active") and hasattr(timing, "v_active"):
|
||||
return timing.h_active, timing.v_active
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
timing = getattr(ucd, "current_timing", None)
|
||||
if timing and hasattr(timing, "h_active") and hasattr(timing, "v_active"):
|
||||
return timing.h_active, timing.v_active
|
||||
|
||||
return default
|
||||
|
||||
|
||||
def send_image_pattern(ucd, image_path, *, bpc=8, color_format=None, colorimetry=None):
|
||||
"""通过 UCD 发送一张本地图片作为显示 Pattern。
|
||||
|
||||
自动停止音频以避免蜂鸣声。颜色参数留空时使用 RGB / sRGB 默认值。
|
||||
返回 True/False。
|
||||
"""
|
||||
if not getattr(ucd, "status", False):
|
||||
return False
|
||||
|
||||
try:
|
||||
pg, ag = get_tx_modules(ucd)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
try:
|
||||
ag.stop_generate()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
color_mode = UniTAP.ColorInfo()
|
||||
color_mode.color_format = color_format or UniTAP.ColorInfo.ColorFormat.CF_RGB
|
||||
color_mode.bpc = bpc
|
||||
color_mode.colorimetry = colorimetry or UniTAP.ColorInfo.Colorimetry.CM_sRGB
|
||||
|
||||
timing = None
|
||||
try:
|
||||
vm = pg.get_vm()
|
||||
timing = getattr(vm, "timing", None)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
if timing:
|
||||
pg.set_vm(vm=UniTAP.VideoMode(timing=timing, color_info=color_mode))
|
||||
pg.set_pattern(pattern=image_path)
|
||||
pg.apply()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
Reference in New Issue
Block a user