Browse Source

Added 19200 baudrate change command

TC 1 day ago
parent
commit
a1b8d0e046
3 changed files with 145 additions and 46 deletions
  1. 65 45
      remdeskd/main.go
  2. 79 0
      remdeskd/mod/remdeshid/ch9329.go
  3. 1 1
      remdeskd/mod/remdeshid/typedef.go

+ 65 - 45
remdeskd/main.go

@@ -9,14 +9,17 @@ import (
 	"os"
 	"os/signal"
 	"syscall"
+	"time"
 
 	"imuslab.com/remdeskvm/remdeskd/mod/remdeshid"
 	"imuslab.com/remdeskvm/remdeskd/mod/usbcapture"
 )
 
-const development = true
+const defaultDevMode = true
 
 var (
+	developent        = flag.Bool("dev", defaultDevMode, "Enable development mode with local static files")
+	mode              = flag.String("mode", "usbkvm", "Mode of operation: kvm or capture")
 	usbKVMDeviceName  = flag.String("usbkvm", "/dev/ttyACM0", "USB KVM device file path")
 	usbKVMBaudRate    = flag.Int("baudrate", 115200, "USB KVM baud rate")
 	captureDeviceName = flag.String("capture", "/dev/video0", "Video capture device file path")
@@ -31,7 +34,7 @@ var webfs http.FileSystem
 
 func init() {
 	// Initiate the web server static files
-	if development {
+	if *developent {
 		webfs = http.Dir("./www")
 	} else {
 		// Embed the ./www folder and trim the prefix
@@ -53,53 +56,70 @@ func main() {
 		ScrollSensitivity: 0x01, // Set mouse scroll sensitivity
 	})
 
-	//Start the HID controller
-	err := usbKVM.Connect()
-	if err != nil {
-		log.Fatal(err)
-	}
+	switch *mode {
+	case "cfgchip":
+		//Start the HID controller
+		err := usbKVM.Connect()
+		if err != nil {
+			log.Fatal(err)
+		}
 
-	// Initiate the video capture device
-	videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
-		DeviceName: *captureDeviceName,
-	})
+		time.Sleep(2 * time.Second) // Wait for the controller to initialize
+		//Configure the HID controller
+		usbKVM.ConfigureChipTo115200()
 
-	if err != nil {
-		log.Fatalf("Failed to create video capture instance: %v", err)
-	}
+	case "usbkvm":
+		log.Println("Starting in USB KVM mode...")
 
-	//Get device information for debug
-	usbcapture.PrintV4L2FormatInfo(*captureDeviceName)
+		//Start the HID controller
+		err := usbKVM.Connect()
+		if err != nil {
+			log.Fatal(err)
+		}
 
-	//Start the video capture device
-	err = videoCapture.StartVideoCapture(&usbcapture.CaptureResolution{
-		Width:  1920,
-		Height: 1080,
-		FPS:    10,
-	})
-	if err != nil {
-		log.Fatal(err)
-	}
+		// Initiate the video capture device
+		videoCapture, err = usbcapture.NewInstance(&usbcapture.Config{
+			DeviceName: *captureDeviceName,
+		})
 
-	// Handle program exit to close the HID controller
-
-	c := make(chan os.Signal, 1)
-	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
-	go func() {
-		<-c
-		log.Println("Shutting down usbKVM...")
-		//usbKVM.Close() //To fix close stuck layer
-		log.Println("Shutting down capture device...")
-		videoCapture.Close()
-		os.Exit(0)
-	}()
-
-	// Start the web server
-	http.Handle("/", http.FileServer(webfs))
-	http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
-	http.HandleFunc("/stream", videoCapture.ServeVideoStream)
-	addr := ":9000"
-	log.Printf("Serving on http://localhost%s\n", addr)
-	log.Fatal(http.ListenAndServe(addr, nil))
+		if err != nil {
+			log.Fatalf("Failed to create video capture instance: %v", err)
+		}
 
+		//Get device information for debug
+		usbcapture.PrintV4L2FormatInfo(*captureDeviceName)
+
+		//Start the video capture device
+		err = videoCapture.StartVideoCapture(&usbcapture.CaptureResolution{
+			Width:  1920,
+			Height: 1080,
+			FPS:    10,
+		})
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		// Handle program exit to close the HID controller
+
+		c := make(chan os.Signal, 1)
+		signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+		go func() {
+			<-c
+			log.Println("Shutting down usbKVM...")
+			//usbKVM.Close() //To fix close stuck layer
+			log.Println("Shutting down capture device...")
+			videoCapture.Close()
+			os.Exit(0)
+		}()
+
+		// Start the web server
+		http.Handle("/", http.FileServer(webfs))
+		http.HandleFunc("/hid", usbKVM.HIDWebSocketHandler)
+		http.HandleFunc("/stream", videoCapture.ServeVideoStream)
+		addr := ":9000"
+		log.Printf("Serving on http://localhost%s\n", addr)
+		log.Fatal(http.ListenAndServe(addr, nil))
+	default:
+		log.Fatalf("Unknown mode: %s. Supported modes are: usbkvm, capture", *mode)
+	}
 }

