Subversion Repositories f9daq

Rev

Rev 197 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2. ***************************************************************************
  3. *
  4. * Author: Teunis van Beelen
  5. *
  6. * Copyright (C) 2010, 2011, 2012 Teunis van Beelen
  7. *
  8. * teuniz@gmail.com
  9. *
  10. ***************************************************************************
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation version 2 of the License.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. ***************************************************************************
  26. *
  27. * This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
  28. *
  29. ***************************************************************************
  30. */
  31.  
  32.  
  33.  
  34. #include "timer.h"
  35.  
  36.  
  37.  
  38. void (*timer_func_handler_pntr)(void);
  39.  
  40.  
  41. #ifdef __linux__
  42.  
  43. void timer_handler(int);
  44.  
  45. struct itimerval timervalue;
  46.  
  47. struct sigaction new_handler, old_handler;
  48.  
  49. void timer_sig_handler(int);
  50.  
  51.  
  52.  
  53. int start_timer(int mSec, void (*timer_func_handler)(void))
  54. {
  55.   timer_func_handler_pntr = timer_func_handler;
  56.  
  57.   timervalue.it_interval.tv_sec = mSec / 1000;
  58.   timervalue.it_interval.tv_usec = (mSec % 1000) * 1000;
  59.   timervalue.it_value.tv_sec = mSec / 1000;
  60.   timervalue.it_value.tv_usec = (mSec % 1000) * 1000;
  61.   if(setitimer(ITIMER_REAL, &timervalue, NULL))
  62.   {
  63.     printf("\nsetitimer() error\n");
  64.     return(1);
  65.   }
  66.  
  67.   new_handler.sa_handler = &timer_sig_handler;
  68.   new_handler.sa_flags = SA_NOMASK;
  69.   if(sigaction(SIGALRM, &new_handler, &old_handler))
  70.   {
  71.     printf("\nsigaction() error\n");
  72.     return(1);
  73.   }
  74.  
  75.   return(0);
  76. }
  77.  
  78.  
  79. void timer_sig_handler(int arg)
  80. {
  81.   timer_func_handler_pntr();
  82. }
  83.  
  84.  
  85. void stop_timer(void)
  86. {
  87.   timervalue.it_interval.tv_sec = 0;
  88.   timervalue.it_interval.tv_usec = 0;
  89.   timervalue.it_value.tv_sec = 0;
  90.   timervalue.it_value.tv_usec = 0;
  91.   setitimer(ITIMER_REAL, &timervalue, NULL);
  92.  
  93.   sigaction(SIGALRM, &old_handler, NULL);
  94. }
  95.  
  96. #else
  97.  
  98. HANDLE win_timer;
  99.  
  100. VOID CALLBACK timer_sig_handler(PVOID, BOOLEAN);
  101.  
  102.  
  103. int start_timer(int mSec, void (*timer_func_handler)(void))
  104. {
  105.   timer_func_handler_pntr = timer_func_handler;
  106.  
  107.   if(CreateTimerQueueTimer(&win_timer, NULL, (WAITORTIMERCALLBACK)timer_sig_handler, NULL, mSec, mSec, WT_EXECUTEINTIMERTHREAD) == 0)
  108.   {
  109.     printf("\nCreateTimerQueueTimer() error\n");
  110.     return(1);
  111.   }
  112.  
  113.   return(0);
  114. }
  115.  
  116.  
  117. VOID CALLBACK timer_sig_handler(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
  118. {
  119.   timer_func_handler_pntr();
  120. }
  121.  
  122.  
  123. void stop_timer(void)
  124. {
  125.   DeleteTimerQueueTimer(NULL, win_timer, NULL);
  126.   CloseHandle(win_timer);
  127. }
  128.  
  129. #endif
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.