如何使用EmberZNet中的EmberTask和EmberEventControl?
如何使用EmberZNet中的EmberTask和EmberEventControl?
一、在EmberZNet中使用了一种事件型的任务机制,其主要内容是:
- 任务,最多可以有256个任务;
- 任务包含事件,一个任务理论上可以有N多个事件;
- 事件,事件通过时间控制运行,时间单位有5种:不运行、毫秒、256毫秒、65536毫秒、尽快运行。
{
/** The event is not scheduled to run. */
EMBER_EVENT_INACTIVE = 0,
/** The execution time is in approximate milliseconds. */
EMBER_EVENT_MS_TIME,
/** The execution time is in 'binary' quarter seconds (256 approximate
milliseconds each). */
EMBER_EVENT_QS_TIME,
/** The execution time is in 'binary' minutes (65536 approximate milliseconds
each). */
EMBER_EVENT_MINUTE_TIME,
/** The event is scheduled to run at the earliest opportunity. */
EMBER_EVENT_ZERO_DELAY
};
二、如何新建一个任务
- 定义一个任务ID变量
// Task ids used to run events through idling
EmberTaskId appTaskId;
- 定义一个事件数组和事件字符串数组,其中事件字符串数组是可选的。
PGM_P appEventStrings[] = {
APP_TEST_EVENT_STRINGS
};
EmberEventData appEvents[] = {
APP_TEST_EVENTS
{ NULL, NULL }
};
- 初始化函数
// A function used to initialize events for idling
void appInitEvents(void)
{
emberTaskEnableIdling(true);
appTaskId = emberTaskInit(appEvents);
}
- 运行任务函数
void appRunEvents(void)
{
// Don't run events while crypto operation is in progress
// (BUGZID: 12127)
if (emAfIsCryptoOperationInProgress()) {
// DEBUG Bugzid: 11944
emberAfCoreFlush();
return;
}
emberRunTask(appTaskId);
}
三、新建一个事件
- 事件的定义和处理可以参考EmberZNet中例子,大概如下:
EmberEventControl appTest1EventControl;
EmberEventControl appTest2EventControl;
EmberEventControl appTest3EventControl;
void appTest1EventHandler(void)
{
emberEventControlSetInactive(appTest1EventControl);
emberAfCorePrintln("appTest1EventHandler");
emberEventControlSetDelayMS(appTest1EventControl, 1000);
}
void appTest2EventHandler(void)
{
emberEventControlSetInactive(appTest2EventControl);
emberAfCorePrintln("appTest2EventHandler");
emberEventControlSetDelayMS(appTest2EventControl, 1000);
}
void appTest3EventHandler(void)
{
emberEventControlSetInactive(appTest3EventControl);
emberAfCorePrintln("appTest3EventHandler");
emberEventControlSetDelayMS(appTest3EventControl, 1000);
}
void vStartTestEvent(void)
{
emberEventControlSetDelayMS(appTest1EventControl, 1000);
emberEventControlSetDelayMS(appTest2EventControl, 1000);
emberEventControlSetDelayMS(appTest3EventControl, 1000);
}
四、初始化任务和运行事件
- 初始化和运行任务,在EmberZNet中的“emberAfMain()”函数中调用“appInitEvents()”进行初始化,在while前或者其他地方调用“vStartTestEvent()”,在while中调用“appRunEvents()”,如下图所示:
- 运行效果