Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CH9329 Controller Library

A C++ library for controlling CH9329 USB-to-Serial devices, providing comprehensive APIs for keyboard, mouse, and HID operations.

πŸ“‹ Features

  • Complete keyboard control (regular and multimedia keys)
  • Absolute and relative mouse operations
  • Custom HID data transmission
  • Device configuration management
  • High-level mouse operations (click, drag, hover, etc.)
  • Cross-platform support (Linux, Windows, macOS)

πŸ› οΈ Dependencies

πŸ“¦ Installation

Install Dependencies

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install libboost-system-dev cmake build-essential

macOS (using Homebrew):

brew install boost cmake

Windows:

Build and Install Library

git clone https://github.com/EnderTheCoder/CH9329Controller
cd CH9329Lib
mkdir build && cd build
cmake ..
make -j$(nproc)
sudo make install

πŸš€ Usage

CMake Integration

Method 1: Find Package (after installation)

find_package(CH9329Controller REQUIRED)

add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE CH9329::CH9329Controller)

Method 2: Add Subdirectory

add_subdirectory(path/to/CH9329Lib)

add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE CH9329Controller)

Basic Example

#include <ch9329/CH9329Controller.hpp>
#include <iostream>
#include <thread>
#include <chrono>

using namespace enderhive::device;
using namespace std::chrono_literals;

int main() {
    try {
        // Initialize controller with serial port
        CH9329Controller controller("/dev/ttyUSB0", 9600);
        
        // Get device information
        auto info = controller.get_info();
        if (info) {
            std::cout << "Device connected: " << info->usb_connected << std::endl;
            std::cout << "Version: " << static_cast<int>(info->version_major) 
                      << "." << static_cast<int>(info->version_minor) << std::endl;
        }
        
        // Send keyboard input: "Hello"
        controller.send_kb_general_data(KeyboardCtrlKey::None, {0x0B, 0x08, 0x0C, 0x0C, 0x12}); // H,e,l,l,o
        
        // Move mouse to absolute position and click
        controller.move_to_absolute(2048, 2048); // Center of screen
        std::this_thread::sleep_for(100ms);
        controller.click(MouseButton::Left);
        
        // Drag operation
        controller.drag_absolute(1000, 1000, 3000, 3000, MouseButton::Left);
        
        // Scroll wheel
        controller.scroll_wheel(-3); // Scroll down
        
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}

πŸ“š API Overview

Device Management

// Constructor
CH9329Controller controller("/dev/ttyUSB0", 9600);

// Get device information
std::optional<DeviceInfo> info = controller.get_info();

// Reset device
controller.reset();

// Restore factory settings
controller.set_default_config();

Keyboard Operations

// Regular keyboard input
controller.send_kb_general_data(KeyboardCtrlKey::LeftShift, {0x04}); // 'A'

// Multimedia keys
controller.send_kb_media_data(0x02, 0xE9); // Volume Up

Mouse Operations

// Absolute positioning
controller.move_to_absolute(4095, 4095); // Bottom-right corner
controller.click_at_absolute(2048, 2048);

// Relative movement
controller.move_mouse(100, -50);
controller.scroll_wheel(2); // Scroll up

// High-level operations
controller.click(MouseButton::Right, 100);
controller.double_click();
controller.drag_select(100, 100, 500, 500);
controller.right_click_menu();

Configuration Management

// USB string descriptors
auto manufacturer = controller.get_usb_string(UsbStringType::Manufacturer);
controller.set_usb_string(UsbStringType::Product, "My Custom Device");

// Parameter configuration
auto config = controller.get_para_config();
if (config) {
    // Modify config...
    controller.set_para_config(*config);
}

🎯 Coordinate Conversion

// Convert screen coordinates to CH9329 absolute coordinates (0-4095)
auto [abs_x, abs_y] = CH9329Controller::convert_screen_to_absolute(
    1920, 1080,  // Screen coordinates
    3840, 2160   // Screen resolution
);

πŸ“– Enumerations

Keyboard Control Keys

enum class KeyboardCtrlKey {
    LeftCtrl, LeftShift, LeftAlt, LeftWin,
    RightCtrl, RightShift, RightAlt, RightWin
};

Mouse Buttons

enum class MouseButton {
    None, Left, Right, Middle
};

USB String Types

enum class UsbStringType {
    Manufacturer, Product, SerialNumber
};

πŸ”§ Building Examples

Enable examples during build:

cmake .. -DBUILD_EXAMPLES=ON
make

Run the demo:

./examples/demo

πŸ“„ License

This library is released under the MIT License. See LICENSE file for details.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

πŸ“ž Support

For issues and feature requests, please open an issue on GitHub.

About

C++ library for CH9329 USB-to-Serial device controller

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages