C
移植 DeviceLink 通信
DeviceLink 组件可实现主机与设备之间的通信。它允许双方交换触摸、屏幕截图、性能或日志数据,以供调试或测试之用。
自定义 DeviceLink
Platform::DeviceLinkInterface 提供了协议功能与硬件之间的接口。必须实现以下函数:
- DeviceLinkInterface::platformInit 初始化接收数据的硬件,例如串行端口。
- DeviceLinkInterface::transmitChars 将已准备好发送的数据写入串行端口。
- DeviceLinkInterface::framebufferFormat 获取指定层的帧缓冲区信息。
以下是上述函数的一个示例实现:
struct ExampleDeviceLinkInterface : Qul::Platform::DeviceLinkInterface { void platformInit() { // Enable and start actual transfer and reception of the serial port, // eg. enabling interrupts } void transmitChars(const uint8_t *data, uint32_t size) { QUL_UNUSED(data); QUL_UNUSED(size); // Use the serial port to transfer the data to the host // HAL_UART_Transmit(&uartHandle, (uint8_t *) data, size, 0xFFFF); } Qul::Platform::FramebufferFormat framebufferFormat(const Qul::PlatformInterface::LayerEngine::ItemLayer *layer) QUL_DECL_OVERRIDE { // Assuming we use 32bpp framebuffers static const int BytesPerPixel = 4; static const int ScreenWidth = QUL_PLATFORM_DEFAULT_SCREEN_WIDTH; static const int ScreenHeight = QUL_PLATFORM_DEFAULT_SCREEN_HEIGHT; Qul::Platform::FramebufferFormat format; format.address = frontBuffer(); format.width = ScreenWidth; format.height = ScreenHeight; format.bytesPerLine = ScreenWidth * BytesPerPixel; format.bitsPerPixel = BytesPerPixel * 4; format.redChannel.offset = 16; format.redChannel.length = 8; format.greenChannel.offset = 8; format.greenChannel.length = 8; format.blueChannel.offset = 0; format.blueChannel.length = 8; format.swapBytes = 0; return format; } };
如果移植的平台具有分层引擎,则需要以不同的方式实现framebufferFormat 函数以适应此情况:
Qul::Platform::FramebufferFormat framebufferFormat(const PlatformInterface::LayerEngine::ItemLayer *layer) { const ExampleItemLayerBase *base = static_cast<const ExampleItemLayerBase *>(layer); Qul::Platform::FramebufferFormat format; format.address = base->drawingDevice.bits(); format.width = base->drawingDevice.width(); format.height = base->drawingDevice.height(); format.bytesPerLine = base->drawingDevice.bytesPerLine(); format.bitsPerPixel = base->drawingDevice.bitsPerPixel(); switch (format.bitsPerPixel) { case 16: format.redChannel.offset = 0; format.redChannel.length = 5; format.greenChannel.offset = 5; format.greenChannel.length = 6; format.blueChannel.offset = 11; format.blueChannel.length = 5; format.swapBytes = 2; break; case 32: format.redChannel.offset = 16; format.redChannel.length = 8; format.greenChannel.offset = 8; format.greenChannel.length = 8; format.blueChannel.offset = 0; format.blueChannel.length = 8; format.swapBytes = 0; break; default: QUL_ASSERT(false, QulError_LayerEngine_UnsupportedColorDepth, format.bitsPerPixel); } return format; }
为了让Qt Quick Ultralite能够使用您的自定义设备链接实现,请创建一个函数来返回您设备的设备链接接口。
DeviceLinkInterface *getDeviceLinkInterface() { static ExampleDeviceLinkInterface deviceLink; return &deviceLink; }
另请参阅QUL_PLATFORM_DEVICELINK_ENABLED。
硬件设置
在您对 `PlatformContext::initializeHardware` 的实现中,请在初始化目标通道(通常为串行端口)的硬件之后,添加设备链接库的初始化代码,
if (Qul::Platform::DeviceLink::instance()) Qul::Platform::DeviceLink::instance()->init();
这还将调用上述实现的platformInit 函数以启动数据接收。
在您的串行接收函数中,将接收到的数据传输到设备链接组件进行解码:
void UART_ReceiveInterrupt() { Qul::PlatformInterface::deviceLinkBytesReceived(&receivedByte, 1); }
请避免向串行端口写入任何其他数据,因为这会干扰传输协议。为确保安全,请重写 printf 或其他函数可能调用的底层写入函数,以将它们的内容通过设备链接协议进行传输。
在大多数平台上,需要重写的低级写入函数包括:
void putChar(char &character); int _write(int file, char *ptr, int len);
应针对上述每个函数调整以下示例实现。所有通过Qt Quick Ultralite日志系统打印的消息,在系统处于活动状态时都会自动通过设备链接进行传输。
int _write(char *message, int len) { auto deviceLink = Qul::Platform::DeviceLink::instance(); if (deviceLink) deviceLink->printMessage(message, len); else sendDataDirectlyToSerialPort(); }
如果平台库是使用 CMake 构建的,请使用 `$<TARGET_PROPERTY:Qul::Platform,QUL_PLATFORM_UART_BAUD_RATE> ` 获取配置的波特率,或者包含已设置 `QUL_PLATFORM_UART_BAUD_RATE ` 编译定义的 `platform_config.h`,并据此配置 UART 串行端口。
另请参阅QUL_PLATFORM_UART_BAUD_RATE。
更新日志记录实现
修改前一步中实现的consoleWrite 函数,使其同样使用设备链接传输。
void ExamplePlatform::consoleWrite(char character) { if (deviceLink) { // You should think about collecting several characters and send them in chunks // instead of 1 by 1 because this will be slow. deviceLink->printMessage(&character, 1); } else { // Write the charactor to your boards output device sendDataDirectlyToSerialPort(); } }
重建平台的要求
安装protobuf 和grpcio-tools Python 包,以便在启用设备链接的情况下重建平台:
pip install protobuf==4.23.4 grpcio-tools==1.60.0 "setuptools<80"
在某些 Qt 许可下可用。
了解更多信息。