定时器管理

    技术2022-05-20  34

    /** *  emTimerManger.h - Related funciton declaration for timer * *  Copyright (C) 2011-2020 carl.wu *  All rights reserved. *  E-MAIL: carljianhua@yeah.net * **/ #ifndef EM_TIMER_MANAGER_2011_03_01_H #define EM_TIMER_MANAGER_2011_03_01_H #ifdef __cplusplus extern "C" { #endif #include "../Common/Common.h" //macro definition #define CFG_ENABLE_TIMER_NAME    1 // timer status definition typedef enum {     // timer is free status     emTimer_Free    = 0x00,     // timer is idle     emTimer_Idle    = 0x01,     // timer is using,     emTimer_Busy    = 0x02,     // timer is arrive the time     emTimer_Expire    = 0x04,     // end of timer status     eof_emTimerStatus =0x08 }eTimerStatus; // timer type definition typedef enum {     // timer is single, once used, never repeat     emTimer_Once    = 0x00,     // timer is repeated, it can use time and time again     emTimer_Repeat    = 0x01,     // timer never add ticks     emTimer_Pause    = 0x02,     // timer never add ticks     emTimer_Unknown    = 0x04,     // end of timer     eof_emTimerType = 0x08 }eTimerType; // timer definition typedef    INT32    name_t; typedef INT32    timer_t; typedef INT32    tick_t; // timer expired handle function definition typedef void (*TimerProc)(arg_t arg); // typedef struct _struct_time_t {     //timer name #if CFG_ENABLE_TIMER_NAME > 0     union     {         char    szName[4];         name_t    tName;     }name; #endif     // the handle of timer     timer_t        tHandle;     // the ticks of timer     tick_t        tCurrTicks;     // the set ticks of timer     tick_t        tSetTicks;     // the type of timer     eTimerType    emType;     // the timer expired handle function     TimerProc    fnEntry;     // the status of timer     eTimerStatus emTimerStaus; }tTimer; // the timer operation interface extern void __timer_compenent_init(void); extern void __timer_compenent_maintance(void); extern void __timer_compenent_process(void); // timer operation interface extern int CreateTimer(char* szName, int nTimerTicks, eTimerType emTimerType, TimerProc fnTimeProc); extern int StartTimer(int nTimer); extern int KillTimer(int nTimer); #ifdef __cplusplus } #endif #endif

     

     

     

     

     

     

     

     

     

     

     

     

     

    /** *  emTimerManger.h - Related funciton definition for timer * *  Copyright (C) 2011-2020 carl.wu *  All rights reserved. *  E-MAIL: carljianhua@yeah.net * **/ #include <string.h> #include "emTimerManager.h" //macro definition #define INVALID_HANDLE_VALUE    -1 #define CFG_TIMER_OBJ_NUM        8 static tTimer s_tTimerPool[CFG_TIMER_OBJ_NUM]; /*<FUNC>*********************************************************************** Function: __timer_compenent_init Description: initialize the timer component. Parameters: Returns: Notes: **<FUNC>**********************************************************************/ void __timer_compenent_init(void) {     INT8 i =0;     // initialize all content of timer     for (i=0; i<CFG_TIMER_OBJ_NUM; i++)     {         // initialize the name of timer         #if CFG_ENABLE_TIMER_NAME > 0         strcpy(s_tTimerPool[i].name.szName, "");         #endif         // initialize the handle of timer         s_tTimerPool[i].tHandle = INVALID_HANDLE_VALUE;         // initialize the current ticks         s_tTimerPool[i].tCurrTicks = 0;         // initialize the set ticks         s_tTimerPool[i].tSetTicks = 0;         // initialize the timer type         s_tTimerPool[i].emType = emTimer_Once;         // initialize the time process function pointer         s_tTimerPool[i].fnEntry = NULL;         // initialize the status of timer         s_tTimerPool[i].emTimerStaus = emTimer_Free;     } } /*<FUNC>*********************************************************************** Function: __timer_compenent_maintance Description: maintance the timer component. Parameters: Returns: Notes: **<FUNC>**********************************************************************/ void __timer_compenent_maintance(void) {     INT8 i = 0;     // find a free timer for user to create     for(i=0; i<CFG_TIMER_OBJ_NUM; i++)     {         if ((s_tTimerPool[i].fnEntry != NULL)             && (s_tTimerPool[i].emTimerStaus == emTimer_Busy))         {             s_tTimerPool[i].tCurrTicks = s_tTimerPool[i].tCurrTicks + 1;             //             if(s_tTimerPool[i].tCurrTicks >= s_tTimerPool[i].tSetTicks)             {                 s_tTimerPool[i].emTimerStaus = emTimer_Expire;             }         }     } } /*<FUNC>*********************************************************************** Function: __timer_compenent_process Description: check the timer component expired. Parameters: Returns: Notes: **<FUNC>**********************************************************************/ void __timer_compenent_process(void) {     INT8 i = 0;     // find a free timer for user to create     for(i=0; i<CFG_TIMER_OBJ_NUM; i++)     {         if ((s_tTimerPool[i].fnEntry != NULL)             && (s_tTimerPool[i].emTimerStaus == emTimer_Expire))         {             //             (*s_tTimerPool[i].fnEntry)(&s_tTimerPool[i]);             if (s_tTimerPool[i].emType == emTimer_Repeat)             {                 s_tTimerPool[i].tCurrTicks = 0;                 s_tTimerPool[i].emTimerStaus = emTimer_Busy;             }             else             {                 s_tTimerPool[i].emTimerStaus = emTimer_Idle;             }         }     } } /*<FUNC>*********************************************************************** Function: CreateTimer Description: Create timer object. Parameters:             szName        -- the name of timer.             nTimerTicks -- the tick counter of timer.             emTimerType    -- the type of timer.             fnTimeProc    -- the function pointer of timer expired. Returns: if successful return the handle of timer, otherwise return -1. Notes: **<FUNC>**********************************************************************/ int CreateTimer(char* szName, int nTimerTicks, eTimerType emTimerType, TimerProc fnTimeProc) {     INT8 i = 0;     // find a free timer for user to create     for(i=0; i<CFG_TIMER_OBJ_NUM; i++)     {         if ((s_tTimerPool[i].fnEntry == NULL)             && (s_tTimerPool[i].emTimerStaus == emTimer_Free))         {             break;         }     }     // set attribute to timer     #if CFG_ENABLE_TIMER_NAME > 0     strcpy(s_tTimerPool[i].name.szName, "");     #endif     // initialize the handle of timer     s_tTimerPool[i].tHandle = &s_tTimerPool[i];     // initialize the current ticks     s_tTimerPool[i].tCurrTicks = 0;     // initialize the set ticks     s_tTimerPool[i].tSetTicks = nTimerTicks;     // initialize the timer type     s_tTimerPool[i].emType = emTimerType;     // initialize the time process function pointer     s_tTimerPool[i].fnEntry = fnTimeProc;     // initialize the status of timer     s_tTimerPool[i].emTimerStaus = emTimer_Idle; } /*<FUNC>*********************************************************************** Function: StartTimer Description: Start timer. Parameters:             nTimer    -- the handle of timer. Returns: if successful return the handle of timer, otherwise return -1. Notes: **<FUNC>**********************************************************************/ int StartTimer(int nTimer) {     //     tTimer* pTimer = NULL;     //     if (nTimer == INVALID_HANDLE_VALUE)     {         return INVALID_HANDLE_VALUE;     }     else     {         pTimer = (tTimer*)nTimer;         //         if (NULL != pTimer)         {             if ((pTimer->fnEntry == NULL)                 || (pTimer->emTimerStaus != emTimer_Idle))             {                 return INVALID_HANDLE_VALUE;             }             else             {                 pTimer->tCurrTicks = 0;                 pTimer->emTimerStaus = emTimer_Busy;             }         }         else         {                 return INVALID_HANDLE_VALUE;         }     }     return 0; } /*<FUNC>*********************************************************************** Function: KillTimer Description: Killer timer. Parameters:             nTimer    -- the handle of timer. Returns: if successful return the handle of timer, otherwise return -1. Notes: **<FUNC>**********************************************************************/ int KillTimer(int nTimer) {     //     tTimer* pTimer = NULL;     //     if (nTimer == INVALID_HANDLE_VALUE)     {         return INVALID_HANDLE_VALUE;     }     else     {         pTimer = (tTimer*)nTimer;         //         if (NULL != pTimer)         {             if (pTimer->fnEntry == NULL)             {                 return INVALID_HANDLE_VALUE;             }             else             {                 pTimer->fnEntry = NULL;                 pTimer->emTimerStaus = emTimer_Free;             }         }         else         {             return INVALID_HANDLE_VALUE;         }     }     return 0; }


    最新回复(0)