guitimer.c
上传用户:zbk8730
上传日期:2017-08-10
资源大小:12168k
文件大小:8k
源码类别:

uCOS

开发平台:

C/C++

  1. /*
  2. *********************************************************************************************************
  3. *                                                uC/GUI
  4. *                        Universal graphic software for embedded applications
  5. *
  6. *                       (c) Copyright 2002, Micrium Inc., Weston, FL
  7. *                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
  8. *
  9. *              礐/GUI is protected by international copyright laws. Knowledge of the
  10. *              source code may not be used to write a similar product. This file may
  11. *              only be used in accordance with a license and should not be redistributed
  12. *              in any way. We appreciate your understanding and fairness.
  13. *
  14. ----------------------------------------------------------------------
  15. File        : GUITimer.c
  16. Purpose     : Supplies timers for new emWin GSC
  17. ----------------------------------------------------------------------
  18. ---------------------------END-OF-HEADER------------------------------
  19. */
  20. #include <stddef.h>           /* needed for definition of NULL */
  21. #include "GUI_Protected.H"
  22. /*
  23.   *****************************************************************
  24.   *                                                               *
  25.   *              Config defaults                                  *
  26.   *                                                               *
  27.   *****************************************************************
  28. */
  29. /*
  30.       *************************************************************
  31.       *                                                           *
  32.       *                 Object definition                         *
  33.       *                                                           *
  34.       *************************************************************
  35. */
  36. typedef struct {
  37.   GUI_TIMER_CALLBACK* cb;
  38.   GUI_TIMER_Handle hNext;
  39.   int Flags;
  40. U32 Context;
  41. GUI_TIMER_TIME t0;
  42. GUI_TIMER_TIME Period;
  43. } GUI_TIMER_Obj;
  44. /*
  45.   *****************************************************************
  46.   *                                                               *
  47.   *              Static data                                      *
  48.   *                                                               *
  49.   *****************************************************************
  50. */
  51. GUI_TIMER_Handle hFirstTimer;
  52. /*
  53.   ********************************************************************
  54.   *                                                                  *
  55.   *                 Macros for internal use                          *
  56.   *                                                                  *
  57.   ********************************************************************
  58. */
  59. #define GUI_TIMER_H2P(h) (GUI_TIMER_Obj*)GUI_ALLOC_H2P(h)
  60. /*
  61.   ********************************************************************
  62.   *                                                                  *
  63.   *                    Static routines                               *
  64.   *                                                                  *
  65.   ********************************************************************
  66. */
  67. static void Unlink(GUI_TIMER_Handle hTimer) {
  68.   GUI_TIMER_Obj* pTimer = GUI_TIMER_H2P(hTimer);
  69.   GUI_TIMER_Handle hi;
  70.   GUI_TIMER_Obj*   pi;
  71. /* Check if it is the first element */
  72.   if (hFirstTimer == hTimer) {
  73.     hFirstTimer = pTimer->hNext;
  74.     return;
  75. }
  76.   hi = hFirstTimer;
  77. /* Try to find it in the list ... */
  78.   while(hi) {
  79.     /* GUI_ASSERT(hi<1000,0); */
  80.     pi = GUI_TIMER_H2P(hi);
  81.     if (pi->hNext == hTimer) {
  82.       pi->hNext = pTimer->hNext;
  83.       break;
  84. }        
  85.     hi = pi->hNext;
  86.   }  
  87. }
  88. /*********************************************************************
  89.    Link new Timer
  90.  ==============
  91. This routine inserts the new timer (referenced by its handle) into
  92. the linked list. The linked list is sorted according to timestamps.
  93. The first element is the timer which expires first.
  94. */
  95. void Link(GUI_TIMER_Handle hNew) {
  96.   GUI_TIMER_Obj*   pNew        = GUI_TIMER_H2P(hNew);
  97.   GUI_TIMER_Obj*   pTimer;
  98.   GUI_TIMER_Obj*   pNext;
  99.   GUI_TIMER_Handle hNext;
  100.   if (hFirstTimer ==0) { /* List is empty, make it the only element */
  101.     hFirstTimer = hNew;
  102.   pNew->hNext = 0;
  103.   } else {
  104.     GUI_TIMER_Obj* pFirstTimer      = GUI_TIMER_H2P(hFirstTimer);
  105. /* Check if we have to make it the first element */
  106.     if ((pNew->t0 - pFirstTimer->t0) <=0) {
  107.       pNew->hNext = hFirstTimer;
  108.       hFirstTimer = hNew;
  109. return;
  110. } else {
  111.       GUI_TIMER_Handle hTimer = hFirstTimer;
  112. /* Put it into the list */
  113.       do {
  114.         pTimer       = GUI_TIMER_H2P(hTimer);
  115.         hNext        = pTimer->hNext;
  116.         if (hNext ==0)
  117. goto Append;
  118.         pNext      = GUI_TIMER_H2P(hNext);
  119. if ((pNew->t0 - pNext->t0) <=0) {
  120.           pNew->hNext  = hNext;
  121.           pTimer->hNext= hNew;
  122.           return;
  123. }
  124. } while(1);
  125. /* Put it at the end of the list */
  126. Append:
  127.       pNew->hNext  = hNext;
  128.       pTimer->hNext= hNew;
  129.       return;
  130. }
  131.   }
  132. }
  133. /*
  134.   ********************************************************************
  135.   *
  136.   *        Static routines:  Exec
  137.   *
  138.   ********************************************************************
  139. */
  140. int GUI_TIMER_Exec(void) {
  141.   int r = 0;
  142.   GUI_TIMER_TIME t = GUI_GetTime();
  143.   GUI_LOCK(); {
  144.     while (hFirstTimer) {
  145.       GUI_TIMER_Obj* pTimer = GUI_TIMER_H2P(hFirstTimer);
  146.       if ((pTimer->t0-t) <=0) {
  147.         GUI_TIMER_MESSAGE tm;
  148.         tm.Time = t;
  149. tm.Context = pTimer->Context;
  150.         hFirstTimer = pTimer->hNext;
  151. pTimer->cb(&tm);
  152.         r = 1;
  153. } else
  154.   break;
  155.     }
  156.     /*
  157. GUI_TIMER_Obj* pObj = GUI_TIMER_H2P(hObj);
  158.     pObj->t0 = Time;
  159.     */
  160.   } GUI_UNLOCK(); 
  161.   return r;
  162. }
  163. /*
  164.   ********************************************************************
  165.   *                                                                  *
  166.   *        Exported routines:  Create                                *
  167.   *                                                                  *
  168.   ********************************************************************
  169. */
  170. GUI_TIMER_Handle GUI_TIMER_Create      (    GUI_TIMER_CALLBACK* cb,
  171.                                     int Time,
  172.                                     U32 Context,
  173.                                     int Flags) {
  174.   GUI_TIMER_Handle hObj;
  175.   GUI_TIMER_Obj* pObj;
  176.   GUI_LOCK();
  177.   GUI_USE_PARA(Flags);
  178.   GUI_USE_PARA(Time);
  179.   GUI_pfTimerExec = GUI_TIMER_Exec;
  180. {
  181.     /* Alloc memory for obj */
  182.     hObj = GUI_ALLOC_ALLOC(sizeof(GUI_TIMER_Obj));
  183.     pObj = GUI_TIMER_H2P(hObj);
  184.     /* init member variables */
  185.     pObj->cb = cb;
  186. pObj->Context = Context;
  187.     /* Link it */
  188. Link(hObj);
  189. } GUI_UNLOCK();
  190.   return hObj;
  191. }
  192. /*
  193.   ********************************************************************
  194.   *                                                                  *
  195.   *        Exported routines:  Delete                                *
  196.   *                                                                  *
  197.   ********************************************************************
  198. */
  199. void GUI_TIMER_Delete(GUI_TIMER_Handle hObj) {
  200. /* Unlink Timer */
  201.   GUI_LOCK();
  202.     Unlink(hObj);
  203.     GUI_ALLOC_FREE(hObj);
  204.   GUI_UNLOCK();
  205. }
  206. /*
  207.   ********************************************************************
  208.   *                                                                  *
  209.   *        Exported routines:  Various methods                       *
  210.   *                                                                  *
  211.   ********************************************************************
  212. */
  213. void GUI_TIMER_SetPeriod(GUI_TIMER_Handle hObj, GUI_TIMER_TIME Period) {
  214.   GUI_LOCK(); {
  215.     GUI_TIMER_Obj* pObj = GUI_TIMER_H2P(hObj);
  216.     pObj->Period = Period;
  217.   } GUI_UNLOCK(); 
  218. }
  219. void GUI_TIMER_SetTime(GUI_TIMER_Handle hObj, GUI_TIMER_TIME Time) {
  220.   GUI_LOCK(); {
  221.     GUI_TIMER_Obj* pObj = GUI_TIMER_H2P(hObj);
  222.     pObj->t0 = Time;
  223.   } GUI_UNLOCK(); 
  224. }
  225. void GUI_TIMER_SetDelay(GUI_TIMER_Handle hObj, GUI_TIMER_TIME Delay) {
  226.   GUI_LOCK(); {
  227.     GUI_TIMER_Obj* pObj = GUI_TIMER_H2P(hObj);
  228.     pObj->t0 = Delay;
  229. Unlink(hObj);
  230. Link(hObj);
  231.   } GUI_UNLOCK(); 
  232. }
  233. void GUI_TIMER_Restart(GUI_TIMER_Handle hObj) {
  234.   GUI_LOCK(); {
  235.     GUI_TIMER_Obj* pObj = GUI_TIMER_H2P(hObj);
  236.     pObj->t0 = GUI_GetTime() +pObj->Period;
  237. Unlink(hObj);
  238. Link(hObj);
  239.   } GUI_UNLOCK(); 
  240. }