ch9329.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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] = 0x01 // Baudrate byte 2
  17. currentConfig[5] = 0xC2 // 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.Println()
  35. fmt.Print("Reply: ")
  36. for _, b := range resp {
  37. fmt.Printf("0x%02X ", b)
  38. }
  39. fmt.Println()
  40. fmt.Println("Baudrate updated to 115200 successfully")
  41. return nil
  42. }
  43. func (c *Controller) WriteChipProperties() ([]byte, error) {
  44. manufacturerString := []byte{
  45. 0x57, 0xAB, 0x00, 0x0B,
  46. 0x09, // Length of payload
  47. 0x00, // Set manufacturer string
  48. 0x07, // Length of the USB manufacturer string
  49. 'i', 'm', 'u', 's', 'l', 'a', 'b',
  50. 0x00, // Checksum placeholder
  51. }
  52. manufacturerString[14] = calcChecksum(manufacturerString[:14])
  53. // Send set manufacturer string
  54. err := c.Send(manufacturerString)
  55. if err != nil {
  56. return nil, fmt.Errorf("failed to send manufacturer string: %v", err)
  57. }
  58. _, err = c.WaitForReply(0x0B)
  59. if err != nil {
  60. return nil, fmt.Errorf("failed to get manufacturer string response: %v", err)
  61. }
  62. productString := []byte{
  63. 0x57, 0xAB, 0x00, 0x0B,
  64. 0x0B, // Length of the payload
  65. 0x01, // Set product string
  66. 0x09, // Length of the USB product string
  67. 'R', 'e', 'm', 'd', 'e', 's', 'K', 'V', 'M',
  68. 0x00, // Checksum placeholder
  69. }
  70. productString[16] = calcChecksum(productString[:16])
  71. // Send set product string
  72. err = c.Send(productString)
  73. if err != nil {
  74. return nil, fmt.Errorf("failed to send product string: %v", err)
  75. }
  76. _, err = c.WaitForReply(0x0B)
  77. if err != nil {
  78. return nil, fmt.Errorf("failed to get product string response: %v", err)
  79. }
  80. return []byte("OK"), nil
  81. }
  82. // GetChipCurrentConfiguration retrieves the current configuration of the chip.
  83. // It sends a command to the chip and waits for a reply.
  84. // Note only the data portion of the response is returned, excluding the header and checksum.
  85. func (c *Controller) GetChipCurrentConfiguration() ([]byte, error) {
  86. //Send the command to get chip configuration and info
  87. cmd := []byte{0x57, 0xAB,
  88. 0x00, 0x08, 0x00,
  89. 0x00, //placeholder for checksum
  90. }
  91. cmd[5] = calcChecksum(cmd[:5])
  92. err := c.Send(cmd)
  93. if err != nil {
  94. fmt.Printf("Error sending command: %v\n", err)
  95. return nil, errors.New("failed to send command")
  96. }
  97. resp, err := c.WaitForReply(0x08)
  98. if err != nil {
  99. fmt.Printf("Error waiting for reply: %v\n", err)
  100. return nil, errors.New("failed to get reply")
  101. }
  102. if len(resp) < 50 {
  103. fmt.Println("Invalid response length")
  104. return nil, errors.New("invalid response length")
  105. }
  106. fmt.Print("Response: ")
  107. for _, b := range resp {
  108. fmt.Printf("0x%02X ", b)
  109. }
  110. fmt.Println()
  111. return resp, nil
  112. }
  113. func (c *Controller) IsModifierKeys(keycode int) bool {
  114. // Modifier keycodes for JavaScript
  115. modifierKeys := []int{16, 17, 18, 91} // Shift, Ctrl, Alt, Meta (Windows/Command key)
  116. for _, key := range modifierKeys {
  117. if keycode == key {
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. // ConstructAndSendCmd constructs a HID command based on the provided HIDCommand and sends it.
  124. func (c *Controller) ConstructAndSendCmd(HIDCommand *HIDCommand) ([]byte, error) {
  125. switch HIDCommand.Event {
  126. case EventTypeKeyPress:
  127. if IsModifierKey(uint8(HIDCommand.Keycode)) {
  128. //modifier keys
  129. return c.SetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
  130. } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
  131. // Numpad enter
  132. return c.SendKeyboardPress(uint8(146))
  133. }
  134. return c.SendKeyboardPress(uint8(HIDCommand.Keycode))
  135. case EventTypeKeyRelease:
  136. if IsModifierKey(uint8(HIDCommand.Keycode)) {
  137. //modifier keys
  138. return c.UnsetModifierKey(uint8(HIDCommand.Keycode), HIDCommand.IsRightModKey)
  139. } else if HIDCommand.Keycode == 13 && HIDCommand.IsRightModKey {
  140. // Numpad enter
  141. return c.SendKeyboardRelease(uint8(146))
  142. }
  143. return c.SendKeyboardRelease(uint8(HIDCommand.Keycode))
  144. case EventTypeMouseMove:
  145. //Map mouse button state to HID state
  146. leftPressed := (HIDCommand.MouseMoveButtonState & 0x01) != 0
  147. middlePressed := (HIDCommand.MouseMoveButtonState & 0x02) != 0
  148. rightPressed := (HIDCommand.MouseMoveButtonState & 0x04) != 0
  149. if leftPressed {
  150. c.hidState.MouseButtons |= 0x01
  151. } else {
  152. c.hidState.MouseButtons &^= 0x01
  153. }
  154. if middlePressed {
  155. c.hidState.MouseButtons |= 0x04
  156. } else {
  157. c.hidState.MouseButtons &^= 0x04
  158. }
  159. if rightPressed {
  160. c.hidState.MouseButtons |= 0x02
  161. } else {
  162. c.hidState.MouseButtons &^= 0x02
  163. }
  164. // Update mouse position
  165. c.lastCursorEventTime = time.Now().UnixMilli()
  166. if HIDCommand.MouseAbsX != 0 || HIDCommand.MouseAbsY != 0 {
  167. xLSB := byte(HIDCommand.MouseAbsX & 0xFF) // Extract LSB of X
  168. xMSB := byte((HIDCommand.MouseAbsX >> 8) & 0xFF) // Extract MSB of X
  169. yLSB := byte(HIDCommand.MouseAbsY & 0xFF) // Extract LSB of Y
  170. yMSB := byte((HIDCommand.MouseAbsY >> 8) & 0xFF) // Extract MSB of Y
  171. return c.MouseMoveAbsolute(xLSB, xMSB, yLSB, yMSB)
  172. } else if HIDCommand.MouseRelX != 0 || HIDCommand.MouseRelY != 0 {
  173. //Todo
  174. }
  175. return []byte{}, nil
  176. case EventTypeMousePress:
  177. if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
  178. return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
  179. }
  180. button := uint8(HIDCommand.MouseButton)
  181. return c.MouseButtonPress(button)
  182. case EventTypeMouseRelease:
  183. if HIDCommand.MouseButton < 1 || HIDCommand.MouseButton > 3 {
  184. return nil, fmt.Errorf("invalid mouse button: %d", HIDCommand.MouseButton)
  185. }
  186. button := uint8(HIDCommand.MouseButton)
  187. return c.MouseButtonRelease(button)
  188. case EventTypeMouseScroll:
  189. if time.Now().UnixMilli()-c.lastCursorEventTime < MinCusorEventInterval {
  190. // Ignore mouse move events that are too close together
  191. return []byte{}, nil
  192. }
  193. c.lastCursorEventTime = time.Now().UnixMilli()
  194. return c.MouseScroll(HIDCommand.MouseScroll)
  195. default:
  196. return nil, fmt.Errorf("unsupported HID command event type: %d", HIDCommand.Event)
  197. }
  198. }