| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- from .BxCmd import baseBxCmd, newBaseCmd
- from .BxCmdCode import CMD_SCREEN_BRIGHTNESS
- class CmdBrightness(baseBxCmd):
- def __init__(self, brightnessType, currentBrightness, brightnessValue):
- # 初始化基础命令
- super().__init__(CMD_SCREEN_BRIGHTNESS.group, CMD_SCREEN_BRIGHTNESS.code)
- self.BrightnessType = brightnessType
-
- # 处理亮度值边界(0-15)
- self.CurrentBrightness = currentBrightness
- if self.CurrentBrightness > 15:
- self.CurrentBrightness = 15
-
- # 处理定时调节的亮度列表长度(确保为48字节)
- self.BrightnessValue = processBrightnessValue(brightnessValue)
- def SetBrightnessType(self, brightnessType):
- self.BrightnessType = brightnessType
- # 切换为定时调节时自动初始化48字节亮度列表
- if brightnessType == 0x02 and len(self.BrightnessValue) != 48:
- self.BrightnessValue = bytearray(48)
- def SetCurrentBrightness(self, currentBrightness):
- if currentBrightness > 15:
- currentBrightness = 15
- self.CurrentBrightness = currentBrightness
- def SetBrightnessValue(self, brightnessValue):
- self.BrightnessValue = processBrightnessValue(brightnessValue)
- def Build(self):
- result = bytearray()
-
- # 写入命令组
- result.append(self.Group())
- # 写入命令
- result.append(self.Cmd())
- # 写入响应标志
- result.append(0x01)
- # 写入保留值
- result.extend([0x00, 0x00])
- # 写入亮度调节方式
- result.append(self.BrightnessType)
-
- # 根据调节方式写入后续参数
- if self.BrightnessType == 0x01:
- # 强制调节:写入亮度值(0-15)
- result.append(self.CurrentBrightness)
- elif self.BrightnessType == 0x02:
- # 定时调节:写入默认亮度值0和48字节亮度列表
- result.append(0x00)
- result.extend(self.BrightnessValue)
- else:
- # 无效类型默认按强制调节处理(亮度0)
- result.append(0x00)
-
- return bytes(result)
- # 处理定时调节的亮度列表(确保长度为48字节)
- def processBrightnessValue(value):
- result = bytearray(48)
- if value:
- if len(value) > 48:
- result[:48] = value[:48] # 超出部分截取
- else:
- result[:len(value)] = value # 不足部分补0
-
- # 确保每个亮度值在0-15范围内
- for i in range(len(result)):
- if result[i] > 15:
- result[i] = 15
-
- return result
- # 初始化亮度设置命令
- def NewCmdBrightness(brightnessType, currentBrightness, brightnessValue):
- return CmdBrightness(brightnessType, currentBrightness, brightnessValue)
|