| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package bx
- import (
- "bytes"
- "encoding/binary"
- )
- // CmdBrightness 对应PDF中8.17设置亮度命令
- type CmdBrightness struct {
- baseBxCmd
- BrightnessType byte // 亮度调节方式:0x01强制调节,0x02定时调节
- CurrentBrightness byte // 强制调节时的亮度值(0-15,15为最高)
- BrightnessValue []byte // 定时调节时的48字节亮度列表(每30分钟一个时段)
- }
- // NewCmdBrightness 初始化亮度设置命令
- // brightnessType: 0x01强制调节,0x02定时调节
- // currentBrightness: 强制调节时的亮度值(0-15)
- // brightnessValue: 定时调节时的48字节亮度列表(不足补0,超出截取前48字节)
- func NewCmdBrightness(brightnessType byte, currentBrightness byte, brightnessValue []byte) CmdBrightness {
- // 初始化基础命令(使用BxCmdCode中定义的亮度命令组和编号)
- baseCmd := newBaseCmd(CMD_SCREEN_BRIGHTNESS.group, CMD_SCREEN_BRIGHTNESS.code)
- // 处理定时调节的亮度列表长度(确保为48字节)
- processedValue := processBrightnessValue(brightnessValue)
- // 处理亮度值边界(0-15)
- processedBrightness := currentBrightness
- if processedBrightness > 15 {
- processedBrightness = 15
- }
- return CmdBrightness{
- baseBxCmd: baseCmd,
- BrightnessType: brightnessType,
- CurrentBrightness: processedBrightness,
- BrightnessValue: processedValue,
- }
- }
- // SetBrightnessType 设置亮度调节方式(0x01强制,0x02定时)
- func (cmd *CmdBrightness) SetBrightnessType(brightnessType byte) {
- cmd.BrightnessType = brightnessType
- // 切换为定时调节时自动初始化48字节亮度列表
- if brightnessType == 0x02 && len(cmd.BrightnessValue) != 48 {
- cmd.BrightnessValue = make([]byte, 48)
- }
- }
- // SetCurrentBrightness 设置强制调节亮度值(0-15)
- func (cmd *CmdBrightness) SetCurrentBrightness(currentBrightness byte) {
- if currentBrightness > 15 {
- currentBrightness = 15
- }
- cmd.CurrentBrightness = currentBrightness
- }
- // SetBrightnessValue 设置定时调节的48字节亮度列表
- func (cmd *CmdBrightness) SetBrightnessValue(brightnessValue []byte) {
- cmd.BrightnessValue = processBrightnessValue(brightnessValue)
- }
- // Build 构建符合PDF协议格式的命令字节流
- func (cmd *CmdBrightness) Build() []byte {
- w := bytes.NewBuffer(make([]byte, 0, 64)) // 预分配足够容量
- // 按PDF协议顺序写入字段:CmdGroup -> Cmd -> Response -> Reserved -> 亮度参数
- binary.Write(w, binary.LittleEndian, cmd.Group()) // 命令分组(0xA3)
- binary.Write(w, binary.LittleEndian, cmd.Cmd()) // 命令编号(0x02)
- binary.Write(w, binary.LittleEndian, byte(0x01)) // Response:要求控制器回复(默认0x01)
- binary.Write(w, binary.LittleEndian, []byte{0x00, 0x00}) // Reserved:2字节保留值
- binary.Write(w, binary.LittleEndian, cmd.BrightnessType) // 亮度调节方式
- // 根据调节方式写入后续参数
- switch cmd.BrightnessType {
- case 0x01:
- // 强制调节:写入亮度值(0-15)
- binary.Write(w, binary.LittleEndian, cmd.CurrentBrightness)
- case 0x02:
- binary.Write(w, binary.LittleEndian, byte(0x00)) // currentBrightness默认0
- // 定时调节:写入48字节亮度列表
- binary.Write(w, binary.LittleEndian, cmd.BrightnessValue)
- default:
- // 无效类型默认按强制调节处理(亮度0)
- binary.Write(w, binary.LittleEndian, byte(0x00))
- }
- return w.Bytes()
- }
- // processBrightnessValue 处理定时调节的亮度列表(确保长度为48字节)
- func processBrightnessValue(value []byte) []byte {
- result := make([]byte, 48)
- if len(value) > 48 {
- copy(result, value[:48]) // 超出部分截取
- } else {
- copy(result, value) // 不足部分补0
- }
- // 确保每个亮度值在0-15范围内
- for i := range result {
- if result[i] > 15 {
- result[i] = 15
- }
- }
- return result
- }
|