ch9329.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package remdeshid
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. )
  7. func (c *Controller) ConfigureChipTo115200() error {
  8. // Send the command to get chip configuration and info
  9. currentConfig, err := c.GetChipCurrentConfiguration()
  10. if err != nil {
  11. fmt.Printf("Error getting current configuration: %v\n", err)
  12. return errors.New("failed to get current configuration")
  13. }
  14. // Modify baudrate bytes in the response
  15. currentConfig[3] = 0x00 // Baudrate byte 1
  16. currentConfig[4] = 0x00 // Baudrate byte 2
  17. currentConfig[5] = 0x4B // Baudrate byte 3
  18. currentConfig[6] = 0x00 // Baudrate byte 4
  19. time.Sleep(1 * time.Second) // Wait for a second before sending the command
  20. // Prepare the command to set the new configuration
  21. setCmd := append([]byte{0x57, 0xAB, 0x00, 0x09, 0x32}, currentConfig[:50]...)
  22. setCmd = append(setCmd, calcChecksum(setCmd[:len(setCmd)-1]))
  23. err = c.Send(setCmd)
  24. if err != nil {
  25. fmt.Printf("Error sending configuration command: %v\n", err)
  26. return errors.New("failed to send configuration command")
  27. }
  28. // Wait for the reply
  29. resp, err := c.WaitForReply(0x09)
  30. if err != nil {
  31. fmt.Printf("Error waiting for reply: %v\n", err)
  32. return errors.New("failed to get reply")
  33. }
  34. fmt.Print("Reply: ")
  35. for _, b := range resp {
  36. fmt.Printf("0x%02X ", b)
  37. }
  38. fmt.Println()
  39. fmt.Println("Baudrate updated to 115200 successfully")
  40. return nil
  41. }
  42. // GetChipCurrentConfiguration retrieves the current configuration of the chip.
  43. // It sends a command to the chip and waits for a reply.
  44. // Note only the data portion of the response is returned, excluding the header and checksum.
  45. func (c *Controller) GetChipCurrentConfiguration() ([]byte, error) {
  46. //Send the command to get chip configuration and info
  47. cmd := []byte{0x57, 0xAB,
  48. 0x00, 0x08, 0x00,
  49. 0x00, //placeholder for checksum
  50. }
  51. cmd[5] = calcChecksum(cmd[:5])
  52. err := c.Send(cmd)
  53. if err != nil {
  54. fmt.Printf("Error sending command: %v\n", err)
  55. return nil, errors.New("failed to send command")
  56. }
  57. resp, err := c.WaitForReply(0x08)
  58. if err != nil {
  59. fmt.Printf("Error waiting for reply: %v\n", err)
  60. return nil, errors.New("failed to get reply")
  61. }
  62. if len(resp) < 50 {
  63. fmt.Println("Invalid response length")
  64. return nil, errors.New("invalid response length")
  65. }
  66. fmt.Print("Response: ")
  67. for _, b := range resp {
  68. fmt.Printf("0x%02X ", b)
  69. }
  70. fmt.Println()
  71. return resp, nil
  72. }
  73. func (c *Controller) IsModifierKeys(keycode int) bool {
  74. // Modifier keycodes for JavaScript
  75. modifierKeys := []int{16, 17, 18, 91} // Shift, Ctrl, Alt, Meta (Windows/Command key)
  76. for _, key := range modifierKeys {
  77. if keycode == key {
  78. return true
  79. }
  80. }
  81. return false
  82. }
  83. // ConstructAndSendCmd constructs a HID command based on the provided HIDCommand and sends it.
  84. func (c *Controller) ConstructAndSendCmd(HIDCommand *HIDCommand) ([]byte, error) {
  85. switch HIDCommand.Event {
  86. case EventTypeKeyPress:
  87. if IsModifierKey(uint8(HIDCommand.Keycode)) {
  88. //modifier keys
  89. return c.SetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
  90. } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
  91. // Numpad enter
  92. return c.SendKeyboardPress(uint8(146))
  93. }
  94. return c.SendKeyboardPress(uint8(HIDCommand.Keycode))
  95. case EventTypeKeyRelease:
  96. if IsModifierKey(uint8(HIDCommand.Keycode)) {
  97. //modifier keys
  98. return c.UnsetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
  99. } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
  100. // Numpad enter
  101. return c.SendKeyboardRelease(uint8(146))
  102. }
  103. return c.SendKeyboardRelease(uint8(HIDCommand.Keycode))
  104. case EventTypeMouseMove:
  105. if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
  106. // Ignore mouse move events that are too close together
  107. return []byte{}, nil
  108. }
  109. c.lastCursorEventTime = time.Now().UnixMilli()
  110. if HIDCommand.MouseAbsX != 0 || HIDCommand.MouseAbsY != 0 {
  111. xLSB := byte(HIDCommand.MouseAbsX & 0xFF) // Extract LSB of X
  112. xMSB := byte((HIDCommand.MouseAbsX >> 8) & 0xFF) // Extract MSB of X
  113. yLSB := byte(HIDCommand.MouseAbsY & 0xFF) // Extract LSB of Y
  114. yMSB := byte((HIDCommand.MouseAbsY >> 8) & 0xFF) // Extract MSB of Y
  115. return c.MouseMoveAbsolute(xLSB, xMSB, yLSB, yMSB)
  116. } else if HIDCommand.MouseRelX != 0 || HIDCommand.MouseRelY != 0 {
  117. //Todo
  118. }
  119. return []byte{}, nil
  120. case EventTypeMousePress:
  121. if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
  122. return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
  123. }
  124. button := uint8(HIDCommand.MouseButton)
  125. return c.MouseButtonPress(button)
  126. case EventTypeMouseRelease:
  127. if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
  128. return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
  129. }
  130. button := uint8(HIDCommand.MouseButton)
  131. return c.MouseButtonRelease(button)
  132. case EventTypeMouseScroll:
  133. if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
  134. // Ignore mouse move events that are too close together
  135. return []byte{}, nil
  136. }
  137. c.lastCursorEventTime = time.Now().UnixMilli()
  138. return c.MouseScroll(HIDCommand.MouseScroll)
  139. default:
  140. return nil, fmt.Errorf("unsupported HID command event type: %d", HIDCommand.Event)
  141. }
  142. }