PA_Timer_Sim.c.svn-base
上传用户:holyzs
上传日期:2022-06-29
资源大小:2335k
文件大小:2k
源码类别:

编辑器/阅读器

开发平台:

C/C++

  1. #include <reader_core.h>
  2. #define ASSERT assert
  3. #define TICK (getSysTick())
  4. #define DAY_TICKS (24 * 3600 * 1000)
  5. struct tTimeMgr gTime;
  6. static unsigned int getSysTick(void) {
  7.   SYSTEMTIME st;
  8.   GetSystemTime(&st);
  9.   return (st.wHour * 3600 + st.wMinute * 60 + st.wSecond) * 1000 + st.wMilliseconds;
  10. }
  11. void StartTime(unsigned char bNew)
  12. {
  13. struct tTimer * pTimer = &(gTime.rBank[0]);
  14. ASSERT(pTimer != NULL);
  15. pTimer->bRunning = true;
  16. if (!bNew)
  17. {
  18. pTimer->nRefTick -= TICK-pTimer->nRefTick;
  19. return;
  20. }
  21. pTimer->nRefTick = 0;
  22.   sim_timer_start();
  23. gTime.nCount = 1;
  24. }
  25. void PauseTime()
  26. {
  27. struct tTimer * pTimer = &(gTime.rBank[0]);
  28. ASSERT(pTimer != NULL);
  29. pTimer->bRunning = false;
  30. }
  31. unsigned char NewTimer(unsigned char bStarted)
  32. {
  33. struct tTimer * pTimer = NULL;
  34. ASSERT(gTime.nCount < TIME_MGR_CAPACITY-1);
  35. pTimer = &(gTime.rBank[gTime.nCount]);
  36. ASSERT(pTimer != NULL);
  37. pTimer->bRunning = bStarted;
  38. pTimer->nRefTick = 0;
  39. if (bStarted)
  40. pTimer->nRefTick = TICK;
  41. return gTime.nCount++;
  42. }
  43. void StartTimer(unsigned char nTimerId)
  44. {
  45. struct tTimer * pTimer = &(gTime.rBank[nTimerId]);
  46. ASSERT(pTimer != NULL);
  47. pTimer->bRunning = true;
  48. pTimer->nRefTick = TICK-pTimer->nRefTick;
  49. }
  50. unsigned int Tick(unsigned char nTimerId)
  51. {
  52. struct tTimer * pTimer = &(gTime.rBank[nTimerId]);
  53.   int elapsed;
  54. ASSERT(pTimer != NULL);
  55.   if (pTimer->bRunning == true) {
  56.     elapsed = (int)(TICK-pTimer->nRefTick);
  57.     //hour turned when a day passed
  58.     if (elapsed < 0) {
  59.       elapsed += DAY_TICKS;
  60.     }
  61.     //assert(elapsed > 0);
  62.     if (elapsed <= 0) {
  63.       char buf[32] = {0};
  64.       sprintf(buf, "elasped=%d", elapsed);
  65.       MessageBox(reader_hwnd, buf, "elaspsed value error", MB_OK);
  66.     }
  67.     return elapsed;
  68.   }
  69. return pTimer->nRefTick;
  70. }
  71. void PauseTimer(unsigned char nTimerId)
  72. {
  73. struct tTimer * pTimer = &(gTime.rBank[nTimerId]);
  74. ASSERT(pTimer != NULL);
  75. pTimer->nPauseTick = TICK;
  76. pTimer->bRunning = false;
  77. }
  78. void UnpauseTimer(unsigned char nTimerId)
  79. {
  80. struct tTimer * pTimer = &(gTime.rBank[nTimerId]);
  81. ASSERT(pTimer != NULL);
  82. pTimer->nRefTick += TICK - pTimer->nPauseTick;
  83. pTimer->bRunning = true;
  84. }
  85. void ResetTimer(unsigned char nTimerId)
  86. {
  87. struct tTimer * pTimer = &(gTime.rBank[nTimerId]);
  88. ASSERT(pTimer != NULL);
  89. pTimer->nRefTick = TICK;
  90. }