C
实现基本函数
平台上下文的概念
Qt Quick Ultralite 内核通过Qul::Platform::PlatformContext 虚拟类与平台进行通信。该类包含一组虚拟方法,必须实现这些方法才能访问硬件资源。每个平台通过继承Qul::Platform::PlatformContext 来创建其自身的平台上下文类实现。
Qt Quick Ultralite内核通过Qul::Platform::getPlatformInstance 函数获取Qul::Platform::PlatformContext 的特定于平台的实现。
实现基本功能
本章列出了运行Qt Quick Ultralite 内核必须实现的基本函数和类方法。您可以将platform/boards/qt/example-baremetal/platform_context.cpp 的内容作为自身实现的基础,并在指定位置添加您的代码。
平台上下文的实现
平台必须通过继承来实现Qul::Platform::PlatformContext 虚拟类。
struct ExamplePlatform : PlatformContext { void initializeHardware() QUL_DECL_OVERRIDE; void initializePlatform() QUL_DECL_OVERRIDE; void exec() QUL_DECL_OVERRIDE; uint64_t update() QUL_DECL_OVERRIDE; double rand() QUL_DECL_OVERRIDE; void scheduleEngineUpdate(uint64_t timestamp) QUL_DECL_OVERRIDE; uint64_t currentTimestamp() QUL_DECL_OVERRIDE; void consoleWrite(char character) QUL_DECL_OVERRIDE;
注意:这些 方法及其示例实现已在《平台移植指南》的不同章节中进行了说明。
Qul::Platform::getPlatformInstance 必须返回一个特定于平台的上下文类的实例。
PlatformContext *getPlatformInstance() { static ExamplePlatform platform; return &platform; }
Qt Quick Ultralite 核心使用返回的实例,并在运行时调用上下文方法与硬件和平台进行通信。
初始化硬件
硬件组件的初始化可以在PlatformContext::initializeHardware 方法中进行。
这可能包括设置时钟、引脚、外设、总线和内存。您也可以选择不实现它,而是执行自己的硬件初始化,例如在应用程序的main() 函数中。
void ExamplePlatform::initializeHardware() { #if 0 Replace the following code with actual code for your device. setup_system_clocks(); setup_flash_memory(); setup_sd_ram(); setup_usart(); setup_rnd_generator(); #endif } ...
平台初始化
PlatformContext::intializePlatform 方法在Qt Quick Ultralite引擎运行之前被调用,需要配置一些针对您所用开发板的特定资源。
这主要用于图形资源,因此目前可以保持为空。
void PlatformContext::intializePlatform() { Qul::PlatformInterface::log("Called platform initialization\n"); }
当前时间戳
渲染和定时器的一个关键部分是获取系统的当前时间戳。PlatformContext::currentTimestamp 方法必须返回该系统时间。
uint64_t ExamplePlatform::currentTimestamp() { // Replace this line to make it return the system timestamp in milliseconds. return 0; }
渲染循环回调调度
Qt Quick Ultralite 的渲染循环和事件处理必须在指定时间被调用。
Qt Quick Ultralite 核心将调用 `PlatformContext::scheduleEngineUpdate ` 方法,以通知平台其需要执行任务的下一个回调时间戳。
void ExamplePlatform::scheduleEngineUpdate(uint64_t timestamp) { nextUpdate = timestamp; }
time_stamp 参数指定渲染循环下次需要运行的时间。如果您的平台提供硬件定时器或操作系统提供的定时器,您可以使用它们来实现调度超时。一个简单的解决方案是将此超时值存储在一个变量中,供下面的函数使用。
警告: PlatformContext::scheduleEngineUpdate 的实现可能在中断中被调用,因此必须确保其在中断上下文中运行是安全的。
渲染循环更新
Qt Quick Ultralite 引擎需要被调用指定次数。
PlatformContext::update 方法负责单核引擎的更新,通过调用 `PlatformInterface::updateEngine ` 来更新其计时器并显示动画。它使用由Qt Quick Ultralite 核心引擎设置的时间戳,如上一节所述。
uint64_t ExamplePlatform::update() { const uint64_t timestamp = this->currentTimestamp(); if (timestamp >= nextUpdate) { // Handle deadline or pending events Qul::PlatformInterface::updateEngine(timestamp); } return nextUpdate; }
该函数返回预计下次被调用的时间戳。如果该时间戳小于当前时间戳,甚至为 0,则应尽快再次调用该函数。 如果时间戳值大于当前时间戳,则表示平台实现应在指定时间调用PlatformContext::update 。在预定时间到来之前,设备可以让出执行权或进入睡眠模式。
该函数可以尽可能频繁地被调用,因为它会检查由PlatformContext::scheduleEngineUpdate 安排的时间戳是否已经过去。若需以其他方式处理预定时间,则仅需调用PlatformInterface::updateEngine 即可。
主循环
PlatformContext::exec 包含exec循环,并会永久运行(或至少在应用程序运行期间持续运行),负责在适当的时候调用PlatformContext::update 。当核心引擎不需要更新调用时,设备会在可能的情况下进入睡眠状态。
如果您计划由应用程序而非Qt Quick Ultralite 平台来驱动主循环,则可以跳过该实现,转而在应用程序的主循环中编写类似的代码,且无需调用PlatformContext::exec 。
void ExamplePlatform::exec() { while (true) { logFlush(); // Flush partially filled log buffer const uint64_t timestamp = this->update(); if (timestamp > Platform::getPlatformInstance()->currentTimestamp()) { // The device may yield or go into sleep mode } } }
用于让出执行权或进入睡眠状态的函数必须能够返回,以应对以下情况:当通过中断向Qt Quick Ultralite核心发送了事件(如触摸事件),或者到达了下一次计划更新的时间。
注意:如果 未实现睡眠或省电模式,CPU 将始终以满负荷运行。
注意:只有 当您不打算使用任何演示、示例、测试、调用 Application::exec() 或使用 app_common 框架。
随机数
为了向 QML 应用程序提供随机数,平台要求重写PlatformContext::rand 。
double ExamplePlatform::rand() { // Replace std::rand() by the proper call to the random number generator on your device, if available. const uint32_t number = std::rand(); return number / (std::numeric_limits<uint32_t>::max() + 1.0); }
基本内存分配
Qt Quick Ultralite 核心库使用以下函数处理小规模内存分配:
您可以如下所示将这些调用转发给平台特有的函数:
void *qul_malloc(std::size_t size) { return std::malloc(size); } void qul_free(void *ptr) { std::free(ptr); } void *qul_realloc(void *ptr, size_t s) { return std::realloc(ptr, s); }
所有与图形相关的内存分配将在本指南的后续部分中进行说明。
IAR 编译器的附加函数
为了使时间基础设施正常工作,IAR 库需要实现一些附加函数。这些函数必须具有 C 链接方式,即将其放置在.c 文件中,或位于extern "C" 部分内。有关更多信息,请参阅 IAR 发布的《IAR C/C++ 开发人员指南》。
// The number of times an internal timing event occurs per second int const CLOCKS_PER_SECOND = 1000; clock_t clock(void) { QUL_UNUSED(CLOCKS_PER_SECOND); // This function should return a value, which after division by CLOCKS_PER_SECOND, // is the processor time in seconds. return (clock_t) HAL_GetTick(); } // The time_t type is defined in bxarm/arm/inc/c/time{32,64}.h #if _DLIB_TIME_USES_64 time_t __time64(time_t *t) #else time_t __time32(time_t *t) #endif { uint64_t timeAtStartup = 0; // Read this from a time source like a real time clock; uint64_t currentTimestamp = timestamp(); // same timestamp as _gettimeofday time_t curtime = (time_t) (timeAtStartup + (currentTimestamp / 1000)); if (t) *t = curtime; return curtime; } char const *__getzone() { // See <IAR>/src/lib/time/getzone.c for documentation // For Germany as a default timezone return ":GMT+1:GMT+2:0100:032502+0:102502+0"; } __ATTRIBUTES char *_DstMalloc(size_t); __ATTRIBUTES void _DstFree(char *); char *_DstMalloc(size_t s) { // Return a buffer that can hold the maximum number of DST entries of // of any timezone available on the device. // Each DST entry takes up a structure of 5 bytes plus regular alignment. // Instead of a static buffer a dynamically allocated memory can be used as well. // With the two entries shown above the required buffer size would be // 2 * (5 bytes size + 3 bytes alignment) = 16 bytes static char buffert[8 * 4]; return buffert; } void _DstFree(char *p) { // Nothing required here because of static buffer in _DstMalloc QUL_UNUSED(p); }
在某些 Qt 许可证下可用。
了解更多。