ptz.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package gb28181
  2. import "fmt"
  3. var (
  4. name2code = map[string]uint8{
  5. "stop": 0,
  6. "right": 1,
  7. "left": 2,
  8. "down": 4,
  9. "downright": 5,
  10. "downleft": 6,
  11. "up": 8,
  12. "upright": 9,
  13. "upleft": 10,
  14. "zoomin": 16,
  15. "zoomout": 32,
  16. }
  17. )
  18. func toPtzStrByCmdName(cmdName string, horizontalSpeed, verticalSpeed, zoomSpeed uint8) (string, error) {
  19. c, err := toPtzCode(cmdName)
  20. if err != nil {
  21. return "", err
  22. }
  23. return toPtzStr(c, horizontalSpeed, verticalSpeed, zoomSpeed), nil
  24. }
  25. func toPtzStr(cmdCode, horizontalSpeed, verticalSpeed, zoomSpeed uint8) string {
  26. checkCode := uint16(0xA5+0x0F+0x01+cmdCode+horizontalSpeed+verticalSpeed+(zoomSpeed&0xF0)) % 0x100
  27. return fmt.Sprintf("A50F01%02X%02X%02X%01X0%02X",
  28. cmdCode,
  29. horizontalSpeed,
  30. verticalSpeed,
  31. zoomSpeed>>4, // 根据 GB28181 协议,zoom 只取 4 bit
  32. checkCode,
  33. )
  34. }
  35. func toPtzCode(cmd string) (uint8, error) {
  36. if code, ok := name2code[cmd]; ok {
  37. return code, nil
  38. } else {
  39. return 0, fmt.Errorf("invalid ptz cmd %q", cmd)
  40. }
  41. }