BxCmdBrightness.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package bx
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. )
  6. // CmdBrightness 对应PDF中8.17设置亮度命令
  7. type CmdBrightness struct {
  8. baseBxCmd
  9. BrightnessType byte // 亮度调节方式:0x01强制调节,0x02定时调节
  10. CurrentBrightness byte // 强制调节时的亮度值(0-15,15为最高)
  11. BrightnessValue []byte // 定时调节时的48字节亮度列表(每30分钟一个时段)
  12. }
  13. // NewCmdBrightness 初始化亮度设置命令
  14. // brightnessType: 0x01强制调节,0x02定时调节
  15. // currentBrightness: 强制调节时的亮度值(0-15)
  16. // brightnessValue: 定时调节时的48字节亮度列表(不足补0,超出截取前48字节)
  17. func NewCmdBrightness(brightnessType byte, currentBrightness byte, brightnessValue []byte) CmdBrightness {
  18. // 初始化基础命令(使用BxCmdCode中定义的亮度命令组和编号)
  19. baseCmd := newBaseCmd(CMD_SCREEN_BRIGHTNESS.group, CMD_SCREEN_BRIGHTNESS.code)
  20. // 处理定时调节的亮度列表长度(确保为48字节)
  21. processedValue := processBrightnessValue(brightnessValue)
  22. // 处理亮度值边界(0-15)
  23. processedBrightness := currentBrightness
  24. if processedBrightness > 15 {
  25. processedBrightness = 15
  26. }
  27. return CmdBrightness{
  28. baseBxCmd: baseCmd,
  29. BrightnessType: brightnessType,
  30. CurrentBrightness: processedBrightness,
  31. BrightnessValue: processedValue,
  32. }
  33. }
  34. // SetBrightnessType 设置亮度调节方式(0x01强制,0x02定时)
  35. func (cmd *CmdBrightness) SetBrightnessType(brightnessType byte) {
  36. cmd.BrightnessType = brightnessType
  37. // 切换为定时调节时自动初始化48字节亮度列表
  38. if brightnessType == 0x02 && len(cmd.BrightnessValue) != 48 {
  39. cmd.BrightnessValue = make([]byte, 48)
  40. }
  41. }
  42. // SetCurrentBrightness 设置强制调节亮度值(0-15)
  43. func (cmd *CmdBrightness) SetCurrentBrightness(currentBrightness byte) {
  44. if currentBrightness > 15 {
  45. currentBrightness = 15
  46. }
  47. cmd.CurrentBrightness = currentBrightness
  48. }
  49. // SetBrightnessValue 设置定时调节的48字节亮度列表
  50. func (cmd *CmdBrightness) SetBrightnessValue(brightnessValue []byte) {
  51. cmd.BrightnessValue = processBrightnessValue(brightnessValue)
  52. }
  53. // Build 构建符合PDF协议格式的命令字节流
  54. func (cmd *CmdBrightness) Build() []byte {
  55. w := bytes.NewBuffer(make([]byte, 0, 64)) // 预分配足够容量
  56. // 按PDF协议顺序写入字段:CmdGroup -> Cmd -> Response -> Reserved -> 亮度参数
  57. binary.Write(w, binary.LittleEndian, cmd.Group()) // 命令分组(0xA3)
  58. binary.Write(w, binary.LittleEndian, cmd.Cmd()) // 命令编号(0x02)
  59. binary.Write(w, binary.LittleEndian, byte(0x01)) // Response:要求控制器回复(默认0x01)
  60. binary.Write(w, binary.LittleEndian, []byte{0x00, 0x00}) // Reserved:2字节保留值
  61. binary.Write(w, binary.LittleEndian, cmd.BrightnessType) // 亮度调节方式
  62. // 根据调节方式写入后续参数
  63. switch cmd.BrightnessType {
  64. case 0x01:
  65. // 强制调节:写入亮度值(0-15)
  66. binary.Write(w, binary.LittleEndian, cmd.CurrentBrightness)
  67. case 0x02:
  68. binary.Write(w, binary.LittleEndian, byte(0x00)) // currentBrightness默认0
  69. // 定时调节:写入48字节亮度列表
  70. binary.Write(w, binary.LittleEndian, cmd.BrightnessValue)
  71. default:
  72. // 无效类型默认按强制调节处理(亮度0)
  73. binary.Write(w, binary.LittleEndian, byte(0x00))
  74. }
  75. return w.Bytes()
  76. }
  77. // processBrightnessValue 处理定时调节的亮度列表(确保长度为48字节)
  78. func processBrightnessValue(value []byte) []byte {
  79. result := make([]byte, 48)
  80. if len(value) > 48 {
  81. copy(result, value[:48]) // 超出部分截取
  82. } else {
  83. copy(result, value) // 不足部分补0
  84. }
  85. // 确保每个亮度值在0-15范围内
  86. for i := range result {
  87. if result[i] > 15 {
  88. result[i] = 15
  89. }
  90. }
  91. return result
  92. }