home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / DTIMER.CPP < prev    next >
C/C++ Source or Header  |  1993-09-26  |  2KB  |  95 lines

  1. // -------- dtimer.cpp
  2. //
  3. // OS/2 Version created to use the OS/2 Timer Services
  4. // jw21sep93
  5. //
  6. // renamed from 'timer.cpp' to match header
  7. //
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "dtimer.h"
  12.  
  13. Timer::Timer()
  14.     {
  15.     // create semaphore
  16.     ULONG SemAttr = DC_SEM_SHARED;      // needs to be shared
  17.     disabled = True;                    // start in disabled state
  18.     timer = 0;
  19.  
  20.     rc = DosCreateEventSem(NULL, &sem, SemAttr, 1);
  21.  
  22.     if(rc != NO_ERROR)
  23.         {
  24.         printf("DosCreateEventSem failed: rc = %d\n",rc);
  25.         }
  26.  
  27.     }
  28.  
  29. Timer::~Timer()
  30.     {
  31.     rc = DosStopTimer(timer);
  32.     rc = DosCloseEventSem(sem);
  33.     }
  34.  
  35. void Timer::SetTimer(int ticks) 
  36.     {
  37.     ULONG ct;
  38.  
  39.     disabled = False;
  40.     if (timer)
  41.         {
  42.         rc = DosStopTimer(timer);
  43.         }
  44.  
  45.     rc = DosResetEventSem(sem, &ct);
  46.     if(rc != NO_ERROR  && rc != ERROR_ALREADY_RESET)
  47.         {
  48.         printf("DosResetEventSem failed: rc = %d\n",rc);
  49.         }
  50.  
  51.     rc = DosAsyncTimer(ticks*18, (HSEM)sem, &timer); 
  52.     if(rc != NO_ERROR)
  53.         {
  54.         printf("DosAsyncTimer failed: rc = %d\n",rc);
  55.         }
  56.     }
  57.  
  58. void Timer::DisableTimer()
  59.     {
  60.     disabled = True;
  61.     }
  62.  
  63. Bool Timer::TimedOut()
  64.     {
  65.     ULONG ct = 0L;
  66.  
  67.     if (disabled == False)
  68.         {
  69.         rc = DosQueryEventSem(sem, &ct);
  70.         if(rc != NO_ERROR)
  71.             {
  72.             printf("DosQueryEventSem failed: rc = %d\n",rc);
  73.             }
  74.         }
  75.     return (Bool) (ct != 0L);
  76.     }
  77.  
  78.  
  79. Bool Timer::TimerRunning()
  80.     {
  81.     ULONG ct;
  82.  
  83.     if (disabled == False)
  84.         {
  85.         rc = DosQueryEventSem(sem, &ct);
  86.         if(rc != NO_ERROR)
  87.             {
  88.             printf("DosQueryEventSem failed: rc = %d\n",rc);
  89.             }
  90.         return (Bool) (ct == 0L);
  91.         }
  92.  
  93.     return(False);
  94.     }
  95.