timerutils.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 1998-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$I useamigasmartlink.inc}
  13. {$ifdef use_amiga_smartlink}
  14. {$smartlink on}
  15. {$endif use_amiga_smartlink}
  16. unit timerutils;
  17. {
  18. History:
  19. First version of this unit.
  20. 06 Sep 2000.
  21. Added the define use_amiga_smartlink.
  22. 13 Jan 2003.
  23. [email protected]
  24. }
  25. interface
  26. uses exec, timer, amigalib;
  27. Function CreateTimer(theUnit : longint) : pTimeRequest;
  28. Function SetTimer(WhichTimer : pTimeRequest;
  29. Seconds, Microseconds : longint) : pMsgPort;
  30. Procedure WaitTimer(WhichTimer : pTimeRequest;
  31. Seconds, Microseconds : longint);
  32. Procedure DeleteTimer(WhichTimer : pTimeRequest);
  33. implementation
  34. Function CreateTimer(theUnit : longint) : pTimeRequest;
  35. var
  36. Error : longint;
  37. TimerPort : pMsgPort;
  38. TimeReq : pTimeRequest;
  39. begin
  40. TimerPort := CreatePort(Nil, 0);
  41. if TimerPort = Nil then
  42. CreateTimer := Nil;
  43. TimeReq := pTimeRequest(CreateExtIO(TimerPort,sizeof(tTimeRequest)));
  44. if TimeReq = Nil then begin
  45. DeletePort(TimerPort);
  46. CreateTimer := Nil;
  47. end;
  48. Error := OpenDevice(TIMERNAME, theUnit, pIORequest(TimeReq), 0);
  49. if Error <> 0 then begin
  50. DeleteExtIO(pIORequest(TimeReq));
  51. DeletePort(TimerPort);
  52. CreateTimer := Nil;
  53. end;
  54. TimerBase := pointer(TimeReq^.tr_Node.io_Device);
  55. CreateTimer := pTimeRequest(TimeReq);
  56. end;
  57. Function SetTimer(WhichTimer : pTimeRequest; Seconds, Microseconds : longint) : pMsgPort;
  58. var
  59. TempPort : pMsgPort;
  60. begin
  61. with WhichTimer^ do begin
  62. TempPort := tr_Node.io_Message.mn_ReplyPort;
  63. tr_Node.io_Command := TR_ADDREQUEST; { add a new timer request }
  64. tr_Time.tv_Secs := Seconds; { seconds }
  65. tr_Time.tv_Micro := Microseconds; { microseconds }
  66. SendIO(pIORequest(WhichTimer));
  67. SetTimer := TempPort;
  68. end;
  69. end;
  70. Procedure WaitTimer(WhichTimer : pTimeRequest;
  71. Seconds, Microseconds : longint);
  72. var
  73. Error : Integer;
  74. begin
  75. with WhichTimer^ do begin
  76. tr_Node.io_Command := TR_ADDREQUEST; { add a new timer request }
  77. tr_Time.tv_Secs := Seconds; { seconds }
  78. tr_Time.tv_Micro := Microseconds; { microseconds }
  79. Error := DoIO(pIORequest(WhichTimer));
  80. end;
  81. end;
  82. Procedure DeleteTimer(WhichTimer : pTimeRequest);
  83. var
  84. WhichPort : pMsgPort;
  85. begin
  86. WhichPort := WhichTimer^.tr_Node.io_Message.mn_ReplyPort;
  87. if assigned(WhichTimer) then begin
  88. CloseDevice(pIORequest(WhichTimer));
  89. DeleteExtIO(pIORequest(WhichTimer));
  90. end;
  91. if assigned(WhichPort) then
  92. DeletePort(WhichPort);
  93. end;
  94. end.