remdeshid.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package remdeshid
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "github.com/tarm/serial"
  7. )
  8. type Config struct {
  9. /* Bindings and callback */
  10. OnWriteError func(error) // Callback for when an error occurs while writing to USBKVM device
  11. OnReadError func(error) // Callback for when an error occurs while reading from USBKVM device
  12. OnDataHandler func([]byte, error) // Callback for when data is received from USBKVM device
  13. /* Serial port configs */
  14. PortName string
  15. BaudRate int
  16. }
  17. // Controller is a struct that represents a HID controller
  18. type Controller struct {
  19. Config *Config
  20. serialPort *serial.Port
  21. serialRunning bool
  22. stopChan chan bool
  23. }
  24. func NewHIDController(config *Config) *Controller {
  25. return &Controller{
  26. Config: config,
  27. serialRunning: false,
  28. }
  29. }
  30. // Connect opens the serial port and starts reading from it
  31. func (c *Controller) Connect() error {
  32. // Open the serial port
  33. config := &serial.Config{
  34. Name: c.Config.PortName,
  35. Baud: c.Config.BaudRate,
  36. Size: 8,
  37. Parity: serial.ParityNone,
  38. }
  39. port, err := serial.OpenPort(config)
  40. if err != nil {
  41. return err
  42. }
  43. c.serialPort = port
  44. //Start reading from the serial port
  45. go func() {
  46. buf := make([]byte, 128)
  47. for {
  48. n, err := port.Read(buf)
  49. if err != nil {
  50. if c.Config.OnReadError != nil {
  51. c.Config.OnReadError(err)
  52. } else {
  53. log.Println(err.Error())
  54. }
  55. return
  56. }
  57. if n > 0 {
  58. if c.Config.OnDataHandler != nil {
  59. c.Config.OnDataHandler(buf[:n], nil)
  60. } else {
  61. fmt.Print("Received bytes: ")
  62. for i := 0; i < n; i++ {
  63. fmt.Printf("0x%02X ", buf[i])
  64. }
  65. fmt.Println()
  66. }
  67. }
  68. }
  69. }()
  70. //Send over an opr queue reset signal
  71. err = c.Send([]byte{OPR_TYPE_DATA_RESET})
  72. if err != nil {
  73. return err
  74. }
  75. //Reset keyboard press state
  76. err = c.Send([]byte{OPR_TYPE_KEYBOARD_WRITE, SUBTYPE_KEYBOARD_SPECIAL_RESET, 0x00})
  77. if err != nil {
  78. return err
  79. }
  80. //Reset mouse press state
  81. err = c.Send([]byte{OPR_TYPE_MOUSE_WRITE, SUBTYPE_MOUSE_RESET, 0x00})
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func (c *Controller) Send(data []byte) error {
  88. n, err := c.serialPort.Write(data)
  89. if err != nil {
  90. return err
  91. }
  92. if n != len(data) {
  93. return fmt.Errorf("Failed to write all bytes to serial port")
  94. }
  95. time.Sleep(10 * time.Millisecond) //Minimum delay between writes
  96. return nil
  97. }
  98. func (c *Controller) Close() {
  99. if c.stopChan != nil {
  100. c.stopChan <- true
  101. }
  102. c.serialPort.Close()
  103. }