Răsfoiți Sursa

Added mouse click and scroll firmware

Toby Chui 1 zi în urmă
părinte
comite
4d0281d283

+ 7 - 4
usbkvm/testcase_fw2/mouse_click.bat

@@ -1,11 +1,14 @@
 @echo off
+
+.\send.exe COM3 115200 0xFF
+timeout /t 1 /nobreak >nul
 :: Hold the left mouse button for 3 seconds
-.\send.exe COM4 115200 0x02 0x02 0x01
+.\send.exe COM3 115200 0x02 0x05 0x01
 timeout /t 3 /nobreak >nul
-.\send.exe COM4 115200 0x02 0x03 0x01
+.\send.exe COM3 115200 0x02 0x06 0x01
 timeout /t 3 /nobreak >nul
 :: Do a right click
-.\send.exe COM4 115200 0x02 0x01 0x02
+.\send.exe COM3 115200 0x02 0x05 0x02 0x02 0x06 0x02
 timeout /t 3 /nobreak >nul
 :: Do a middle click
-.\send.exe COM4 115200 0x02 0x01 0x03
+.\send.exe COM3 115200 0x02 0x05 0x03 0x02 0x06 0x00

+ 7 - 2
usbkvm/testcase_fw2/mouse_scroll.bat

@@ -1,6 +1,11 @@
 @echo off
+.\send.exe COM3 115200 0xFF
+timeout /t 1 /nobreak >nul
+
 :: Scroll up (Tested on Windows)
-.\send.exe COM4 115200 0x04 0x00 0x02
+echo "Scrolling Up"
+.\send.exe COM3 115200 0x02 0x07 0x08
 timeout /t 3 /nobreak >nul
 :: Scroll down
-.\send.exe COM4 115200 0x04 0x01 0x02
+echo "Scrolling down"
+.\send.exe COM3 115200 0x02 0x08 0x08

+ 6 - 0
usbkvm/usbkvm_fw2/ch9329_utils.ino

