1、定義一個時鐘結構體
static Clock_Struct clk_spa;
2、將週期性事件ID傳遞給時鐘處理程序的內存
// Application events
#define SP_STATE_CHANGE_EVT 0
#define SP_CHAR_CHANGE_EVT 1
#define SP_KEY_CHANGE_EVT 2
#define SP_ADV_EVT 3
//添加自定義事件ID
#define SPA_EVT 10//不要和其他事件ID重複
spClockEventData_t arg_SPA =
{ .event = SPA_EVT };
3、定義執行的周期函數
static void SPA_performPeriodicTask(void)
{
static int aa = 0;
Display_printf(dispHandle, 20, 0, "========%d============",aa++);
}
4、在處理傳入的GAP事件的函數中添加時鐘初始化函數,參數1000毫秒,其餘參數看函數聲明
static void SimplePeripheral_processGapMessage(gapEventHdr_t *pMsg){
//--------------------------------------------
// Create one-shot clock event.
Util_constructClock(&clk_spa, SimplePeripheral_clockHandler,
1000, 0, true,
(UArg) &arg_SPA);
}
5、在時鐘超時處理函數中添加
static void SimplePeripheral_clockHandler(UArg arg){
//---------------------------------------------
else if (pData->event == SPA_EVT)
{
//Start the next period
Util_startClock(&clk_spa);
//Post event queueMsg
SimplePeripheral_enqueueMsg(SPA_EVT, NULL);
}
}
6、在處理的消息的函數中添加case
static void SimplePeripheral_processAppMsg(spEvt_t *pMsg){
//---------------------------------------------
case SPA_EVT:
SPA_performPeriodicTask();
break;
}
7、也可以把再次啓動函數放到執行函數中,這樣就執行完成以後再次啓動執行
static void SPA_performPeriodicTask(void)
{
static int aa = 0;
Display_printf(dispHandle, 20, 0, "========%d============",aa++);
Util_startClock(&clk_spa);
}
8、運行截圖