home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / DC-POS24.LZX / pOS / pOS_RKRM.lzx / pOS_RKRM / pTimer / SysTime.c
Encoding:
C/C++ Source or Header  |  1997-03-18  |  7.8 KB  |  267 lines

  1.  
  2. /*******************************************************************
  3.  $CRT 01 Jun 1996 : hb
  4.  
  5.  $AUT Holger Burkarth
  6.  $DAT >>SysTime.c<<   29 Jan 1997    13:19:52 - (C) ProDAD
  7. *******************************************************************/
  8.  
  9. //##ex mcpp:cppc -gs -o pos:pos/Ex/systime p:pLib/StartCode.o p:/pOS_RKRM/pTimer/SysTime.c p:pLib/StdIO.o -l pOSStub -l pOS
  10.  
  11. /***********************************************************
  12.   pOS programing example - Copyright (C) 1995-97 proDAD
  13.  
  14.   This code was written as an easy to understand example,
  15.   how to program pOS features. It is provided 'as-is',
  16.   without any express or implied warranty.
  17.  
  18.   Permission is hereby granted to use, copy and modify
  19.   this source code for any purpose, without fee, subject
  20.   to the following conditions:
  21.  
  22.     (1) This notice may not be removed or altered from any
  23.         source distribution.
  24.  
  25.     (2) Altered source versions must be plainly marked as
  26.         such, and must not be misrepresented as being
  27.         the original source code.
  28.  
  29.     (3) If only executable code is distributed, then the
  30.         accompanying documentation have to state that
  31.         "this software is based in part on examples of
  32.         the pOS developer packet".
  33.  
  34.     (4) Permission for use of this code is granted only
  35.         if the user accepts full responsibility for any
  36.         undesirable consequences. proDAD accept NO LIABILITY
  37.         for damages of any kind.
  38.  
  39.   ©proDAD
  40. ***********************************************************/
  41.  
  42. /*\
  43. *** Example:
  44. *** struct pOS_TimeVal
  45. *** struct pOS_MsgPort
  46. *** struct pOS_TimerIO
  47. ***
  48. *** pOS_ConstructMsgPort
  49. *** pOS_CreateIORequest
  50. *** pOS_OpenDevice
  51. *** pOS_GetSysTime
  52. *** pOS_DoIO
  53. *** pOS_SendIO
  54. *** pOS_CheckIO
  55. *** pOS_WaitIO
  56. *** pOS_WaitSignal
  57. *** pOS_AbortIO
  58. *** pOS_SubTime
  59. *** pOS_CloseDevice
  60. *** pOS_DeleteIORequest
  61. *** pOS_DestructMsgPort
  62. ***
  63. \*/
  64.  
  65.  
  66. #define __COMPUTER_AMIGA 1
  67. #define NOMYDEBUG
  68.  
  69. #include "p:pExec/Types.h"
  70. #include "p:Device/Timer.h"
  71. #include "p:pDOS/ArgTags.h"
  72. #include "p:pDOS/DosErrors.h"
  73. #include "p:pDOS/DosSig.h"
  74. #include "p:proto/pLibExt.h"
  75. #include "p:proto/pExec2.h"
  76. #include "p:proto/pDOS2.h"
  77. #include "p:proto/pTimer2.h"
  78.  
  79. #ifdef _____ME_____
  80.   #include "grund/inc_string.h"
  81.   #include "grund/inc_stdio.h"
  82. #else
  83.  #ifdef __cplusplus
  84.  extern "C" {
  85.  #endif
  86.   #include <string.h>
  87.   #include <stdio.h>
  88.  #ifdef __cplusplus
  89.  }
  90.  #endif
  91. #endif
  92.  
  93.  
  94. struct pOS_TimerDevice *gb_TimerBase;
  95.  
  96.  
  97. const CHAR *HelpText=
  98. ""
  99. ;
  100.  
  101. const CHAR *PrgHeader=
  102. "Simple pOS-Timer.device demo";
  103.  
  104. const CHAR *PrgVerText=
  105. "$VER: 1.0 ("__DATE2__") (Copyright 1996 by proDAD) (Created by Holger Burkarth)";
  106.  
  107.  
  108.  
  109. /*----------------------------------
  110. -----------------------------------*/
  111. #ifdef __cplusplus
  112. extern "C"
  113. #endif
  114.  
  115. VOID main()
  116. {
  117.   struct pOS_DosArgs* Args;
  118.   UWORD Err=0;
  119.   ULONG Ops[1]={0};
  120.  
  121.   Args=pOS_ReadDosArgs("",Ops,sizeof(Ops)/sizeof(ULONG),
  122.     ARGTAG_PrgHeaderText, (ULONG)PrgHeader,    /* kurze Programm-Beschreibung */
  123.     ARGTAG_HelpText,      (ULONG)HelpText,     /* Help-Texte */
  124.     ARGTAG_PrgVerText,    (ULONG)PrgVerText,   /* VER-String */
  125.     TAG_END);
  126.  
  127.   if(Args) {
  128.     struct pOS_TimeVal TV;
  129.     struct pOS_MsgPort ReplyPort;  /* Reply-Port für die Device-IOs */
  130.     struct pOS_TimerIO* TimerIO;   /* Timer-IO-Struct */
  131.     ULONG days,hrs,secs,mins,mics;
  132.  
  133.     pOS_ConstructMsgPort(&ReplyPort); /* MsgPort initialisieren mit setzen vom Signal-Bit */
  134.  
  135.     if(TimerIO=(struct pOS_TimerIO*)pOS_CreateIORequest(&ReplyPort,sizeof(struct pOS_TimerIO))) {
  136.       pOS_OpenDevice("ptimer.device",TIMERUNIT_VBlank,(struct pOS_IORequest*)TimerIO,0,0);
  137.       if(TimerIO->tio_Error==0) {
  138.         gb_TimerBase=TimerIO->tio_Device;
  139.  
  140. // --------
  141.         pOS_GetSysTime(&TV); /* aktuelle System-Time lesen */
  142.  
  143.         mics=TV.tv_Micro;
  144.         secs=TV.tv_Secs;
  145.  
  146.         /* Compute days, hours, etc. */
  147.         mins=secs / 60;
  148.         hrs =mins / 60;
  149.         days=hrs / 24;
  150.         secs=secs % 60;
  151.         mins=mins % 60;
  152.         hrs =hrs % 24;
  153.  
  154.         printf("System Time (measured from 1.Jan.,1992)\n");
  155.         printf(" Days    Hours   Minutes Seconds Microseconds\n");
  156.         printf("%6ld  %6ld  %6ld  %6ld  %10ld\n\n\n",days,hrs,mins,secs,mics);
  157.  
  158. // --------
  159.         printf("Wait 2 Seconds ...");
  160.  
  161.         TimerIO->tio_Command=TRCMD_AddRequest;
  162.         TimerIO->tio_Time.tv_Secs=2;
  163.         TimerIO->tio_Time.tv_Micro=0;
  164.         pOS_DoIO((struct pOS_IORequest*)TimerIO); /* Kommando synchron ausführen */
  165.  
  166.         printf("\n\n");
  167.  
  168.  
  169. // --------
  170.         printf("Start 2 Seconds asynch. counting with cursor ...\n");
  171.  
  172.         TimerIO->tio_Command=TRCMD_AddRequest;
  173.         TimerIO->tio_Time.tv_Secs=2;
  174.         TimerIO->tio_Time.tv_Micro=0;
  175.         pOS_SendIO((struct pOS_IORequest*)TimerIO); /* Kommando asynchron ausführen */
  176.         mics=0; // counter;
  177.         do {
  178.           printf("\r%ld\033[K",++mics);
  179.  
  180. /*\
  181. *** Der pOS_CheckIO() liefert so lange NULL, bis der IO bearbeitet ist
  182. *** (die 2 Sekunden verstrichen sind).
  183. *** Da jeder IO nach der Bearbeitung an den ReplyPort gesendet wird, muß
  184. *** der IO wieder vom ReplyPort entfernt werden. (ein Node kann nur gleichen
  185. *** Zeit nur in einer Liste Mitglied sein)
  186. *** Der einfachste Weg, einen fertigen IO vom ReplyPort zu entfernen ist
  187. *** pOS_WaitIO() aufzurufen.
  188. \*/
  189.         } while( pOS_CheckIO((struct pOS_IORequest*)TimerIO)==NULL );
  190.  
  191.         pOS_WaitIO((struct pOS_IORequest*)TimerIO);  /* remove IO-Reply */
  192.         printf("\nDone\n");
  193.  
  194. // --------
  195.         printf("Start 2 Seconds asynch. counting without cursor ...\n");
  196.         printf("\033[0 p"); // *** Cursor OFF
  197.  
  198.         TimerIO->tio_Command=TRCMD_AddRequest;
  199.         TimerIO->tio_Time.tv_Secs=2;
  200.         TimerIO->tio_Time.tv_Micro=0;
  201.         pOS_SendIO((struct pOS_IORequest*)TimerIO);  /* Kommando asynchron ausführen */
  202.         mics=0; // counter;
  203.         do {
  204.           printf("\r%ld\033[K",++mics);
  205.         } while( pOS_CheckIO((struct pOS_IORequest*)TimerIO)==NULL );
  206.  
  207.         pOS_WaitIO((struct pOS_IORequest*)TimerIO);  /* remove IO-Reply */
  208.         printf("\033[1 p"); // *** Cursor ON
  209.         printf("\nDone\n");
  210.  
  211.  
  212.  
  213. // --------
  214.         printf("Forever Second-Counter ( Ctrl-C ) ...\n");
  215.         printf("\033[0 p"); // *** Cursor OFF
  216.  
  217.         pOS_GetSysTime(&TV);  /* Start-Zeitpunkt vermerken */
  218.  
  219.         for(;;) {
  220.           ULONG Sig;
  221.  
  222.           TimerIO->tio_Command=TRCMD_AddRequest;
  223.           TimerIO->tio_Time.tv_Secs=1;
  224.           TimerIO->tio_Time.tv_Micro=0;
  225.           pOS_SendIO((struct pOS_IORequest*)TimerIO);  /* Kommando asynchron ausführen */
  226.  
  227.           Sig=pOS_WaitSignal(DOSSIGF_CTRL_C | (1<<ReplyPort.mp_SigBit));
  228. /*\
  229. *** Es wurde Ctrl-C gedrückt und der aktuell laufende TimerIO muß vorzeitig
  230. *** abgebrochen werden. pOS_AbortIO() bewirkt, daß der IO sofort aus der
  231. *** Bearbeitung entfernt wird und zum ReplyPort gesendet wird. Der folgende
  232. *** Aufruf von pOS_WaitIO() dient jediglich zum Entfernen vom TimerIO vom
  233. *** ReplyPort;
  234. \*/
  235.           if(Sig & DOSSIGF_CTRL_C) {
  236.             pOS_AbortIO((struct pOS_IORequest*)TimerIO);
  237.             pOS_WaitIO((struct pOS_IORequest*)TimerIO);
  238.             printf("\n^C\n");
  239.             break;
  240.           }
  241.           else {
  242.             pOS_TimeVal ActTV;
  243.             pOS_WaitIO((struct pOS_IORequest*)TimerIO);  /* remove IO-Reply */
  244.  
  245.             pOS_GetSysTime(&ActTV);   /* aktuelle Zeit ermitteln */
  246.             pOS_SubTime(&ActTV,&TV);  /* ActTV = ActTV - TV => Verstrichene Zeit */
  247.             printf("\r\033[KSec %ld",ActTV.tv_Secs);
  248.           }
  249.         }
  250.         printf("\033[1 p"); // *** Cursor ON
  251.  
  252.  
  253. // --------
  254.         pOS_CloseDevice((struct pOS_IORequest*)TimerIO);  /* Device wieder schließen */
  255.       }
  256.       else printf("Cannot open ptimer.device, tio_Error=%ld\n",TimerIO->tio_Error);
  257.       pOS_DeleteIORequest((struct pOS_IORequest*)TimerIO);  /* Memory freigeben */
  258.     }
  259.     pOS_DestructMsgPort(&ReplyPort); /* Signal-Bit freigeben */
  260.  
  261.     pOS_DeleteDosArgs(Args);  /* Args freigeben */
  262.   }
  263.   else Err=DOSFAIL_FAIL;  /* vollkommen fehlgeschlagen */
  264.  
  265.   pOS_SetShellFail(Err);
  266. }
  267.