@@ -1,3 +1,9 @@
+/*
+  ch9329_utils.ino
+
+  This file contain codes that communicate with the ch9329 IC via UART0
+*/
+
 // Checksum = sum of all bytes except last
 uint8_t calcChecksum(uint8_t* data, uint8_t len) {
   uint8_t sum = 0;

+ 1 - 0
usbkvm/usbkvm_fw2/keyboard_emu.ino

@@ -60,6 +60,7 @@ void handle_keyboard_get_info_reply() {
 #endif
 }
 
+
 /* Send key combinations to CH9329*/
 int keyboard_send_key_combinations() {
   uint8_t packet[14] = {

+ 104 - 0
usbkvm/usbkvm_fw2/mouse_emu.ino

@@ -0,0 +1,104 @@
+/*
+  mouse_emu.ino
+
+  This file contain code that emulate mouse movements
+*/
+
+uint8_t mouse_button_state = 0x00;
+
+//Move the mouse to given position, range 0 - 4096 for both x and y value
+void mouse_move_absolute(uint8_t x_lsb, uint8_t x_msb, uint8_t y_lsb, uint8_t y_msb) {
+  uint8_t packet[12] = {
+    0x57, 0xAB, 0x00, 0x04, 0x07, 0x02,
+    mouse_button_state,
+    x_lsb,  // X LSB
+    x_msb,  // X MSB
+    y_lsb,  // Y LSB
+    y_msb,  // Y MSB
+    0x00    // Checksum placeholder
+  };
+
+  packet[11] = calcChecksum(packet, 11);
+  Serial0_writeBuf(packet, 12);
+}
+
+//Move the mouse to given relative position
+void mouse_move_relative(int8_t dx, int8_t dy, int8_t wheel) {
+  uint8_t packet[11] = {
+    0x57, 0xAB, 0x00, 0x05, 0x05, 0x01,
+    mouse_button_state,
+    dx,
+    dy,
+    wheel,
+    0x00  // Checksum placeholder
+  };
+
+  packet[10] = calcChecksum(packet, 10);
+  Serial0_writeBuf(packet, 11);
+}
+
+
+int mouse_scroll_up(uint8_t tilt) {
+  if (tilt > 0x7F)
+    tilt = 0x7F;
+  if (tilt == 0) {
+    //No need to move
+    return 0;
+  }
+  mouse_move_relative(0, 0, tilt);
+  return 0;
+}
+
+int mouse_scroll_down(uint8_t tilt) {
+  if (tilt > 0x7E)
+    tilt = 0x7E;
+  if (tilt == 0) {
+    //No need to move
+    return 0;
+  }
+  mouse_move_relative(0, 0, 0xFF-tilt);
+  return 0;
+}
+
+//handle mouse button press events
+int mouse_button_press(uint8_t opcode) {
+  switch (opcode) {
+    case 0x01:  // Left
+      mouse_button_state |= 0x01;
+      break;
+    case 0x02:  // Right
+      mouse_button_state |= 0x02;
+      break;
+    case 0x03:  // Middle
+      mouse_button_state |= 0x04;
+      break;
+    default:
+      return -1;
+  }
+  // Send updated button state with no movement
+  mouse_move_relative(0, 0, 0);
+  return 0;
+}
+
+//handle mouse button release events
+int mouse_button_release(uint8_t opcode) {
+  switch (opcode) {
+    case 0x00:  // Release all
+      mouse_button_state = 0x00;
+      break;
+    case 0x01:  // Left
+      mouse_button_state &= ~0x01;
+      break;
+    case 0x02:  // Right
+      mouse_button_state &= ~0x02;
+      break;
+    case 0x03:  // Middle
+      mouse_button_state &= ~0x04;
+      break;
+    default:
+      return -1;
+  }
+  // Send updated button state with no movement
+  mouse_move_relative(0, 0, 0);
+  return 0;
+}

+ 36 - 5
usbkvm/usbkvm_fw2/usbkvm_fw2.ino

@@ -28,13 +28,18 @@ void flush_cmd_resp();
 
 //keyboard_emu.ino
 void keyboard_get_info();
-void keyboard_reset(); 
+void keyboard_reset();
 void handle_keyboard_get_info_reply();
 int keyboard_press_key(uint8_t keycode);
 int keyboard_release_key(uint8_t keycode);
 int keyboard_press_modkey(uint8_t opcode);
 int keyboard_release_modkey(uint8_t opcode);
 
+//mouse_emu.ino
+int mouse_button_press(uint8_t opcode);
+int mouse_button_release(uint8_t opcode);
+int mouse_scroll_up(uint8_t tilt);
+int mouse_scroll_down(uint8_t tilt);
 
 // Set the USB descriptor exposed to the slave device
 void setup_keyboard_chip_cfg() {
@@ -62,30 +67,56 @@ void handle_cmd_processing(uint8_t cmd) {
 }
 
 //This function will handle the incoming cmd from RemdesKVM control software
+//return 0x00 if success and 0x01 if error
 void handle_ctrl_cmd() {
   uint8_t cmd = cmdBuf[1];    //Type of operation
   uint8_t value = cmdBuf[2];  //values for 2 bytes cmd
+  int ret = 0;
   switch (cmd) {
     case 0x01:
       //keyboard press
-      keyboard_press_key(value);
+      ret = keyboard_press_key(value);
       break;
     case 0x02:
       //keyboard release
-      keyboard_release_key(value);
+      ret = keyboard_release_key(value);
       break;
     case 0x03:
       //keyboard modifier key press
-      keyboard_press_modkey(value);
+      ret = keyboard_press_modkey(value);
       break;
     case 0x04:
       //keyboard modifier key release
-      keyboard_release_modkey(value);
+      ret = keyboard_release_modkey(value);
+      break;
+    case 0x05:
+      //mouse button press
+      ret = mouse_button_press(value);
+      break;
+    case 0x06:
+      //mouse button release
+      ret = mouse_button_release(value);
+      break;
+    case 0x07:
+      //Mouse scroll up
+      ret = mouse_scroll_up(value);
+      break;
+    case 0x08:
+      //Mouse scroll down
+      ret = mouse_scroll_down(value);
       break;
     default:
       //unknown operation, do nothing
+      ret = -1;
       break;
   }
+
+  if (ret < 0){
+    //Error
+    USBSerial_print(0x01);
+  }else{
+    USBSerial_print(0x00);
+  }
 }
 
 void setup() {