Communication Transport
The basic transport layer represents an abstract communication backend. The TransportCWrapper class encapsulates HID device operations, providing a C++-style interface.
TransportCWrapper Class
TransportCWrapper is a C++ wrapper class designed to simplify the use of the transport_c.h library, providing RAII-style resource management.
Constructor
explicit TransportCWrapper(const hid_device_info& device_info);
Initialize connection using device information.
Destructor
Automatically releases the underlying TransportHandle resources.
Basic Communication Interface
Check if device is writable
bool canWrite() const;
Read device data
void read(uint8_t* response, size_t* length, int32_t timeoutMs = -1) const;
Get firmware version
std::string getFirmwareVesion() const;
Screen Control Interface
Wake up screen
void wakeupScreen() const;
Set key brightness
void setKeyBrightness(uint8_t brightness) const;
Refresh display
void refresh() const;
Sleep mode
void sleep() const;
Key Control Interface
Clear all keys
void clearAllKeys() const;
Clear specific key
void clearKey(uint8_t key_value) const;
Image Transfer Interface
Set key image (JPEG stream)
void setKeyImgFileStream(const std::string& jpegData, uint8_t keyValue) const;
Set background image (JPEG stream)
void setBackgroundImgStream(const std::string& jpegData, int32_t timeoutMs = 3000) const;
Set background image (bitmap stream)
void setBackgroundBitmap(const std::string& bitmapStream, int32_t timeoutMs = 5000) const;
Set background frame (for animation)
void setBackgroundFrameStream(const std::string& jpegData, uint16_t width, uint16_t height,
uint16_t x = 0, uint16_t y = 0, uint8_t FBlayer = 0x00) const;
Clear background frame
void clearBackgroundFrameStream(uint8_t postion = 0x03) const;
LED Control Interface
Set LED brightness
void setLedBrightness(uint8_t brightness) const;
Set LED color
void setLedColor(uint16_t count, uint8_t r, uint8_t g, uint8_t b) const;
Reset LED color
void resetLedColor() const;
Device Configuration Interface
Set device configuration
void setDeviceConfig(std::vector<uint8_t> configs) const;
Change device mode
void changeMode(uint8_t mode) const;
Connection Management Interface
Disconnect
void disconnected() const;
Send heartbeat packet
void heartbeat() const;
Report Settings Interface
Set report ID
void setReportID(uint8_t reportID) const;
Get report ID
uint8_t reportID() const;
Set report size
void setReportSize(uint16_t input_report_size, uint16_t output_report_size, uint16_t feature_report_size);
Error Handling Interface
Get last HID error
void rawHidLastError(wchar_t* errMsg, size_t* length) const;
Static Methods
Disable output
static void disableOutput(bool isDisable = true);
Asynchronous Operation Support
The SDK supports asynchronous operations, allowing you to perform other operations while reading return information:
// Check if writable
if (transport.canWrite()) {
// Asynchronously set background image
transport.setBackgroundImgStream(jpegData, 3000);
// No need to wait, can continue with other operations
transport.setKeyBrightness(100);
}
Multi-threading Safety
TransportCWrapper can be used simultaneously in multiple threads:
// Thread 1: Read device data
std::thread readThread([&transport]() {
uint8_t response[64];
size_t length;
while (running) {
transport.read(response, &length, 1000);
// Process response data
}
});
// Thread 2: Send commands
std::thread writeThread([&transport]() {
transport.setKeyImgFileStream(jpegData, 1);
transport.refresh();
});
Device Hot-plug Support
TransportCWrapper works with DeviceManager to support device hot-plugging on Linux and Windows platforms:
Error Handling
Provides detailed error information:
// Get last error message
wchar_t errMsg[128];
size_t length;
transport.rawHidLastError(errMsg, &length);
// Convert to string
std::wstring errorStr(errMsg, length);
std::wcout << L"Error: " << errorStr << std::endl;
