📄 plx_altimer.c
字号:
/***************************************************************************
*
* Pollex Mobile Platform
*
* Copyright (c) 2004 by Pollex Mobile Software Co., Ltd.
* All Rights Reserved
*
* Module : Pollex OS Adaption Layer
*
* Purpose : Implements the timer function.
*
\**************************************************************************/
#include "TimerEvents.h"
#include "plxdef.h"
#include "plxfs.h"
#include "plxclib.h"
#include "plxdbg.h"
#include "plxtimer.h"
#define PLX_MAX_TIMERS 32
#define PLX_MIN_ELAPSE 100 // mtk 的定时精度
#define PLX_MAX_ELAPSE 0xffffffff
typedef struct tagTIMERSTRUCT
{
DWORD dwMilliseconds;
PLXTIMERPROC pTimerProc;
void* pParam;
int bSync;
} TIMERSTRUCT, *PTIMERSTRUCT;
static TIMERSTRUCT TimerList[PLX_MAX_TIMERS];
static int last = -1;
static DWORD dwStartTime;
// Internal function prototypes
static int plx_StartTimer(DWORD dwMilliseconds, PLXTIMERPROC pTimerProc, void* pParam);
static void plx_RestartTimer(int i);
typedef void (*FuncPtr)(void);
typedef unsigned short U16;
typedef unsigned int U32;
extern void StartTimer(U16 timerid, U32 delay, FuncPtr funcPtr);
extern void StopTimer(U16 timerid);
/****************************************************************************
** Function : PlxGetTickCount
** Purpose :
** Retrieves the number of milliseconds that have elapsed since the
** system was started.
****************************************************************************/
#ifdef MMI_ON_HARDWARE_P
typedef unsigned int kal_uint32;
void kal_get_time ( kal_uint32* ticks_ptr);
#endif
DWORD PlxGetTickCount(void)
{
unsigned long last_time;
#ifdef MMI_ON_HARDWARE_P
unsigned int last_ticks;
kal_get_time ( &last_ticks );
last_time = kal_ticks_to_milli_secs ( last_ticks );
return last_time;
#else
__declspec(dllimport) unsigned long _stdcall GetTickCount(void);
last_time = GetTickCount();
return last_time;
#endif
}
/*************************************************************************
** Function : PlxStartTimer
** Purpose :
** Starts a timer that measures time in milliseconds.
** Params :
** dwMilliseconds : Specifies the time-out value, in milliseconds.
** Return :
** If the function succeeds, return an non zero integer identifying
** the new timer. An application can pass this value, if it exists,
** to the StopTimer function to stop the timer. If the function fails
** to start a timer, return zero.
******************************************************************************/
int PlxStartTimer(DWORD dwMilliseconds, PLXTIMERPROC pTimerProc, void* pParam)
{
return plx_StartTimer(dwMilliseconds, pTimerProc, pParam);
}
/****************************************************************************
** Function : PlxStopTimer
** Purpose :
** Stops the specified timer.
** Params :
** id : Specifies the timer to be stop. This parameter must be the
** timer identifier returned by plx_StartTimer or StartTimerSync.
** Return :
** If the function succeeds, return nonzero. If the function fails,
** return zero.
******************************************************************************/
int PlxStopTimer(int id)
{
int i;
if (id < 1 || id > PLX_MAX_TIMERS)
{
PlxTrace("[PLXTIMER] PlxStopTimer : invalid timer id=%d\r\n", id);
return 0;
}
i = id - 1;
if (TimerList[i].pTimerProc == NULL)
{
PlxTrace("[PLXTIMER] PlxStopTimer : stop a null timer id=%d\r\n", id);
return 0;
}
PlxTrace("[PLXTIMER] PlxStopTimer: the stop i=%d, last=%d, pTimerProc=%x, pParam=%x",
i, last, TimerList[i].pTimerProc, TimerList[i].pParam);
TimerList[i].pTimerProc = NULL;
// 停止的Timer是当前的Timer,需要重新设置Timer
if (i == last)
plx_RestartTimer(i);
return 0;
}
/****************************************************************************
** Function : plx_StartTimer
** Purpose :
** Starts a timer the measures in milliseconds.
****************************************************************************/
static int plx_StartTimer(DWORD dwMilliseconds, PLXTIMERPROC pTimerProc, void* pParam)
{
int i;
PlxTrace("[PLXTIMER] plx_StartTimer : dwMilliseconds=%d, pTimerProc=%x, pParam=%x",
dwMilliseconds, pTimerProc, pParam);
if (pTimerProc == NULL)
return 0;
for ( i = 0; i < PLX_MAX_TIMERS; i++ )
{
if ( TimerList[i].pTimerProc == NULL )
break;
}
if ( i == PLX_MAX_TIMERS )
return 0;
if ( dwMilliseconds < PLX_MIN_ELAPSE )
dwMilliseconds = PLX_MIN_ELAPSE;
TimerList[i].dwMilliseconds = dwMilliseconds;
TimerList[i].pTimerProc = pTimerProc;
TimerList[i].pParam = pParam;
// TimerList中的时间是相对于上次设置Timer的时间,如果已经启动了Timer, 需
// 要加上上次设置Timer后过去的时间 ( 以上次启动 timer 时的时间点为准 )
if ( last != -1 )
{
TimerList[i].dwMilliseconds += PlxGetTickCount() - dwStartTime;
// 启动的timer的时间小于新Timer的时间,不需要重新启动Timer
if ( TimerList[last].dwMilliseconds < TimerList[i].dwMilliseconds )
return i + 1;
}
plx_RestartTimer(i);
PlxTrace("[PLXTIMER] plx_StartTimer return id=%d", i+1);
return i + 1;
}
/**************************************************************************/
/* Using GSM Timer */
/**************************************************************************/
#ifdef MMI_ON_WIN32
extern BOOL PlxPostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
static long plx_TimerProc(void* receiver, UINT message, long wParam, long lParam)
{
plx_RestartTimer(-1);
return 0;
}
#endif
static void plx_TimerCallback(void)
{
#ifdef MMI_ON_WIN32
#ifndef WM_TIMER
#define WM_TIMER 0x0113
#endif
PlxPostMessage((HWND)plx_TimerProc, WM_TIMER, 0, 0);
#else
plx_RestartTimer(-1);
#endif
}
/***************************************************************************
** Function : plx_RestartTimer
** Purpose :
** Restart the GSM timer when the timer is created, deleted or time out
*****************************************************************************/
static void plx_RestartTimer(int i)
{
static BOOL bInCallback;
DWORD dwTimerOut;
DWORD dwElapse;
PLXTIMERPROC pTimerProc;
void* pParam;
if (last != -1) // 已经启动了Timer
{
// Timer到时回调后会调用本函数,直接返回
if ( bInCallback )
return;
dwElapse = PlxGetTickCount() - dwStartTime; // 距上次起动 timer 的时间差
// 判断是否有到时的Timer,如果有回调
bInCallback = TRUE;
for (i = 0; i < PLX_MAX_TIMERS; i++)
{
if (TimerList[i].pTimerProc != NULL &&
TimerList[i].dwMilliseconds <= dwElapse)
{
PlxTrace("[PLXTIMER] plx_RestartTimer: the stop i=%d, last=%d, pTimerProc=%x, pParam=%x",
i, last, TimerList[i].pTimerProc, TimerList[i].pParam);
// 回调前释放表项,回调中设置timer可以使用此表项
pTimerProc = TimerList[i].pTimerProc;
pParam = TimerList[i].pParam;
TimerList[i].pTimerProc = NULL;
// 回调,可能会再次设置Timer导致重入
pTimerProc(pParam);
}
}
bInCallback = FALSE;
StopTimer( POLLEX_GUI_TIMER );
last = -1;
// 所有Timer递减elapse time,并计算最短Timer
dwTimerOut = PLX_MAX_ELAPSE;
for (i = 0; i < PLX_MAX_TIMERS; i++)
{
if (TimerList[i].pTimerProc != NULL)
{
if (TimerList[i].dwMilliseconds > dwElapse)
TimerList[i].dwMilliseconds -= dwElapse; // 减去时间差,以当前时间为基准
else
TimerList[i].dwMilliseconds = 0;
if (TimerList[i].dwMilliseconds < dwTimerOut)
{
dwTimerOut = TimerList[i].dwMilliseconds;
last = i;
}
}
}
}
else
{
// 新启动Timer,一定是因为TIMER_START
dwTimerOut = TimerList[i].dwMilliseconds;
last = i;
}
if ( dwTimerOut != PLX_MAX_ELAPSE )
{
if ( dwTimerOut > 0x7FFFFFFF )
dwTimerOut = 0x7FFFFFFF;
// dwTimerOut = MSECS(dwTimerOut);
if ( dwTimerOut == 0 )
dwTimerOut = PLX_MIN_ELAPSE;
StartTimer (POLLEX_GUI_TIMER , dwTimerOut, plx_TimerCallback );
dwStartTime = PlxGetTickCount();
}
}
/*********************************************************************
* Function
* Purpose
* Params
* Return
* Remarks
**********************************************************************/
// 采用 mtk 的函数来实现。
typedef unsigned short U16;
typedef unsigned char U8;
typedef struct
{
U16 nYear;
U8 nMonth;
U8 nDay;
U8 nHour;
U8 nMin;
U8 nSec;
U8 DayIndex; /* 0=Sunday */
}MYTIME;
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
extern void GetDateTime(MYTIME *t);
void PlxGetLocalTime(SYSTEMTIME * pSystemTime)
{
MYTIME mytime;
GetDateTime(&mytime );
pSystemTime->wYear = (WORD)mytime.nYear;
pSystemTime->wMonth = (WORD)mytime.nMonth;
pSystemTime->wDay = (WORD)mytime.nDay;
pSystemTime->wHour = (WORD)mytime.nHour;
pSystemTime->wMinute= (WORD)mytime.nMin;
pSystemTime->wSecond= (WORD)mytime.nSec;
pSystemTime->wDayOfWeek = mytime.DayIndex;
pSystemTime->wMilliseconds = 0;
return;
}
/*********************************************************************
* Function
* Purpose
* Params
* Return
* Remarks
**********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -