remdeshid.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package remdeshid
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/tarm/serial"
  6. )
  7. type Config struct {
  8. /* Bindings and callback */
  9. OnWriteError func(error) // Callback for when an error occurs while writing to USBKVM device
  10. OnReadError func(error) // Callback for when an error occurs while reading from USBKVM device
  11. OnDataHandler func([]byte, error) // Callback for when data is received from USBKVM device
  12. /* Serial port configs */
  13. PortName string
  14. BaudRate int
  15. }
  16. // Controller is a struct that represents a HID controller
  17. type Controller struct {
  18. Config *Config
  19. serialPort *serial.Port
  20. serialRunning bool
  21. writeQueue chan []byte
  22. lastScrollTime int64
  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. //Create a loop to write to the serial port
  71. c.writeQueue = make(chan []byte, 1)
  72. c.serialRunning = true
  73. go func() {
  74. for {
  75. data := <-c.writeQueue
  76. _, err := port.Write(data)
  77. if err != nil {
  78. if c.Config.OnWriteError != nil {
  79. c.Config.OnWriteError(err)
  80. } else {
  81. log.Println(err.Error())
  82. }
  83. return
  84. }
  85. }
  86. }()
  87. //Send over an opr queue reset signal
  88. err = c.Send([]byte{OPR_TYPE_DATA_RESET})
  89. if err != nil {
  90. return err
  91. }
  92. //Reset keyboard press state
  93. err = c.Send([]byte{OPR_TYPE_KEYBOARD_WRITE, SUBTYPE_KEYBOARD_SPECIAL_RESET, 0x00})
  94. if err != nil {
  95. return err
  96. }
  97. //Reset mouse press state
  98. err = c.Send([]byte{OPR_TYPE_MOUSE_WRITE, SUBTYPE_MOUSE_RESET, 0x00})
  99. if err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. func (c *Controller) Send(data []byte) error {
  105. if !c.serialRunning {
  106. return fmt.Errorf("serial port is not running")
  107. }
  108. c.writeQueue <- data
  109. return nil
  110. }
  111. func (c *Controller) Close() {
  112. if c.serialPort != nil {
  113. c.serialPort.Close()
  114. }
  115. }