BxCmdBrightness.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from .BxCmd import baseBxCmd, newBaseCmd
  2. from .BxCmdCode import CMD_SCREEN_BRIGHTNESS
  3. class CmdBrightness(baseBxCmd):
  4. def __init__(self, brightnessType, currentBrightness, brightnessValue):
  5. # 初始化基础命令
  6. super().__init__(CMD_SCREEN_BRIGHTNESS.group, CMD_SCREEN_BRIGHTNESS.code)
  7. self.BrightnessType = brightnessType
  8. # 处理亮度值边界(0-15)
  9. self.CurrentBrightness = currentBrightness
  10. if self.CurrentBrightness > 15:
  11. self.CurrentBrightness = 15
  12. # 处理定时调节的亮度列表长度(确保为48字节)
  13. self.BrightnessValue = processBrightnessValue(brightnessValue)
  14. def SetBrightnessType(self, brightnessType):
  15. self.BrightnessType = brightnessType
  16. # 切换为定时调节时自动初始化48字节亮度列表
  17. if brightnessType == 0x02 and len(self.BrightnessValue) != 48:
  18. self.BrightnessValue = bytearray(48)
  19. def SetCurrentBrightness(self, currentBrightness):
  20. if currentBrightness > 15:
  21. currentBrightness = 15
  22. self.CurrentBrightness = currentBrightness
  23. def SetBrightnessValue(self, brightnessValue):
  24. self.BrightnessValue = processBrightnessValue(brightnessValue)
  25. def Build(self):
  26. result = bytearray()
  27. # 写入命令组
  28. result.append(self.Group())
  29. # 写入命令
  30. result.append(self.Cmd())
  31. # 写入响应标志
  32. result.append(0x01)
  33. # 写入保留值
  34. result.extend([0x00, 0x00])
  35. # 写入亮度调节方式
  36. result.append(self.BrightnessType)
  37. # 根据调节方式写入后续参数
  38. if self.BrightnessType == 0x01:
  39. # 强制调节:写入亮度值(0-15)
  40. result.append(self.CurrentBrightness)
  41. elif self.BrightnessType == 0x02:
  42. # 定时调节:写入默认亮度值0和48字节亮度列表
  43. result.append(0x00)
  44. result.extend(self.BrightnessValue)
  45. else:
  46. # 无效类型默认按强制调节处理(亮度0)
  47. result.append(0x00)
  48. return bytes(result)
  49. # 处理定时调节的亮度列表(确保长度为48字节)
  50. def processBrightnessValue(value):
  51. result = bytearray(48)
  52. if value:
  53. if len(value) > 48:
  54. result[:48] = value[:48] # 超出部分截取
  55. else:
  56. result[:len(value)] = value # 不足部分补0
  57. # 确保每个亮度值在0-15范围内
  58. for i in range(len(result)):
  59. if result[i] > 15:
  60. result[i] = 15
  61. return result
  62. # 初始化亮度设置命令
  63. def NewCmdBrightness(brightnessType, currentBrightness, brightnessValue):
  64. return CmdBrightness(brightnessType, currentBrightness, brightnessValue)