+ 79 - 0
remdeskd/mod/remdeshid/ch9329.go

@@ -1,10 +1,89 @@
 package remdeshid
 
 import (
+	"errors"
 	"fmt"
 	"time"
 )
 
+func (c *Controller) ConfigureChipTo115200() error {
+	// Send the command to get chip configuration and info
+	currentConfig, err := c.GetChipCurrentConfiguration()
+	if err != nil {
+		fmt.Printf("Error getting current configuration: %v\n", err)
+		return errors.New("failed to get current configuration")
+	}
+
+	// Modify baudrate bytes in the response
+	currentConfig[3] = 0x00 // Baudrate byte 1
+	currentConfig[4] = 0x00 // Baudrate byte 2
+	currentConfig[5] = 0x4B // Baudrate byte 3
+	currentConfig[6] = 0x00 // Baudrate byte 4
+
+	time.Sleep(1 * time.Second) // Wait for a second before sending the command
+	// Prepare the command to set the new configuration
+	setCmd := append([]byte{0x57, 0xAB, 0x00, 0x09, 0x32}, currentConfig[:50]...)
+	setCmd = append(setCmd, calcChecksum(setCmd[:len(setCmd)-1]))
+
+	err = c.Send(setCmd)
+	if err != nil {
+		fmt.Printf("Error sending configuration command: %v\n", err)
+		return errors.New("failed to send configuration command")
+	}
+
+	// Wait for the reply
+	resp, err := c.WaitForReply(0x09)
+	if err != nil {
+		fmt.Printf("Error waiting for reply: %v\n", err)
+		return errors.New("failed to get reply")
+	}
+
+	fmt.Print("Reply: ")
+	for _, b := range resp {
+		fmt.Printf("0x%02X ", b)
+	}
+	fmt.Println()
+	fmt.Println("Baudrate updated to 115200 successfully")
+	return nil
+}
+
+// GetChipCurrentConfiguration retrieves the current configuration of the chip.
+// It sends a command to the chip and waits for a reply.
+// Note only the data portion of the response is returned, excluding the header and checksum.
+func (c *Controller) GetChipCurrentConfiguration() ([]byte, error) {
+	//Send the command to get chip configuration and info
+	cmd := []byte{0x57, 0xAB,
+		0x00, 0x08, 0x00,
+		0x00, //placeholder for checksum
+	}
+
+	cmd[5] = calcChecksum(cmd[:5])
+	err := c.Send(cmd)
+	if err != nil {
+		fmt.Printf("Error sending command: %v\n", err)
+		return nil, errors.New("failed to send command")
+	}
+
+	resp, err := c.WaitForReply(0x08)
+	if err != nil {
+		fmt.Printf("Error waiting for reply: %v\n", err)
+		return nil, errors.New("failed to get reply")
+	}
+
+	if len(resp) < 50 {
+		fmt.Println("Invalid response length")
+		return nil, errors.New("invalid response length")
+	}
+
+	fmt.Print("Response: ")
+	for _, b := range resp {
+		fmt.Printf("0x%02X ", b)
+	}
+	fmt.Println()
+
+	return resp, nil
+}
+
 func (c *Controller) IsModifierKeys(keycode int) bool {
 	// Modifier keycodes for JavaScript
 	modifierKeys := []int{16, 17, 18, 91} // Shift, Ctrl, Alt, Meta (Windows/Command key)

+ 1 - 1
remdeskd/mod/remdeshid/typedef.go

@@ -14,7 +14,7 @@ const (
 	EventTypeHIDCommand
 )
 
-const MinCusorEventInterval = 36 // Minimum interval between cursor events in milliseconds
+const MinCusorEventInterval = 40 // Minimum interval between cursor events in milliseconds
 
 type Config struct {
 	/* Serial port configs */