home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / lowlevel / TimerInt / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-17  |  6.9 KB  |  337 lines

  1. /****** CDGS/TimerInt **********************************************************
  2. *
  3. *   NAME
  4. *       TimerInt -- test program for lowlevel.library/AddTimerInt(),
  5. *                lowlevel.library/RemTimerInt(), lowlevel.library/StartTimerInt(),
  6. *                and lowlevel.library/StopTimerInt().
  7. *
  8. *   SYNOPSIS
  9. *       TimerInt
  10. *
  11. *   FUNCTION
  12. *
  13. *       Test program for lowlevel.library/AddTimerInt(),
  14. *       lowlevel.library/RemTimerInt(), lowlevel.library/StartTimerInt(),
  15. *       and lowlevel.library/StopTimerInt().
  16. *
  17. *       To terminate this program, select the close gadget.
  18. *
  19. *   INPUTS
  20. *       None
  21. *
  22. *   RESULT
  23. *       RETURN_OK (0)       -   success
  24. *       RETURN_WARN (5)     -   warning
  25. *       RETURN_ERROR (10)   -   error
  26. *       RETURN_FAIL (20)    -   failure
  27. *
  28. *   EXAMPLE
  29. *
  30. *   NOTES
  31. *
  32. *   BUGS
  33. *       If timer interrupt deviates from timing with timer.device by one second
  34. *       or more, an incorrect deviation will be reported because the deviation
  35. *       is measured in microseconds only.
  36. *
  37. *       Hopefully very few.
  38. *
  39. *   SEE ALSO
  40. *
  41. ******************************************************************************
  42. *
  43. */
  44.  
  45. /*
  46.  * System includes
  47.  */
  48.  
  49. #include <exec/types.h>
  50. #include <exec/memory.h>
  51.  
  52. #include <dos/dos.h>
  53.  
  54. #include <intuition/intuition.h>
  55. #include <intuition/gadgetclass.h>
  56.  
  57. #include <libraries/gadtools.h>
  58.  
  59. #include <devices/timer.h>
  60.  
  61. #include <libraries/lowlevel.h>
  62.  
  63. #include <clib/exec_protos.h>
  64. #include <clib/dos_protos.h>
  65. #include <clib/intuition_protos.h>
  66. #include <clib/gadtools_protos.h>
  67. #include <clib/timer_protos.h>
  68. #include <clib/lowlevel_protos.h>
  69. #include <clib/alib_protos.h>
  70. /*#include <clib/debug_protos.h>*/
  71. extern void kprintf(const char *,...);
  72.  
  73. #include <pragmas/lowlevel_pragmas.h>
  74.  
  75. /*
  76.  * ANSI includes
  77.  */
  78.  
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82.  
  83. /*
  84.  * Local includes
  85.  */
  86.  
  87. #define MAIN
  88.  
  89. #include "timerint.h"
  90.  
  91. /****** TimerInt/main ******************************************
  92. *
  93. *   NAME
  94. *       main -- main entry point
  95. *
  96. *   SYNOPSIS
  97. *       main(argc,argv);
  98. *
  99. *       void main(int argc,char *argv[]);
  100. *
  101. *   FUNCTION
  102. *       Main entry point.
  103. *
  104. *   INPUTS
  105. *       argc -- argument count
  106. *       argv -- argument value arrraay
  107. *
  108. *   RESULT
  109. *       None
  110. *
  111. *   EXAMPLE
  112. *
  113. *   NOTES
  114. *
  115. *   BUGS
  116. *       If there are any, you can get to 'em from here!
  117. *
  118. *   SEE ALSO
  119. *       shutdown()
  120. *
  121. ******************************************************************************
  122. *
  123. */
  124. void main(int argc,char *argv[])
  125. {
  126.  
  127.     BYTE error;
  128.  
  129.     /*
  130.      * Open libraries
  131.      */
  132.  
  133.     /* Open intuition.library */
  134.     IntuitionBase=OpenLibrary("intuition.library",KICKSTART_VERSION);
  135.     if (!IntuitionBase) {
  136.         Printf("%s: Error opening intuition.library V%ld\n",
  137.             PROGRAM_NAME,KICKSTART_VERSION);
  138.         goodbye(RETURN_FAIL);
  139.     }
  140.  
  141.     /* Open gadtool.library */
  142.     GadtoolsBase=OpenLibrary("gadtools.library",KICKSTART_VERSION);
  143.     if (!GadtoolsBase) {
  144.         Printf("%s: Error opening gadtools.library V%ld\n",
  145.             PROGRAM_NAME,KICKSTART_VERSION);
  146.         goodbye(RETURN_FAIL);
  147.     }
  148.  
  149.     /* Open lowlevel.library */
  150.     LowLevelBase=OpenLibrary("lowlevel.library",KICKSTART_VERSION);
  151.     if (!LowLevelBase) {
  152.         Printf("%s: Error opening lowlevel.library V%d\n",
  153.             PROGRAM_NAME,KICKSTART_VERSION);
  154.         goodbye(RETURN_FAIL);
  155.     }
  156.  
  157.     /*
  158.      * Open timer.device
  159.      */
  160.  
  161.     /* Create timer.device reply port */
  162.     timerPort=CreateMsgPort();
  163.     if (!timerPort) {
  164.         Printf("%s: Error creating timer.device reply port\n",PROGRAM_NAME);
  165.         goodbye(RETURN_FAIL);
  166.     }
  167.  
  168.     /* Create timer.device I/O request */
  169.     timerRequest=CreateIORequest(timerPort,sizeof(struct timerequest));
  170.     if (!timerRequest) {
  171.         Printf("%s: Error creating timer.device I/O request\n",PROGRAM_NAME);
  172.         goodbye(RETURN_FAIL);
  173.     }
  174.  
  175.     /* Open timer.device microhertz timer */
  176.     error=OpenDevice("timer.device",UNIT_MICROHZ,timerRequest,NULL);
  177.     if (error) {
  178.         Printf("%s: Error %lu opening timer.device\n",PROGRAM_NAME,error);
  179.         goodbye(RETURN_FAIL);
  180.     }
  181.  
  182.     /* Fetch timer.device library base; timer.device I/O request is not
  183.        actually used for timer functions */
  184.     TimerBase=(struct Library *) timerRequest->tr_node.io_Device;
  185.  
  186.     /*
  187.      * Set-up timer interrupt handler
  188.      */
  189.  
  190.     /* Allocate timer interrupt signal bit */
  191.     timerSignal=AllocSignal(-1);
  192.     if (timerSignal==-1) {
  193.         Printf("%s: Error allocating signal bit for timer interrupt\n",
  194.             PROGRAM_NAME);
  195.         goodbye(RETURN_FAIL);
  196.     }
  197.  
  198.     /* Find main task */
  199.     mainTask=FindTask(NULL);
  200.     if (!mainTask) {
  201.         Printf("%s: Error finding main task\n",PROGRAM_NAME);
  202.         goodbye(RETURN_FAIL);
  203.     }
  204.  
  205.     /* Add timer interrupt handler */
  206.     timerIntHandle=AddTimerInt(timerInterrupt,(APTR) TIMERINT_COOKIE);
  207.     if (!timerIntHandle) {
  208.         Printf("%s: Error adding timer interrupt handler\n",PROGRAM_NAME);
  209.         goodbye(RETURN_FAIL);
  210.     }
  211.  
  212.     /*
  213.      * GUI
  214.      */
  215.  
  216.     /* Open GUI */
  217.     if (!guiOpen()) {
  218.         Printf("%s: Error opening user interface\n",PROGRAM_NAME);
  219.         goodbye(RETURN_FAIL);
  220.     }
  221.  
  222.     /* Process GUI */
  223.     guiLoop();
  224.  
  225.     /*
  226.      * Termination
  227.      */
  228.  
  229.     /* If any bad data to keyboard interrupt handler ... */
  230.     if (timerIntBadData) {
  231.         /* Warning */
  232.         Printf("%s: %lu calls to timer interrupt handler with bad data\n",
  233.             PROGRAM_NAME,timerIntBadData);
  234.     }
  235.  
  236.     /* Exit */
  237.     goodbye(timerIntBadData?RETURN_WARN:RETURN_OK);
  238.  
  239. }
  240.  
  241. /****** TimerInt/goodbye ******************************************
  242. *
  243. *   NAME
  244. *       goodbye -- terminate program
  245. *
  246. *   SYNOPSIS
  247. *       goodbye(returnCode);
  248. *
  249. *       void goodbye(int returnCode);
  250. *
  251. *   FUNCTION
  252. *       Terminate program.
  253. *
  254. *   INPUTS
  255. *       returnCode -- return code (from dos/dos.h RETURN_*)
  256. *
  257. *   RESULT
  258. *       None
  259. *
  260. *   EXAMPLE
  261. *
  262. *   NOTES
  263. *
  264. *   BUGS
  265. *
  266. *   SEE ALSO
  267. *       main()
  268. *
  269. ******************************************************************************
  270. *
  271. */
  272. void goodbye(int returnCode)
  273. {
  274.  
  275.     /*
  276.      * Close GUI
  277.      */
  278.  
  279.     /* Close GUI */
  280.     guiClose();
  281.  
  282.     /*
  283.      * Remove timer interrupt handler
  284.      */
  285.  
  286.     /* Remove timer interrupt handler */
  287.     if (timerIntHandle) {
  288.         RemTimerInt(timerIntHandle);
  289.     }
  290.  
  291.     /*
  292.      * Close timer.device
  293.      */
  294.  
  295.     /* Close timer.device */
  296.     if (TimerBase) {
  297.         CloseDevice(timerRequest);
  298.     }
  299.  
  300.     /* Delete timer.device I/O request */
  301.     if (timerRequest) {
  302.         DeleteIORequest(timerRequest);
  303.     }
  304.  
  305.     /* Delete timer.device message port */
  306.     if (timerPort) {
  307.         DeleteMsgPort(timerPort);
  308.     }
  309.  
  310.     /*
  311.      * Close libraries
  312.      */
  313.  
  314.     /* Close lowlevel.library */
  315.     if (LowLevelBase) {
  316.         CloseLibrary(LowLevelBase);
  317.     }
  318.  
  319.     /* Close gadtools.library */
  320.     if (GadtoolsBase) {
  321.         CloseLibrary(GadtoolsBase);
  322.     }
  323.  
  324.     /* Close intuition.library */
  325.     if (IntuitionBase) {
  326.         CloseLibrary(IntuitionBase);
  327.     }
  328.  
  329.     /*
  330.      * Exit
  331.      */
  332.  
  333.     /* Exit */
  334.     exit(returnCode);
  335.  
  336. }
  337.