ch9329.go 6.6 KB

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